[RFC PATCH v9 06/29] mount: Support MOVE_MOUNT_T_MNTNS in move_mount()

sashiko-bot at kernel.org sashiko-bot at kernel.org
Tue Dec 1 03:36:20 UTC 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] fs: wrong cross-userns mount locking in attach_recursive_mnt()
  propagation
--

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

mount: Support MOVE_MOUNT_T_MNTNS in move_mount()

This commit adds the MOVE_MOUNT_T_MNTNS flag to move_mount() to attach
detached anonymous mount trees into a target mount namespace specified by
an nsfs file descriptor.

> diff --git a/fs/namespace.c b/fs/namespace.c
> index 1bb9f90a65239..8eea00d1dbdbf 100644
> --- a/fs/namespace.c
> +++ b/fs/namespace.c

[ ... ]

> @@ -2632,13 +2647,15 @@ enum mnt_tree_flags_t {
>   *         Otherwise a negative error code is returned.
>   */
>  static int attach_recursive_mnt(struct mount *source_mnt,
> -				const struct pinned_mountpoint *dest)
> +				const struct pinned_mountpoint *dest,
> +				struct user_namespace *src_user_ns)
>  {
>  	struct mount *dest_mnt = dest->parent;
>  	struct mountpoint *dest_mp = dest->mp;
>  	HLIST_HEAD(tree_list);
>  	struct mnt_namespace *ns = dest_mnt->mnt_ns;
>  	struct user_namespace *user_ns = ns->user_ns;
> +	bool cross_src = src_user_ns && !in_userns(ns->user_ns, src_user_ns);
>  	struct pinned_mountpoint root = {};
>  	struct mountpoint *shorter = NULL;
>  	struct mount *child, *p;
> @@ -2688,7 +2705,8 @@ static int attach_recursive_mnt(struct mount *source_mnt,
>  		err = invent_group_ids(source_mnt, true);
>  		if (err)
>  			goto out;
> -		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
> +		err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list,
> +				    cross_src);
>  	}
>  	lock_mount_hash();
>  	if (err)
> @@ -2725,11 +2743,22 @@ static int attach_recursive_mnt(struct mount *source_mnt,
>  	hlist_add_head(&source_mnt->mnt_hash, &tree_list);
>  
>  	hlist_for_each_entry_safe(child, n, &tree_list, mnt_hash) {
> +		struct user_namespace *child_user_ns =
> +			child->mnt_parent->mnt_ns->user_ns;
> +		bool cross_child_src = cross_src &&
> +				       !in_userns(child_user_ns, src_user_ns);
>  		struct mount *q;
> +
>  		hlist_del_init(&child->mnt_hash);
> -		/* Notice when we are propagating across user namespaces */
> -		if (child->mnt_parent->mnt_ns->user_ns != user_ns)
> +		/*
> +		 * Notice when we are propagating or attaching a foreign
> +		 * source tree across user namespaces.
> +		 */
> +		if (child_user_ns != user_ns || cross_child_src) {
>  			lock_mnt_tree(child);
> +			if (cross_child_src && list_empty(&child->mnt_expire))
> +				child->mnt.mnt_flags |= MNT_LOCKED;
> +		}
>  		q = __lookup_mnt(&child->mnt_parent->mnt,
>  				 child->mnt_mountpoint);
>  		commit_tree(child);

[Severity: High]
Does gating cross_child_src and propagate_mnt()'s lock_source on cross_src
mishandle shared mount propagation across different user namespaces?

cross_src only compares src_user_ns against the primary destination
ns->user_ns.  When in_userns(ns->user_ns, src_user_ns) is true (so
cross_src is false), but dest_mnt propagates to a slave or peer mount in a
less-privileged user namespace where !in_userns(child_user_ns, src_user_ns)
holds, propagate_mnt() runs without CL_LOCK_SOURCE and copy_tree()
silently drops unlocked unbindable or nsfs submounts instead of returning
-EPERM, exposing the underlying covered directories.  In the tree_list
loop, cross_child_src also stays false, leaving child without MNT_LOCKED
even when child_user_ns == user_ns does not hold or when lock_mnt_tree()
only locks submounts below child.

Conversely, when cross_src is true, propagate_mnt() passes CL_LOCK_SOURCE
to copy_tree() for every secondary mount n regardless of
n->mnt_ns->user_ns (and chains copy = this with MNT_LOCKED already set on
submounts), rejecting unbindable/nsfs submounts with -EPERM and locking
submounts even in peer namespaces that are privileged over src_user_ns.

Checking !in_userns(n->mnt_ns->user_ns, src_user_ns) per secondary target
in propagate_mnt() (without mutating the source tree used for subsequent
peers) and dropping cross_src from the cross_child_src condition would
evaluate each target namespace against src_user_ns directly.

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



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