ksmbd: fix named-pipe reply loss and operation races
Subsystems (2)
cover ksmbd: fix named-pipe reply loss and operation races
This series fixes two related problems in the ksmbd SMB2 named-pipe implementation.
The first problem is that RPC IPC replies are matched only by the pipe handle. READ, WRITE, FSCTL_PIPE_TRANSCEIVE and CLOSE on one pipe all use that handle, and the session RPC lock is dropped before an IPC request waits for its reply. There is no separate IPC correlation ID, so two operations can overlap and interfere with reply delivery. CLOSE can also release the handle while an earlier operation is still in flight. Protecting only the handle table is not enough: an active pipe needs per-pipe serialization and lifetime.
Patch 1 serializes operations per pipe, keeps the pipe state and handle alive until all users finish, and rechecks cancellation, connection release and session teardown after taking the pipe lock. It erases a closed pipe before waiting for its active operation and releases the handle id only after the last user. Session logoff now clears the RPC pipe list after the other requests have drained. Failed pipe operations use standard SMB2 error replies while preserving the selected status.
The second problem is reply loss with small output buffers. FSCTL_PIPE_TRANSCEIVE copies at most MaxOutputResponse bytes and returns STATUS_BUFFER_OVERFLOW when the RPC reply is larger, but then frees the whole IPC reply. The unread bytes are no longer available to a follow-up READ, so the RPC call can fail even though userspace produced a complete reply. READ also ignores the requested Length when copying an IPC reply.
Patch 2 builds on the per-pipe lifetime from patch 1 and keeps the original reply allocation and an offset on the pipe.
READ and TRANSCEIVE consume only the bytes that fit and retain the rest. The retained reply is freed only when drained, or when the pipe is closed, logged off or disconnected. A failed response preparation or input request does not discard output the client has not received. An IPC failure is reported as an I/O error instead of an empty read or a successful write without a reply. No kernel RPC queue is added; additional reply queueing remains userspace's responsibility.
Reproduction:
- A reproduced NetShareEnum case produced a 2632-byte RPC reply. With a 1024-byte MaxOutputResponse the baseline returned 1024 bytes and discarded the remaining 1608 bytes; a follow-up READ returned STATUS_UNEXPECTED_IO_ERROR. With a 512-byte output limit, 2120 bytes were lost.
- The same overlap/close sequence was exercised with READ, WRITE and TRANSCEIVE on one handle, including CANCEL and LOGOFF.
Wang Zhaolong (2):
ksmbd: avoid races between named-pipe operations
ksmbd: fix named-pipe reply loss with small output buffers
fs/smb/server/mgmt/user_session.c | 127 +++++++++----- fs/smb/server/mgmt/user_session.h | 18 ++ fs/smb/server/smb2pdu.c | 283 ++++++++++++++++++++---------- fs/smb/server/transport_ipc.c | 45 +++-- fs/smb/server/transport_ipc.h | 14 +- 5 files changed, 323 insertions(+), 164 deletions(-)
base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
Signature · 1 line
2.56.0
PATCH 1/2 ksmbd: avoid races between named-pipe operations
RPC IPC replies are matched by pipe handle. READ, WRITE, TRANSCEIVE and CLOSE on the same pipe all use that handle. The session RPC lock protects the handle table, but is dropped before a pipe I/O request waits for its reply. Another operation, including CLOSE, can issue a request with the same handle while that wait is still in progress.
These requests have no separate IPC correlation ID, so overlapping waits can interfere with reply delivery. CLOSE can also release the handle before an earlier operation finishes. Protecting table access alone does not provide the serialization or lifetime needed by an active pipe.
Serialize operations per pipe and keep its state and handle alive until all users finish. Avoid holding the session table lock across IPC waits, so a blocked pipe does not hold up unrelated pipes. This also provides the lifetime needed to retain unread replies in the following patch.
Recheck cancellation, connection release and session teardown after taking the pipe lock, and mark a removed pipe closed before waiting for its active operation. Close all session RPC pipes on logoff after the other requests have drained; keeping the TCP connection open must not retain old handles. Use standard SMB2 error replies for failed pipe operations while preserving the selected status.
--- fs/smb/server/mgmt/user_session.c | 126 +++++++++++++------- fs/smb/server/mgmt/user_session.h | 15 +++ fs/smb/server/smb2pdu.c | 189 +++++++++++++++++++++--------- fs/smb/server/transport_ipc.c | 45 ++++--- fs/smb/server/transport_ipc.h | 14 ++- 5 files changed, 262 insertions(+), 127 deletions(-)
fs/smb/server/mgmt/user_session.c +84 -42
@@ -3,6 +3,7 @@* Copyright (C) 2018 Samsung Electronics Co., Ltd. */ +#include <linux/err.h> #include <linux/list.h> #include <linux/slab.h> #include <linux/rwsem.h>
@@ -26,10 +27,6 @@ static DEFINE_IDA(session_ida); static DEFINE_HASHTABLE(sessions_table, SESSION_HASH_BITS);static DECLARE_RWSEM (sessions_table_lock); -struct ksmbd_session_rpc { -int id; -unsigned int method; -};#ifdef CONFIG_PROC_FS
@@ -278,32 +275,34 @@ static void free_channel_list(struct ksmbd_session *sess)up_write (&sess->chann_lock); } -static void __session_rpc_close (struct ksmbd_session *sess, -struct ksmbd_session_rpc *entry) +static void ksmbd_session_rpc_free (struct ksmbd_session_rpc *entry) +{ +if (refcount_dec_and_test (&entry->refcount)) { +ksmbd_rpc_id_free (entry->id); +kfree (entry); + } +} + +static void __session_rpc_close (struct ksmbd_session_rpc *entry) {struct ksmbd_rpc_command *resp; - resp =ksmbd_rpc_close (sess, entry->id); +mutex_lock (&entry->lock); + resp =ksmbd_rpc_close (entry);if (!resp)pr_err ("Unable to close RPC pipe %d\n" , entry->id); -kvfree (resp); -ksmbd_rpc_id_free (entry->id); -kfree (entry); +mutex_unlock (&entry->lock); +ksmbd_session_rpc_free (entry); } -static void ksmbd_session_rpc_clear_list (struct ksmbd_session *sess) +void ksmbd_session_rpc_clear_list (struct ksmbd_session *sess) {struct ksmbd_session_rpc *entry; -long index; - -down_write (&sess->rpc_lock); -xa_for_each (&sess->rpc_handle_list, index, entry) { -xa_erase (&sess->rpc_handle_list, index); -__session_rpc_close (sess, entry); - } -up_write (&sess->rpc_lock); +unsigned long index; +xa_for_each (&sess->rpc_handle_list, index, entry) +ksmbd_session_rpc_close (sess, index);xa_destroy (&sess->rpc_handle_list); }
@@ -333,9 +332,9 @@ static int __rpc_method(char *rpc_name)int ksmbd_session_rpc_open (struct ksmbd_session *sess,char *rpc_name) { -struct ksmbd_session_rpc *entry, *old; +struct ksmbd_session_rpc *entry;struct ksmbd_rpc_command *resp; -int method, id; +int method, id, ret; method =__rpc_method (rpc_name);if (method <0 )
@@ -347,31 +346,29 @@ int ksmbd_session_rpc_open(struct ksmbd_session *sess, char *rpc_name) entry->method = method; entry->id = id =ksmbd_ipc_id_alloc (); -if (id <0 ) - goto free_entry; - -down_write (&sess->rpc_lock); - old =xa_store (&sess->rpc_handle_list, id, entry,KSMBD_DEFAULT_GFP ); -if (xa_is_err (old)) { -up_write (&sess->rpc_lock); - goto free_id; +if (id <0 ) { +kfree (entry); +return id; } +mutex_init (&entry->lock); +refcount_set (&entry->refcount,1 ); - resp =ksmbd_rpc_open (sess, id); + resp =ksmbd_rpc_open (entry);if (!resp) { -xa_erase (&sess->rpc_handle_list, entry->id); -up_write (&sess->rpc_lock); - goto free_id; +ksmbd_session_rpc_free (entry); +return -EINVAL ; } +kvfree (resp); +down_write (&sess->rpc_lock); + ret =xa_err (xa_store (&sess->rpc_handle_list, id, entry, +KSMBD_DEFAULT_GFP ));up_write (&sess->rpc_lock); -kvfree (resp); +if (ret) { +__session_rpc_close (entry); +return ret; + }return id; -free_id: -ksmbd_rpc_id_free (entry->id); -free_entry: -kfree (entry); -return -EINVAL ; }void ksmbd_session_rpc_close (struct ksmbd_session *sess,int id)
@@ -381,8 +378,53 @@ void ksmbd_session_rpc_close(struct ksmbd_session *sess, int id)down_write (&sess->rpc_lock); entry =xa_erase (&sess->rpc_handle_list, id);if (entry) -__session_rpc_close (sess, entry); +WRITE_ONCE (entry->closed, true);up_write (&sess->rpc_lock); +if (entry) +__session_rpc_close (entry); +} + +/* The reference pins the entry while waiting without holding rpc_lock. */ +struct ksmbd_session_rpc *ksmbd_session_rpc_get (struct ksmbd_work *work, +int id) +{ +struct ksmbd_session_rpc *entry; +int err; + +down_read (&work->sess->rpc_lock); + entry =xa_load (&work->sess->rpc_handle_list, id); +if (entry) +refcount_inc (&entry->refcount); +up_read (&work->sess->rpc_lock); +if (!entry) +return ERR_PTR (-ENOENT ); + +mutex_lock (&entry->lock); +if (READ_ONCE (work->state) ==KSMBD_WORK_CANCELLED ) { + err = -ECANCELED ; + goto err_put; + } +if (ksmbd_conn_exiting (work->conn) || +ksmbd_conn_releasing (work->conn) || +READ_ONCE (work->sess->tearing_down)) { + err = -ESHUTDOWN ; + goto err_put; + } +if (READ_ONCE (entry->closed)) { + err = -EBADF ; + goto err_put; + } +return entry; + +err_put: +ksmbd_session_rpc_put (entry); +return ERR_PTR (err); +} + +void ksmbd_session_rpc_put (struct ksmbd_session_rpc *entry) +{ +mutex_unlock (&entry->lock); +ksmbd_session_rpc_free (entry); }int ksmbd_session_rpc_method (struct ksmbd_session *sess,int id)
@@ -779,14 +821,14 @@ void destroy_previous_session(struct ksmbd_conn *conn,up_write (&prev_sess->chann_lock); goto out; } - prev_sess->tearing_down = true; +WRITE_ONCE (prev_sess->tearing_down, true);up_write (&prev_sess->chann_lock);ksmbd_all_conn_set_status (prev_sess,KSMBD_SESS_NEED_RECONNECT ); err =ksmbd_conn_wait_idle_sess (conn, prev_sess);if (err) {down_write (&prev_sess->chann_lock); - prev_sess->tearing_down = false; +WRITE_ONCE (prev_sess->tearing_down, false);up_write (&prev_sess->chann_lock);ksmbd_all_conn_set_status (prev_sess,KSMBD_SESS_GOOD ); goto out;
fs/smb/server/mgmt/user_session.h +15 -0
@@ -7,6 +7,8 @@#define __USER_SESSION_MANAGEMENT_H__#include <linux/hashtable.h> +#include <linux/mutex.h> +#include <linux/refcount.h> #include <linux/xarray.h> #include "../smb_common.h"
@@ -17,6 +19,15 @@#define PREAUTH_HASHVALUE_SIZE 64struct ksmbd_file_table ; +struct ksmbd_work ; + +struct ksmbd_session_rpc { +int id; +unsigned int method; +struct mutex lock; +refcount_t refcount; +bool closed; +};struct channel {char sess_key[CIFS_KEY_SIZE ];
@@ -123,7 +134,11 @@ void ksmbd_release_tree_conn_id(struct ksmbd_session *sess, int id);int ksmbd_session_rpc_open (struct ksmbd_session *sess,char *rpc_name);void ksmbd_session_rpc_close (struct ksmbd_session *sess,int id); +void ksmbd_session_rpc_clear_list (struct ksmbd_session *sess);int ksmbd_session_rpc_method (struct ksmbd_session *sess,int id); +struct ksmbd_session_rpc *ksmbd_session_rpc_get (struct ksmbd_work *work, +int id); +void ksmbd_session_rpc_put (struct ksmbd_session_rpc *entry);void ksmbd_user_session_get (struct ksmbd_session *sess);void ksmbd_user_session_put (struct ksmbd_session *sess);int create_proc_sessions (void );
fs/smb/server/smb2pdu.c +133 -56
294 lines not on this page · raw message
fs/smb/server/transport_ipc.c +21 -24
125 lines not on this page · raw message
fs/smb/server/transport_ipc.h +9 -5
@@ -16,6 +16,7 @@ struct ksmbd_login_response_ext * ksmbd_ipc_login_request_ext(const char *account);struct ksmbd_session ; +struct ksmbd_session_rpc ;struct ksmbd_share_config ;struct ksmbd_tree_connect ;struct sockaddr ;
@@ -34,12 +35,15 @@ struct ksmbd_spnego_authen_response * ksmbd_ipc_spnego_authen_request(const char *spnego_blob, int blob_len);int ksmbd_ipc_id_alloc (void );void ksmbd_rpc_id_free (int handle); -struct ksmbd_rpc_command *ksmbd_rpc_open (struct ksmbd_session *sess,int handle); -struct ksmbd_rpc_command *ksmbd_rpc_close (struct ksmbd_session *sess,int handle); -struct ksmbd_rpc_command *ksmbd_rpc_write (struct ksmbd_session *sess,int handle, +struct ksmbd_rpc_command *ksmbd_rpc_open (struct ksmbd_session_rpc *entry); +struct ksmbd_rpc_command *ksmbd_rpc_close (struct ksmbd_session_rpc *entry); +struct ksmbd_rpc_command *ksmbd_rpc_write (struct ksmbd_session *sess, +struct ksmbd_session_rpc *entry,void *payload,size_t payload_sz); -struct ksmbd_rpc_command *ksmbd_rpc_read (struct ksmbd_session *sess,int handle); -struct ksmbd_rpc_command *ksmbd_rpc_ioctl (struct ksmbd_session *sess,int handle, +struct ksmbd_rpc_command *ksmbd_rpc_read (struct ksmbd_session *sess, +struct ksmbd_session_rpc *entry); +struct ksmbd_rpc_command *ksmbd_rpc_ioctl (struct ksmbd_session *sess, +struct ksmbd_session_rpc *entry,void *payload,size_t payload_sz);void ksmbd_ipc_release (void );void ksmbd_ipc_soft_reset (void );
Signature · 1 line
2.56.0
PATCH 2/2 ksmbd: fix named-pipe reply loss with small output buffers
In reply to Wang ZhaolongFSCTL_PIPE_TRANSCEIVE copies at most MaxOutputResponse bytes from an RPC reply and reports STATUS_BUFFER_OVERFLOW when it does not fit. It then frees the complete IPC reply, including the bytes that were not returned. The client cannot recover those bytes with a subsequent READ, so an RPC call can fail even though userspace produced a complete reply. Pipe READ also ignores the requested Length when copying an IPC reply.
In the reproduced Windows NetShareEnum case, a 2632-byte reply is limited to 1024 bytes of SMB output. The remaining 1608 bytes are discarded instead of being available to the follow-up READ. Increasing the application-level enumeration budget does not change that SMB output limit.
Keep unread reply data on the pipe and respect each operation's output limit. Preserve it until successfully included in a response: response preparation failures and later input must not discard output that the client has yet to receive. Treat a failed IPC exchange as an I/O error, rather than reporting an empty read or acknowledging a write without an IPC reply. Additional RPC reply queueing remains userspace's responsibility.
Keep cached bytes on failed WRITE or TRANSCEIVE requests, and consume them only after the response is pinned successfully. Release the original reply when drained or when its pipe is closed, logged off or disconnected.
--- fs/smb/server/mgmt/user_session.c | 1 + fs/smb/server/mgmt/user_session.h | 3 + fs/smb/server/smb2pdu.c | 164 +++++++++++++++++------------- 3 files changed, 96 insertions(+), 72 deletions(-)
fs/smb/server/mgmt/user_session.c +1 -0
@@ -279,6 +279,7 @@ static void ksmbd_session_rpc_free(struct ksmbd_session_rpc *entry) {if (refcount_dec_and_test (&entry->refcount)) {ksmbd_rpc_id_free (entry->id); +kvfree (entry->pending);kfree (entry); } }
fs/smb/server/mgmt/user_session.h +3 -0
@@ -19,6 +19,7 @@#define PREAUTH_HASHVALUE_SIZE 64struct ksmbd_file_table ; +struct ksmbd_rpc_command ;struct ksmbd_work ;struct ksmbd_session_rpc {
@@ -27,6 +28,8 @@ struct ksmbd_session_rpc {struct mutex lock;refcount_t refcount;bool closed; +struct ksmbd_rpc_command *pending; +unsigned int pending_offset; };struct channel {
fs/smb/server/smb2pdu.c +92 -72
250 lines not on this page · raw message
Signature · 1 line
2.56.0
kernel test robot
build botRe: [PATCH 2/2] ksmbd: fix named-pipe reply loss with small output buffers
kernel test robot noticed the following build warnings:
Hi Wang,
kernel test robot noticed the following build warnings:
[auto build test WARNING on f47d223627f80cb7a1eb14c585fe6cb35de5e984]
url: https://github.com/intel-lab-lkp/linux/commits/Wang-Zhaolong/ksmbd-avoid-races-between-named-pipe-operations/20261025-173237 base: f47d223627f80cb7a1eb14c585fe6cb35de5e984 patch link: https://lore.kernel.org/r/TYZPR02MB695916EEB5AC02FE8DBC31BCE6622%40TYZPR02MB6959.apcprd02.prod.outlook.com patch subject: [PATCH 2/2] ksmbd: fix named-pipe reply loss with small output buffers config: powerpc64-randconfig-1001-20261026 (https://download.01.org/0day-ci/archive/20261026/[email protected]/config) compiler: powerpc64-linux-gcc (GCC) 14.4.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261026/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: fs/smb/server/smb2pdu.c:8670 function parameter 'rpc' not described in 'smb2_pipe_response_done' >> Warning: fs/smb/server/smb2pdu.c:8670 function parameter 'nbytes' not described in 'smb2_pipe_response_done' >> Warning: fs/smb/server/smb2pdu.c:8670 expecting prototype for smb2_read_pipe(). Prototype was for smb2_pipe_response_done() instead
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki