All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] smb: server: fix the nonce of encrypted responses and a listener leak
@ 2026-11-27 16:48 Stefan Metzmacher
  2026-11-27 16:48 ` [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses Stefan Metzmacher
  2026-11-27 16:48 ` [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors Stefan Metzmacher
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:48 UTC (permalink / raw)
  To: Namjae Jeon
  Cc: metze, Sergey Senozhatsky, Tom Talpey, ChenXiaoSong, linux-cifs,
	stable

Hi Namjae,

these are fixes for ksmbd:

  - The nonce of encrypted responses and of the payload of encrypted
    RDMA reads is a 64-bit counter of the session followed by random
    bits of the session, like Samba does, instead of random bytes,
    which only made a reuse unlikely. With AES-GCM a reused nonce
    reveals the authentication key.

  - smb_direct_listen() leaked the smbdirect socket if binding or
    listening failed, e.g. with -EADDRINUSE (reported by Sashiko).

The client got the same nonce fix in the urgent client series, the
urgent smbdirect fixes are posted separately as well, they are all
independent of each other.

These patches were developed with the help of Claude (Anthropic),
which was used for code analysis, writing parts of the code and the
commit messages. I reviewed all changes and take full responsibility
for them.

Testing: on real hardware, client and server running the urgent
client, smbdirect and ksmbd series together:

  - encrypted (seal) mounts over TCP and over RDMA (RoCE with mlx5
    and irdma) with AES128-GCM and data verification
  - ksmbd started while the RDMA port 445 is already in use,
    smbdirect_socket_bind() fails, no warnings and ksmbd starts again
    afterwards
  - the RDMA tests of the urgent smbdirect series, including 10 ksmbd
    restarts per device while I/O is running

As we are already late in the 7.4 cycle, I leave it up to you to
decide if they should still go into 7.4 or into the next merge
window, both are marked for stable.

Base: v7.4-rc4, like the other smb series. The patches also apply
cleanly (git am) on cifs-next (61c581ea62ee), on ksmbd-for-next
(3c31349456c5) and on both merged, with or without the urgent client
and smbdirect series, and build on both merged.

Stefan Metzmacher (2):
  smb: server: use a counter for the nonce of encrypted responses
  smb: server: don't leak the smbdirect listener socket on errors

 fs/smb/server/auth.c              | 43 +++++++++++++++++++++++++++++++
 fs/smb/server/auth.h              |  4 +++
 fs/smb/server/mgmt/user_session.c |  1 +
 fs/smb/server/mgmt/user_session.h |  3 +++
 fs/smb/server/smb2pdu.c           | 25 +++++++++++-------
 fs/smb/server/transport_rdma.c    |  2 +-
 6 files changed, 67 insertions(+), 11 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses
  2026-11-27 16:48 [PATCH 0/2] smb: server: fix the nonce of encrypted responses and a listener leak Stefan Metzmacher
@ 2026-11-27 16:48 ` Stefan Metzmacher
  2026-11-27 20:49   ` Ian Bridges
  2026-11-28  0:23   ` Namjae Jeon
  2026-11-27 16:48 ` [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors Stefan Metzmacher
  1 sibling, 2 replies; 7+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:48 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Sergey Senozhatsky, Tom Talpey,
	ChenXiaoSong

The nonce of an encrypted message must not be used for any other
encrypted message within the session (MS-SMB2 3.1.4.3). This also
covers the payload of RDMA reads with an
SMB2_RDMA_TRANSFORM_TYPE_ENCRYPTION transform, which is encrypted with
the same key. fill_transform_hdr() and smb2_prep_rdma_read() filled it
with random bytes, which only makes a collision unlikely: with 11
random bytes for AES-CCM and 12 for AES-GCM the probability of a
collision after n messages is about n^2 / 2^89 or n^2 / 2^97. But with
AES-CCM and AES-GCM a reused nonce reveals the XOR of the two
plaintexts and, for AES-GCM, the authentication key, which allows
forging messages.

Like Samba, use a 64-bit counter of the session for the first 8 bytes
of the nonce, followed by random bits chosen when the session is
created, 32 bits for AES-GCM and 24 bits for AES-CCM. This makes the
nonce unique for the session, also
when several channels encrypt in parallel, see ksmbd_new_enc_nonce().

Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Fixes: 05c978948ea5 ("ksmbd: add SMB Direct RDMA encryption transform")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: ChenXiaoSong <chenxiaosong@chenxiaosong.com>
Cc: linux-cifs@vger.kernel.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/server/auth.c              | 43 +++++++++++++++++++++++++++++++
 fs/smb/server/auth.h              |  4 +++
 fs/smb/server/mgmt/user_session.c |  1 +
 fs/smb/server/mgmt/user_session.h |  3 +++
 fs/smb/server/smb2pdu.c           | 25 +++++++++++-------
 5 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/fs/smb/server/auth.c b/fs/smb/server/auth.c
index 0c51ca0cb14d..51b33cf139d4 100644
--- a/fs/smb/server/auth.c
+++ b/fs/smb/server/auth.c
@@ -18,6 +18,7 @@
 #include <crypto/utils.h>
 #include <linux/random.h>
 #include <linux/scatterlist.h>
+#include <linux/unaligned.h>
 
 #include "auth.h"
 #include "glob.h"
@@ -713,6 +714,48 @@ int ksmbd_gen_preauth_integrity_hash(struct ksmbd_conn *conn, char *buf,
 	return 0;
 }
 
+/**
+ * ksmbd_new_enc_nonce() - generate the nonce for an encrypted response
+ * @sess: session whose encryption key is used
+ * @cipher_type: cipher of the connection
+ * @nonce: nonce output, 11 bytes for AES-CCM, 12 bytes for AES-GCM
+ *
+ * The nonce must not be used for any other encrypted message within the
+ * session (MS-SMB2 3.1.4.3), this also covers the payload of RDMA reads with
+ * an SMB2_RDMA_TRANSFORM_TYPE_ENCRYPTION transform, which uses the same key.
+ * A random nonce only makes a collision unlikely, but with AES-GCM and
+ * AES-CCM a reused nonce reveals the XOR of the plaintexts and allows
+ * forgeries.
+ *
+ * So, like Samba, the nonce is a 64-bit counter of the session followed by
+ * random bits chosen when the session is created, 32 bits for AES-GCM and
+ * 24 bits for AES-CCM.
+ *
+ * Return: 0 on success, otherwise a negative errno
+ */
+int ksmbd_new_enc_nonce(struct ksmbd_session *sess, __le16 cipher_type,
+			u8 *nonce)
+{
+	u64 low = atomic64_inc_return(&sess->enc_nonce_low);
+	u32 high = sess->enc_nonce_high;
+
+	/* the counter can't wrap in practice, but never reuse a nonce */
+	if (WARN_ON_ONCE(low == 0))
+		return -EIO;
+
+	put_unaligned_le64(low, nonce);
+	if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
+	    cipher_type == SMB2_ENCRYPTION_AES256_GCM) {
+		put_unaligned_le32(high, nonce + 8);
+	} else {
+		nonce[8] = high;
+		nonce[9] = high >> 8;
+		nonce[10] = high >> 16;
+	}
+
+	return 0;
+}
+
 static int ksmbd_get_encryption_key(struct ksmbd_work *work, __u64 ses_id,
 				    int enc, u8 *key)
 {
diff --git a/fs/smb/server/auth.h b/fs/smb/server/auth.h
index 7ce9c42d58f1..38613ed025ae 100644
--- a/fs/smb/server/auth.h
+++ b/fs/smb/server/auth.h
@@ -36,6 +36,10 @@ struct ksmbd_conn;
 struct ksmbd_work;
 struct kvec;
 
+struct ksmbd_session;
+
+int ksmbd_new_enc_nonce(struct ksmbd_session *sess, __le16 cipher_type,
+			u8 *nonce);
 int ksmbd_crypt_message(struct ksmbd_work *work, struct kvec *iov,
 			unsigned int nvec, int enc);
 int ksmbd_crypt_rdma(struct ksmbd_conn *conn, const u8 *key,
diff --git a/fs/smb/server/mgmt/user_session.c b/fs/smb/server/mgmt/user_session.c
index 15662fd745ab..3f0546d1c573 100644
--- a/fs/smb/server/mgmt/user_session.c
+++ b/fs/smb/server/mgmt/user_session.c
@@ -1042,6 +1042,7 @@ static struct ksmbd_session *__session_create(struct ksmbd_conn *conn, int proto
 	xa_init(&sess->rpc_handle_list);
 	INIT_LIST_HEAD(&sess->dispose_entry);
 	sess->sequence_number = 1;
+	get_random_bytes(&sess->enc_nonce_high, sizeof(sess->enc_nonce_high));
 	atomic_set(&sess->refcnt, 2);
 	init_rwsem(&sess->tree_conns_lock);
 	init_rwsem(&sess->rpc_lock);
diff --git a/fs/smb/server/mgmt/user_session.h b/fs/smb/server/mgmt/user_session.h
index 5f0f6276e316..fd455c52ce1a 100644
--- a/fs/smb/server/mgmt/user_session.h
+++ b/fs/smb/server/mgmt/user_session.h
@@ -75,6 +75,9 @@ struct ksmbd_session {
 	struct xarray			rpc_handle_list;
 
 	__u8				smb3encryptionkey[SMB3_ENC_DEC_KEY_SIZE];
+	/* see ksmbd_new_enc_nonce() */
+	atomic64_t			enc_nonce_low;
+	u32				enc_nonce_high;
 	__u8				smb3decryptionkey[SMB3_ENC_DEC_KEY_SIZE];
 	__u8				smb3signingkey[SMB3_SIGN_KEY_SIZE];
 
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index fe17d4061237..332f9bb963e1 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -9631,7 +9631,9 @@ static int smb2_prep_rdma_read(struct ksmbd_work *work,
 	crypto->NonceLength = cpu_to_le16(nonce_len);
 	nonce = crypto->Signature + SMB2_SIGNATURE_SIZE;
 
-	get_random_bytes(nonce, nonce_len);
+	err = ksmbd_new_enc_nonce(work->sess, conn->cipher_type, nonce);
+	if (err)
+		return err;
 	err = ksmbd_crypt_rdma(conn,
 			       work->sess->smb3encryptionkey,
 			       data, datalen, nonce, nonce_len,
@@ -13118,24 +13120,23 @@ void smb3_preauth_hash_rsp(struct ksmbd_work *work)
 	}
 }
 
-static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type)
+static int fill_transform_hdr(struct ksmbd_work *work, void *tr_buf)
 {
 	struct smb2_transform_hdr *tr_hdr = tr_buf + 4;
-	struct smb2_hdr *hdr = smb_get_msg(old_buf);
-	unsigned int orig_len = get_rfc1002_len(old_buf);
+	struct smb2_hdr *hdr = smb_get_msg(work->response_buf);
+	unsigned int orig_len = get_rfc1002_len(work->response_buf);
 
 	/* tr_buf must be cleared by the caller */
 	tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
 	tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
 	tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED);
-	if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
-	    cipher_type == SMB2_ENCRYPTION_AES256_GCM)
-		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
-	else
-		get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
 	memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
 	inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr));
 	inc_rfc1001_len(tr_buf, orig_len);
+
+	/* the same session as in ksmbd_get_encryption_key() */
+	return ksmbd_new_enc_nonce(work->sess, work->conn->cipher_type,
+				   tr_hdr->Nonce);
 }
 
 int smb3_encrypt_resp(struct ksmbd_work *work)
@@ -13149,7 +13150,11 @@ int smb3_encrypt_resp(struct ksmbd_work *work)
 		return rc;
 
 	/* fill transform header */
-	fill_transform_hdr(tr_buf, work->response_buf, work->conn->cipher_type);
+	rc = fill_transform_hdr(work, tr_buf);
+	if (rc) {
+		kfree(tr_buf);
+		return rc;
+	}
 
 	iov[0].iov_base = tr_buf;
 	iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors
  2026-11-27 16:48 [PATCH 0/2] smb: server: fix the nonce of encrypted responses and a listener leak Stefan Metzmacher
  2026-11-27 16:48 ` [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses Stefan Metzmacher
@ 2026-11-27 16:48 ` Stefan Metzmacher
  2026-11-27 20:51   ` Ian Bridges
  2026-11-28  0:24   ` Namjae Jeon
  1 sibling, 2 replies; 7+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 16:48 UTC (permalink / raw)
  To: linux-cifs, samba-technical
  Cc: metze, stable, Namjae Jeon, Sergey Senozhatsky, Tom Talpey,
	ChenXiaoSong, Sashiko

smb_direct_listen() only assigned listener->socket after
smbdirect_socket_listen() succeeded. If setting the parameters,
binding (e.g. -EADDRINUSE) or listening failed, the error path called
smb_direct_listener_destroy() with listener->socket still NULL, so the
socket and its rdma cm id were never released.

Assign listener->socket right after the socket is created, so
smb_direct_listener_destroy() releases it on all error paths.

Fixes: 2eff5e51f976 ("smb: server: make use of smbdirect_socket_{listen,accept}()")
Cc: stable@vger.kernel.org
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: ChenXiaoSong <chenxiaosong@chenxiaosong.com>
Cc: linux-cifs@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-983b1ca6-9d6c-4aab-bcc6-2909770bdced
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
 fs/smb/server/transport_rdma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/smb/server/transport_rdma.c b/fs/smb/server/transport_rdma.c
index ee28a4d1cc86..8f44e5c3c373 100644
--- a/fs/smb/server/transport_rdma.c
+++ b/fs/smb/server/transport_rdma.c
@@ -418,6 +418,7 @@ static int smb_direct_listen(struct smb_direct_listener *listener,
 		       ret, ERR_PTR(ret));
 		return ret;
 	}
+	listener->socket = sc;
 
 	/*
 	 * Create the initial parameters
@@ -467,7 +468,6 @@ static int smb_direct_listen(struct smb_direct_listener *listener,
 	}
 
 	listener->port = port;
-	listener->socket = sc;
 
 	kthread = kthread_run(smb_direct_listener_kthread_fn,
 			      listener,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses
  2026-11-27 16:48 ` [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses Stefan Metzmacher
@ 2026-11-27 20:49   ` Ian Bridges
  2026-11-28  0:23   ` Namjae Jeon
  1 sibling, 0 replies; 7+ messages in thread
From: Ian Bridges @ 2026-11-27 20:49 UTC (permalink / raw)
  To: metze
  Cc: linux-cifs, samba-technical, stable, linkinjeon, senozhatsky, tom,
	chenxiaosong, Ian Bridges

I tested this patch on ksmbd-for-next 3dcffd93d4da over TCP, with
KASAN. A raw client used SMB 3.1.1 with AES-128-CCM, AES-128-GCM,
AES-256-CCM and AES-256-GCM, and SMB 3.0 and 3.0.2. It ran two sessions
on one connection and one on another, including interim responses.
Every encrypted response decrypted. In each session the counter started
at 1 and the random bytes stayed the same. The random bytes differed
between sessions, and no nonce repeated. Sealed Linux client mounts
over SMB 3.0, 3.0.2 and 3.1.1, one of them multichannel, read and wrote
data correctly. A capture of that traffic showed no repeated nonce.
Encrypted smbtorture runs gave the same results as without the patch,
and KASAN reported nothing. A Linux client mount over SMB Direct with
soft-RoCE also read and wrote data correctly. That client does not
negotiate the RDMA encryption transform, so I only reviewed the
encrypted RDMA read part.

The counter also shows a pre-existing waste, which this patch does not
cause. ksmbd encrypts some responses that it never sends.
__handle_ksmbd_work() encrypts the response at its send label without
checking work->send_no_response, and __ksmbd_conn_write() then drops
it. An encrypted CANCEL takes this path. So does a cancelled
CHANGE_NOTIFY. smb2_notify() sends its STATUS_CANCELLED reply through
smb2_send_interim_resp() and then sets send_no_response, so the send
label encrypts its response once more. In my test each cancelled
notify used two counter values that never reached the client. The
nonces stay unique, so the only cost is an encryption for each unsent
response.

Reviewed-by: Ian Bridges <icb@fastmail.org>
Tested-by: Ian Bridges <icb@fastmail.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors
  2026-11-27 16:48 ` [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors Stefan Metzmacher
@ 2026-11-27 20:51   ` Ian Bridges
  2026-11-28  0:24   ` Namjae Jeon
  1 sibling, 0 replies; 7+ messages in thread
From: Ian Bridges @ 2026-11-27 20:51 UTC (permalink / raw)
  To: metze
  Cc: linux-cifs, samba-technical, stable, linkinjeon, senozhatsky, tom,
	chenxiaosong, sashiko-bot, Ian Bridges

I tested this patch on ksmbd-for-next 3dcffd93d4da with KASAN and
kmemleak, using soft-RoCE. I started ksmbd ten times while another RDMA
CM listener held port 445, and ten times while one held port 5445. Each
start logged the failed bind. Without the patch, kmemleak reported the
listener socket and its rdma_cm id allocated in smb_direct_listen() as
leaked. With the patch it reported none. After the other listener was
gone, ksmbd listened again, and a Linux client mount over SMB Direct
read and wrote data correctly. KASAN reported nothing.

Reviewed-by: Ian Bridges <icb@fastmail.org>
Tested-by: Ian Bridges <icb@fastmail.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses
  2026-11-27 16:48 ` [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses Stefan Metzmacher
  2026-11-27 20:49   ` Ian Bridges
@ 2026-11-28  0:23   ` Namjae Jeon
  1 sibling, 0 replies; 7+ messages in thread
From: Namjae Jeon @ 2026-11-28  0:23 UTC (permalink / raw)
  To: Stefan Metzmacher
  Cc: linux-cifs, samba-technical, stable, Sergey Senozhatsky,
	Tom Talpey, ChenXiaoSong

> @@ -36,6 +36,10 @@ struct ksmbd_conn;
>  struct ksmbd_work;
>  struct kvec;
>
> +struct ksmbd_session;
I have directly removed this line and applied this patch to #ksmbd-for-next.
Thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors
  2026-11-27 16:48 ` [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors Stefan Metzmacher
  2026-11-27 20:51   ` Ian Bridges
@ 2026-11-28  0:24   ` Namjae Jeon
  1 sibling, 0 replies; 7+ messages in thread
From: Namjae Jeon @ 2026-11-28  0:24 UTC (permalink / raw)
  To: Stefan Metzmacher
  Cc: linux-cifs, samba-technical, stable, Sergey Senozhatsky,
	Tom Talpey, ChenXiaoSong, Sashiko

On Sat, Nov 28, 2026 at 1:49 AM Stefan Metzmacher <metze@samba.org> wrote:
>
> smb_direct_listen() only assigned listener->socket after
> smbdirect_socket_listen() succeeded. If setting the parameters,
> binding (e.g. -EADDRINUSE) or listening failed, the error path called
> smb_direct_listener_destroy() with listener->socket still NULL, so the
> socket and its rdma cm id were never released.
>
> Assign listener->socket right after the socket is created, so
> smb_direct_listener_destroy() releases it on all error paths.
>
> Fixes: 2eff5e51f976 ("smb: server: make use of smbdirect_socket_{listen,accept}()")
> Cc: stable@vger.kernel.org
> Cc: Namjae Jeon <linkinjeon@kernel.org>
> Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
> Cc: Tom Talpey <tom@talpey.com>
> Cc: ChenXiaoSong <chenxiaosong@chenxiaosong.com>
> Cc: linux-cifs@vger.kernel.org
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/bug/linux-983b1ca6-9d6c-4aab-bcc6-2909770bdced
> Signed-off-by: Stefan Metzmacher <metze@samba.org>
Applied it with Ian's tags to #ksmbd-for-next.
Thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-28  0:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 16:48 [PATCH 0/2] smb: server: fix the nonce of encrypted responses and a listener leak Stefan Metzmacher
2026-11-27 16:48 ` [PATCH 1/2] smb: server: use a counter for the nonce of encrypted responses Stefan Metzmacher
2026-11-27 20:49   ` Ian Bridges
2026-11-28  0:23   ` Namjae Jeon
2026-11-27 16:48 ` [PATCH 2/2] smb: server: don't leak the smbdirect listener socket on errors Stefan Metzmacher
2026-11-27 20:51   ` Ian Bridges
2026-11-28  0:24   ` Namjae Jeon

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.