ksmbd: pin parent dentry in rename share check
Subsystems (3)
PATCH 1/1 ksmbd: pin parent dentry in rename share check
ksmbd_vfs_check_rename_share() passes old_path->dentry->d_parent to ksmbd_lookup_fd_inode() without taking a reference to the parent. Although the call in ksmbd_vfs_rename() runs with the parent directory locked, smb2_rename() also calls this helper before entering the VFS rename path, without that lock.
A concurrent rename can move the child to another directory and drop the last reference to its old parent. If that parent is then removed and freed, ksmbd_lookup_fd_inode() reads d_inode from a freed dentry. Holding a reference to the child does not keep its old parent alive across a rename.
KASAN caught this on an x86_64 QEMU guest with a test-only 100 ms delay between sampling d_parent and calling ksmbd_lookup_fd_inode():
BUG: KASAN: slab-use-after-free in ksmbd_lookup_fd_inode+0x28c/0x2b0 Read of size 8 at addr ffff88810faa6f30 by task kworker/3:2/76 Workqueue: ksmbd-io handle_ksmbd_work Call Trace: ksmbd_lookup_fd_inode+0x28c/0x2b0 ksmbd_vfs_check_rename_share+0x8e/0x240 smb2_set_info+0x2b71/0x3bb0 handle_ksmbd_work+0xad4/0x2110
The read is 48 bytes into a freed 192-byte dentry, at its d_inode member. The stack reaches the unlocked smb2_rename() call, inlined into smb2_set_info(), rather than the check inside ksmbd_vfs_rename().
Take a parent reference with dget_parent() and hold it until the lookup completes. If the parent has no inode, skip the lookup and report no parent sharing conflict. The returned ksmbd_file has its own reference, so the parent can be released before checking sharing.
OpenAI Codex assisted with reviewing the archived fix and preparing the changelog and validation materials.
---
This is a standalone fix against ksmbd-for-next at c710c654b8a3. It is independent of the maximal-access patch sent separately.
The existing KASAN report was captured on 7e1392a42ea2 using a local rename/rmdir workload in a tmpfs share and an SMB rename request. The 100 ms delay was only in the test kernel and is not in this patch. The reproducer and full report are available on request.
Testing: git apply --check and an x86_64 KASAN build of fs/smb/server/vfs.o on c710c654b8a3. Both this patch and the maximal-access patch apply in either order. Checkpatch reports no errors; its only warning is the missing Fixes commit in the local shallow clone, whose subject and diff were checked upstream.
With the fix, the same 100 ms delay was placed after dget_parent() and before the d_inode() check. On a 7e1392a42ea2 KASAN guest that also included 0383a66dabd7 and the maximal-access fix, 80 SMB rename requests each took about 100 ms and returned STATUS_OBJECT_NAME_INVALID. There was no KASAN report and the guest stayed up. Those two extra patches only keep the open path off the other parent-dentry bugs; this fix does not depend on them. The race has not been tested without the artificial delay.
fs/smb/server/vfs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
fs/smb/server/vfs.c +8 -1
@@ -731,9 +731,16 @@ const struct path *old_path) {struct ksmbd_file *parent_fp; +struct dentry *parent;int err =0 ; - parent_fp =ksmbd_lookup_fd_inode (old_path->dentry->d_parent); + parent =dget_parent (old_path->dentry); +if (!d_inode (parent)) { +dput (parent); +return 0 ; + } + parent_fp =ksmbd_lookup_fd_inode (parent); +dput (parent);if (!parent_fp)return 0 ;
base-commit: c710c654b8a3c92b34e38740dc17c29daf5b222e
Quoted 34 lines · Yuansheng Yang
ksmbd_vfs_check_rename_share()passesold_path->dentry->d_parenttoksmbd_lookup_fd_inode()without taking a reference to the parent. Although the call inksmbd_vfs_rename()runs with the parent directory locked,smb2_rename()also calls this helper before entering the VFS rename path, without that lock.A concurrent rename can move the child to another directory and drop the last reference to its old parent. If that parent is then removed and freed,
ksmbd_lookup_fd_inode()readsd_inodefrom a freed dentry. Holding a reference to the child does not keep its old parent alive across a rename.KASAN caught this on an
x86_64QEMU guest with a test-only 100 ms delay between samplingd_parentand callingksmbd_lookup_fd_inode():BUG: KASAN: slab-use-after-free in ksmbd_lookup_fd_inode+0x28c/0x2b0 Read of size 8 at addr ffff88810faa6f30 by task kworker/3:2/76 Workqueue: ksmbd-io handle_ksmbd_work Call Trace: ksmbd_lookup_fd_inode+0x28c/0x2b0 ksmbd_vfs_check_rename_share+0x8e/0x240 smb2_set_info+0x2b71/0x3bb0 handle_ksmbd_work+0xad4/0x2110The read is 48 bytes into a freed 192-byte dentry, at its
d_inodemember. The stack reaches the unlockedsmb2_rename()call, inlined intosmb2_set_info(), rather than the check insideksmbd_vfs_rename().Take a parent reference with
dget_parent()and hold it until the lookup completes. If the parent has no inode, skip the lookup and report no parent sharing conflict. The returnedksmbd_filehas its own reference, so the parent can be released before checking sharing.
Quoted 6 lines
OpenAI Codex assisted with reviewing the archived fix and preparing the changelog and validation materials.
Fixes 7f8029591bed ("ksmbd: synchronize lease breaks before renaming files")Signed-off-by Yuansheng Yang <[email protected]>
Applied it to #ksmbd-for-next.
Thanks!