[PATCH] smb: client: fix dentry leak and data race in open_cached_dir()

From: Siddharth Chintamaneni

Date: Mon Nov 30 2026 - 13:36:00 EST


find_or_create_cached_dir() adds a newly allocated cached_fid entry onto
cfids->entries under cfids->cfid_list_lock with cfid->dentry == NULL and
cfid->on_list == true, after which open_cached_dir() drops
cfid_list_lock to resolve the dentry via path_to_dentry() and assigns
cfid->dentry = dentry without re-acquiring cfid_list_lock.

This unlocked assignment creates two concurrency issues:

1. If invalidate_all_cached_dirs() runs while open_cached_dir() is in
path_to_dentry(), it moves cfid from cfids->entries to cfids->dying,
clears cfid->on_list, and schedules cfids_laundromat_worker(). When
cfids_laundromat_worker() pops cfid from cfids->dying and executes
dput(cfid->dentry) followed by cfid->dentry = NULL concurrently with
open_cached_dir() assigning cfid->dentry = dentry, the worker can
call dput(NULL) and then overwrite the newly assigned cfid->dentry
with NULL:

Thread 1 (open_cached_dir) Thread 2 (invalidate / laundromat)
-------------------------- ----------------------------------
find_or_create_cached_dir()
list_add(&cfid->entry, &entries)
cfid->on_list = true
spin_unlock(&cfid_list_lock)
dentry = path_to_dentry(...) invalidate_all_cached_dirs()
list_move(&cfid->entry, &dying)
cfid->on_list = false
cfids_laundromat_worker()
dput(cfid->dentry) [sees NULL]
cfid->dentry = dentry
cfid->dentry = NULL [overwrites]
close_cached_dir(cfid)
smb2_close_cached_fid()
[sees cfid->dentry == NULL]
-> dentry leaked, triggering BUG: Dentry still in use on unmount!

2. Reader paths walking cfids->entries or cfids->dying under
cfids->cfid_list_lock (such as close_all_cached_dirs() and
open_cached_dir_by_dentry()) read and dereference cfid->dentry
concurrently with the unlocked write in open_cached_dir().

Fix this by holding cfids->cfid_list_lock when checking parent_cfid and
initializing cfid->dentry and cfid->tcon in open_cached_dir(), and
aborting with dput(dentry) if !cfid->on_list indicates the entry was
already invalidated while cfid_list_lock was dropped.

Fixes: 5c86919455c1 ("smb: client: fix use-after-free in smb2_query_info_compound()")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@xxxxxxxxx>
---
fs/smb/client/cached_dir.c | 39 ++++++++++++++++++++++----------------
1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c
index 2c6243cbe10e4..b9b6e2183d17d 100644
--- a/fs/smb/client/cached_dir.c
+++ b/fs/smb/client/cached_dir.c
@@ -227,28 +227,35 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
rc = -ENOENT;
goto out;
}
- if (dentry->d_parent && server->dialect >= SMB30_PROT_ID) {
- struct cached_fid *parent_cfid;
-
- spin_lock(&cfids->cfid_list_lock);
- list_for_each_entry(parent_cfid, &cfids->entries, entry) {
- if (parent_cfid->dentry == dentry->d_parent) {
- cifs_dbg(FYI, "found a parent cached file handle\n");
- if (is_valid_cached_dir(parent_cfid)) {
- lease_flags
- |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
- memcpy(pfid->parent_lease_key,
- parent_cfid->fid.lease_key,
- SMB2_LEASE_KEY_SIZE);
- }
- break;
+ }
+
+ spin_lock(&cfids->cfid_list_lock);
+ if (!cfid->on_list) {
+ spin_unlock(&cfids->cfid_list_lock);
+ dput(dentry);
+ rc = -ENOENT;
+ goto out;
+ }
+ if (npath[0] && dentry->d_parent && server->dialect >= SMB30_PROT_ID) {
+ struct cached_fid *parent_cfid;
+
+ list_for_each_entry(parent_cfid, &cfids->entries, entry) {
+ if (parent_cfid->dentry == dentry->d_parent) {
+ cifs_dbg(FYI, "found a parent cached file handle\n");
+ if (is_valid_cached_dir(parent_cfid)) {
+ lease_flags |=
+ SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
+ memcpy(pfid->parent_lease_key,
+ parent_cfid->fid.lease_key,
+ SMB2_LEASE_KEY_SIZE);
}
+ break;
}
- spin_unlock(&cfids->cfid_list_lock);
}
}
cfid->dentry = dentry;
cfid->tcon = tcon;
+ spin_unlock(&cfids->cfid_list_lock);

/*
* We do not hold the lock for the open because in case
--
2.43.0