Skip to contentSkip to conversation

smb: client: fix use-after-free of server->smbd_conn and the encryption nonce

PATCH In review Stefan Metzmacher 1 participant 10 messages Nov 27

Subsystems (4)
lore
Share & tools
lore

Stefan Metzmacher

author reviewer
#0 lore raw

cover smb: client: fix use-after-free of server->smbd_conn and the encryption nonce

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/[email protected]/),
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(-)
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH 1/8 smb: smbdirect: add smbdirect_socket_get() and smbdirect_socket_put()

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
---
 fs/smb/smbdirect/socket.c | 27 +++++++++++++++++++++++++++
 include/linux/smbdirect.h |  3 +++
 2 files changed, 30 insertions(+)
fs/smb/smbdirect/socket.c +27 -0
index e270e36b3145..13cf50b96bc9 100644
@@ -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,
include/linux/smbdirect.h +3 -0
index 97f5ba730fa7..7f0b1ed6cf16 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH 2/8 smb: client: return the real error from smbd_get_connection()

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Bharath SM <[email protected]>
Cc Long Li <[email protected]>
---
 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(-)
fs/smb/client/connect.c +3 -2
index 750b5bb72dc7..e79f9afe4128 100644
@@ -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;
 		}
 	}
fs/smb/client/smbdirect.c +29 -19

83 lines not on this page · raw message

fs/smb/client/smbdirect.h +1 -1
index be205ec02077..52fef7354f2a 100644
@@ -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; }
Signature · 1 line
2.43.0
author reviewer
#3 lore raw

PATCH 3/8 smb: client: change server->smbd_conn only under server->srv_lock

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/connect.c +13 -6
index e79f9afe4128..064b70bf1fdf 100644
@@ -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);
fs/smb/client/smbdirect.c +23 -4
index d05d90482e74..e9739f9e36c9 100644
@@ -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;
fs/smb/client/smbdirect.h +4 -0
index 52fef7354f2a..283809a57fb6 100644
@@ -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; }
Signature · 1 line
2.43.0
author reviewer
#4 lore raw

PATCH 4/8 smb: client: pass server to smbd_get_parameters()

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/file.c +2 -2
index d272320436b3..f61e80e3d88c 100644
@@ -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;
 	}
fs/smb/client/smb2ops.c +2 -2
index 298f549bd7ed..14383c37ae9e 100644
@@ -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)
 			/*
fs/smb/client/smbdirect.c +5 -3
index e9739f9e36c9..b00224a1d411 100644
@@ -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;
fs/smb/client/smbdirect.h +1 -1
index 283809a57fb6..81fd6b849ade 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#5 lore raw

PATCH 5/8 smb: client: pass server to smbd_register_mr()

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/smb2pdu.c +2 -2
index 7b7d25364e20..d56e84770615 100644
@@ -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;
fs/smb/client/smbdirect.c +2 -1
index b00224a1d411..9fe92db606ad 100644
@@ -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))
fs/smb/client/smbdirect.h +1 -1
index 81fd6b849ade..7d99797de6fa 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#6 lore raw

PATCH 6/8 smb: client: don't use server->smbd_conn without a reference or lock

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/file.c +6 -6
index f61e80e3d88c..1ad742fbe23b 100644
@@ -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;
fs/smb/client/smb2ops.c +8 -8
index 14383c37ae9e..052596978c2b 100644
@@ -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
 
fs/smb/client/smbdirect.c +78 -13

134 lines not on this page · raw message

fs/smb/client/smbdirect.h +2 -1
index 7d99797de6fa..528e2ee81cbd 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#7 lore raw

PATCH 7/8 smb: client: don't use zero RDMA limits without a connection

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/file.c +4 -4
index 1ad742fbe23b..e7ed1ad58f37 100644
@@ -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;
fs/smb/client/smb2ops.c +20 -8
index 052596978c2b..0f82fba40b64 100644
@@ -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
fs/smb/client/smbdirect.c +12 -5
index 99f526281909..5cc199fca558 100644
@@ -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(
fs/smb/client/smbdirect.h +2 -2
index 528e2ee81cbd..ba67bddf5907 100644
@@ -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);
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH 8/8 smb: client: use a counter for the nonce of encrypted messages

In reply to Stefan Metzmacher

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 Paulo Alcantara <[email protected]>
Cc Namjae Jeon <[email protected]>
Cc Ronnie Sahlberg <[email protected]>
Cc Shyam Prasad N <[email protected]>
Cc Tom Talpey <[email protected]>
Cc Bharath SM <[email protected]>
---
 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(-)
fs/smb/client/cifsglob.h +3 -0
index 4e16347886ed..3bfa9b1bacf5 100644
@@ -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];
 
fs/smb/client/misc.c +2 -0
index a8f4c8644562..355b35f5fc63 100644
@@ -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;
 }
fs/smb/client/smb2ops.c +66 -9

94 lines not on this page · raw message

Signature · 1 line
2.43.0
author reviewer
#9 lore raw
In reply to Stefan Metzmacher

Hi Paulo,

FYI: Sashiko reviewed this series against cifs-next, without the urgent smbdirect fixes:
https://sashiko.dev/#/patchset/cover.1795782695.git.metze%40samba.org

- The integer underflow in smb3_negotiate_wsize()/smb3_negotiate_rsize() it mentions for patches 2-4 is the existing problem fixed by patch 7 ("smb: client: don't use zero RDMA limits without a connection").

- For patches 1 and 6 it points out that smbdirect_socket_get() only keeps the memory of the socket alive, so smbdirect_connection_register_mr_io() can still race with smbdirect_socket_release(). That's correct, but it's not a
regression: without this series the same race is a use-after-free of the connection, with it the socket stays valid and only the smaller window inside smbdirect_connection_register_mr_io() remains. That one is fixed by the urgent smbdirect series, "smb:
smbdirect: don't register MRs on a torn-down connection" in v1
(https://lore.kernel.org/linux-cifs/[email protected]/)
and, for the remaining cases Sashiko found there, in v2, which I'll post soon. Both series are needed for the complete fix.

metze

Am 27.11.26 um 13:41 schrieb Stefan Metzmacher:

Quoted 102 lines

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/[email protected]/),
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(-)