[PATCH 1/2] ksmbd: fix active_num_conn leak when alloc_transport() fails
From: DaeMyung Kang
Date: Sat Apr 18 2026 - 13:30:49 EST
ksmbd_kthread_fn() increments active_num_conn right after accept(),
before calling ksmbd_tcp_new_connection(). The decrement normally
happens in ksmbd_tcp_disconnect() at the end of the connection's
lifetime.
If alloc_transport() fails in ksmbd_tcp_new_connection(), the function
releases the socket and returns -ENOMEM without going through
ksmbd_tcp_disconnect(), so active_num_conn never gets decremented.
Under memory pressure, repeated failures monotonically inflate the
counter until max_connections is reached and new clients are refused
indefinitely.
Decrement active_num_conn on this error path, matching the accounting
rule used by ksmbd_kthread_fn() and ksmbd_tcp_disconnect().
Commit 77ffbcac4e56 ("smb: server: fix leak of active_num_conn in
ksmbd_tcp_new_connection()") fixed the sibling leak on the kthread_run()
failure path; this patch closes the remaining one.
Reproduced with a debug build that adds a temporary module parameter
guarding an early return at the top of alloc_transport(), forcing
the first N accept-time transport allocations to fail:
* Configure ksmbd with "max connections = 3".
* Force 5 successive alloc_transport() failures at the accept path.
* Without the fix: active_num_conn drifts up to max_connections and
subsequent legitimate mount.cifs attempts are refused with
"ksmbd: Limit the maximum number of connections(3)" in dmesg.
* With the fix: the counter is correctly decremented on each
failure and legitimate mounts continue to succeed.
Tested by injecting 5 alloc_transport() failures with
max_connections=3 and verifying that subsequent mount.cifs attempts
still succeed on the patched kernel while the unpatched kernel
refuses them.
Fixes: 0d0d4680db22 ("ksmbd: add max connections parameter")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: DaeMyung Kang <charsyam@xxxxxxxxx>
---
fs/smb/server/transport_tcp.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/fs/smb/server/transport_tcp.c b/fs/smb/server/transport_tcp.c
index 7e29b06820e2..400412444838 100644
--- a/fs/smb/server/transport_tcp.c
+++ b/fs/smb/server/transport_tcp.c
@@ -182,6 +182,8 @@ static int ksmbd_tcp_new_connection(struct socket *client_sk)
t = alloc_transport(client_sk);
if (!t) {
+ if (server_conf.max_connections)
+ atomic_dec(&active_num_conn);
sock_release(client_sk);
return -ENOMEM;
}
--
2.43.0