[RFC PATCH 09/13] fs: Support installing rootns mount trees via move_mount()

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 05:09:54 UTC 2026


Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] fs: do_move_mount() calls lock_mnt_tree() after
  attach_recursive_mnt()
- [High] fs: relative target paths in move_mount_rootns() bypass is_root
  checks
- [High] fs: TOCTOU race on rootns->state in move_mount_rootns()
  corrupts state
- [High] fs: fsmount() alloc_mnt_ns(fc->user_ns) bypasses submount
  MNT_LOCKED
- [High] fs: check_for_nsfs_mounts() checks caller mnt_ns instead of
  rootns target
--

commit b8cf56b982aa2378115c847c099e10376c22e6b8
Author: Jarkko Sakkinen <jarkko at kernel.org>

fs: Support installing rootns mount trees via move_mount()

This commit adds MOVE_MOUNT_T_ROOTNS_ROOT to move_mount() to allow
installing detached anonymous mount trees into a target rootns via its
control file descriptor, and allocates fsmount() anonymous mount
namespaces in fc->user_ns.

> diff --git a/fs/namespace.c b/fs/namespace.c
> index aecd05ac01705..5376d0230899a 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c
[ ... ]
> @@ -3792,7 +3796,13 @@ static int do_move_mount(const struct path *old_path,
>  	if (IS_ERR(mp.parent))
>  		return PTR_ERR(mp.parent);
>  
> -	if (check_mnt(old)) {
> +	if (flags & MNT_TREE_ROOTNS) {
> +		if (!anon_ns_root(old) || old->mnt_ns == mp.parent->mnt_ns)
> +			return -EINVAL;
> +		if ((flags & MNT_TREE_ROOTNS_INIT) &&
> +		    mp.parent != real_mount(new_path->mnt))
> +			return -EBUSY;
> +	} else if (check_mnt(old)) {
[ ... ]
> @@ -3845,7 +3855,16 @@ static int do_move_mount(const struct path *old_path,
>  	if (mount_is_ancestor(old, mp.parent))
>  		return -ELOOP;

[Severity: High]
Just above this line, do_move_mount() calls check_for_nsfs_mounts(old),
which checks mnt_ns_loop():

fs/namespace.c:mnt_ns_loop() {
    ...
	return current->nsproxy->mnt_ns->ns.ns_id >= mnt_ns->ns.ns_id;
}

When MNT_TREE_ROOTNS is set, the destination mount namespace is
mp.parent->mnt_ns rather than current->nsproxy->mnt_ns.  Can a caller in an
older mount namespace clone an nsfs mount of rootns_mnt_ns with
open_tree(OPEN_TREE_CLONE) and attach it into rootns_mnt_ns via
MOVE_MOUNT_T_ROOTNS_ROOT?  Because current->nsproxy->mnt_ns->ns.ns_id is
smaller than rootns_mnt_ns->ns.ns_id, mnt_ns_loop() returns false, creating
an unbreakable reference cycle between rootns_mnt_ns and its nsfs inode
that permanently leaks the mount namespace and all mounts inside it.

> -	return attach_recursive_mnt(old, &mp);
> +	lock_tree = (flags & MNT_TREE_ROOTNS) &&
> +		    old->mnt_ns->user_ns != mp.parent->mnt_ns->user_ns;
> +	err = attach_recursive_mnt(old, &mp);
> +	if (!err && lock_tree) {
> +		scoped_guard(mount_writer) {
> +			lock_mnt_tree(old);
> +			old->mnt.mnt_flags |= MNT_LOCKED;
> +		}
> +	}
> +	return err;
>  }

[Severity: High]
Does calling lock_mnt_tree(old) after attach_recursive_mnt(old, &mp) happen
too late?

Inside attach_recursive_mnt(), when an existing mount q is present at the
destination (for example with MOVE_MOUNT_BENEATH or overmounting), q is
reparented onto topmost_overmount(old) via mnt_change_mountpoint() and has
MNT_LOCKED cleared on it so it remains unmountable.  Running
lock_mnt_tree(old) afterwards walks into q and all of its descendants
(since p != old), setting MNT_LOCKED and MNT_LOCK_* on preexisting mounts
in the target rootns and preventing q from being unmounted.

In addition, if mp.parent is a shared mount, propagate_mnt() clones old
before lock_mnt_tree(old) runs.  Because attach_recursive_mnt() sets
user_ns = ns->user_ns (the target rootns user_ns rather than
old->mnt_ns->user_ns), child->mnt_parent->mnt_ns->user_ns != user_ns is
false for peer and slave mounts in the target user_ns, leaving all
propagated copies unlocked without MNT_LOCKED or MNT_LOCK_* flags.
Finally, attach_recursive_mnt() commits old and drops mount_lock before
mount_writer is re-acquired here, briefly exposing the unlocked tree in
rootns_mnt_ns.

> @@ -4699,7 +4718,7 @@ SYSCALL_DEFINE3(fsmount, int, fs_fd, unsigned int, flags,
>  		return FD_ADD((flags & FSMOUNT_CLOEXEC) ? O_CLOEXEC : 0,
>  			      open_new_namespace(&new_path, MOUNT_COPY_NEW));
>  
> -	ns = alloc_mnt_ns(current->nsproxy->mnt_ns->user_ns, true);
> +	ns = alloc_mnt_ns(fc->user_ns, true);
>  	if (IS_ERR(ns))
>  		return PTR_ERR(ns);
>  	mnt = real_mount(new_path.mnt);

[Severity: High]
Can allocating the anonymous mount namespace in fc->user_ns leave host
submounts unlocked when a multi-mount tree is installed into an
unprivileged rootns?

When a process in init_user_ns configures a context with
FSCONFIG_SET_ROOTNS for a ROOTNS_USER rootns (user_ns_B), fc->user_ns is
user_ns_B, so fsmount() creates a root mount M_root with
M_root->mnt_ns->user_ns == user_ns_B.  If the caller then attaches a host
submount M_sub from init_user_ns (such as a read-only bind mount or a
masking mount) onto M_root via move_mount(), attach_recursive_mnt() moves
M_sub into M_root->mnt_ns and calls lock_mnt_tree(M_sub):

fs/namespace.c:lock_mnt_tree() {
    ...
		if (list_empty(&p->mnt_expire) && p != mnt)
			flags |= MNT_LOCKED;
    ...
}

That only sets MNT_LOCKED on descendants p != M_sub, leaving M_sub itself
without MNT_LOCKED.  When M_root is subsequently installed into the rootns
via MOVE_MOUNT_T_ROOTNS_ROOT, old->mnt_ns->user_ns ==
mp.parent->mnt_ns->user_ns (both are user_ns_B), so lock_tree is false and
lock_mnt_tree(M_root) is never called.  Tasks in user_ns_B can then unmount
M_sub and expose the underlying directory.

Conversely, if the caller attaches a submount that was also created with
FSCONFIG_SET_ROOTNS onto M_root, attach_recursive_mnt() sets user_ns to
current->nsproxy->mnt_ns->user_ns (because is_anon_ns(M_root->mnt_ns) is
true) and spuriously locks the submount; and if an anonymous mount with
old->mnt_ns->user_ns != mp.parent->mnt_ns->user_ns is attached via normal
move_mount() without MOVE_MOUNT_T_ROOTNS_ROOT, neither do_move_mount() nor
attach_recursive_mnt() locks the tree.

[ ... ]

> +static int move_mount_rootns(int from_dfd, const char __user *from_pathname,
> +			     int to_dfd, const char __user *to_pathname,
> +			     unsigned int flags, enum mnt_tree_flags_t mflags)
> +{
[ ... ]
> +	rootns_mnt_ns = rootns->ns->mnt_ns;
> +	is_root = to_name->name[0] == '/' && to_name->name[1] == '\0';
> +	if (is_root) {
> +		if (flags & MOVE_MOUNT_BENEATH)
> +			return -EINVAL;
> +		if (READ_ONCE(rootns->state) != ROOTNS_EMPTY)
> +			return -EBUSY;
> +
> +		to_path.mnt = mntget(&rootns_mnt_ns->root->mnt);
> +		to_path.dentry = dget(rootns_mnt_ns->root->mnt.mnt_root);
> +		mflags |= MNT_TREE_ROOTNS_INIT;
> +	} else {
> +		if (to_name->name[0] == '/')
> +			return -EINVAL;
> +
> +		scoped_guard(spinlock, &rootns->lock) {
> +			if (rootns->state == ROOTNS_EMPTY)
> +				return -ENOENT;
> +		}
> +
> +		lflags = 0;
> +		if (flags & MOVE_MOUNT_T_SYMLINKS)
> +			lflags |= LOOKUP_FOLLOW;
> +		if (flags & MOVE_MOUNT_T_AUTOMOUNTS)
> +			lflags |= LOOKUP_AUTOMOUNT;
> +		ret = filename_lookup(AT_FDCWD, to_name, lflags, &to_path,
> +				      &rootns->root);
> +		if (ret)
> +			return ret;
> +		if (real_mount(to_path.mnt)->mnt_ns != rootns_mnt_ns)
> +			return -EINVAL;
> +	}

[Severity: High]
Can relative target paths such as ".", "subdir/..", or relative symlinks
to "/" bypass the is_root checks here?

Because is_root only tests for the literal string "/", passing "." enters
the else branch, where filename_lookup(..., &rootns->root) resolves to_path
to rootns->root itself:

1. With MOVE_MOUNT_BENEATH, this bypasses the MOVE_MOUNT_BENEATH check in
   the is_root branch, mounts from_path beneath rootns->root directly onto
   the nullfs root, and clears MNT_LOCKED on rootns->root in
   attach_recursive_mnt(), allowing an unprivileged rootns owner to unmount
   a cross-userns locked root mount.

2. Without MOVE_MOUNT_BENEATH, vfs_move_mount() runs without
   MNT_TREE_ROOTNS_INIT and overmounts rootns->root without updating
   rootns->root (and similarly on initial "/" installation if from_path
   already has overmounts on its root dentry, rootns->root = from_path
   stores the bottom mount rather than topmost_overmount()), leaving
   subsequent rootns_enter() tasks and relative lookups trapped in the
   shadowed lower mount.

> +
> +	ret = lookup_move_mount_from(from_dfd, from_pathname, flags,
> +				     &from_path);
> +	if (ret)
> +		return ret;
> +
> +	ret = vfs_move_mount(&from_path, &to_path, mflags | MNT_TREE_ROOTNS);
> +	if (!ret && is_root) {
> +		scoped_guard(spinlock, &rootns->lock) {
> +			path_get(&from_path);
> +			rootns->root = from_path;
> +			WRITE_ONCE(rootns->state, ROOTNS_READY);
> +		}
> +	}
> +
> +	return ret;
> +}

[Severity: High]
Can a race on rootns->state when is_root is true overwrite rootns->root and
reset a running or dead rootns back to ROOTNS_READY?

CPU0                                  CPU1
move_mount_rootns(..., "/")           move_mount_rootns(..., "/")
  READ_ONCE(rootns->state)
    == ROOTNS_EMPTY
                                        READ_ONCE(rootns->state)
                                          == ROOTNS_EMPTY
                                        lookup_move_mount_from()
  vfs_move_mount() -> attaches M1
  rootns->root = M1
  rootns->state = ROOTNS_READY
rootns_enter()
  rootns->state = ROOTNS_RUNNING
  umount2("/", MNT_DETACH)
    (detaches M1 from nullfs)
                                        vfs_move_mount()
                                          mp.parent == new_path->mnt
                                          attaches M2 onto nullfs
                                        rootns->root = M2
                                        rootns->state = ROOTNS_READY  <-- resets state

Because do_move_mount() with MNT_TREE_ROOTNS_INIT only checks whether
rootns_mnt_ns->root currently has an overmount (mp.parent !=
real_mount(new_path->mnt)), detaching M1 allows CPU1 to succeed.  CPU1 then
overwrites rootns->root without calling path_put() on M1 (and while
concurrent readers in filename_lookup() or copy_fs() may be reading
rootns->root without rootns->lock), and resets rootns->state to
ROOTNS_READY.  A subsequent rootns_enter() will then bootstrap a second
init process in copy_rootns(), overwriting c->init_pid without put_pid()
and taking a second get_rootns(c) reference that exit_rootns() never drops.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117050841.3289652-1-jarkko@kernel.org?part=9



More information about the Linux-security-module-archive mailing list