ksmbd_vfs_query_maximal_access() reads dentry->d_parent without holding a reference to the parent. smb2_open() calls it after path lookup has returned without the parent inode lock. The AppInstanceId takeover path also calls it on the old handle's filp->f_path without holding that lock. A reference to the child dentry does not pin its old parent across a rename. A concurrent rename can therefore move the child to another directory and drop the last reference to the old parent. If the old parent is then removed and freed, d_inode(parent) reads the d_inode field of a freed dentry. If the sampled parent has become negative, passing its NULL inode to inode_permission() can also cause a NULL dereference. KASAN caught the following on an x86_64 QEMU guest with a test-only 100 ms delay between reading d_parent and d_inode: BUG: KASAN: slab-use-after-free in ksmbd_vfs_query_maximal_access+0x2ba/0x300 Read of size 8 at addr ffff8881100f0830 by task kworker/2:1/81 Workqueue: ksmbd-io handle_ksmbd_work Call Trace: ksmbd_vfs_query_maximal_access+0x2ba/0x300 smb2_open+0x60d5/0xec30 handle_ksmbd_work+0xad4/0x2110 process_one_work+0x745/0x10f0 worker_thread+0x511/0xc80 kthread+0x330/0x410 Allocated by task 319: __d_alloc+0x35/0x860 d_alloc+0x46/0x1f0 lookup_one_qstr_excl+0x171/0x250 filename_mkdirat+0xba/0x4c0 __x64_sys_mkdir+0x6a/0x90 Freed by task 81: kmem_cache_free+0xb8/0x3b0 rcu_core+0x549/0x17d0 The read is 48 bytes into a freed 192-byte dentry, which is exactly the d_inode member. Commit 2e450920d58b ("ksmbd: move oplock handling after unlock parent dir") released the parent lock and reference before the final MXAC query in smb2_open(), leaving this helper's bare d_parent access unprotected. Commit 4e45cca31d4e ("smb/server: add ksmbd_vfs_kern_path()") later switched the earlier open path to an unlocked lookup, exposing the MAXIMUM_ALLOWED query as well. Commit 0383a66dabd7 ("ksmbd: pin parent dentries in SMB2 CREATE ACL path") fixed the same race class in smb2_open() and smb_inherit_dacl(), but did not cover this helper. Take a parent reference with dget_parent() and hold it across the permission check. If the parent has no inode, leave FILE_DELETE unset. OpenAI Codex assisted with reviewing the fix and revising the changelog. Fixes: 2e450920d58b ("ksmbd: move oplock handling after unlock parent dir") Cc: stable@vger.kernel.org Signed-off-by: Yuansheng Yang <0xbeac@gmail.com> --- The parent lease break sites in oplock.c are covered by a separate fix. Testing used an x86_64 KASAN QEMU guest based on 7e1392a42ea2 with 0383a66dabd7 applied. An SMB client issued MAXIMUM_ALLOWED opens while a local process in the guest repeatedly renamed the child and removed and recreated its old parent directory on tmpfs. A test-only mdelay(100) between reading d_parent and d_inode triggered the UAF within three open attempts. With the fix, the same delay was placed after dget_parent(): 789 open attempts completed, including 561 successful opens, without a KASAN report. The delay is not in this patch. Without the delay, the unfixed kernel did not reproduce the issue in 67,381 open attempts over 300 seconds. diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c index 7c3edbe6cb77..0f68500183d7 100644 --- a/fs/smb/server/vfs.c +++ b/fs/smb/server/vfs.c @@ -124,6 +124,9 @@ static int ksmbd_vfs_path_lookup(struct void ksmbd_vfs_query_maximal_access(const struct mnt_idmap *idmap, struct dentry *dentry, __le32 *daccess) { + struct dentry *parent; + struct inode *parent_inode; + *daccess = cpu_to_le32(FILE_READ_ATTRIBUTES | READ_CONTROL | SYNCHRONIZE); @@ -139,8 +142,12 @@ void ksmbd_vfs_query_maximal_access(cons if (!inode_permission(idmap, d_inode(dentry), MAY_OPEN | MAY_EXEC)) *daccess |= FILE_EXECUTE_LE; - if (!inode_permission(idmap, d_inode(dentry->d_parent), MAY_EXEC | MAY_WRITE)) + parent = dget_parent(dentry); + parent_inode = d_inode(parent); + if (parent_inode && + !inode_permission(idmap, parent_inode, MAY_EXEC | MAY_WRITE)) *daccess |= FILE_DELETE_LE; + dput(parent); } /** base-commit: c710c654b8a3c92b34e38740dc17c29daf5b222e