fs/namespace: fix NULL pointer dereference in do_lock_mount()
PATCH 1/1 fs/namespace: fix NULL pointer dereference in do_lock_mount()
Syzkaller reported a NULL pointer dereference in do_lock_mount() when calling inode_lock() on a dentry without an associated inode.
where_to_mount() may return a negative dentry (without an inode) due to concurrent unlink or mount changes. The current code does not validate this before calling inode_lock(), leading to a crash.
Fix this by checking dentry and dentry->d_inode before acquiring the inode lock.
--- fs/namespace.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/namespace.c +11 -0
@@ -2760,6 +2760,17 @@ static void do_lock_mount(const struct path *path, } } +if (unlikely (!dentry || !dentry->d_inode)) { + err = -ENOENT ; +if (&m->mnt != path->mnt) { +if (dentry) +dput (dentry); +mntput (&m->mnt); + } + res->parent =ERR_PTR (err); +return ; + } +inode_lock (dentry->d_inode);namespace_lock ();
Signature · 1 line
2.54.0
Quoted 6 lines · Vineet Agarwal
Syzkaller reported a NULL pointer dereference in
do_lock_mount()when callinginode_lock()on a dentry without an associated inode.
where_to_mount()may return a negative dentry (without an inode) due to concurrent unlink or mount changes. The current code does not validate this before callinginode_lock(), leading to a crash.
Can you explain what call path could lead to having path here refer to a negative dentry? Is it not the case that a positive dentry's d_inode is stable as long as you hold a reference to the dentry? If, so it cannot be killed underneath us.
Quoted 5 lines
Fix this by checking dentry and
dentry->d_inodebefore acquiring the inode lock.Reported-by syzbot <[email protected]>
This is not enough. Which syzbot issue are you trying to fix?
Quoted 31 lines
Signed-off-by Vineet Agarwal <[email protected]>--- fs/namespace.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/namespace.c +11
-0@@ -2760,6 +2760,17 @@ static void do_lock_mount(const struct path *path, } } +if (unlikely (!dentry || !dentry->d_inode)) { + err = -ENOENT ; +if (&m->mnt != path->mnt) { +if (dentry) +dput (dentry); +mntput (&m->mnt); + } + res->parent =ERR_PTR (err); +return ; + } +inode_lock (dentry->d_inode);namespace_lock ();Signature · 1 line
2.54.0
Thanks,
Jori.
A message that is not in the archive (sent off-list or not yet received): CA+rjy8fM+8ahj7dcBWjnACU8xBWBqW4ssQFpi68dqRc-F-V3Xg@mail.gmail.com
Al Viro
maintainerQuoted 9 lines · Vineet Agarwal
Hi Jori,
Thanks for the review.
Quoted 1 line
From the syzkaller runs, the NULL dereference in
do_lock_mount()isreproducible across multiple VMs, which suggests a real race rather than a one-off condition.
The suspected window is between
where_to_mount()andinode_lock(). While
Suspected by whom or by what?
Quoted 4 lines
where_to_mount()returns a dentry undermount_locked_reader, that protection is dropped beforeinode_lock()is taken. During this window, concurrent mount topology changes (e.g. rename,move_mount, or unmount) can occur.
Which of the exits from where_to_mount() has it taken? And which of your conditions does it trigger?
Quoted 1 line
As a result, the dentry obtained from
where_to_mount()is not revalidated
"revalidated" in which sense? "Has that local variable suddenly become NULL while we hadn't been looking"?
Quoted 6 lines
before use, and may no longer be in a state suitable for
inode_lock(), leading to the observed NULL pointer dereference.I’m still investigating the exact sequence that leads to this, and also whether retrying (similar to the existing
-EAGAINpath) would be more appropriate than the current handling.
Do you have a reproducer? Which kernel it is and what .config is used? Incidentally, do the checks in your patch actually trigger on the setup you are testing?
Al Viro
maintainerQuoted 3 lines · Al Viro
Do you have a reproducer? Which kernel it is and what .config is used? Incidentally, do the checks in your patch actually trigger on the setup you are testing?
While we are at it, could you post the stack traces of these oopsen?
Hi,
Thanks for the follow-up.
I don’t have a stable reproducer yet; the issue is triggered by syzkaller under fuzzing on a local build (7.1.0-rc1 with KASAN enabled).
However, I was able to capture consistent KASAN reports. The issue is not limited to a NULL dereference — I am also observing slab-use-after-free in do_lock_mount().
From the reports, the accessed object belongs to mnt_cache, and is freed via mntput_no_expire_slowpath() from a concurrent umount path, while do_lock_mount() is still using the mount returned by where_to_mount().
The stack traces consistently follow this pattern:
do_lock_mount()
→ where_to_mount()
→ do_move_mount()
→ __do_sys_move_mount()
with the freeing happening via:
mntput_no_expire_slowpath()
→ path_umount()
→ __x64_sys_umount()
This seems consistent with a race between move_mount and umount, where the mount object returned by where_to_mount() is no longer valid after dropping mount_locked_reader.
I agree that my earlier explanation focusing on dentry state was not accurate. The issue appears to be related to mount lifetime rather than dentry revalidation.
Below is one of the stack traces:
==================================================================
BUG: KASAN: slab-use-after-free in topmost_overmount fs/mount.h:239 [inline] BUG: KASAN: slab-use-after-free in where_to_mount fs/namespace.c:2695 [inline] BUG: KASAN: slab-use-after-free in where_to_mount fs/namespace.c:2688 [inline] BUG: KASAN: slab-use-after-free in do_lock_mount.part.0+0xa1b/0xbf0
fs/namespace.c:2756
Call Trace: <TASK> topmost_overmount fs/mount.h:239 [inline] where_to_mount fs/namespace.c:2695 [inline] where_to_mount fs/namespace.c:2688 [inline] do_lock_mount.part.0+0xa1b/0xbf0 fs/namespace.c:2756 do_lock_mount fs/namespace.c:2751 [inline] do_move_mount.isra.0+0x1ac/0xce0 fs/namespace.c:3645 vfs_move_mount fs/namespace.c:4561 [inline] __do_sys_move_mount+0x62e/0x770 fs/namespace.c:4629 do_syscall_64+0xe0/0x5a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK>
Allocated by task:
alloc_vfsmnt fs/namespace.c:287 vfs_create_mount fs/namespace.c:1171 fc_mount fs/namespace.c:1191 do_new_mount fs/namespace.c:3840 path_mount fs/namespace.c:4160 __x64_sys_mount fs/namespace.c:4366
Freed by task:
mntput_no_expire_slowpath fs/namespace.c:1379 path_umount fs/namespace.c:2046 __x64_sys_umount fs/namespace.c:2068 ================================================================== ------------------------------
Regarding the earlier patch, the added checks do not reliably trigger under this workload, which suggests that they are not addressing the actual root cause.
I’m now investigating the lifetime guarantees around the mount returned by where_to_mount() and whether additional synchronization or revalidation is required.
Also, the .config used for testing is attached.
Thanks,
Vineet
Quoted 8 lines · Al Viro
Quoted 3 lines · Al Viro
Do you have a reproducer? Which kernel it is and what .config is used? Incidentally, do the checks in your patch actually trigger on the setup you are testing?
While we are at it, could you post the stack traces of these oopsen?
Christian Brauner
maintainerQuoted 62 lines · Vineet Agarwal
Hi,
Thanks for the follow-up.
I don’t have a stable reproducer yet; the issue is triggered by syzkaller under fuzzing on a local build (7.1.0-rc1 with KASAN enabled).
However, I was able to capture consistent KASAN reports. The issue is not limited to a NULL dereference — I am also observing slab-use-after-free in
do_lock_mount().Quoted 1 line
From the reports, the accessed object belongs to
mnt_cache, and is freedvia
mntput_no_expire_slowpath()from a concurrent umount path, whiledo_lock_mount()is still using the mount returned bywhere_to_mount().The stack traces consistently follow this pattern:
do_lock_mount()
→where_to_mount()
→do_move_mount()
→__do_sys_move_mount()with the freeing happening via:
mntput_no_expire_slowpath()
→path_umount()
→__x64_sys_umount()This seems consistent with a race between
move_mountand umount, where the mount object returned bywhere_to_mount()is no longer valid after droppingmount_locked_reader.I agree that my earlier explanation focusing on dentry state was not accurate. The issue appears to be related to mount lifetime rather than dentry revalidation.
Below is one of the stack traces:
==================================================================
BUG: KASAN: slab-use-after-free in topmost_overmount fs/mount.h:239 [inline] BUG: KASAN: slab-use-after-free in where_to_mount fs/namespace.c:2695 [inline] BUG: KASAN: slab-use-after-free in where_to_mount fs/namespace.c:2688 [inline] BUG: KASAN: slab-use-after-free in do_lock_mount.part.0+0xa1b/0xbf0
fs/namespace.c:2756Call Trace: <TASK> topmost_overmount fs/mount.h:239 [inline] where_to_mount fs/namespace.c:2695 [inline] where_to_mount fs/namespace.c:2688 [inline] do_lock_mount.part.0+0xa1b/0xbf0 fs/namespace.c:2756 do_lock_mount fs/namespace.c:2751 [inline] do_move_mount.isra.0+0x1ac/0xce0 fs/namespace.c:3645 vfs_move_mount fs/namespace.c:4561 [inline] __do_sys_move_mount+0x62e/0x770 fs/namespace.c:4629 do_syscall_64+0xe0/0x5a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK>Allocated by task:
Quoted 8 lines
alloc_vfsmnt fs/namespace.c:287 vfs_create_mount fs/namespace.c:1171 fc_mount fs/namespace.c:1191 do_new_mount fs/namespace.c:3840 path_mount fs/namespace.c:4160 __x64_sys_mount fs/namespace.c:4366Freed by task:
Nothing in here makes sense imho and the details you provide are fuzzy at best and I see a bunch of patches that got rejected already. The stacktraces here look rather sketchy and I'm not sure
This doesn't free anything. This just schedules task work and KASAN would always show the actual release/free paths. So this looks forged or tampered with.
Provide all the details. Unless it is of the same quality and clarify as syzbot I'm inclined to call this hallucinated or based on some old kernel.