Skip to contentSkip to conversation

smb: client: protect server->smbd_conn with RCU and refcounting

PATCH New Qican Ma 1 participant 1 message Nov 24

Subsystems (3)
lore
Share & tools
lore

Qican Ma

author
#0 lore raw

PATCH v2 1/1 smb: client: protect server->smbd_conn with RCU and refcounting

Several paths check and dereference server->smbd_conn without holding a lock that serializes against smbd_destroy() during reconnect or teardown (including smb2_new_read_req(), smb2_async_writev(), smbd_send(), cifs_readv_from_socket(), cifs_prepare_read/write(), smb3_negotiate_r/wsize(), and smbd_debug_proc_show()). Meanwhile smbd_destroy() releases the underlying socket and calls kfree(info) before clearing server->smbd_conn = NULL, exposing a use-after-free window to concurrent readers.

Annotate server->smbd_conn with __rcu and add a refcount and completion to struct smbd_connection. Readers acquire a short-lived reference via smbd_conn_get() under rcu_read_lock() and drop it with smbd_conn_put(). In smbd_destroy(), clear server->smbd_conn first, shut down the socket to wake any blocked callers, wait for active references and an RCU grace period to complete, and then release the socket and free info in process context. Pass server directly to smbd_recv() and smbd_register_mr() so connection pinning stays encapsulated inside smbdirect.c.

Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (b6a9fa34ce4). Build-tested with CONFIG_CIFS_SMB_DIRECT=y and CONFIG_CIFS_SMB_DIRECT=n; not runtime-tested due to lack of an SMBDirect RDMA test setup.

Fixes f198186aa9bb ("CIFS: SMBD: Establish SMB Direct connection")

---
v2: resend via direct SMTP without line-wrapping corruption in the diff.

 fs/smb/client/cifsglob.h  |   2 +-
 fs/smb/client/connect.c   |  11 ++--
 fs/smb/client/file.c      |  26 ++++++---
 fs/smb/client/smb2ops.c   |  72 ++++++++++++++-----------
 fs/smb/client/smb2pdu.c   |   6 +--
 fs/smb/client/smbdirect.c | 111 ++++++++++++++++++++++++++++----------
 fs/smb/client/smbdirect.h |  16 ++++--
 fs/smb/client/transport.c |   4 +-
 8 files changed, 165 insertions(+), 83 deletions(-)
fs/smb/client/cifsglob.h +1 -1
index 4e16347886e..c0f75f756de 100644
@@ -760,7 +760,7 @@ struct TCP_Server_Info {
 	/* use SMBD connection instead of socket */
 	bool	rdma;
 	/* point to the SMBD connection if RDMA is used instead of socket */
-	struct smbd_connection *smbd_conn;
+	struct smbd_connection __rcu *smbd_conn;
 	struct delayed_work	echo; /* echo ping workqueue job */
 	char	*smallbuf;	/* pointer to current "small" buffer */
 	char	*bigbuf;	/* pointer to current "big" buffer */
fs/smb/client/connect.c +6 -5
index 750b5bb72dc..03072d7a790 100644
@@ -774,8 +774,8 @@ cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
 
 		if (server_unresponsive(server))
 			return -ECONNABORTED;
-		if (cifs_rdma_enabled(server) && server->smbd_conn)
-			length = smbd_recv(server->smbd_conn, smb_msg);
+		if (cifs_rdma_enabled(server))
+			length = smbd_recv(server, smb_msg);
 		else
 			length = sock_recvmsg(server->ssocket, smb_msg, 0);
 
@@ -1888,9 +1888,10 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
 		rc = -ENOENT;
 		goto out_err_crypto_release;
 #endif
-		tcp_ses->smbd_conn = smbd_get_connection(
-			tcp_ses, (struct sockaddr *)&ctx->dstaddr);
-		if (tcp_ses->smbd_conn) {
+		rcu_assign_pointer(tcp_ses->smbd_conn,
+				   smbd_get_connection(tcp_ses,
+						       (struct sockaddr *)&ctx->dstaddr));
+		if (rcu_access_pointer(tcp_ses->smbd_conn)) {
 			cifs_dbg(VFS, "RDMA transport established\n");
 			rc = 0;
 			goto smbd_connected;
fs/smb/client/file.c +18 -8
index d272320436b..a842695f2f0 100644
@@ -391,11 +391,16 @@ static void cifs_prepare_write(struct netfs_io_subrequest *subreq)
 			      cifs_trace_rw_credits_write_prepare);
 
 #ifdef CONFIG_CIFS_SMB_DIRECT
-	if (server->smbd_conn) {
-		const struct smbdirect_socket_parameters *sp =
-			smbd_get_parameters(server->smbd_conn);
+	{
+		struct smbd_connection *conn = smbd_conn_get(server);
 
-		stream->sreq_max_segs = sp->max_frmr_depth;
+		if (conn) {
+			const struct smbdirect_socket_parameters *sp =
+				smbd_get_parameters(conn);
+
+			stream->sreq_max_segs = sp->max_frmr_depth;
+			smbd_conn_put(conn);
+		}
 	}
 #endif
 }
@@ -610,11 +615,16 @@ static int cifs_prepare_read(struct netfs_io_subrequest *subreq)
 			      cifs_trace_rw_credits_read_submit);
 
 #ifdef CONFIG_CIFS_SMB_DIRECT
-	if (server->smbd_conn) {
-		const struct smbdirect_socket_parameters *sp =
-			smbd_get_parameters(server->smbd_conn);
+	{
+		struct smbd_connection *conn = smbd_conn_get(server);
 
-		rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth;
+		if (conn) {
+			const struct smbdirect_socket_parameters *sp =
+				smbd_get_parameters(conn);
+
+			rreq->io_streams[0].sreq_max_segs = sp->max_frmr_depth;
+			smbd_conn_put(conn);
+		}
 	}
 #endif
 	return 0;
fs/smb/client/smb2ops.c +40 -32

84 lines not on this page · raw message

fs/smb/client/smb2pdu.c +3 -3
index 7b7d25364e2..d407ec7c3b7 100644
@@ -4651,7 +4651,7 @@ static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
 		return false;
 
 	/* we can only offload on an rdma connection */
-	if (!server->rdma || !server->smbd_conn)
+	if (!server->rdma || !rcu_access_pointer(server->smbd_conn))
 		return false;
 
 	/* we don't support signed offload yet */
@@ -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 +83 -28

211 lines not on this page · raw message

fs/smb/client/smbdirect.h +13 -3
index be205ec0207..e8cf8f901ca 100644
@@ -12,6 +12,9 @@
 
 #include "cifsglob.h"
 
+#include <linux/completion.h>
+#include <linux/rcupdate.h>
+#include <linux/refcount.h>
 #include <linux/smbdirect.h>
 
 extern int rdma_readwrite_threshold;
@@ -25,8 +28,13 @@ extern int smbd_receive_credit_max;
 
 struct smbd_connection {
 	struct smbdirect_socket *socket;
+	refcount_t refcount;
+	struct completion destroy_done;
 };
 
+struct smbd_connection *smbd_conn_get(struct TCP_Server_Info *server);
+void smbd_conn_put(struct smbd_connection *info);
+
 /* Create a SMBDirect session */
 struct smbd_connection *smbd_get_connection(
 	struct TCP_Server_Info *server, struct sockaddr *dstaddr);
@@ -39,13 +47,13 @@ int smbd_reconnect(struct TCP_Server_Info *server);
 void smbd_destroy(struct TCP_Server_Info *server);
 
 /* Interface for carrying upper layer I/O through send/recv */
-int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
+int smbd_recv(struct TCP_Server_Info *server, struct msghdr *msg);
 int smbd_send(struct TCP_Server_Info *server,
 	int num_rqst, struct smb_rqst *rqst);
 
 /* 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);
@@ -60,8 +68,10 @@ static inline void *smbd_get_connection(
 	struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
 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; }
+static inline int smbd_recv(struct TCP_Server_Info *server, struct msghdr *msg) {return -1; }
 static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
+static inline struct smbd_connection *smbd_conn_get(struct TCP_Server_Info *server) {return NULL; }
+static inline void smbd_conn_put(struct smbd_connection *info) {}
 #endif
 
 #endif
fs/smb/client/transport.c +1 -3
index 375b821ab3e..63d6700e5bd 100644
@@ -257,9 +257,7 @@ int __smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
 	cifs_in_send_inc(server);
 	if (cifs_rdma_enabled(server)) {
 		/* return -EAGAIN when connecting or reconnecting */
-		rc = -EAGAIN;
-		if (server->smbd_conn)
-			rc = smbd_send(server, num_rqst, rqst);
+		rc = smbd_send(server, num_rqst, rqst);
 		goto smbd_done;
 	}
 
Signature · 1 line
2.50.1 (Apple Git-155)