From nobody Tue Jun 16 11:17:56 2026 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB08F40DFBB for ; Sat, 18 Apr 2026 17:30:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776533438; cv=none; b=KK9XJ3NSPXniTS9Lrq4bOzPQ9fjHSLjs+hOjNc6pcpFOp6WPvpHVWajiRXNFhN8FcT5sbW8dJs0QL6gWFLR3/LWxIBj407B3OnamEzRpsojc2FYoa+VjXEHJkt4yh7DcnKX8rxspu+fdC5TF/uIuI+giQJjY10ljwp5+0hi55YY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776533438; c=relaxed/simple; bh=vv+uXWXTBOrZTKIXNUPiy6z/3X6izwegkuK8RtLwUfA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e/IC98SknvAtt7e4t1Xwdns391kUjr2xjCKNAcKPRDoqdLfmcFlsjxhp6LjDMwJjpHG4pF2Ol02ic9qcj0QFKRHdAmTBee68c++7K4fC3eafV3OqjN5UAgwthuRXa8fyDG6kcsHlsyloMK9V0wTknKYOoQRbU4/4vvbR7nUuIOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S4a7aoy3; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S4a7aoy3" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-35e4617925cso291170a91.2 for ; Sat, 18 Apr 2026 10:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776533436; x=1777138236; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3lo0L+uxbWuB7MGwK8DGzVQOOZjXqL0ZQUgEg2W2lmI=; b=S4a7aoy3tUvTUTXw+BOjDg7X0HRrz6kWXCg9C2Yv65Nm5h5NuBjeZTpQDaly1AHg5K Qoiv1j2J1X6LgZs09hW/VRPoyqTM3mQFxAG5f03Au6iBd/LXz7jXsuujJYpc21DWUjKP j+eJ7imew6hzPXzC9jHoBfXL1GE4tHznPb+TTM/A5TiQW64X1qmu9oWsuOCabYOjKtrU 04ijDFJpdOeOY0sFhxLG7es7YDROQGMMlUgR8Qw6MUsCTKcsJ8RxDV3hgxUBesppvVI/ yCXYeySgBFHVmqHFeUbO/7+yFr8JU7ktyVwUTcJwb+roC7SEHPJ4bNWm1rWa3SHcjbqL TVRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776533436; x=1777138236; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=3lo0L+uxbWuB7MGwK8DGzVQOOZjXqL0ZQUgEg2W2lmI=; b=ocVSC7Fe1i5NpFu6uxat41m820N7qZEs3Qpd2ypx5xwP5y/zPFeTN/v/J2vNnKWjaV wv2U7j8Elzs0pfufA0url0r0UJXgQxk6Ik31+iS7RVlYvbaA3OmWMdkM9qQ4fYc5eRpw aYfdd6enA4x1bGsJfVGUKRphtqOMSaxDRfsXMsCefWDl5yNRRTNJCJGgJxqm6XX4McPU fsjBeZ2w/mHT4dWNmgt9E24kXRm31Q0T8VSnKKLgoewkJDEUUx4zwRIxd9k+0Y2Yf1wi gTR00BMgfSyNTa3o48UORW0XPHiUybhe2jjY5WD6XhQLt6yFFYWO9HDKZf0FBT4jwSaB 953A== X-Forwarded-Encrypted: i=1; AFNElJ+ZktDlu+1nYH0ZDTw7CPkDrgEkDvSNta+NikQSewxFCREi5JnvhOATecqwy26S6k4PGx1rkI8tjKk5v0s=@vger.kernel.org X-Gm-Message-State: AOJu0YyUz1m2sInHPyqQfgZFW9Arf/5gVS2Mvbz/98gMt4Q0hX2Y36yB /CvpOp0LoLpxmEUdvm1Urm4KqCfKYf3skpBG6U+ic7WQlQpIU7q9UAOc X-Gm-Gg: AeBDievTGhwuRonquUFGEYf/0n6qhtRnuXfdQQPgqUKle0xk1ji8JndY9laebojokvo tHim3rQrmoqVNxKWm0NXFNiKhjbiqP5nlN3FVh/urawt08ekv1hmNtKJ770WGrZcQgZydplouln jdelVA8Xp883rAldbHY9xerSSxvt0SiHpFMyxckD3WdjKLvCs7xUwYIY83JozSVkX5+pHQ2UzAn hTX8sm3vakhFZyW14qjuG/gQCJ/oPoi3M0H3RGiYRZTykbSxlUtp3v9Nua2B2U19o7BP66r65Hy xy/lwghteVO8bcrBuWCOQX4Z+zg0DwzVcN2WTVnrlMwmdJE23PDwcR/7neXCBnfLBigb9zMHcP/ qGbEiKMcyqiGawTqz1OR/yWYywT1M9gRanCPnwaOk90cb3rATLWn7J3hz75uHlyT/o0uH0CRELj eG0KOZ8f5oAR5pRkX3/8er2Cxo1wk= X-Received: by 2002:a17:902:d505:b0:2ae:3f3f:67c4 with SMTP id d9443c01a7336-2b5f9d67131mr40408135ad.0.1776533436099; Sat, 18 Apr 2026 10:30:36 -0700 (PDT) Received: from ser8.. ([221.156.231.192]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff3bfsm69694965ad.7.2026.04.18.10.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Apr 2026 10:30:35 -0700 (PDT) From: DaeMyung Kang To: linkinjeon@kernel.org, smfrench@gmail.com Cc: senozhatsky@chromium.org, tom@talpey.com, linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Henrique Carvalho , DaeMyung Kang Subject: [PATCH 1/2] ksmbd: fix active_num_conn leak when alloc_transport() fails Date: Sun, 19 Apr 2026 02:28:43 +0900 Message-ID: <20260418172844.1333378-2-charsyam@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260418172844.1333378-1-charsyam@gmail.com> References: <20260418172844.1333378-1-charsyam@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 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=3D3 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@vger.kernel.org Signed-off-by: DaeMyung Kang --- 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 *clie= nt_sk) =20 t =3D alloc_transport(client_sk); if (!t) { + if (server_conf.max_connections) + atomic_dec(&active_num_conn); sock_release(client_sk); return -ENOMEM; } --=20 2.43.0 From nobody Tue Jun 16 11:17:56 2026 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB1893101B8 for ; Sat, 18 Apr 2026 17:30:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776533442; cv=none; b=Cnbb37GFcZDMURUuIcb3ZtA9SmHj7VBDC3V9UsOT/RHB5hZOjXFHwcbIv2fK/mDcHpVewhuip5qg/+LhfVJbOkVhkQXJEQK+yc1c7vdSzDujt7eFpULNGFLdRFZTAgOSkSrlW8nlJlaZ+4+iwGLC14/kVS8JqcsFyRlfGMNXCXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776533442; c=relaxed/simple; bh=ezf//pPazx1mLelB2gmRNccf8HxZrkHgU//I5FWrCyw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hvxXXrTci9spVXJkFqCeYF3zQdkLt1Xyro83DOVFxpbW2gAfH9XRFLF8UEzxdR21U5bgYqkPDC7AhLfxOeoLWKupqum0fOVuLM4nEtvDYSmsJund7NcLZkpL7Z3k9Arcz4GqMusMxTICxr8hmeoB21/oXoCRLdMvVupoX3j3mRw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QurXQ/H5; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QurXQ/H5" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2b2ea1752f2so1138335ad.0 for ; Sat, 18 Apr 2026 10:30:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776533440; x=1777138240; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n1YbAZG9CJiEeAf7NGLPiDB355/n4BunXq6TPbpCs08=; b=QurXQ/H5ToZI50kQGmZb+B3u4q4k9XFMiYZ+KArrL6tEn5Xjfy4DfzaDDuXnYlAC+i 1V5oC06DP6uwk9ZBRC3jZjEhKg1k2ZhT+3Q/9uGW7uqAkPAEBMF9QjxvF9Efxies3L0H pbqtYt59yj5MgxOcvSWl4iUxHEIo2M+O/CAdnFqhd/7CCEH6957MWtIe6xstZ5OzjxVk /Mw9UMLCSxg/g/iHeC8BvZxO4cghs70Xs4c1G8lz4RQjgGsS1rQ8+VnC6FYnXyIsfz48 mPyZBZLFAO6SEbtAzVtWJdKUfrrX/RtD+FM0quWR0I8qjoNxdBMWwvtQWLJ2D2c8Wq4R YqmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776533440; x=1777138240; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=n1YbAZG9CJiEeAf7NGLPiDB355/n4BunXq6TPbpCs08=; b=peL0yDLOgiHgdAl5OhMuBDszTVrT67MWb7zbum/6o4wgRvBrTemqyRqVCEx4GAv9Bs xEiJMIsqfZaHdTl0HqhxVf7DLNdOal86Key/3mBlWyh0tI5XWGF9/d4qH2nWWxO96q52 Pl/ggzHzsVFEZO1fLYORkvhxV91BeR7ZoqktDK1cZtv7wgl2jd5iPzNCf4UMEy+QwzDQ r+02fuesox3NEBuRjvcoXfSrlsr8cE7HxdTKr4ffd9KXmQ5oFQ5f7D5SkECjzN0eYUAI GeXswOdXKvfeS0Z5xK6eQgm7iGDNN2Ykk9gPTG7hiiyDVHK14dsWsaMz8a8L+/bSudEY 3U+A== X-Forwarded-Encrypted: i=1; AFNElJ8G4c4o64GSfonY8F8dY5B07CDDWyjdirIsqU6HWynU1V7d2DZ77bis8nfklSI7l4GhHc3yFd2OU2B1b6I=@vger.kernel.org X-Gm-Message-State: AOJu0YxfvdeODCfwtawlDqi55mc3jW87TSn3+RNGS88ySsLEJRNgHmHJ 6rsQ91bePwKzpl92aA2YaNA/tYZ2ZmijQSbzmTe7d1ixXWpTtLhr/NxL X-Gm-Gg: AeBDievt4xV5VnzYeYdjcPTnnbfZp4ViU/cpGbR8xtoH28W8BhU9aB7YpxrP43LmFMr Ui6wKnPd2K0y34BECrEWL2xLA9IovnYObfaNeDdDEdOE9wkWAK+fPmUlWvM0bf9d0PvrsiBAFOx IjEUgfq2VZj+ujAxPZ1qD9PQMcJQTd2hf8inzUt99yPc0gNTrLvJcAuJoNidPxg5cdkgK1dQ9l1 s+E/g1jupDruMQoTWqIWAzwr/LdXPVVSnvJSC/PcET37jJUt4FU1N65yObuDFjE2gZOifYtMF1s Kfn9wo7Ig1ogvVAtJhWUpdGdAS2atZode5w80PGm/mFoGL5BbztYTmYrJ0FOBb5YNOL20uzLzDt AQCngnx/eCWzv5v6ui0mGsS5niM7aZkG4V7hv0nUOa+8hgQxTH9fsTJXSpUpHpCc92t98VSn+9M HDdaN4xN5GMmqFWYO5YcILXeCt4Uc= X-Received: by 2002:a17:902:c40a:b0:2b2:ac6f:f6a with SMTP id d9443c01a7336-2b5f9d679e8mr42742655ad.0.1776533440022; Sat, 18 Apr 2026 10:30:40 -0700 (PDT) Received: from ser8.. ([221.156.231.192]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff3bfsm69694965ad.7.2026.04.18.10.30.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Apr 2026 10:30:39 -0700 (PDT) From: DaeMyung Kang To: linkinjeon@kernel.org, smfrench@gmail.com Cc: senozhatsky@chromium.org, tom@talpey.com, linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Henrique Carvalho , DaeMyung Kang Subject: [PATCH 2/2] ksmbd: reset rcount per connection in ksmbd_conn_wait_idle_sess_id() Date: Sun, 19 Apr 2026 02:28:44 +0900 Message-ID: <20260418172844.1333378-3-charsyam@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260418172844.1333378-1-charsyam@gmail.com> References: <20260418172844.1333378-1-charsyam@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" rcount is intended to be connection-specific: 2 for curr_conn, 1 for every other connection sharing the same session. However, it is initialised only once before the hash iteration and is never reset. After the loop visits curr_conn, later sibling connections are also checked against rcount =3D=3D 2, so a sibling with req_running =3D=3D 1 is incorrectly treated as idle. This makes the outcome depend on the hash iteration order: whether a given sibling is checked against the loose (< 2) or the strict (< 1) threshold is decided by whether it happens to be visited before or after curr_conn. The function's contract is "wait until every connection sharing this session is idle" so that destroy_previous_session() can safely tear the session down. The latched rcount violates that contract and reopens the teardown race window the wait logic was meant to close: destroy_previous_session() may proceed before sibling channels have actually quiesced, overlapping session teardown with in-flight work on those connections. Recompute rcount inside the loop so each connection is compared against its own threshold regardless of iteration order. This is a code-inspection fix for an iteration-order-dependent logic error; a targeted reproducer would require SMB3 multichannel with in-flight work on a sibling channel landing after curr_conn in hash order, which is not something that can be triggered reliably. Fixes: 76e98a158b20 ("ksmbd: fix race condition between destroy_previous_se= ssion() and smb2 operations()") Cc: stable@vger.kernel.org Signed-off-by: DaeMyung Kang --- fs/smb/server/connection.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fs/smb/server/connection.c b/fs/smb/server/connection.c index a26899d12df1..b5e077f272cf 100644 --- a/fs/smb/server/connection.c +++ b/fs/smb/server/connection.c @@ -237,7 +237,7 @@ int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *cur= r_conn, u64 sess_id) { struct ksmbd_conn *conn; int rc, retry_count =3D 0, max_timeout =3D 120; - int rcount =3D 1, bkt; + int rcount, bkt; =20 retry_idle: if (retry_count >=3D max_timeout) @@ -246,8 +246,7 @@ int ksmbd_conn_wait_idle_sess_id(struct ksmbd_conn *cur= r_conn, u64 sess_id) down_read(&conn_list_lock); hash_for_each(conn_list, bkt, conn, hlist) { if (conn->binding || xa_load(&conn->sessions, sess_id)) { - if (conn =3D=3D curr_conn) - rcount =3D 2; + rcount =3D (conn =3D=3D curr_conn) ? 2 : 1; if (atomic_read(&conn->req_running) >=3D rcount) { rc =3D wait_event_timeout(conn->req_running_q, atomic_read(&conn->req_running) < rcount, --=20 2.43.0