* [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce
@ 2026-11-27 12:41 Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 1/8] smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put() Stefan Metzmacher
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: Paulo Alcantara
Cc: metze, Namjae Jeon, Ronnie Sahlberg, Shyam Prasad N, Tom Talpey,
Bharath SM, linux-cifs, samba-technical, stable
Hi Paulo,
these are client fixes reported by Sashiko:
- smbd_get_parameters(), smbd_register_mr() and
smbd_debug_proc_show() used server->smbd_conn without any
locking, while cifs_abort_connection() can destroy it at any
time (use-after-free). server->smbd_conn is now only changed
under srv_lock and only used with a reference on its socket
(the new smbdirect_socket_get()/put()) or under the lock.
- smb3_negotiate_wsize() and smb3_negotiate_rsize() used the zero
limits of a missing connection during a reconnect, with signing
the header sizes were subtracted from 0, which underflows, so the
RDMA limit was lost.
- smbd_get_connection() returns the real error instead of -ENOENT,
the following patches depend on that.
- The nonce of encrypted messages 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: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put()"
touches fs/smb/smbdirect, it's only used by the client changes.
Reports fixed by this series:
- https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org
smb: client: don't use server->smbd_conn without a reference or lock
- https://sashiko.dev/#/bug/linux-211bb822-8907-4243-9dd3-da2595595552
smb: client: don't use server->smbd_conn without a reference or lock
- https://sashiko.dev/#/bug/linux-d40006cd-d7c3-4ea8-8ebb-1bf2b55b0db0
smb: client: don't use zero RDMA limits without a connection
The other urgent fixes for fs/smb/smbdirect and ksmbd (including
the same nonce fix for the server) are posted separately, they are
independent of these, the remaining smbdirect fixes follow on top of
all of them.
This also covers the problem addressed by "[PATCH v3] smb: client:
protect server->smbd_conn with RCU and refcounting" from Qican Ma
(https://lore.kernel.org/all/179570959875.92741.4311009098248753929@gmail.com/),
without waiting for references in smbd_destroy(), which is called
with cifs_server_lock() held.
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 and smbdirect series together, over RoCE with mlx5 (ConnectX),
irdma (Intel E810) and rxe:
- xfstests (59 tests of the cifs and generic groups) and O_DIRECT
reads/writes with data verification
- removing the local address or setting the link down while a
connection is established, the umount finishes in time
- 2x10 ksmbd restarts while I/O is running and
/proc/fs/cifs/DebugData is read in a loop, each restart reconnects
- mounts killed by a signal while their RDMA connect is pending
(the syzbot case), no tasks in D state and no warnings
Note I didn't test with iWarp as there are existing problems with it,
I have patches to fix iWarp, but I'll post them later, as they
also rely on some rdma-core changes.
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, all of them 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 smbdirect
series, and build on both merged.
Stefan Metzmacher (8):
smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put()
smb: client: return the real error from smbd_get_connection()
smb: client: change server->smbd_conn only under server->srv_lock
smb: client: pass server to smbd_get_parameters()
smb: client: pass server to smbd_register_mr()
smb: client: don't use server->smbd_conn without a reference or lock
smb: client: don't use zero RDMA limits without a connection
smb: client: use a counter for the nonce of encrypted messages
fs/smb/client/cifsglob.h | 3 +
fs/smb/client/connect.c | 18 ++--
fs/smb/client/file.c | 12 +--
fs/smb/client/misc.c | 2 +
fs/smb/client/smb2ops.c | 111 ++++++++++++++++++++-----
fs/smb/client/smb2pdu.c | 4 +-
fs/smb/client/smbdirect.c | 170 ++++++++++++++++++++++++++++++--------
fs/smb/client/smbdirect.h | 11 ++-
fs/smb/smbdirect/socket.c | 27 ++++++
include/linux/smbdirect.h | 3 +
10 files changed, 291 insertions(+), 70 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/8] smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put()
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 2/8] smb: client: return the real error from smbd_get_connection() Stefan Metzmacher
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Tom Talpey
These take and drop a reference on sc->refs.destroy, which only keeps
the memory of the socket alive. smbdirect_socket_release() still
disconnects and destroys the socket, after that it is no longer
connected and all functions fail gracefully.
This will be used by the smb client in order to use the socket,
e.g. for smbdirect_connection_register_mr_io(), while
cifs_abort_connection() may release it at the same time.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/smbdirect/socket.c | 27 +++++++++++++++++++++++++++
include/linux/smbdirect.h | 3 +++
2 files changed, 30 insertions(+)
diff --git a/fs/smb/smbdirect/socket.c b/fs/smb/smbdirect/socket.c
index e270e36b3145..13cf50b96bc9 100644
--- a/fs/smb/smbdirect/socket.c
+++ b/fs/smb/smbdirect/socket.c
@@ -851,6 +851,33 @@ void smbdirect_socket_release(struct smbdirect_socket *sc)
}
EXPORT_SYMBOL_GPL(smbdirect_socket_release);
+/*
+ * smbdirect_socket_get() and smbdirect_socket_put() only keep
+ * the memory of the socket alive, they don't prevent
+ * smbdirect_socket_release() from disconnecting and destroying
+ * it, after that the socket is no longer connected and
+ * all functions fail gracefully.
+ *
+ * This allows a caller to use the socket, e.g. for
+ * smbdirect_connection_register_mr_io(), while another
+ * thread may call smbdirect_socket_release() at the same time.
+ *
+ * smbdirect_socket_put() may free the socket,
+ * so it must be called from process context.
+ */
+void smbdirect_socket_get(struct smbdirect_socket *sc)
+{
+ kref_get(&sc->refs.destroy);
+}
+EXPORT_SYMBOL_GPL(smbdirect_socket_get);
+
+void smbdirect_socket_put(struct smbdirect_socket *sc)
+{
+ might_sleep();
+ kref_put(&sc->refs.destroy, smbdirect_socket_release_destroy);
+}
+EXPORT_SYMBOL_GPL(smbdirect_socket_put);
+
int smbdirect_socket_wait_for_credits(struct smbdirect_socket *sc,
enum smbdirect_socket_status expected_status,
int unexpected_errno,
diff --git a/include/linux/smbdirect.h b/include/linux/smbdirect.h
index 97f5ba730fa7..7f0b1ed6cf16 100644
--- a/include/linux/smbdirect.h
+++ b/include/linux/smbdirect.h
@@ -111,6 +111,9 @@ void smbdirect_socket_shutdown(struct smbdirect_socket *sc);
void smbdirect_socket_release(struct smbdirect_socket *sc);
+void smbdirect_socket_get(struct smbdirect_socket *sc);
+void smbdirect_socket_put(struct smbdirect_socket *sc);
+
int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc,
struct smbdirect_send_batch *batch,
bool is_last);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/8] smb: client: return the real error from smbd_get_connection()
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 1/8] smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put() Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 3/8] smb: client: change server->smbd_conn only under server->srv_lock Stefan Metzmacher
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Ronnie Sahlberg,
Shyam Prasad N, Tom Talpey, Bharath SM, Long Li
smbd_get_connection() returned NULL on failure, so the callers had
to make up an error: mount returned -ENOENT ("No such file or
directory") and smbd_reconnect() as well, which hides the real
reason, e.g. -ECONNREFUSED or -ENODEV if the route to the server
uses a netdev without an RDMA device.
Return an ERR_PTR() instead. If both ports fail, return the error of
SMBD_PORT, unless it's -ENODEV, which is what the attempt with the
wrong device type returns, as SMBD_PORT only allows iWarp devices and
SMB_PORT doesn't allow them.
Fixes: 2f8946464b11 ("CIFS: SMBD: Upper layer connects to SMBDirect session")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: Long Li <longli@microsoft.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/connect.c | 5 ++--
fs/smb/client/smbdirect.c | 48 +++++++++++++++++++++++----------------
fs/smb/client/smbdirect.h | 2 +-
3 files changed, 33 insertions(+), 22 deletions(-)
diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c
index 750b5bb72dc7..e79f9afe4128 100644
--- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c
@@ -1890,12 +1890,13 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
#endif
tcp_ses->smbd_conn = smbd_get_connection(
tcp_ses, (struct sockaddr *)&ctx->dstaddr);
- if (tcp_ses->smbd_conn) {
+ if (!IS_ERR(tcp_ses->smbd_conn)) {
cifs_dbg(VFS, "RDMA transport established\n");
rc = 0;
goto smbd_connected;
} else {
- rc = -ENOENT;
+ rc = PTR_ERR(tcp_ses->smbd_conn);
+ tcp_ses->smbd_conn = NULL;
goto out_err_crypto_release;
}
}
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index 563ef488a225..d05d90482e74 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -220,6 +220,8 @@ void smbd_destroy(struct TCP_Server_Info *server)
*/
int smbd_reconnect(struct TCP_Server_Info *server)
{
+ struct smbd_connection *conn;
+
log_rdma_event(INFO, "reconnecting rdma session\n");
if (!server->smbd_conn) {
@@ -236,16 +238,17 @@ int smbd_reconnect(struct TCP_Server_Info *server)
create_conn:
log_rdma_event(INFO, "creating rdma session\n");
- server->smbd_conn = smbd_get_connection(
- server, (struct sockaddr *) &server->dstaddr);
-
- if (server->smbd_conn) {
- cifs_dbg(VFS, "RDMA transport re-established\n");
- trace_smb3_smbd_connect_done(server->hostname, server->conn_id, &server->dstaddr);
- return 0;
+ conn = smbd_get_connection(server, (struct sockaddr *)&server->dstaddr);
+ if (IS_ERR(conn)) {
+ server->smbd_conn = NULL;
+ trace_smb3_smbd_connect_err(server->hostname, server->conn_id, &server->dstaddr);
+ return PTR_ERR(conn);
}
- trace_smb3_smbd_connect_err(server->hostname, server->conn_id, &server->dstaddr);
- return -ENOENT;
+
+ server->smbd_conn = conn;
+ cifs_dbg(VFS, "RDMA transport re-established\n");
+ trace_smb3_smbd_connect_done(server->hostname, server->conn_id, &server->dstaddr);
+ return 0;
}
/* Create a SMBD connection, called by upper layer */
@@ -302,7 +305,7 @@ static struct smbd_connection *_smbd_get_connection(
info = kzalloc_obj(*info);
if (!info)
- return NULL;
+ return ERR_PTR(-ENOMEM);
ret = smbdirect_socket_create_kern(net, &sc);
if (ret)
goto socket_init_failed;
@@ -337,7 +340,7 @@ static struct smbd_connection *_smbd_get_connection(
smbdirect_socket_release(sc);
socket_init_failed:
kfree(info);
- return NULL;
+ return ERR_PTR(ret);
}
const struct smbdirect_socket_parameters *smbd_get_parameters(struct smbd_connection *conn)
@@ -356,18 +359,25 @@ struct smbd_connection *smbd_get_connection(
{
struct smbd_connection *ret;
const struct smbdirect_socket_parameters *sp;
- int port = SMBD_PORT;
-try_again:
- ret = _smbd_get_connection(server, dstaddr, port);
+ ret = _smbd_get_connection(server, dstaddr, SMBD_PORT);
/* Try SMB_PORT if SMBD_PORT doesn't work */
- if (!ret && port == SMBD_PORT) {
- port = SMB_PORT;
- goto try_again;
+ if (IS_ERR(ret)) {
+ struct smbd_connection *ret2;
+
+ ret2 = _smbd_get_connection(server, dstaddr, SMB_PORT);
+ /*
+ * SMBD_PORT only allows iWarp devices and SMB_PORT
+ * doesn't allow them, the attempt with the wrong
+ * device type fails with -ENODEV, so we return
+ * the error of the other one.
+ */
+ if (!IS_ERR(ret2) || PTR_ERR(ret) == -ENODEV)
+ ret = ret2;
}
- if (!ret)
- return NULL;
+ if (IS_ERR(ret))
+ return ret;
sp = smbd_get_parameters(ret);
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index be205ec02077..52fef7354f2a 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -57,7 +57,7 @@ void smbd_debug_proc_show(struct TCP_Server_Info *server, struct seq_file *m);
#define cifs_rdma_enabled(server) 0
struct smbd_connection {};
static inline void *smbd_get_connection(
- struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
+ struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return ERR_PTR(-EOPNOTSUPP); }
static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
static inline void smbd_destroy(struct TCP_Server_Info *server) {}
static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/8] smb: client: change server->smbd_conn only under server->srv_lock
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 1/8] smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put() Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 2/8] smb: client: return the real error from smbd_get_connection() Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 4/8] smb: client: pass server to smbd_get_parameters() Stefan Metzmacher
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Tom Talpey,
Ronnie Sahlberg, Shyam Prasad N, Bharath SM
smbd_get_parameters(), smbd_register_mr() and smbd_debug_proc_show()
use server->smbd_conn without holding cifs_server_lock(), while
cifs_abort_connection() can destroy it via smbd_destroy() at any time,
which frees the smbd_connection and releases the smbdirect socket.
smbd_debug_proc_show() is called with cifs_tcp_ses_lock held, so
cifs_server_lock() can't be used there.
Change server->smbd_conn only under server->srv_lock, via the new
smbd_set_connection() and in smbd_destroy(), which now clears the
pointer under the lock before releasing the socket. This allows
readers to use server->smbd_conn under server->srv_lock.
cifs_get_tcp_session() calls smbd_destroy() on failure, also for
early failures before server->srv_lock is initialized, so only call
it if server->smbd_conn is set.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/connect.c | 19 +++++++++++++------
fs/smb/client/smbdirect.c | 27 +++++++++++++++++++++++----
fs/smb/client/smbdirect.h | 4 ++++
3 files changed, 40 insertions(+), 10 deletions(-)
diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c
index e79f9afe4128..064b70bf1fdf 100644
--- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c
@@ -1883,20 +1883,22 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
tcp_ses->echo_interval = ctx->echo_interval * HZ;
if (tcp_ses->rdma) {
+ struct smbd_connection *conn;
+
#ifndef CONFIG_CIFS_SMB_DIRECT
cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
rc = -ENOENT;
goto out_err_crypto_release;
#endif
- tcp_ses->smbd_conn = smbd_get_connection(
- tcp_ses, (struct sockaddr *)&ctx->dstaddr);
- if (!IS_ERR(tcp_ses->smbd_conn)) {
+ conn = smbd_get_connection(tcp_ses,
+ (struct sockaddr *)&ctx->dstaddr);
+ if (!IS_ERR(conn)) {
+ smbd_set_connection(tcp_ses, conn);
cifs_dbg(VFS, "RDMA transport established\n");
rc = 0;
goto smbd_connected;
} else {
- rc = PTR_ERR(tcp_ses->smbd_conn);
- tcp_ses->smbd_conn = NULL;
+ rc = PTR_ERR(conn);
goto out_err_crypto_release;
}
}
@@ -1967,7 +1969,12 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
kfree(tcp_ses->leaf_fullpath);
if (tcp_ses->ssocket)
sock_release(tcp_ses->ssocket);
- smbd_destroy(tcp_ses);
+ /*
+ * srv_lock is not initialized yet for
+ * the early gotos, but smbd_conn is NULL then.
+ */
+ if (tcp_ses->smbd_conn)
+ smbd_destroy(tcp_ses);
kfree(tcp_ses);
}
return ERR_PTR(rc);
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index d05d90482e74..e9739f9e36c9 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -201,7 +201,17 @@ static int smbd_post_send_full_iter(struct smbdirect_socket *sc,
*/
void smbd_destroy(struct TCP_Server_Info *server)
{
- struct smbd_connection *info = server->smbd_conn;
+ struct smbd_connection *info;
+
+ /*
+ * server->smbd_conn is changed under server->srv_lock,
+ * so that readers which don't hold cifs_server_lock()
+ * can use it under server->srv_lock.
+ */
+ spin_lock(&server->srv_lock);
+ info = server->smbd_conn;
+ server->smbd_conn = NULL;
+ spin_unlock(&server->srv_lock);
if (!info) {
log_rdma_event(INFO, "rdma session already destroyed\n");
@@ -211,7 +221,17 @@ void smbd_destroy(struct TCP_Server_Info *server)
smbdirect_socket_release(info->socket);
kfree(info);
- server->smbd_conn = NULL;
+}
+
+/*
+ * Publish a new connection, see smbd_destroy()
+ */
+void smbd_set_connection(struct TCP_Server_Info *server,
+ struct smbd_connection *info)
+{
+ spin_lock(&server->srv_lock);
+ server->smbd_conn = info;
+ spin_unlock(&server->srv_lock);
}
/*
@@ -240,12 +260,11 @@ int smbd_reconnect(struct TCP_Server_Info *server)
log_rdma_event(INFO, "creating rdma session\n");
conn = smbd_get_connection(server, (struct sockaddr *)&server->dstaddr);
if (IS_ERR(conn)) {
- server->smbd_conn = NULL;
trace_smb3_smbd_connect_err(server->hostname, server->conn_id, &server->dstaddr);
return PTR_ERR(conn);
}
- server->smbd_conn = conn;
+ smbd_set_connection(server, conn);
cifs_dbg(VFS, "RDMA transport re-established\n");
trace_smb3_smbd_connect_done(server->hostname, server->conn_id, &server->dstaddr);
return 0;
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index 52fef7354f2a..283809a57fb6 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -30,6 +30,8 @@ struct smbd_connection {
/* Create a SMBDirect session */
struct smbd_connection *smbd_get_connection(
struct TCP_Server_Info *server, struct sockaddr *dstaddr);
+void smbd_set_connection(struct TCP_Server_Info *server,
+ struct smbd_connection *info);
const struct smbdirect_socket_parameters *smbd_get_parameters(struct smbd_connection *conn);
@@ -58,6 +60,8 @@ void smbd_debug_proc_show(struct TCP_Server_Info *server, struct seq_file *m);
struct smbd_connection {};
static inline void *smbd_get_connection(
struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return ERR_PTR(-EOPNOTSUPP); }
+static inline void smbd_set_connection(struct TCP_Server_Info *server,
+ struct smbd_connection *info) {}
static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
static inline void smbd_destroy(struct TCP_Server_Info *server) {}
static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 4/8] smb: client: pass server to smbd_get_parameters()
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
` (2 preceding siblings ...)
2026-11-27 12:41 ` [PATCH 3/8] smb: client: change server->smbd_conn only under server->srv_lock Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 5/8] smb: client: pass server to smbd_register_mr() Stefan Metzmacher
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Tom Talpey,
Ronnie Sahlberg, Shyam Prasad N, Bharath SM
This is a mechanical change of the prototype of smbd_get_parameters()
to take a struct TCP_Server_Info *server instead of a
struct smbd_connection *, deriving server->smbd_conn inside the
function, with no change in behaviour. A following change uses this to
access server->smbd_conn under server->srv_lock.
smbd_get_connection() is called before the connection is published in
server->smbd_conn, so it uses
smbdirect_socket_get_current_parameters() directly.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/file.c | 4 ++--
fs/smb/client/smb2ops.c | 4 ++--
fs/smb/client/smbdirect.c | 8 +++++---
fs/smb/client/smbdirect.h | 2 +-
4 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c
index d272320436b3..f61e80e3d88c 100644
--- a/fs/smb/client/file.c
+++ b/fs/smb/client/file.c
@@ -393,7 +393,7 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->smbd_conn) {
const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server->smbd_conn);
+ smbd_get_parameters(server);
stream->sreq_max_segs = sp->max_frmr_depth;
}
@@ -612,7 +612,7 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->smbd_conn) {
const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server->smbd_conn);
+ smbd_get_parameters(server);
rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth;
}
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 298f549bd7ed..14383c37ae9e 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -521,7 +521,7 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->rdma) {
const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server->smbd_conn);
+ smbd_get_parameters(server);
if (server->sign)
/*
@@ -572,7 +572,7 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->rdma) {
const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server->smbd_conn);
+ smbd_get_parameters(server);
if (server->sign)
/*
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index e9739f9e36c9..b00224a1d411 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -362,8 +362,10 @@ static struct smbd_connection *_smbd_get_connection(
return ERR_PTR(ret);
}
-const struct smbdirect_socket_parameters *smbd_get_parameters(struct smbd_connection *conn)
+const struct smbdirect_socket_parameters *smbd_get_parameters(struct TCP_Server_Info *server)
{
+ struct smbd_connection *conn = server->smbd_conn;
+
if (unlikely(!conn->socket)) {
static const struct smbdirect_socket_parameters zero_params;
@@ -398,7 +400,7 @@ struct smbd_connection *smbd_get_connection(
if (IS_ERR(ret))
return ret;
- sp = smbd_get_parameters(ret);
+ sp = smbdirect_socket_get_current_parameters(ret->socket);
server->rdma_readwrite_threshold =
rdma_readwrite_threshold > sp->max_fragmented_send_size ?
@@ -443,7 +445,7 @@ int smbd_send(struct TCP_Server_Info *server,
{
struct smbd_connection *info = server->smbd_conn;
struct smbdirect_socket *sc = info->socket;
- const struct smbdirect_socket_parameters *sp = smbd_get_parameters(info);
+ const struct smbdirect_socket_parameters *sp = smbd_get_parameters(server);
struct smb_rqst *rqst;
struct iov_iter iter;
struct smbdirect_send_batch_storage bstorage;
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index 283809a57fb6..81fd6b849ade 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -33,7 +33,7 @@ struct smbd_connection *smbd_get_connection(
void smbd_set_connection(struct TCP_Server_Info *server,
struct smbd_connection *info);
-const struct smbdirect_socket_parameters *smbd_get_parameters(struct smbd_connection *conn);
+const struct smbdirect_socket_parameters *smbd_get_parameters(struct TCP_Server_Info *server);
/* Reconnect SMBDirect session */
int smbd_reconnect(struct TCP_Server_Info *server);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 5/8] smb: client: pass server to smbd_register_mr()
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
` (3 preceding siblings ...)
2026-11-27 12:41 ` [PATCH 4/8] smb: client: pass server to smbd_get_parameters() Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 6/8] smb: client: don't use server->smbd_conn without a reference or lock Stefan Metzmacher
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Tom Talpey,
Ronnie Sahlberg, Shyam Prasad N, Bharath SM
This is a mechanical change of the prototype of smbd_register_mr()
to take a struct TCP_Server_Info *server instead of a
struct smbd_connection *, deriving server->smbd_conn inside the
function, with no change in behaviour. A following change uses this to
take a reference on server->smbd_conn's socket under server->srv_lock.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/smb2pdu.c | 4 ++--
fs/smb/client/smbdirect.c | 3 ++-
fs/smb/client/smbdirect.h | 2 +-
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/fs/smb/client/smb2pdu.c b/fs/smb/client/smb2pdu.c
index 7b7d25364e20..d56e84770615 100644
--- a/fs/smb/client/smb2pdu.c
+++ b/fs/smb/client/smb2pdu.c
@@ -4721,7 +4721,7 @@ int smb2_new_read_req(void **buf, unsigned int *total_len,
struct smbdirect_buffer_descriptor_v1 *v1;
bool need_invalidate = server->dialect == SMB30_PROT_ID;
- rdata->mr = smbd_register_mr(server->smbd_conn, &rdata->subreq.io_iter,
+ rdata->mr = smbd_register_mr(server, &rdata->subreq.io_iter,
true, need_invalidate);
if (!rdata->mr) {
rc = -EAGAIN;
@@ -5362,7 +5362,7 @@ smb2_async_writev(struct cifs_io_subrequest *wdata)
struct smbdirect_buffer_descriptor_v1 *v1;
bool need_invalidate = server->dialect == SMB30_PROT_ID;
- wdata->mr = smbd_register_mr(server->smbd_conn, &wdata->subreq.io_iter,
+ wdata->mr = smbd_register_mr(server, &wdata->subreq.io_iter,
false, need_invalidate);
if (!wdata->mr) {
rc = -EAGAIN;
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index b00224a1d411..9fe92db606ad 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -545,10 +545,11 @@ int smbd_send(struct TCP_Server_Info *server,
* need_invalidate: true if this MR needs to be locally invalidated after I/O
* return value: the MR registered, NULL if failed.
*/
-struct smbdirect_mr_io *smbd_register_mr(struct smbd_connection *info,
+struct smbdirect_mr_io *smbd_register_mr(struct TCP_Server_Info *server,
struct iov_iter *iter,
bool writing, bool need_invalidate)
{
+ struct smbd_connection *info = server->smbd_conn;
struct smbdirect_socket *sc = info->socket;
if (!smbdirect_connection_is_connected(sc))
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index 81fd6b849ade..7d99797de6fa 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -47,7 +47,7 @@ int smbd_send(struct TCP_Server_Info *server,
/* Interfaces to register and deregister MR for RDMA read/write */
struct smbdirect_mr_io *smbd_register_mr(
- struct smbd_connection *info, struct iov_iter *iter,
+ struct TCP_Server_Info *server, struct iov_iter *iter,
bool writing, bool need_invalidate);
void smbd_mr_fill_buffer_descriptor(struct smbdirect_mr_io *mr,
struct smbdirect_buffer_descriptor_v1 *v1);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 6/8] smb: client: don't use server->smbd_conn without a reference or lock
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
` (4 preceding siblings ...)
2026-11-27 12:41 ` [PATCH 5/8] smb: client: pass server to smbd_register_mr() Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 7/8] smb: client: don't use zero RDMA limits without a connection Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 8/8] smb: client: use a counter for the nonce of encrypted messages Stefan Metzmacher
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Sashiko, Paulo Alcantara, Namjae Jeon, Tom Talpey,
Ronnie Sahlberg, Shyam Prasad N, Bharath SM
smbd_get_parameters(), smbd_register_mr() and smbd_debug_proc_show()
used server->smbd_conn without any locking, while
cifs_abort_connection() can destroy it via smbd_destroy() at any
time, which frees the smbd_connection and releases the smbdirect
socket.
smbd_register_mr() now uses the new smbd_get_socket() and
smbd_put_socket(), which take and drop a reference on the socket of
server->smbd_conn under server->srv_lock via smbdirect_socket_get()
and smbdirect_socket_put(). Once the connection is destroyed, the
socket is no longer connected, so
smbdirect_connection_register_mr_io() fails gracefully. A registered
mr has its own reference counting.
smbd_debug_proc_show() is called with cifs_tcp_ses_lock held, so it
holds server->srv_lock instead, which prevents server->smbd_conn from
being destroyed.
smbd_get_parameters() returned a pointer into the socket, which can't
be kept valid for the callers. Now it copies the parameters into a
caller-provided struct under server->srv_lock, so each caller gets its
own stable snapshot. If there's no connection (anymore), the snapshot
is filled with zeros. This also fixes a possible NULL pointer
dereference in smb2_negotiate_wsize() and smb2_negotiate_rsize(),
which called smbd_get_parameters(server->smbd_conn) without checking
server->smbd_conn, e.g. during a reconnect. The per-caller snapshot
also avoids a torn read, where a concurrent connection drop could zero
the parameters mid-use, e.g. underflowing a zero
max_fragmented_send_size in those functions or reading max_frmr_depth
as 0.
smbd_send() is called under cifs_server_lock(), so it uses
smbdirect_socket_get_current_parameters() directly.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot+sashiko@kernel.org>
Closes: https://sashiko.dev/#/patchset/cover.1791225453.git.metze%40samba.org
Closes: https://sashiko.dev/#/bug/linux-211bb822-8907-4243-9dd3-da2595595552
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/file.c | 12 +++---
fs/smb/client/smb2ops.c | 16 +++----
fs/smb/client/smbdirect.c | 91 +++++++++++++++++++++++++++++++++------
fs/smb/client/smbdirect.h | 3 +-
4 files changed, 94 insertions(+), 28 deletions(-)
diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c
index f61e80e3d88c..1ad742fbe23b 100644
--- a/fs/smb/client/file.c
+++ b/fs/smb/client/file.c
@@ -392,10 +392,10 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->smbd_conn) {
- const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server);
+ struct smbdirect_socket_parameters sp;
- stream->sreq_max_segs = sp->max_frmr_depth;
+ smbd_get_parameters(server, &sp);
+ stream->sreq_max_segs = sp.max_frmr_depth;
}
#endif
}
@@ -611,10 +611,10 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->smbd_conn) {
- const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server);
+ struct smbdirect_socket_parameters sp;
- rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth;
+ smbd_get_parameters(server, &sp);
+ rreq->io_streams[0].sreq_max_segs = sp.max_frmr_depth;
}
#endif
return 0;
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 14383c37ae9e..052596978c2b 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -520,9 +520,9 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
wsize = min_t(unsigned int, wsize, server->max_write);
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->rdma) {
- const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server);
+ struct smbdirect_socket_parameters sp;
+ smbd_get_parameters(server, &sp);
if (server->sign)
/*
* Account for SMB2 data transfer packet header and
@@ -530,12 +530,12 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
*/
wsize = min_t(unsigned int,
wsize,
- sp->max_fragmented_send_size -
+ sp.max_fragmented_send_size -
SMB2_READWRITE_PDU_HEADER_SIZE -
sizeof(struct smb2_transform_hdr));
else
wsize = min_t(unsigned int,
- wsize, sp->max_read_write_size);
+ wsize, sp.max_read_write_size);
}
#endif
if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
@@ -571,9 +571,9 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
rsize = min_t(unsigned int, rsize, server->max_read);
#ifdef CONFIG_CIFS_SMB_DIRECT
if (server->rdma) {
- const struct smbdirect_socket_parameters *sp =
- smbd_get_parameters(server);
+ struct smbdirect_socket_parameters sp;
+ smbd_get_parameters(server, &sp);
if (server->sign)
/*
* Account for SMB2 data transfer packet header and
@@ -581,12 +581,12 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
*/
rsize = min_t(unsigned int,
rsize,
- sp->max_fragmented_recv_size -
+ sp.max_fragmented_recv_size -
SMB2_READWRITE_PDU_HEADER_SIZE -
sizeof(struct smb2_transform_hdr));
else
rsize = min_t(unsigned int,
- rsize, sp->max_read_write_size);
+ rsize, sp.max_read_write_size);
}
#endif
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index 9fe92db606ad..99f526281909 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -362,17 +362,58 @@ static struct smbd_connection *_smbd_get_connection(
return ERR_PTR(ret);
}
-const struct smbdirect_socket_parameters *smbd_get_parameters(struct TCP_Server_Info *server)
+/*
+ * server->smbd_conn can be destroyed by cifs_abort_connection()
+ * at any time, so we take a reference on its socket while we use
+ * it. server->smbd_conn is changed under server->srv_lock,
+ * see smbd_set_connection() and smbd_destroy().
+ *
+ * Once the connection is destroyed, the socket is no longer
+ * connected, so the smbdirect functions fail gracefully.
+ *
+ * smbd_put_socket() may free the socket, so this
+ * can only be used in process context.
+ */
+static struct smbdirect_socket *smbd_get_socket(struct TCP_Server_Info *server)
{
- struct smbd_connection *conn = server->smbd_conn;
-
- if (unlikely(!conn->socket)) {
- static const struct smbdirect_socket_parameters zero_params;
+ struct smbdirect_socket *sc = NULL;
- return &zero_params;
+ spin_lock(&server->srv_lock);
+ if (server->smbd_conn && server->smbd_conn->socket) {
+ sc = server->smbd_conn->socket;
+ smbdirect_socket_get(sc);
}
+ spin_unlock(&server->srv_lock);
- return smbdirect_socket_get_current_parameters(conn->socket);
+ return sc;
+}
+
+static void smbd_put_socket(struct smbdirect_socket *sc)
+{
+ if (sc)
+ smbdirect_socket_put(sc);
+}
+
+/*
+ * Copy the current smbdirect socket parameters into *sp, so each caller
+ * gets its own stable snapshot. server->smbd_conn and its socket can be
+ * destroyed at any time, so we can't return a pointer into the socket,
+ * and a single shared copy would be torn by a concurrent update.
+ *
+ * The copy is done under server->srv_lock, which also prevents
+ * server->smbd_conn from being destroyed, see smbd_destroy().
+ *
+ * If there's no connection (anymore), *sp is filled with zeros.
+ */
+void smbd_get_parameters(struct TCP_Server_Info *server,
+ struct smbdirect_socket_parameters *sp)
+{
+ spin_lock(&server->srv_lock);
+ if (server->smbd_conn && server->smbd_conn->socket)
+ *sp = *smbdirect_socket_get_current_parameters(server->smbd_conn->socket);
+ else
+ memset(sp, 0, sizeof(*sp));
+ spin_unlock(&server->srv_lock);
}
struct smbd_connection *smbd_get_connection(
@@ -400,6 +441,9 @@ struct smbd_connection *smbd_get_connection(
if (IS_ERR(ret))
return ret;
+ /*
+ * ret is not published in server->smbd_conn yet
+ */
sp = smbdirect_socket_get_current_parameters(ret->socket);
server->rdma_readwrite_threshold =
@@ -445,7 +489,13 @@ int smbd_send(struct TCP_Server_Info *server,
{
struct smbd_connection *info = server->smbd_conn;
struct smbdirect_socket *sc = info->socket;
- const struct smbdirect_socket_parameters *sp = smbd_get_parameters(server);
+ /*
+ * We're called under cifs_server_lock(), so
+ * server->smbd_conn can't be destroyed while
+ * we use it.
+ */
+ const struct smbdirect_socket_parameters *sp =
+ smbdirect_socket_get_current_parameters(sc);
struct smb_rqst *rqst;
struct iov_iter iter;
struct smbdirect_send_batch_storage bstorage;
@@ -549,13 +599,19 @@ struct smbdirect_mr_io *smbd_register_mr(struct TCP_Server_Info *server,
struct iov_iter *iter,
bool writing, bool need_invalidate)
{
- struct smbd_connection *info = server->smbd_conn;
- struct smbdirect_socket *sc = info->socket;
+ struct smbdirect_socket *sc = smbd_get_socket(server);
+ struct smbdirect_mr_io *mr = NULL;
- if (!smbdirect_connection_is_connected(sc))
- return NULL;
+ /*
+ * The mr has its own reference counting
+ * and doesn't need the socket reference
+ * once registered.
+ */
+ if (sc && smbdirect_connection_is_connected(sc))
+ mr = smbdirect_connection_register_mr_io(sc, iter, writing, need_invalidate);
+ smbd_put_socket(sc);
- return smbdirect_connection_register_mr_io(sc, iter, writing, need_invalidate);
+ return mr;
}
void smbd_mr_fill_buffer_descriptor(struct smbdirect_mr_io *mr,
@@ -580,7 +636,15 @@ void smbd_debug_proc_show(struct TCP_Server_Info *server, struct seq_file *m)
if (!server->rdma)
return;
+ /*
+ * We're called with cifs_tcp_ses_lock held, so we
+ * can't use smbd_get_socket(), but holding
+ * server->srv_lock prevents server->smbd_conn
+ * from being destroyed, see smbd_destroy().
+ */
+ spin_lock(&server->srv_lock);
if (!server->smbd_conn) {
+ spin_unlock(&server->srv_lock);
seq_puts(m, "\nSMBDirect transport not available");
return;
}
@@ -588,6 +652,7 @@ void smbd_debug_proc_show(struct TCP_Server_Info *server, struct seq_file *m)
smbdirect_connection_legacy_debug_proc_show(server->smbd_conn->socket,
server->rdma_readwrite_threshold,
m);
+ spin_unlock(&server->srv_lock);
}
MODULE_IMPORT_NS("SMBDIRECT");
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index 7d99797de6fa..528e2ee81cbd 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -33,7 +33,8 @@ struct smbd_connection *smbd_get_connection(
void smbd_set_connection(struct TCP_Server_Info *server,
struct smbd_connection *info);
-const struct smbdirect_socket_parameters *smbd_get_parameters(struct TCP_Server_Info *server);
+void smbd_get_parameters(struct TCP_Server_Info *server,
+ struct smbdirect_socket_parameters *sp);
/* Reconnect SMBDirect session */
int smbd_reconnect(struct TCP_Server_Info *server);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 7/8] smb: client: don't use zero RDMA limits without a connection
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
` (5 preceding siblings ...)
2026-11-27 12:41 ` [PATCH 6/8] smb: client: don't use server->smbd_conn without a reference or lock Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 8/8] smb: client: use a counter for the nonce of encrypted messages Stefan Metzmacher
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Sashiko, Paulo Alcantara, Namjae Jeon, Tom Talpey,
Ronnie Sahlberg, Shyam Prasad N, Bharath SM
smbd_get_parameters() fills the parameters with zeros if there's no
connection (anymore), e.g. during a reconnect. With signing
smb3_negotiate_wsize() and smb3_negotiate_rsize() subtracted the
header sizes from the zero max_fragmented_send_size and
max_fragmented_recv_size, which underflows, so the RDMA limit was
lost, without signing they used the zero max_read_write_size.
cifs_prepare_write() and cifs_prepare_read() used the zero
max_frmr_depth for sreq_max_segs.
Let smbd_get_parameters() return -ENOTCONN in that case and only
apply the RDMA limits if it succeeded. cifs_negotiate_wsize() and
cifs_negotiate_rsize() only lower an already negotiated size, so the
limits of the last connection stay in place until
cifs_renegotiate_iosize() applies the ones of the new connection
after the reconnect.
Fixes: b8aef8c8808c ("smb: client: make use of smbdirect_socket_create_kern()/smbdirect_socket_release()")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot+sashiko@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-d40006cd-d7c3-4ea8-8ebb-1bf2b55b0db0
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Tom Talpey <tom@talpey.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/file.c | 8 ++++----
fs/smb/client/smb2ops.c | 28 ++++++++++++++++++++--------
fs/smb/client/smbdirect.c | 17 ++++++++++++-----
fs/smb/client/smbdirect.h | 4 ++--
4 files changed, 38 insertions(+), 19 deletions(-)
diff --git a/fs/smb/client/file.c b/fs/smb/client/file.c
index 1ad742fbe23b..e7ed1ad58f37 100644
--- a/fs/smb/client/file.c
+++ b/fs/smb/client/file.c
@@ -394,8 +394,8 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
if (server->smbd_conn) {
struct smbdirect_socket_parameters sp;
- smbd_get_parameters(server, &sp);
- stream->sreq_max_segs = sp.max_frmr_depth;
+ if (smbd_get_parameters(server, &sp) == 0)
+ stream->sreq_max_segs = sp.max_frmr_depth;
}
#endif
}
@@ -613,8 +613,8 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
if (server->smbd_conn) {
struct smbdirect_socket_parameters sp;
- smbd_get_parameters(server, &sp);
- rreq->io_streams[0].sreq_max_segs = sp.max_frmr_depth;
+ if (smbd_get_parameters(server, &sp) == 0)
+ rreq->io_streams[0].sreq_max_segs = sp.max_frmr_depth;
}
#endif
return 0;
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 052596978c2b..0f82fba40b64 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -514,15 +514,21 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
{
struct TCP_Server_Info *server = tcon->ses->server;
unsigned int wsize;
+#ifdef CONFIG_CIFS_SMB_DIRECT
+ struct smbdirect_socket_parameters sp;
+#endif
/* start with specified wsize, or default */
wsize = ctx->got_wsize ? ctx->vol_wsize : SMB3_DEFAULT_IOSIZE;
wsize = min_t(unsigned int, wsize, server->max_write);
#ifdef CONFIG_CIFS_SMB_DIRECT
- if (server->rdma) {
- struct smbdirect_socket_parameters sp;
-
- smbd_get_parameters(server, &sp);
+ /*
+ * Without a connection there are no RDMA limits, the zeros would
+ * underflow below. The caller keeps the limits of the last
+ * connection, see cifs_negotiate_wsize(), and
+ * cifs_renegotiate_iosize() applies the ones of the next one.
+ */
+ if (server->rdma && smbd_get_parameters(server, &sp) == 0) {
if (server->sign)
/*
* Account for SMB2 data transfer packet header and
@@ -565,15 +571,21 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
{
struct TCP_Server_Info *server = tcon->ses->server;
unsigned int rsize;
+#ifdef CONFIG_CIFS_SMB_DIRECT
+ struct smbdirect_socket_parameters sp;
+#endif
/* start with specified rsize, or default */
rsize = ctx->got_rsize ? ctx->vol_rsize : SMB3_DEFAULT_IOSIZE;
rsize = min_t(unsigned int, rsize, server->max_read);
#ifdef CONFIG_CIFS_SMB_DIRECT
- if (server->rdma) {
- struct smbdirect_socket_parameters sp;
-
- smbd_get_parameters(server, &sp);
+ /*
+ * Without a connection there are no RDMA limits, the zeros would
+ * underflow below. The caller keeps the limits of the last
+ * connection, see cifs_negotiate_rsize(), and
+ * cifs_renegotiate_iosize() applies the ones of the next one.
+ */
+ if (server->rdma && smbd_get_parameters(server, &sp) == 0) {
if (server->sign)
/*
* Account for SMB2 data transfer packet header and
diff --git a/fs/smb/client/smbdirect.c b/fs/smb/client/smbdirect.c
index 99f526281909..5cc199fca558 100644
--- a/fs/smb/client/smbdirect.c
+++ b/fs/smb/client/smbdirect.c
@@ -403,17 +403,24 @@ static void smbd_put_socket(struct smbdirect_socket *sc)
* The copy is done under server->srv_lock, which also prevents
* server->smbd_conn from being destroyed, see smbd_destroy().
*
- * If there's no connection (anymore), *sp is filled with zeros.
+ * If there's no connection (anymore), *sp is filled with zeros and
+ * -ENOTCONN is returned, the callers must not use the zeros as limits.
*/
-void smbd_get_parameters(struct TCP_Server_Info *server,
- struct smbdirect_socket_parameters *sp)
+int smbd_get_parameters(struct TCP_Server_Info *server,
+ struct smbdirect_socket_parameters *sp)
{
+ int ret = 0;
+
spin_lock(&server->srv_lock);
- if (server->smbd_conn && server->smbd_conn->socket)
+ if (server->smbd_conn && server->smbd_conn->socket) {
*sp = *smbdirect_socket_get_current_parameters(server->smbd_conn->socket);
- else
+ } else {
memset(sp, 0, sizeof(*sp));
+ ret = -ENOTCONN;
+ }
spin_unlock(&server->srv_lock);
+
+ return ret;
}
struct smbd_connection *smbd_get_connection(
diff --git a/fs/smb/client/smbdirect.h b/fs/smb/client/smbdirect.h
index 528e2ee81cbd..ba67bddf5907 100644
--- a/fs/smb/client/smbdirect.h
+++ b/fs/smb/client/smbdirect.h
@@ -33,8 +33,8 @@ struct smbd_connection *smbd_get_connection(
void smbd_set_connection(struct TCP_Server_Info *server,
struct smbd_connection *info);
-void smbd_get_parameters(struct TCP_Server_Info *server,
- struct smbdirect_socket_parameters *sp);
+int smbd_get_parameters(struct TCP_Server_Info *server,
+ struct smbdirect_socket_parameters *sp);
/* Reconnect SMBDirect session */
int smbd_reconnect(struct TCP_Server_Info *server);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 8/8] smb: client: use a counter for the nonce of encrypted messages
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
` (6 preceding siblings ...)
2026-11-27 12:41 ` [PATCH 7/8] smb: client: don't use zero RDMA limits without a connection Stefan Metzmacher
@ 2026-11-27 12:41 ` Stefan Metzmacher
7 siblings, 0 replies; 9+ messages in thread
From: Stefan Metzmacher @ 2026-11-27 12:41 UTC (permalink / raw)
To: linux-cifs, samba-technical
Cc: metze, stable, Paulo Alcantara, Namjae Jeon, Ronnie Sahlberg,
Shyam Prasad N, Tom Talpey, Bharath SM
The nonce of an encrypted message must not be used for any other
encrypted message within the session (MS-SMB2 3.1.4.3).
fill_transform_hdr() 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
allocated, 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 smb3_new_enc_nonce().
fill_transform_hdr() now fails if the session isn't found, like
crypt_message() does when it looks up the encryption key.
Fixes: 026e93dc0a3e ("CIFS: Encrypt SMB3 requests before sending")
Cc: stable@vger.kernel.org
Cc: Paulo Alcantara <pc@manguebit.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: linux-cifs@vger.kernel.org
Cc: samba-technical@lists.samba.org
Signed-off-by: Stefan Metzmacher <metze@samba.org>
---
fs/smb/client/cifsglob.h | 3 ++
fs/smb/client/misc.c | 2 ++
fs/smb/client/smb2ops.c | 75 +++++++++++++++++++++++++++++++++++-----
3 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/fs/smb/client/cifsglob.h b/fs/smb/client/cifsglob.h
index 4e16347886ed..3bfa9b1bacf5 100644
--- a/fs/smb/client/cifsglob.h
+++ b/fs/smb/client/cifsglob.h
@@ -1100,6 +1100,9 @@ struct cifs_ses {
__u16 session_flags;
__u8 smb3signingkey[SMB3_SIGN_KEY_SIZE];
__u8 smb3encryptionkey[SMB3_ENC_DEC_KEY_SIZE];
+ /* see smb3_new_enc_nonce() */
+ atomic64_t enc_nonce_low;
+ u32 enc_nonce_high;
__u8 smb3decryptionkey[SMB3_ENC_DEC_KEY_SIZE];
__u8 preauth_sha_hash[SMB2_PREAUTH_HASH_SIZE];
diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c
index a8f4c8644562..355b35f5fc63 100644
--- a/fs/smb/client/misc.c
+++ b/fs/smb/client/misc.c
@@ -84,6 +84,8 @@ sesInfoAlloc(void)
spin_lock_init(&ret_buf->iface_lock);
INIT_LIST_HEAD(&ret_buf->iface_list);
spin_lock_init(&ret_buf->chan_lock);
+ get_random_bytes(&ret_buf->enc_nonce_high,
+ sizeof(ret_buf->enc_nonce_high));
}
return ret_buf;
}
diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 0f82fba40b64..15f559787068 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -4720,9 +4720,66 @@ smb2_dir_needs_close(struct cifsFileInfo *cfile)
return !cfile->invalidHandle;
}
-static void
-fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
- struct smb_rqst *old_rq, __le16 cipher_type)
+/*
+ * The nonce of an encrypted message must not be used for any other encrypted
+ * message within the session (MS-SMB2 3.1.4.3), 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 allocated, 32 bits for AES-GCM and
+ * 24 bits for AES-CCM.
+ */
+static int
+smb3_new_enc_nonce(struct TCP_Server_Info *server, __u64 ses_id,
+ __le16 cipher_type, u8 *nonce)
+{
+ struct TCP_Server_Info *pserver;
+ struct cifs_ses *ses;
+ bool found = false;
+ u64 low = 0;
+ u32 high = 0;
+
+ /* If server is a channel, select the primary channel */
+ pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
+
+ spin_lock(&cifs_tcp_ses_lock);
+ list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+ if (ses->Suid == ses_id) {
+ low = atomic64_inc_return(&ses->enc_nonce_low);
+ high = ses->enc_nonce_high;
+ found = true;
+ break;
+ }
+ }
+ spin_unlock(&cifs_tcp_ses_lock);
+
+ if (!found) {
+ trace_smb3_ses_not_found(ses_id);
+ return -EAGAIN;
+ }
+
+ /* 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
+fill_transform_hdr(struct TCP_Server_Info *server,
+ struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
+ struct smb_rqst *old_rq)
{
struct smb2_hdr *shdr =
(struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
@@ -4731,12 +4788,10 @@ fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
tr_hdr->Flags = cpu_to_le16(0x01);
- 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, &shdr->SessionId, 8);
+
+ return smb3_new_enc_nonce(server, le64_to_cpu(shdr->SessionId),
+ server->cipher_type, tr_hdr->Nonce);
}
static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
@@ -5009,7 +5064,9 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
}
/* fill the 1st iov with a transform header */
- fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
+ rc = fill_transform_hdr(server, tr_hdr, orig_len, old_rq);
+ if (rc)
+ goto err_free;
rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
--
2.43.0
^ permalink raw reply related [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-11-27 12:42 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 12:41 [PATCH 0/8] smb: client: fix use-after-free of server->smbd_conn and the encryption nonce Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 1/8] smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put() Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 2/8] smb: client: return the real error from smbd_get_connection() Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 3/8] smb: client: change server->smbd_conn only under server->srv_lock Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 4/8] smb: client: pass server to smbd_get_parameters() Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 5/8] smb: client: pass server to smbd_register_mr() Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 6/8] smb: client: don't use server->smbd_conn without a reference or lock Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 7/8] smb: client: don't use zero RDMA limits without a connection Stefan Metzmacher
2026-11-27 12:41 ` [PATCH 8/8] smb: client: use a counter for the nonce of encrypted messages Stefan Metzmacher
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox