Skip to contentSkip to conversation

ksmbd: fix residual active_num_conn leak on 6.1.y

PATCH New Ferry Meng 1 participant 3 messages May 12 Stable branch

lore
Share & tools
lore

Ferry Meng

author
#0 lore raw

cover ksmbd: fix residual active_num_conn leak on 6.1.y

This series closes a residual active_num_conn leak on the 6.1.y stable

Upstream commits 77ffbcac4e56 ("smb: server: fix leak of active_num_conn in ksmbd_tcp_new_connection()") and 6551300dc452 ("smb: server: fix active_num_conn leak on transport allocation failure") have already been backported to 6.1.y, but upstream 77ffbcac4e56 was built on top of 5da92a251e41 ("ksmbd: make ksmbd thread names distinct by client IP"), which removes kernel_getpeername() and the out_error: label in ksmbd_tcp_new_connection().

This series picks up the missing dependency commit and the follow-up fix so that 6.1.y has the same failure-path semantics as mainline.

Michael Bommarito (1):
smb: server: fix active_num_conn leak on transport allocation failure

Namjae Jeon (1):
ksmbd: make ksmbd thread names distinct by client IP

 fs/smb/server/transport_tcp.c | 41 +++++++++++++----------------------
 1 file changed, 15 insertions(+), 26 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/2 ksmbd: make ksmbd thread names distinct by client IP

From: Namjae Jeon <[email protected]>

commit 5da92a251e41f824d7e6b4d54d65dcdcfd69fda3 upstream.

This patch makes ksmbd thread names distinct by client IP address.

100943 ? S 0:00 [ksmbd:::ffff:10.177.110.57]
or
101752 ? S 0:00 [ksmbd:10.177.110.57]

Stable-dep-of: 77ffbcac4e56 ("smb: server: fix leak of active_num_conn in ksmbd_tcp_new_connection()")
Stable-dep-of: 97f8d2648ef4 ("smb: server: fix active_num_conn leak on transport allocation failure")

---
 fs/smb/server/transport_tcp.c | 39 ++++++++++++-----------------------
 1 file changed, 13 insertions(+), 26 deletions(-)
fs/smb/server/transport_tcp.c +13 -26
index 8a1a61d22dac..179b0985645e 100644
@@ -169,17 +169,6 @@ static struct kvec *get_conn_iovec(struct tcp_transport *t, unsigned int nr_segs
 	return new_iov;
 }
 
-static unsigned short ksmbd_tcp_get_port(const struct sockaddr *sa)
-{
-	switch (sa->sa_family) {
-	case AF_INET:
-		return ntohs(((struct sockaddr_in *)sa)->sin_port);
-	case AF_INET6:
-		return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
-	}
-	return 0;
-}
-
 /**
  * ksmbd_tcp_new_connection() - create a new tcp session on mount
  * @client_sk:	socket associated with new connection
@@ -191,7 +180,6 @@ static unsigned short ksmbd_tcp_get_port(const struct sockaddr *sa)
  */
 static int ksmbd_tcp_new_connection(struct socket *client_sk)
 {
-	struct sockaddr *csin;
 	int rc = 0;
 	struct tcp_transport *t;
 	struct task_struct *handler;
@@ -202,27 +190,26 @@ static int ksmbd_tcp_new_connection(struct socket *client_sk)
 		return -ENOMEM;
 	}
 
-	csin = KSMBD_TCP_PEER_SOCKADDR(KSMBD_TRANS(t)->conn);
-	if (kernel_getpeername(client_sk, csin) < 0) {
-		pr_err("client ip resolution failed\n");
-		rc = -EINVAL;
-		goto out_error;
-	}
-
+#if IS_ENABLED(CONFIG_IPV6)
+	if (client_sk->sk->sk_family == AF_INET6)
+		handler = kthread_run(ksmbd_conn_handler_loop,
+				KSMBD_TRANS(t)->conn, "ksmbd:%pI6c",
+				&KSMBD_TRANS(t)->conn->inet6_addr);
+	else
+		handler = kthread_run(ksmbd_conn_handler_loop,
+				KSMBD_TRANS(t)->conn, "ksmbd:%pI4",
+				&KSMBD_TRANS(t)->conn->inet_addr);
+#else
 	handler = kthread_run(ksmbd_conn_handler_loop,
-			      KSMBD_TRANS(t)->conn,
-			      "ksmbd:%u",
-			      ksmbd_tcp_get_port(csin));
+			KSMBD_TRANS(t)->conn, "ksmbd:%pI4",
+			&KSMBD_TRANS(t)->conn->inet_addr);
+#endif
 	if (IS_ERR(handler)) {
 		pr_err("cannot start conn thread\n");
 		rc = PTR_ERR(handler);
 		ksmbd_tcp_disconnect(KSMBD_TRANS(t));
 	}
 	return rc;
-
-out_error:
-	free_transport(t);
-	return rc;
 }
 
 /**
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 2/2 smb: server: fix active_num_conn leak on transport allocation failure

In reply to Ferry Meng

From: Michael Bommarito <[email protected]>

commit 6551300dc452ac16a855a83dbd1e74899542d3b3 upstream.

Commit 77ffbcac4e56 ("smb: server: fix leak of active_num_conn in ksmbd_tcp_new_connection()") addressed the kthread_run() failure path. The earlier alloc_transport() == NULL path in the same function has the same leak, is reachable pre-authentication via any TCP connect to port 445, and was empirically reproduced on UML (ARCH=um, v7.0-rc7): a small number of forced allocation failures were sufficient to put ksmbd into a state where every subsequent connection attempt was rejected for the remainder of the boot.

ksmbd_kthread_fn() increments active_num_conn before calling ksmbd_tcp_new_connection() and discards the return value, so when alloc_transport() returns NULL the socket is released and -ENOMEM returned without decrementing the counter. Each such failure permanently consumes one slot from the max_connections pool; once cumulative failures reach the cap, atomic_inc_return() hits the threshold on every subsequent accept and every new connection is rejected. The counter is only reset by module reload.

An unauthenticated remote attacker can drive the server toward the memory pressure that makes alloc_transport() fail by holding open connections with large RFC1002 lengths up to MAX_STREAM_PROT_LEN (0x00FFFFFF); natural transient allocation failures on a loaded host produce the same drift more slowly.

Mirror the existing rollback pattern in ksmbd_kthread_fn(): on the alloc_transport() failure path, decrement active_num_conn gated on server_conf.max_connections.

Repro details: with the patch reverted, forced alloc_transport() NULL returns leaked counter slots and subsequent connection attempts -- including legitimate connects issued after the forced-fail window had closed -- were all rejected with "Limit the maximum number of connections". With this patch applied, the same connect sequence produces no rejections and the counter cycles cleanly between zero and one on every accept.

Fixes 0d0d4680db22 ("ksmbd: add max connections parameter")
Assisted-by Claude:claude-opus-4-6
Assisted-by Codex:gpt-5-4
---
 fs/smb/server/transport_tcp.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/server/transport_tcp.c +2 -0
index 179b0985645e..f9961df03339 100644
@@ -187,6 +187,8 @@ static int ksmbd_tcp_new_connection(struct socket *client_sk)
 	t = alloc_transport(client_sk);
 	if (!t) {
 		sock_release(client_sk);
+		if (server_conf.max_connections)
+			atomic_dec(&active_num_conn);
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.43.5