Linux Security Modules development
 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>,
	"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>,
	"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>
Subject: Re: [RFC PATCH 09/13] fs: Support installing rootns mount trees via move_mount()
Date: Wed, 18 Nov 2026 05:09:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-220144@kernel.org> (raw)
In-Reply-To: <20261117050841.3289652-10-jarkko@kernel.org>

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

  reply	other threads:[~2026-11-18  5:09 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:08 [RFC PATCH 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-17  5:08 ` [RFC PATCH 01/13] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 02/13] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 04/13] rootns: Add rootns_create() system call Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 05/13] rootns: Add rootns_wait() " Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 06/13] rootns: Add rootns_kill() " Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 07/13] rootns: Add rootns_enter() " Jarkko Sakkinen
2026-11-17 17:42   ` Andy Lutomirski
2026-11-17 20:03     ` Jarkko Sakkinen
2026-11-17 20:53       ` Andy Lutomirski
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 08/13] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 09/13] fs: Support installing rootns mount trees via move_mount() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot [this message]
2026-11-17  5:08 ` [RFC PATCH 10/13] arch: Wire up rootns system calls Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 11/13] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 12/13] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 13/13] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-17 11:10 ` [RFC PATCH 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-17 17:44 ` Andy Lutomirski
2026-11-17 18:48   ` Jarkko Sakkinen
2026-11-17 19:10     ` 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-220144@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=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=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 \
    /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