[PATCH] fs: prevent access to ns if it is not mounted

From: Lizhi Xu
Date: Fri Feb 07 2025 - 02:13:50 EST


syzbot reported a null ptr deref in clone_private_mount. [1]

The mnt_ns member should be accessed after confirming that it has been mounted.

[1]
KASAN: null-ptr-deref in range [0x0000000000000048-0x000000000000004f]
CPU: 0 UID: 0 PID: 5834 Comm: syz-executor772 Not tainted 6.14.0-rc1-next-20250206-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 12/27/2024
RIP: 0010:is_anon_ns fs/mount.h:159 [inline]
RIP: 0010:clone_private_mount+0x184/0x3e0 fs/namespace.c:2425
Code: 89 d8 48 83 c4 10 5b 41 5c 41 5d 41 5e 41 5f 5d c3 cc cc cc cc 48 83 c3 48 48 89 d8 48 c1 e8 03 48 b9 00 00 00 00 00 fc ff df <80> 3c 08 00 4d 89 fc 74 08 48 89 df e8 db dd e4 ff 48 8b 1b 31 ff
RSP: 0018:ffffc90003e2f958 EFLAGS: 00010206
RAX: 0000000000000009 RBX: 0000000000000048 RCX: dffffc0000000000
RDX: 0000000000000001 RSI: 0000000000000008 RDI: ffff888032eb2710
RBP: 0000000000000000 R08: ffffffff8ea81ca7 R09: 1ffffffff1d50394
R10: dffffc0000000000 R11: fffffbfff1d50395 R12: ffff888032eb2700
R13: ffff888032eb2720 R14: 1ffff11006b34091 R15: ffff8880359a0488
FS: 0000555584629380(0000) GS:ffff8880b8600000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000200000001000 CR3: 00000000786c2000 CR4: 00000000003526f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
<TASK>
ovl_get_layers fs/overlayfs/super.c:1061 [inline]
ovl_get_lowerstack fs/overlayfs/super.c:1156 [inline]
ovl_fill_super+0x1a24/0x3560 fs/overlayfs/super.c:1404
vfs_get_super fs/super.c:1280 [inline]
get_tree_nodev+0xb7/0x140 fs/super.c:1299
vfs_get_tree+0x90/0x2b0 fs/super.c:1814
do_new_mount+0x2be/0xb40 fs/namespace.c:3659
do_mount fs/namespace.c:3999 [inline]
__do_sys_mount fs/namespace.c:4210 [inline]
__se_sys_mount+0x2d6/0x3c0 fs/namespace.c:4187
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported-by: syzbot+62dfea789a2cedac1298@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=62dfea789a2cedac1298
Signed-off-by: Lizhi Xu <lizhi.xu@xxxxxxxxxxxxx>
---
fs/namespace.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/namespace.c b/fs/namespace.c
index 1314f11ed961..8e2ff3dbab58 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2421,6 +2421,9 @@ struct vfsmount *clone_private_mount(const struct path *path)
if (!check_mnt(old_mnt))
return ERR_PTR(-EINVAL);
} else {
+ if (!is_mounted(&old_mnt->mnt))
+ return ERR_PTR(-EINVAL);
+
/* Make sure this isn't something purely kernel internal. */
if (!is_anon_ns(old_mnt->mnt_ns))
return ERR_PTR(-EINVAL);
--
2.43.0