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

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 23:15:21 UTC 2026


Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] fs: check_for_nsfs_mounts() checks caller mnt_ns instead of
  rootns target
- [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
--

commit f0ccb5459fd227ad26c0bf5f42a1937aaa266930
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 allow attaching detached
anonymous mount trees into a rootns mount namespace via its file descriptor,
and allocates fsmount() anonymous mount namespaces in fc->user_ns.

> diff --git a/fs/namespace.c b/fs/namespace.c
> index 010ed5d68d229..2dfb5e01d40f0 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)) {

[Severity: High]
Can moving an nsfs mount into a rootns with MNT_TREE_ROOTNS create an
unbreakable mount namespace reference cycle?

Later in do_move_mount(), check_for_nsfs_mounts(old) calls mnt_ns_loop(),
which checks against current->nsproxy->mnt_ns rather than the destination
mp.parent->mnt_ns:

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

Because the target rootns mount namespace was created after the caller's
mount namespace, current->nsproxy->mnt_ns->ns.ns_id is smaller than
rootns_mnt_ns->ns.ns_id.  If the caller clones an nsfs mount referring to
rootns_mnt_ns via open_tree(OPEN_TREE_CLONE) and moves it into rootns_mnt_ns
with MOVE_MOUNT_T_ROOTNS_ROOT, mnt_ns_loop() returns false.  The mounted
nsfs dentry then pins rootns_mnt_ns while rootns_mnt_ns pins the mount,
permanently leaking the mount namespace and all mounts inside it.

Passing mp.parent->mnt_ns to check_for_nsfs_mounts() and mnt_ns_loop() would
check against the actual destination namespace.

> @@ -3845,7 +3855,16 @@ static int do_move_mount(const struct path *old_path,
>  	if (mount_is_ancestor(old, mp.parent))
>  		return -ELOOP;
>  
> -	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)
mishandle overmounted subtrees, shared mount propagation, and concurrent
readers?

Inside attach_recursive_mnt(), any existing mount q at the target mountpoint
(for example when MOVE_MOUNT_BENEATH or overmounting is used) is reparented
onto topmost_overmount(old) via mnt_change_mountpoint(r, mp, q), and
MNT_LOCKED is cleared on q so it remains unmountable.  Running
lock_mnt_tree(old) afterwards walks into q and all of q's descendants via
next_mnt(p, old), setting MNT_LOCKED and MNT_LOCK_* on pre-existing mounts
in the rootns and preventing q from being unmounted.

In addition, if mp.parent is a shared mount in the rootns,
attach_recursive_mnt() clones old for propagation 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), the cross-userns check
child->mnt_parent->mnt_ns->user_ns != user_ns is false for peers in the
target user_ns, leaving propagated copies unlocked.  Finally, dropping
mount_lock in unlock_mount_hash() before re-acquiring mount_writer leaves a
window where old is already visible in the rootns without MNT_LOCKED or
MNT_LOCK_* set.

> @@ -4701,7 +4720,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]
Does allocating the anonymous mount namespace in fc->user_ns allow
submounts from a parent user namespace to be installed into an unprivileged
rootns without MNT_LOCKED?

When a process in a parent user namespace configures a superblock for an
unprivileged rootns via FSCONFIG_SET_ROOTNS, fc->user_ns is set to the
target rootns user_ns, so fsmount() creates a root mount M_root with
M_root->mnt_ns->user_ns equal to the rootns user_ns.  If the process then
attaches a host mount M_sub (for example, a read-only bind mount or masking
mount from init_user_ns) onto M_root via move_mount(), attach_recursive_mnt()
calls lock_mnt_tree(M_sub), which only sets MNT_LOCKED on descendants p !=
M_sub and leaves M_sub itself without MNT_LOCKED.

When M_root is subsequently moved into the rootns with
MOVE_MOUNT_T_ROOTNS_ROOT, do_move_mount() sees M_root->mnt_ns->user_ns ==
mp.parent->mnt_ns->user_ns and skips lock_mnt_tree(M_root).  As a result,
M_sub is never marked MNT_LOCKED in the rootns, allowing an unprivileged
user in the rootns to unmount M_sub and expose the underlying directory.
Conversely, attaching another FSCONFIG_SET_ROOTNS mount onto M_root via
normal move_mount() causes attach_recursive_mnt() to compare against
current->nsproxy->mnt_ns->user_ns (because M_root->mnt_ns is anonymous) and
spuriously lock the same-userns submount.

> @@ -4741,6 +4760,129 @@ static inline int vfs_move_mount(const struct path *from_path,

[ ... ]

> +	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 "." or "subdir/.." resolve to_path to
rootns->root and bypass the is_root checks?

Because is_root only tests for the literal string "/", passing "." after
root installation enters the else branch and resolves to_path to
rootns->root:

1. If MOVE_MOUNT_BENEATH is set with to_pathname = ".", the MOVE_MOUNT_BENEATH
   check in the is_root branch is bypassed.  do_lock_mount() selects the
   underlying nullfs root as mp.parent and mounts the new tree beneath
   rootns->root, clearing MNT_LOCKED on rootns->root in
   attach_recursive_mnt() so an unprivileged rootns user can unmount a
   locked cross-userns root mount.

2. Without MOVE_MOUNT_BENEATH, to_pathname = "." overmounts rootns->root
   without updating rootns->root (and similarly on initial "/" installation
   if from_path already has overmounts stacked on its root dentry).  Because
   copy_fs() and preset-root lookups in path_init() do not traverse
   overmounts on rootns->root, subsequent CLONE_INTO_ROOTNS processes and
   move_mount_rootns() lookups stay 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 concurrent "/" installations race on rootns->state, leaking the previous
rootns->root and resetting a running or dead rootns back to ROOTNS_READY?

When is_root is true, rootns->state == ROOTNS_EMPTY is only checked before
lookup_move_mount_from() and before namespace_lock() is taken, while
MNT_TREE_ROOTNS_INIT in do_move_mount() only checks whether
rootns_mnt_ns->root currently has an overmount:

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
  ...
  clone3(CLONE_INTO_ROOTNS)
    rootns->state = ROOTNS_RUNNING
  umount2("/", MNT_DETACH)
    detaches M1 from nullfs
                                        vfs_move_mount() -> attaches M2
                                        spin_lock(&rootns->lock)
                                        rootns->root = M2  <-- leaks M1 path
                                        rootns->state = ROOTNS_READY
                                          <-- resets state from RUNNING

Overwriting rootns->root without path_put() leaks M1's vfsmount and dentry
references and races with lockless readers of rootns->root in
filename_lookup() and copy_fs().  Resetting rootns->state to ROOTNS_READY
also allows a second clone3(CLONE_INTO_ROOTNS) to run the init bootstrap path
in copy_rootns(), overwriting rootns->init_pid without put_pid() and taking
an extra get_rootns() reference that exit_rootns() never drops.

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



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