ksmbd: fix connection and durable handle teardown races
Subsystems (2)
v2 cover ksmbd: fix connection and durable handle teardown races
This series fixes lifetime bugs around ksmbd connection shutdown, session file-table teardown, and durable handle scavenging.
Patch 1 centralizes the final struct ksmbd_conn release so every last putter runs ida_destroy() and transport cleanup. The release is queued to a dedicated workqueue because transport teardown can sleep, while one known last-putter is an RCU callback.
Patch 2 hardens __close_file_table_ids() by taking a transient ksmbd_file reference, unpublishing from the session idr under ft->lock, and doing sleepable preserve/close work outside ft->lock. It also makes the FP_NEW window visible to the opener through ksmbd_update_fstate().
Patch 2 is scoped to file-table teardown and the FP_NEW publication window. It does not try to fix durable reconnect rollback on later smb2_open() error paths (and the related post-FP_INITED reference window in fresh smb2_open). Both already exist before this series and need either an explicit unpublish-on-error step or an extra session-owned reference. That is left as follow-up work. The FP_NEW -> FP_INITED failure reuses smb2_open()'s existing -ENOENT to STATUS_OBJECT_NAME_INVALID mapping to avoid changing wire behavior in this lifetime fix.
Patch 3 closes two related races in the durable scavenger against any walker that iterates f_ci->m_fp_list (ksmbd_lookup_fd_inode() and the share-mode checks). The scavenger no longer reuses fp->node as a scavenger-private collect-list node, takes an explicit transient reference under global_ft.lock, and drops both the durable lifetime and transient refs with atomic_sub_and_test(2, ...) after the m_fp_list unlink so an in-flight m_fp_list walker that snatched fp owns the final close cleanly. fp->persistent_id is cleared inside __ksmbd_remove_durable_fd() so a delayed final close cannot re-issue idr_remove() on a slot that idr_alloc_cyclic() may have already re-handed to a new durable handle. __put_fd_final() bypasses the per-conn open_files_count decrement when fp is detached from any session table (fp->conn cleared by session_fd_check() at durable preserve, paired with the volatile_id clear at unpublish), since the walker that owns the final close in that case runs from an unrelated work->conn whose counter never tracked this durable fp.
The series is intentionally scoped to lifetime/race fixes and the walker-final-putter regression that the durable scavenger handoff in patch 3 newly exposes. Pre-existing reconnect rollback and per-conn open_files_count accounting gaps are left as follow-up work so this series does not have to claim a full durable-reconnect or accounting cleanup.
Validation:
* abrupt-disconnect kmemleak/kprobe A/B for connection release
* same-session two-tcon DEBUG_LIST/DEBUG_OBJECTS_WORK stress
* forced durable-preserve sleep-path harness for session teardown
* KASAN-enabled direct SMB2 coverage for session/tree teardown and durable-preserve paths
* KASAN-enabled direct SMB2 coverage for durable scavenger expiry racing with m_fp_list lookups
* checkpatch --strict for all patches
* make -j$(nproc) M=fs/smb/server
v1 -> v2:
* Split the original change into bisectable patches: connection final release, session file-table teardown, and durable scavenger races.
* Keep sleepable session preserve/close work out of ft->lock and make the FP_NEW publication race visible through a cleared volatile id.
* Document that durable reconnect rollback on later smb2_open() error paths is a pre-existing follow-up item, and keep the existing -ENOENT to STATUS_OBJECT_NAME_INVALID wire mapping for this lifetime fix.
* Avoid reusing fp->node as a temporary durable scavenger list node and take a transient reference in the durable scavenger so concurrent ksmbd_lookup_fd_inode() walkers cannot UAF on freed fp. These two fixes are folded into a single patch because the list-head-reuse fix alone leaves a deterministic UAF window for m_fp_list walkers; bisecting onto an intermediate state would land on a use-after-free that pre-patch chaos merely made less reproducible.
* Clear fp->persistent_id in __ksmbd_remove_durable_fd() so a holder that owns the final close after a scavenger removal does not re-issue idr_remove() on a persistent id that may have already been handed out to a new durable handle.
* Bypass the per-conn open_files_count decrement in __put_fd_final() when fp is detached from any session table, so an m_fp_list walker that owns the final close of a scavenged durable fp does not underflow an unrelated conn's stats counter.
* Document the ksmbd_conn_wq lifetime invariant in ksmbd_conn_put() instead of guarding with WARN_ON_ONCE, so a violation surfaces as a NULL deref rather than a silent leak of the final release.
DaeMyung Kang (3):
ksmbd: centralize ksmbd_conn final release to plug transport leak
ksmbd: harden file lifetime during session teardown
ksmbd: close durable scavenger races against m_fp_list lookups
fs/smb/server/connection.c | 101 +++++++++-- fs/smb/server/connection.h | 6 + fs/smb/server/oplock.c | 7 +- fs/smb/server/server.c | 12 ++ fs/smb/server/smb2pdu.c | 6 +- fs/smb/server/vfs_cache.c | 341 ++++++++++++++++++++++++++++++------- fs/smb/server/vfs_cache.h | 4 +- 7 files changed, 396 insertions(+), 81 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 ksmbd: centralize ksmbd_conn final release to plug transport leak
ksmbd_conn_free() is one of four sites that can observe the last refcount drop of a struct ksmbd_conn. The other three
fs/smb/server/connection.c ksmbd_conn_r_count_dec()
fs/smb/server/oplock.c __free_opinfo()
fs/smb/server/vfs_cache.c session_fd_check()end the conn with a bare kfree(), skipping ida_destroy(&conn->async_ida) and conn->transport->ops->free_transport(conn->transport). Whenever one of them is the last putter, the embedded async_ida and the entire transport struct leak -- for TCP, that is also the struct socket and the kvec iov.
__free_opinfo() being a final putter is not theoretical. opinfo_put() queues the callback via call_rcu(&opinfo->rcu, free_opinfo_rcu), so ksmbd_server_terminate_conn() can deposit N opinfo releases in RCU and have ksmbd_conn_free() run in the handler thread before any of them fire. ksmbd_conn_free() then observes refcnt > 0 and short-circuits; the last RCU-delivered __free_opinfo() falls onto its bare kfree(conn) branch and the transport is lost.
A/B validation in a QEMU/virtme guest, mounting //127.0.0.1/testshare:
each iteration holds 8 files open via sleep processes, force-closes TCP with "ss -K sport = :445", kills the holders, lazy-umounts; repeated 10 times, then ksmbd shutdown and kmemleak scan.
state conn_alloc conn_free tcp_free opi_rcu kmemleak
---------- ---------- --------- -------- ------- --------
pre-patch 20 20 10 160 7
with patch 20 20 20 160 0Pre-patch conn_free=20 with tcp_free=10 directly demonstrates the bare-kfree paths skipping transport cleanup; kmemleak backtraces point into struct tcp_transport / iov. With this patch tcp_free matches conn_free at 20/20 and kmemleak is clean.
Move the per-struct final release into __ksmbd_conn_release_work() and route the three bare-kfree final-put sites through a new ksmbd_conn_put(). Those sites now pair ida_destroy() and free_transport() with kfree(conn) regardless of which holder happens to release the last reference. stop_sessions() only triggers the transport shutdown and does not itself drop the last conn reference, so it is unaffected.
The centralized release reaches sock_release() -> tcp_close() -> lock_sock_nested() (might_sleep) from every final putter, including __free_opinfo() invoked from an RCU softirq callback, which trips CONFIG_DEBUG_ATOMIC_SLEEP. Defer the release to a dedicated ksmbd_conn_wq workqueue so ksmbd_conn_put() is safe from any non-sleeping context.
Make ksmbd_file own a strong connection reference while fp->conn is non-NULL so durable-preserve and final-close paths cannot dereference a stale connection. ksmbd_open_fd() and ksmbd_reopen_durable_fd() take the reference via ksmbd_conn_get() (the latter also reorders the fp->conn / fp->tcon assignments before __open_id() so the published fp is never observed with fp->conn == NULL); session_fd_check() and __ksmbd_close_fd() drop it via ksmbd_conn_put(). With that invariant, session_fd_check() can take a local conn pointer once and use it across the m_op_list and lock_list iterations even though op->conn puts may otherwise drop the last reference.
At module exit the workqueue is flushed and destroyed after rcu_barrier(), so any release queued by a trailing RCU callback is drained before the inode hash and module text go away.
--- fs/smb/server/connection.c | 101 +++++++++++++++++++++++++++++++------ fs/smb/server/connection.h | 6 +++ fs/smb/server/oplock.c | 7 +-- fs/smb/server/server.c | 12 +++++ fs/smb/server/vfs_cache.c | 60 ++++++++++++++++++---- 5 files changed, 156 insertions(+), 30 deletions(-)
fs/smb/server/connection.c +86 -15
@@ -79,6 +79,81 @@ static int create_proc_clients(void) { return 0; }static void delete_proc_clients (void ) {}#endif +static struct workqueue_struct *ksmbd_conn_wq; + +int ksmbd_conn_wq_init (void ) +{ + ksmbd_conn_wq =alloc_workqueue ("ksmbd-conn-release" , +WQ_UNBOUND |WQ_MEM_RECLAIM ,0 ); +if (!ksmbd_conn_wq) +return -ENOMEM ; +return 0 ; +} + +void ksmbd_conn_wq_destroy (void ) +{ +if (ksmbd_conn_wq) { +destroy_workqueue (ksmbd_conn_wq); + ksmbd_conn_wq =NULL ; + } +} + +/* +* __ksmbd_conn_release_work() - perform the final, once-per-struct cleanup +* of a ksmbd_conn whose refcount has just dropped to zero. +* +* This is the common release path used by ksmbd_conn_put() for the embedded +* state that outlives the connection thread: async_ida and the attached +* transport (which owns the socket and iov for TCP). Called from a workqueue +* so that sleep-allowed teardown (sock_release -> tcp_close -> +* lock_sock_nested) never runs from an RCU softirq callback (free_opinfo_rcu) +* or any other non-sleeping putter context. +*/ +static void __ksmbd_conn_release_work (struct work_struct *work) +{ +struct ksmbd_conn *conn = + container_of(work, struct ksmbd_conn, release_work); + +ida_destroy (&conn->async_ida); + conn->transport->ops->free_transport (conn->transport); +kfree (conn); +} + +/** +* ksmbd_conn_get() - take a reference on @conn and return it. +* +* Returns @conn unchanged so callers can write +* "fp->conn = ksmbd_conn_get(work->conn);" in one expression. Returns NULL +* if @conn is NULL. +*/ +struct ksmbd_conn *ksmbd_conn_get (struct ksmbd_conn *conn) +{ +if (!conn) +return NULL ; + +atomic_inc (&conn->refcnt); +return conn; +} + +/** +* ksmbd_conn_put() - drop a reference and, if it was the last, queue the +* release onto ksmbd_conn_wq so it runs from process context. +* +* Callable from any context including RCU softirq callbacks and non-sleeping +* locks; the actual release is deferred to the workqueue. ksmbd_conn_wq is +* created in ksmbd_server_init() before any conn can be allocated and is +* destroyed in ksmbd_server_exit() after rcu_barrier(), so it is always +* non-NULL while a conn reference is held. +*/ +void ksmbd_conn_put (struct ksmbd_conn *conn) +{ +if (!conn) +return ; + +if (atomic_dec_and_test (&conn->refcnt)) +queue_work (ksmbd_conn_wq, &conn->release_work); +} +/** * ksmbd_conn_free() - free resources of the connection instance *
@@ -93,23 +168,19 @@ void ksmbd_conn_free(struct ksmbd_conn *conn)hash_del (&conn->hlist);up_write (&conn_list_lock); +/* +* request_buf / preauth_info / mechToken are only ever accessed by the +* connection handler thread that owns @conn. ksmbd_conn_free() is +* called from the transport free_transport() path when that thread is +* exiting, so it is safe to release them unconditionally even when +* ksmbd_conn_put() below is not the final putter (oplock / ksmbd_file +* holders only retain the conn pointer, not these per-thread buffers). +*/ xa_destroy (&conn->sessions);kvfree (conn->request_buf);kfree (conn->preauth_info);kfree (conn->mechToken); -if (atomic_dec_and_test (&conn->refcnt)) { -/* -* async_ida is embedded in struct ksmbd_conn, so pair -* ida_destroy() with the final kfree() rather than with -* the unconditional field teardown above. This keeps -* the IDA valid for the entire lifetime of the struct, -* even while other refcount holders (oplock / vfs -* durable handles) still reference the connection. -*/ -ida_destroy (&conn->async_ida); - conn->transport->ops->free_transport (conn->transport); -kfree (conn); - } +ksmbd_conn_put (conn); }/**
@@ -136,6 +207,7 @@ struct ksmbd_conn *ksmbd_conn_alloc(void) conn->um =ERR_PTR (-EOPNOTSUPP );if (IS_ERR (conn->um)) conn->um =NULL ; +INIT_WORK (&conn->release_work, __ksmbd_conn_release_work);atomic_set (&conn->req_running,0 );atomic_set (&conn->r_count,0 );atomic_set (&conn->refcnt,1 );
@@ -512,8 +584,7 @@ void ksmbd_conn_r_count_dec(struct ksmbd_conn *conn)if (!atomic_dec_return (&conn->r_count) &&waitqueue_active (&conn->r_count_q))wake_up (&conn->r_count_q); -if (atomic_dec_and_test (&conn->refcnt)) -kfree (conn); +ksmbd_conn_put (conn); }int ksmbd_conn_transport_init (void )
fs/smb/server/connection.h +6 -0
@@ -16,6 +16,7 @@#include <linux/kthread.h> #include <linux/nls.h> #include <linux/unicode.h> +#include <linux/workqueue.h> #include "smb_common.h" #include "ksmbd_work.h"
@@ -119,6 +120,7 @@ struct ksmbd_conn {bool binding;atomic_t refcnt;bool is_aapl; +struct work_struct release_work; };struct ksmbd_conn_ops {
@@ -163,6 +165,10 @@ void ksmbd_conn_wait_idle(struct ksmbd_conn *conn);int ksmbd_conn_wait_idle_sess_id (struct ksmbd_conn *curr_conn,u64 sess_id);struct ksmbd_conn *ksmbd_conn_alloc (void );void ksmbd_conn_free (struct ksmbd_conn *conn); +struct ksmbd_conn *ksmbd_conn_get (struct ksmbd_conn *conn); +void ksmbd_conn_put (struct ksmbd_conn *conn); +int ksmbd_conn_wq_init (void ); +void ksmbd_conn_wq_destroy (void );bool ksmbd_conn_lookup_dialect (struct ksmbd_conn *c);int ksmbd_conn_write (struct ksmbd_work *work); int ksmbd_conn_rdma_read(struct ksmbd_conn *conn,
fs/smb/server/oplock.c +2 -5
@@ -30,7 +30,6 @@ static DEFINE_RWLOCK(lease_list_lock);static struct oplock_info *alloc_opinfo (struct ksmbd_work *work,u64 id,__u16 Tid) { -struct ksmbd_conn *conn = work->conn;struct ksmbd_session *sess = work->sess;struct oplock_info *opinfo;
@@ -39,7 +38,7 @@ static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,return NULL ; opinfo->sess = sess; - opinfo->conn = conn; + opinfo->conn =ksmbd_conn_get (work->conn); opinfo->level =SMB2_OPLOCK_LEVEL_NONE ; opinfo->op_state =OPLOCK_STATE_NONE ; opinfo->pending_break =0 ;
@@ -50,7 +49,6 @@ static struct oplock_info *alloc_opinfo(struct ksmbd_work *work,init_waitqueue_head (&opinfo->oplock_brk);atomic_set (&opinfo->refcount,1 );atomic_set (&opinfo->breaking_cnt,0 ); -atomic_inc (&opinfo->conn->refcnt);return opinfo; }
@@ -132,8 +130,7 @@ static void __free_opinfo(struct oplock_info *opinfo) {if (opinfo->is_lease)free_lease (opinfo); -if (opinfo->conn &&atomic_dec_and_test (&opinfo->conn->refcnt)) -kfree (opinfo->conn); +ksmbd_conn_put (opinfo->conn);kfree (opinfo); }
fs/smb/server/server.c +12 -0
@@ -596,8 +596,14 @@ static int __init ksmbd_server_init(void)if (ret) goto err_crypto_destroy; + ret =ksmbd_conn_wq_init (); +if (ret) + goto err_workqueue_destroy; +return 0 ; +err_workqueue_destroy: +ksmbd_workqueue_destroy (); err_crypto_destroy:ksmbd_crypto_destroy (); err_release_inode_hash:
@@ -623,6 +629,12 @@ static void __exit ksmbd_server_exit(void) {ksmbd_server_shutdown ();rcu_barrier (); +/* +* ksmbd_conn_put() defers the final release onto ksmbd_conn_wq, +* so drain it after rcu_barrier() has fired any pending RCU +* callbacks that may have queued a release. +*/ +ksmbd_conn_wq_destroy ();ksmbd_release_inode_hash (); }
fs/smb/server/vfs_cache.c +50 -10
123 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 2/3 ksmbd: harden file lifetime during session teardown
In reply to DaeMyung Kang__close_file_table_ids() is the per-session teardown that closes every fp belonging to a session (or to one tree connect on that session) by walking the session's volatile-id idr. The current loop has three related problems on busy or racing workloads:
* Sleeping under ft->lock. The session-teardown skip callback, session_fd_check(), already sleeps in ksmbd_vfs_copy_durable_owner() -> kstrdup(GFP_KERNEL) and down_write(&fp->f_ci->m_lock) (a rw_semaphore). Running the callback inside write_lock(&ft->lock) trips CONFIG_DEBUG_ATOMIC_SLEEP / CONFIG_PROVE_LOCKING on a durable-fd workload.
* Refcount accounting blind to f_state. The unconditional atomic_dec_and_test(&fp->refcount) does not distinguish FP_INITED (idr-owned reference still intact) from FP_CLOSED (an earlier ksmbd_close_fd() already consumed the idr-owned reference while leaving fp in the idr because a holder kept refcount non-zero). When the latter races with teardown the same path over-decrements into a holder reference and ksmbd_fd_put() later UAFs that holder.
* FP_NEW window. Between __open_id() publishing fp into the session idr and ksmbd_update_fstate(..., FP_INITED) committing the transition at the end of smb2_open(), an fp is in FP_NEW and an intervening teardown that takes a transient reference and unpublishes the volatile id leaves the original idr-owned reference orphaned -- the opener is unaware that fp has been unpublished, returns success to the client, and the fp leaks at refcount = 1.
Refactor __close_file_table_ids() to take a transient reference on fp and unpublish fp from the session idr *under ft->lock* before calling skip() outside the lock. A transient ref protects lifetime but not concurrent field mutation, so the idr_remove() is what keeps __ksmbd_lookup_fd() through this session's idr from granting a new ksmbd_fp_get() reference to an fp whose fp->conn / fp->tcon / fp->volatile_id / op->conn / lock_list links are about to be rewritten by session_fd_check(). Durable reconnect is unaffected because it reaches fp through the global durable table (ksmbd_lookup_durable_fd -> global_ft).
Decide n_to_drop together with any FP_INITED -> FP_CLOSED transition under ft->lock so teardown and ksmbd_close_fd() never both consume the idr-owned reference. See ksmbd_mark_fp_closed() for the per-state accounting. For the FP_NEW path to be safe, the opener has to learn that fp was unpublished: ksmbd_update_fstate() now returns -ENOENT when an FP_NEW -> FP_INITED transition finds f_state already advanced or the volatile id cleared (both committed by teardown under ft->lock); smb2_open() propagates that as STATUS_OBJECT_NAME_INVALID and drops the original reference via ksmbd_fd_put().
The list removal cannot be left for a deferred final putter because fp->volatile_id has already been cleared and __ksmbd_remove_fd() will intentionally skip both idr_remove() and list_del_init(). Move the m_fp_list unlink in __ksmbd_remove_fd() above the volatile-id check so that an FP_NEW fp that happened to be added to m_fp_list (smb2_open() adds fp->node before ksmbd_update_fstate() runs) is still cleaned up on the deferred putter path; list_del_init() on an empty node is a no-op and remains safe for fps that were never added.
Add a defensive guard in session_fd_check() that refuses non-FP_INITED fps so that even if a teardown reaches an FP_NEW fp it falls into the close branch (where the n_to_drop = 1 accounting keeps the opener's reference alive) instead of the durable-preserve branch (which mutates fp->conn / fp->tcon).
Validation on a debug kernel additionally built with CONFIG_DEBUG_LIST and CONFIG_DEBUG_OBJECTS_WORK used a same-session two-tcon workload (open/write storm on one tcon, 50 tree disconnects on the other) and reported no list-corruption, work_struct ODEBUG, sleep-in-atomic, lockdep or kmemleak reports. Reverting only the __close_file_table_ids() hunk while keeping a forced-is_reconnectable() harness produced the expected sleep-in-atomic at vfs_cache.c:1095, confirming the ft->lock-out-of-sleepable-skip discipline.
KASAN-enabled direct SMB2 coverage with durable handles enabled exercised ksmbd_close_tree_conn_fds(), ksmbd_close_session_fds(), the FP_NEW failure path, tree_conn_fd_check(), and a non-zero session_fd_check() durable-preserve return. This produced no KASAN, DEBUG_LIST, ODEBUG, or WARNING reports.
--- fs/smb/server/smb2pdu.c | 6 +- fs/smb/server/vfs_cache.c | 179 +++++++++++++++++++++++++++++++++----- fs/smb/server/vfs_cache.h | 4 +- 3 files changed, 164 insertions(+), 25 deletions(-)
fs/smb/server/smb2pdu.c +4 -2
@@ -3767,8 +3767,10 @@ int smb2_open(struct ksmbd_work *work) err_out2:if (!rc) { -ksmbd_update_fstate (&work->sess->file_table, fp,FP_INITED ); - rc =ksmbd_iov_pin_rsp (work, (void *)rsp, iov_len); + rc =ksmbd_update_fstate (&work->sess->file_table, fp, +FP_INITED ); +if (!rc) + rc =ksmbd_iov_pin_rsp (work, (void *)rsp, iov_len); }if (rc) {if (rc == -EINVAL )
fs/smb/server/vfs_cache.c +158 -21
252 lines not on this page · raw message
fs/smb/server/vfs_cache.h +2 -2
@@ -172,8 +172,8 @@ int ksmbd_close_inode_fds(struct ksmbd_work *work, struct inode *inode);int ksmbd_init_global_file_table (void );void ksmbd_free_global_file_table (void );void ksmbd_set_fd_limit (unsigned long limit); -void ksmbd_update_fstate (struct ksmbd_file_table *ft,struct ksmbd_file *fp, -unsigned int state); +int ksmbd_update_fstate (struct ksmbd_file_table *ft,struct ksmbd_file *fp, +unsigned int state);bool ksmbd_vfs_compare_durable_owner (struct ksmbd_file *fp,struct ksmbd_user *user);
Signature · 1 line
2.43.0
PATCH v2 3/3 ksmbd: close durable scavenger races against m_fp_list lookups
In reply to DaeMyung Kangksmbd_durable_scavenger() has two related races against any walker that iterates f_ci->m_fp_list, including ksmbd_lookup_fd_inode() (used by ksmbd_vfs_rename) and the share-mode checks in fs/smb/server/smb_common.c.
(1) fp->node list-head reuse. Durable-preserved handles can remain linked on f_ci->m_fp_list after session teardown so share-mode checks still see them while the handle is reconnectable. The scavenger collected expired handles by adding fp->node to a local scavenger_list after removing them from the global durable idr. Because fp->node is the same list_head used by m_fp_list, list_add(&fp->node, &scavenger_list) overwrites the m_fp_list links and corrupts both lists. CONFIG_DEBUG_LIST can report this on the share-mode walk path.
(2) Refcount race against m_fp_list walkers. The scavenger qualifies an expired durable handle with atomic_read(&fp->refcount) > 1 and fp->conn under global_ft.lock, removes fp from global_ft, then drops global_ft.lock before unlinking fp from m_fp_list and freeing it. During that gap fp is still linked on m_fp_list with f_state == FP_INITED. ksmbd_lookup_fd_inode() under m_lock read calls ksmbd_fp_get() (atomic_inc_not_zero on refcount that is still 1) and takes a live reference; the scavenger then unlinks and frees fp while the holder owns a reference, leading to UAF on the holder's subsequent ksmbd_fd_put() and on any field reads performed by a concurrent share-mode walker that iterates m_fp_list without taking ksmbd_fp_get() (smb_check_perm_dleases-like paths).
Fix both:
* Stop reusing fp->node as a scavenger-private list node. Remove one expired handle from global_ft under global_ft.lock, take an explicit transient reference, drop the lock, unlink fp->node from m_fp_list under f_ci->m_lock, then drop both the durable lifetime and transient references with atomic_sub_and_test(2, &fp->refcount). If the scavenger is the last putter the close runs there; otherwise an in-flight holder that already raced through the m_fp_list lookup owns the final close via its ksmbd_fd_put() path. The one-at-a-time disposal can rescan the durable idr when multiple handles expire in the same pass, but durable scavenging is a background expiration path and the final full scan recomputes min_timeout before the next wait.
* Clear fp->persistent_id inside __ksmbd_remove_durable_fd() right after idr_remove(), so a delayed final close from a holder that snatched fp does not re-issue idr_remove() on a persistent id that idr_alloc_cyclic() in ksmbd_open_durable_fd() may have already handed out to a brand-new durable handle.
* Bypass the per-conn open_files_count decrement in __put_fd_final() when fp is detached from any session table (fp->conn cleared by session_fd_check() at durable preserve -- paired with the volatile_id clear at unpublish, so checking fp->conn alone is sufficient). The walker that owns the final close runs from an unrelated work->conn whose stats.open_files_count never tracked this durable fp; without this guard the holder would underflow that unrelated counter.
The two races are folded into one patch because patch (1) alone cleans up the corrupted list but leaves a deterministic UAF window for m_fp_list walkers that the transient-reference and persistent_id discipline in (2) close; bisecting onto an intermediate state would land on a UAF that pre-patch chaos merely made less reproducible.
Validation:
* CONFIG_DEBUG_LIST coverage for the list_head reuse path.
* KASAN-enabled direct SMB2 durable-handle coverage that exercised ksmbd_durable_scavenger() and non-NULL ksmbd_lookup_fd_inode() returns while durable handles expired under concurrent rename lookups, with no KASAN, UAF, list-corruption, ODEBUG, or WARNING reports.
* checkpatch --strict
* make -j$(nproc) M=fs/smb/server
--- fs/smb/server/vfs_cache.c | 102 ++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 26 deletions(-)
fs/smb/server/vfs_cache.c +76 -26
@@ -418,6 +418,14 @@ static void __ksmbd_remove_durable_fd(struct ksmbd_file *fp)return ;idr_remove (global_ft.idr, fp->persistent_id); +/* +* Clear persistent_id so a later __ksmbd_close_fd() that runs from a +* delayed putter (e.g. when a concurrent ksmbd_lookup_fd_inode() +* walker held the final reference) does not re-issue idr_remove() on +* an id that idr_alloc_cyclic() may have already handed out to a new +* durable handle. +*/ + fp->persistent_id =KSMBD_NO_FID ; }static void ksmbd_remove_durable_fd (struct ksmbd_file *fp)
@@ -521,6 +529,20 @@ static struct ksmbd_file *__ksmbd_lookup_fd(struct ksmbd_file_table *ft,static void __put_fd_final (struct ksmbd_work *work,struct ksmbd_file *fp) { +/* +* Detached durable fp -- session_fd_check() cleared fp->conn at +* preserve, so this fp is no longer tracked by any conn's +* stats.open_files_count. This happens when +* ksmbd_scavenger_dispose_dh() hands the final close off to an +* m_fp_list walker (e.g. ksmbd_lookup_fd_inode()) whose work->conn +* is unrelated to the conn that originally opened the handle; close +* via the NULL-ft path so we do not underflow that unrelated +* counter. +*/ +if (!fp->conn) { +__ksmbd_close_fd (NULL , fp); +return ; + }__ksmbd_close_fd (&work->sess->file_table, fp);atomic_dec (&work->conn->stats.open_files_count); }
@@ -1033,24 +1055,37 @@ static bool ksmbd_durable_scavenger_alive(void)return true; } -static void ksmbd_scavenger_dispose_dh (struct list_head *head) +static void ksmbd_scavenger_dispose_dh (struct ksmbd_file *fp) { -while (!list_empty (head)) { -struct ksmbd_file *fp; +/* +* Durable-preserved fp can remain linked on f_ci->m_fp_list for +* share-mode checks. Unlink it before final close; fp->node is not +* available as a scavenger-private list node because re-adding it to +* another list corrupts m_fp_list. +*/ +down_write (&fp->f_ci->m_lock); +list_del_init (&fp->node); +up_write (&fp->f_ci->m_lock); - fp = list_first_entry(head, struct ksmbd_file, node); -list_del_init (&fp->node); +/* +* Drop both the durable lifetime reference and the transient reference +* taken by the scavenger under global_ft.lock. If a concurrent +* ksmbd_lookup_fd_inode() (or any other m_fp_list walker) snatched fp +* before the unlink above, that holder owns the final close via +* ksmbd_fd_put() -> __ksmbd_close_fd(). Otherwise the scavenger is +* the last putter and finalises fp here. +*/ +if (atomic_sub_and_test (2 , &fp->refcount))__ksmbd_close_fd (NULL , fp); - } }static int ksmbd_durable_scavenger (void *dummy) {struct ksmbd_file *fp =NULL ; +struct ksmbd_file *expired_fp;unsigned int id;unsigned int min_timeout =1 ;bool found_fp_timeout; -LIST_HEAD (scavenger_list);unsigned long remaining_jiffies;__module_get (THIS_MODULE );
@@ -1060,8 +1095,6 @@ static int ksmbd_durable_scavenger(void *dummy)if (try_to_freeze ())continue ; - found_fp_timeout = false; - remaining_jiffies =wait_event_timeout (dh_wq,ksmbd_durable_scavenger_alive () == false,__msecs_to_jiffies (min_timeout));
@@ -1070,23 +1103,39 @@ static int ksmbd_durable_scavenger(void *dummy)else min_timeout =DURABLE_HANDLE_MAX_TIMEOUT ; -write_lock (&global_ft.lock); -idr_for_each_entry (global_ft.idr, fp, id) { -if (!fp->durable_timeout) -continue ; + do { + expired_fp = NULL; + found_fp_timeout = false; -if (atomic_read (&fp->refcount) >1 || - fp->conn) -continue ; - - found_fp_timeout = true; -if (fp->durable_scavenger_timeout <= -jiffies_to_msecs (jiffies)) { -__ksmbd_remove_durable_fd (fp); -list_add (&fp->node, &scavenger_list); - }else { + write_lock(&global_ft.lock); + idr_for_each_entry(global_ft.idr, fp, id) { unsigned long durable_timeout; + if (!fp->durable_timeout) + continue; + + if (atomic_read(&fp->refcount) > 1 || + fp->conn) + continue; + + found_fp_timeout = true; + if (fp->durable_scavenger_timeout <= + jiffies_to_msecs(jiffies)) { + __ksmbd_remove_durable_fd(fp); + /* + * Take a transient reference so fp + * cannot be freed by an in-flight + * ksmbd_lookup_fd_inode() that found + * it through f_ci->m_fp_list while we + * drop global_ft.lock and reach the + * m_fp_list unlink in + * ksmbd_scavenger_dispose_dh(). + */ + atomic_inc(&fp->refcount); + expired_fp = fp; + break; + } + durable_timeout = fp->durable_scavenger_timeout - jiffies_to_msecs(jiffies);
@@ -1094,10 +1143,11 @@ static int ksmbd_durable_scavenger(void *dummy)if (min_timeout > durable_timeout) min_timeout = durable_timeout; } - } -write_unlock (&global_ft.lock); +write_unlock (&global_ft.lock); -ksmbd_scavenger_dispose_dh (&scavenger_list); +if (expired_fp) +ksmbd_scavenger_dispose_dh (expired_fp); + }while (expired_fp);if (found_fp_timeout == false)break ;
Signature · 1 line
2.43.0
Quoted 89 lines · DaeMyung Kang
This series fixes lifetime bugs around ksmbd connection shutdown, session file-table teardown, and durable handle scavenging.
Patch 1 centralizes the final struct
ksmbd_connrelease so every last putter runsida_destroy()and transport cleanup. The release is queued to a dedicated workqueue because transport teardown can sleep, while one known last-putter is an RCU callback.Patch 2 hardens
__close_file_table_ids()by taking a transientksmbd_filereference, unpublishing from the session idr underft->lock, and doing sleepable preserve/close work outsideft->lock. It also makes theFP_NEWwindow visible to the opener throughksmbd_update_fstate().Patch 2 is scoped to file-table teardown and the
FP_NEWpublication window. It does not try to fix durable reconnect rollback on latersmb2_open()error paths (and the related post-FP_INITED reference window in freshsmb2_open). Both already exist before this series and need either an explicit unpublish-on-error step or an extra session-owned reference. That is left as follow-up work. TheFP_NEW->FP_INITEDfailure reuses smb2_open()'s existing-ENOENTtoSTATUS_OBJECT_NAME_INVALIDmapping to avoid changing wire behavior in this lifetime fix.Patch 3 closes two related races in the durable scavenger against any walker that iterates
f_ci->m_fp_list(ksmbd_lookup_fd_inode()and the share-mode checks). The scavenger no longer reusesfp->nodeas a scavenger-private collect-list node, takes an explicit transient reference under global_ft.lock, and drops both the durable lifetime and transient refs withatomic_sub_and_test(2, ...)after them_fp_listunlink so an in-flightm_fp_listwalker that snatched fp owns the final close cleanly.fp->persistent_idis cleared inside__ksmbd_remove_durable_fd()so a delayed final close cannot re-issueidr_remove()on a slot thatidr_alloc_cyclic()may have already re-handed to a new durable handle.__put_fd_final()bypasses the per-connopen_files_countdecrement when fp is detached from any session table (fp->conncleared bysession_fd_check()at durable preserve, paired with thevolatile_idclear at unpublish), since the walker that owns the final close in that case runs from an unrelatedwork->connwhose counter never tracked this durable fp.The series is intentionally scoped to lifetime/race fixes and the walker-final-putter regression that the durable scavenger handoff in patch 3 newly exposes. Pre-existing reconnect rollback and per-conn
open_files_countaccounting gaps are left as follow-up work so this series does not have to claim a full durable-reconnect or accounting cleanup.Validation:
* abrupt-disconnect kmemleak/kprobe A/B for connection release
* same-session two-tcon DEBUG_LIST/DEBUG_OBJECTS_WORK stress
* forced durable-preserve sleep-path harness for session teardown
* KASAN-enabled direct SMB2 coverage for session/tree teardown and durable-preserve paths
* KASAN-enabled direct SMB2 coverage for durable scavenger expiry racing withm_fp_listlookups
* checkpatch --strict for all patches
* make -j$(nproc) M=fs/smb/serverv1 -> v2:
* Split the original change into bisectable patches: connection final release, session file-table teardown, and durable scavenger races.
* Keep sleepable session preserve/close work out offt->lockand make theFP_NEWpublication race visible through a cleared volatile id.
* Document that durable reconnect rollback on latersmb2_open()error paths is a pre-existing follow-up item, and keep the existing-ENOENTtoSTATUS_OBJECT_NAME_INVALIDwire mapping for this lifetime fix.
* Avoid reusingfp->nodeas a temporary durable scavenger list node and take a transient reference in the durable scavenger so concurrentksmbd_lookup_fd_inode()walkers cannot UAF on freed fp. These two fixes are folded into a single patch because the list-head-reuse fix alone leaves a deterministic UAF window form_fp_listwalkers; bisecting onto an intermediate state would land on a use-after-free that pre-patch chaos merely made less reproducible.
* Clearfp->persistent_idin__ksmbd_remove_durable_fd()so a holder that owns the final close after a scavenger removal does not re-issueidr_remove()on a persistent id that may have already been handed out to a new durable handle.
* Bypass the per-connopen_files_countdecrement in__put_fd_final()when fp is detached from any session table, so anm_fp_listwalker that owns the final close of a scavenged durable fp does not underflow an unrelated conn's stats counter.
* Document theksmbd_conn_wqlifetime invariant inksmbd_conn_put()instead of guarding withWARN_ON_ONCE, so a violation surfaces as a NULL deref rather than a silent leak of the final release.
Quoted 4 lines
DaeMyung Kang (3):
ksmbd: centralizeksmbd_connfinal release to plug transport leak
ksmbd: harden file lifetime during session teardown
ksmbd: close durable scavenger races againstm_fp_listlookups
Applied them to #ksmbd-for-next-next.
Thanks!