Skip to content

Commit 09cad07

Browse files
anakryikotorvalds
authored andcommitted
fs: fix NULL dereference due to data race in prepend_path()
Fix data race in prepend_path() with re-reading mnt->mnt_ns twice without holding the lock. is_mounted() does check for NULL, but is_anon_ns(mnt->mnt_ns) might re-read the pointer again which could be NULL already, if in between reads one of kern_unmount()/kern_unmount_array()/umount_tree() sets mnt->mnt_ns to NULL. This is seen in production with the following stack trace: BUG: kernel NULL pointer dereference, address: 0000000000000048 ... RIP: 0010:prepend_path.isra.4+0x1ce/0x2e0 Call Trace: d_path+0xe6/0x150 proc_pid_readlink+0x8f/0x100 vfs_readlink+0xf8/0x110 do_readlinkat+0xfd/0x120 __x64_sys_readlinkat+0x1a/0x20 do_syscall_64+0x42/0x110 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Fixes: f2683bd ("[PATCH] fix d_absolute_path() interplay with fsmount()") Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Cc: Alexander Viro <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4da9af0 commit 09cad07

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

fs/d_path.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static int prepend_path(const struct path *path,
102102

103103
if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
104104
struct mount *parent = READ_ONCE(mnt->mnt_parent);
105+
struct mnt_namespace *mnt_ns;
106+
105107
/* Escaped? */
106108
if (dentry != vfsmnt->mnt_root) {
107109
bptr = *buffer;
@@ -116,7 +118,9 @@ static int prepend_path(const struct path *path,
116118
vfsmnt = &mnt->mnt;
117119
continue;
118120
}
119-
if (is_mounted(vfsmnt) && !is_anon_ns(mnt->mnt_ns))
121+
mnt_ns = READ_ONCE(mnt->mnt_ns);
122+
/* open-coded is_mounted() to use local mnt_ns */
123+
if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
120124
error = 1; // absolute root
121125
else
122126
error = 2; // detached or not attached yet

0 commit comments

Comments
 (0)