Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>
Cc: linux-mm@kvack.org, linux-security-module@vger.kernel.org,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Ben Segall" <bsegall@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Ingo Molnar" <mingo@redhat.com>, "Jan Kara" <jack@suse.cz>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"John Johansen" <john.johansen@canonical.com>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Kees Cook" <kees@kernel.org>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Manfred Spraul" <manfred@colorfullife.com>,
	"Mel Gorman" <mgorman@suse.de>, "Michal Hocko" <mhocko@suse.com>,
	"Mike Rapoport" <rppt@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Paul Moore" <paul@paul-moore.com>,
	"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: Re: [RFC PATCH v2 09/13] fs: Support installing rootns mount trees via move_mount()
Date: Wed, 18 Nov 2026 23:15:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-222163@kernel.org> (raw)
In-Reply-To: <20261117231352.3816603-10-jarkko@kernel.org>

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@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


  reply	other threads:[~2026-11-18 23:15 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 23:13 [RFC PATCH v2 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-17 23:13 ` [RFC PATCH v2 01/13] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-17 23:13 ` [RFC PATCH v2 02/13] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-17 23:13 ` [RFC PATCH v2 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 04/13] rootns: Add rootns_create() system call Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 05/13] rootns: Add rootns_wait() " Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 06/13] rootns: Add rootns_kill() " Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 07/13] fork: Support CLONE_INTO_ROOTNS in clone3() Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 08/13] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 09/13] fs: Support installing rootns mount trees via move_mount() Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot [this message]
2026-11-17 23:13 ` [RFC PATCH v2 10/13] arch: Wire up rootns system calls Jarkko Sakkinen
2026-11-17 23:13 ` [RFC PATCH v2 11/13] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-17 23:13 ` [RFC PATCH v2 12/13] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-18 23:15   ` sashiko-bot
2026-11-17 23:13 ` [RFC PATCH v2 13/13] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-18 14:47 ` [RFC PATCH v2 00/13] Namespace for namespace roots Josef Bacik
2026-11-18 15:28   ` Jarkko Sakkinen
2026-11-18 15:39     ` Jarkko Sakkinen
2026-11-18 15:46       ` Jarkko Sakkinen
2026-11-18 16:59 ` Andy Lutomirski
2026-11-18 17:04   ` Jarkko Sakkinen
2026-11-18 17:08     ` Jarkko Sakkinen
2026-11-18 17:30       ` Jarkko Sakkinen
2026-11-18 17:46       ` Andy Lutomirski
2026-11-18 18:23         ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-222163@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=corbet@lwn.net \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jack@suse.cz \
    --cc=jarkko@kernel.org \
    --cc=john.johansen@canonical.com \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=luto@amacapital.net \
    --cc=manfred@colorfullife.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=paul@paul-moore.com \
    --cc=peterz@infradead.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sergeh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=zygmunt.krynicki@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox