[PATCH v2 03/12] smb: client: let in-flight requests hold a TCP_Server_Info reference

From: Stefan Metzmacher

Date: Fri Dec 04 2026 - 08:34:07 EST


Since commit 482c117c6dbe ("smb: client: hold server info until cifsd
exits") struct TCP_Server_Info is freed by the last of two holders:
the session side (srv_count) and the cifsd demultiplex thread at the
end of clean_demultiplex_info().

But in-flight requests don't hold a reference. clean_demultiplex_info()
completes the pending mids, sleeps 46 seconds if pending_mid_q is still
not empty and then drops its reference, normally the last one. A
request can still use the server at that point, e.g. a thread that just
removed its mid from pending_mid_q, so that the unlocked list_empty()
check skips the 46 second sleep:

CPU0 (request) CPU1 (cifsd)
compound_send_recv()
delete_mid()
spin_lock(&mid_queue_lock)
list_del_init(&mid->qhead)
clean_demultiplex_info()
list_empty(&pending_mid_q) == true
cifs_tcp_server_put(server)
kfree_sensitive(server)
spin_unlock(&mid_queue_lock) <- use after free
release_mid()
__release_mid()
server->vals, server->ops <- use after free

The same applies to everything a request does after removing its mid:
cifs_server_unlock(), cond_sock_shutdown() and add_credits() in
compound_send_recv() and cifs_call_async(), and to requests that are
still running after the 46 seconds.

No time based wait can be correct here, it only makes the race window
smaller. struct TCP_Server_Info has to stay alive as long as anything
still uses it.

So let each mid hold a server reference: alloc_mid() and
smb2_mid_entry_alloc() take one and __release_mid() drops it as its
very last action. The mid callbacks and their callers dereference the
server after the callback released its mid reference, but all callers
of mid_execute_callback() hold their own mid reference across the
callback, so that is covered by the mid reference as well.

compound_send_recv() and cifs_call_async() use the server after
deleting their mids (on send failure) and after the response of an
async request may already have been processed, so they hold their own
reference across the whole request.

Now the struct lives until the last request using it is done, so
clean_demultiplex_info() dropping its reference is no longer a problem.
The 46 second sleep is kept for now.

Fixes: 482c117c6dbe ("smb: client: hold server info until cifsd exits")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Cc: Eduard Zateev <hackerowskiy@xxxxxxxxx>
Cc: Paulo Alcantara <pc@xxxxxxxxxxxxx>
Cc: Namjae Jeon <linkinjeon@xxxxxxxxxx>
Cc: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Cc: Shyam Prasad N <sprasad@xxxxxxxxxxxxx>
Cc: Tom Talpey <tom@xxxxxxxxxx>
Cc: Bharath SM <bharathsm@xxxxxxxxxxxxx>
Cc: linux-cifs@xxxxxxxxxxxxxxx
Cc: samba-technical@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Stefan Metzmacher <metze@xxxxxxxxx>
---
fs/smb/client/smb1transport.c | 2 +
fs/smb/client/smb2transport.c | 2 +
fs/smb/client/transport.c | 82 +++++++++++++++++++++++++++++------
3 files changed, 72 insertions(+), 14 deletions(-)

diff --git a/fs/smb/client/smb1transport.c b/fs/smb/client/smb1transport.c
index 42e95cc1bd89..4ba3a3316288 100644
--- a/fs/smb/client/smb1transport.c
+++ b/fs/smb/client/smb1transport.c
@@ -47,6 +47,8 @@ alloc_mid(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
memset(temp, 0, sizeof(struct mid_q_entry));
refcount_set(&temp->refcount, 1);
spin_lock_init(&temp->mid_lock);
+ /* dropped as the very last step of __release_mid() */
+ cifs_tcp_server_get(server);
temp->mid = get_mid(smb_buffer);
temp->pid = current->pid;
temp->command = cpu_to_le16(smb_buffer->Command);
diff --git a/fs/smb/client/smb2transport.c b/fs/smb/client/smb2transport.c
index 7c4b5faf9de6..53ef87091f47 100644
--- a/fs/smb/client/smb2transport.c
+++ b/fs/smb/client/smb2transport.c
@@ -627,6 +627,8 @@ smb2_mid_entry_alloc(const struct smb2_hdr *shdr,
memset(temp, 0, sizeof(struct mid_q_entry));
refcount_set(&temp->refcount, 1);
spin_lock_init(&temp->mid_lock);
+ /* dropped as the very last step of __release_mid() */
+ cifs_tcp_server_get(server);
temp->mid = le64_to_cpu(shdr->MessageId);
temp->credits = credits > 0 ? credits : 1;
temp->pid = current->pid;
diff --git a/fs/smb/client/transport.c b/fs/smb/client/transport.c
index ea1f4fe42ee5..0fefb84fcd0e 100644
--- a/fs/smb/client/transport.c
+++ b/fs/smb/client/transport.c
@@ -111,6 +111,13 @@ void __release_mid(struct TCP_Server_Info *server, struct mid_q_entry *midEntry)
put_task_struct(midEntry->creator);

mempool_free(midEntry, &cifs_mid_pool);
+
+ /*
+ * Drop the server reference taken when the mid was allocated.
+ * This might be the last reference, so @server must not be
+ * touched after this.
+ */
+ cifs_tcp_server_put(server);
}

void
@@ -694,15 +701,11 @@ static void cond_sock_shutdown(struct TCP_Server_Info *server)
kernel_sock_shutdown(server->ssocket, SHUT_RDWR);
}

-/*
- * Send a SMB request and set the callback function in the mid to handle
- * the result. Caller is responsible for dealing with timeouts.
- */
-int
-cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
- mid_receive_t receive, mid_callback_t callback,
- mid_handle_t handle, void *cbdata, const int flags,
- const struct cifs_credits *exist_credits)
+static int
+__cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
+ mid_receive_t receive, mid_callback_t callback,
+ mid_handle_t handle, void *cbdata, const int flags,
+ const struct cifs_credits *exist_credits)
{
int rc;
struct mid_q_entry *mid;
@@ -776,6 +779,31 @@ cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
return rc;
}

+/*
+ * Send a SMB request and set the callback function in the mid to handle
+ * the result. Caller is responsible for dealing with timeouts.
+ */
+int
+cifs_call_async(struct TCP_Server_Info *server, struct smb_rqst *rqst,
+ mid_receive_t receive, mid_callback_t callback,
+ mid_handle_t handle, void *cbdata, const int flags,
+ const struct cifs_credits *exist_credits)
+{
+ int rc;
+
+ /*
+ * Once the request is sent, the response can be processed and
+ * the mid released before we return. Hold our own reference,
+ * as we still unlock the server and return credits after that.
+ */
+ cifs_tcp_server_get(server);
+ rc = __cifs_call_async(server, rqst, receive, callback, handle,
+ cbdata, flags, exist_credits);
+ cifs_tcp_server_put(server);
+
+ return rc;
+}
+
int cifs_sync_mid_result(struct mid_q_entry *mid, struct TCP_Server_Info *server)
{
int rc = 0;
@@ -911,11 +939,11 @@ struct TCP_Server_Info *cifs_pick_channel(struct cifs_ses *ses)
return server;
}

-int
-compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
- struct TCP_Server_Info *server,
- const int flags, const int num_rqst, struct smb_rqst *rqst,
- int *resp_buf_type, struct kvec *resp_iov)
+static int
+__compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
+ struct TCP_Server_Info *server,
+ const int flags, const int num_rqst, struct smb_rqst *rqst,
+ int *resp_buf_type, struct kvec *resp_iov)
{
int i, j, optype, rc = 0;
int num_processed = 0;
@@ -1188,6 +1216,32 @@ compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
return rc;
}

+int
+compound_send_recv(const unsigned int xid, struct cifs_ses *ses,
+ struct TCP_Server_Info *server,
+ const int flags, const int num_rqst, struct smb_rqst *rqst,
+ int *resp_buf_type, struct kvec *resp_iov)
+{
+ int rc;
+
+ /*
+ * The mids hold server references, but we still use the server
+ * after deleting them (e.g. unlock, cond_sock_shutdown() and
+ * add_credits() on send failure) and cancelled mids are
+ * released by the demultiplex thread. Hold our own reference.
+ *
+ * __compound_send_recv() reports a NULL server.
+ */
+ if (server)
+ cifs_tcp_server_get(server);
+ rc = __compound_send_recv(xid, ses, server, flags, num_rqst, rqst,
+ resp_buf_type, resp_iov);
+ if (server)
+ cifs_tcp_server_put(server);
+
+ return rc;
+}
+
int
cifs_send_recv(const unsigned int xid, struct cifs_ses *ses,
struct TCP_Server_Info *server,
--
2.43.0