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 07/13] fork: Support CLONE_INTO_ROOTNS in clone3()
Date: Wed, 18 Nov 2026 23:15:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-222161@kernel.org> (raw)
In-Reply-To: <20261117231352.3816603-8-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Critical] cred: missing dumpability reset and shared mm on
  CLONE_INTO_ROOTNS
- [Critical] cred: privilege escalation and LSM bypass in
  may_enter_rootns_creds()
- [High] rootns: UAF and refcount underflow on pid_ns->rootns without
  CLONE_INTO_ROOTNS
- [Medium] fork: copy_rootns() sets ROOTNS_RUNNING before
  attach_pid(PIDTYPE_PID)
- [Medium] nsproxy: missing timens_commit() and CLONE_VM check in
  copy_namespaces()
--

commit 32f57f44b3303d6e1266e96415fc759ba100f243
Author: Jarkko Sakkinen <jarkko@kernel.org>

fork: Support CLONE_INTO_ROOTNS in clone3()

This commit adds CLONE_INTO_ROOTNS and struct clone_args.rootns to allow
clone3() to spawn a child process directly into an initialized root
namespace.

> diff --git a/kernel/cred.c b/kernel/cred.c
> index d01df40a9a386..afc91eed26563 100644
> --- a/kernel/cred.c
> +++ b/kernel/cred.c

[ ... ]

> @@ -251,6 +255,25 @@ struct cred *prepare_exec_creds(void)
>  	return new;
>  }
>  
> +static bool cred_cap_issubset(const struct cred *set,
> +			      const struct cred *subset);
> +
> +static bool may_enter_rootns_creds(const struct cred *old,
> +				   const struct cred *target)
> +{
> +	if (cred_cap_issubset(old, target) &&
> +	    uid_eq(old->euid, target->euid) &&
> +	    gid_eq(old->egid, target->egid))
> +		return true;
> +
> +	if (old->user_ns == target->user_ns)
> +		return false;
> +
> +	return ns_capable(target->user_ns->parent, CAP_SYS_ADMIN) &&
> +	       ns_capable(target->user_ns->parent, CAP_SETUID) &&
> +	       ns_capable(target->user_ns->parent, CAP_SETGID);
> +}

[Severity: Critical]
Can a caller escalate privileges or bypass LSM restrictions when
copy_creds() replaces the child's credentials with dup_creds(rootns->cred)?

dup_creds(rootns->cred) copies all fields of struct cred from rootns->cred,
including uid, suid, fsuid, gid, sgid, fsgid, group_info, cap_bset,
cap_effective, cap_inheritable, cap_ambient, and security (passing
rootns->cred as old to security_prepare_creds()).

In the first branch of may_enter_rootns_creds(), only cap_permitted, euid,
and egid are checked.  A process that creates or inherits a rootns fd and
then drops its real/saved UIDs or GIDs, clears supplementary groups with
setgroups(), drops capabilities from cap_bset or cap_effective, sets
no_new_privs, or confines itself with an LSM such as Landlock still passes
this check and spawns a child with the original unconfined rootns->cred.

In the second branch, when old->user_ns != target->user_ns, only
ns_capable() on target->user_ns->parent is checked, without verifying
cred_cap_issubset(old, target) or checking that target's UIDs, GIDs, and
group_info are mapped into target->user_ns->parent.  If a process with
GLOBAL_ROOT_UID enters a child user namespace whose parent is owned by an
unprivileged user and calls rootns_create(0), the unprivileged owner of
target->user_ns->parent can clone into that rootns and gain
GLOBAL_ROOT_UID.

Conversely, when old->user_ns == target->user_ns and old->euid !=
target->euid, the early return rejects callers even when they hold
CAP_SYS_ADMIN, CAP_SETUID, and CAP_SETGID in target->user_ns.

> @@ -260,7 +283,8 @@ struct cred *prepare_exec_creds(void)
>   * The new process gets the current process's subjective credentials as its
>   * objective and subjective credentials
>   */
> -int copy_creds(struct task_struct *p, u64 clone_flags)
> +int copy_creds(struct task_struct *p, u64 clone_flags,
> +	       struct rootns *rootns)
>  {
>  	struct cred *new;
>  	int ret;
> @@ -283,7 +307,13 @@ int copy_creds(struct task_struct *p, u64 clone_flags)
>  		return 0;
>  	}
>  
> -	new = prepare_creds();
> +	if (rootns) {
> +		if (!may_enter_rootns_creds(current_cred(), rootns->cred))
> +			return -EPERM;
> +		new = dup_creds(rootns->cred);
> +	} else {
> +		new = prepare_creds();
> +	}
>  	if (!new)
>  		return -ENOMEM;

[Severity: Critical]
Does switching p to rootns->cred leave p->exec_state->dumpable set to
TASK_DUMPABLE_OWNER and allow sharing current->mm across privilege
boundaries?

In copy_process(), copy_exec_state() runs before copy_creds() and copies
current->exec_state (or refcount-shares it when CLONE_VM | CLONE_VFORK is
used).  Unlike commit_creds(), copy_creds() does not reset dumpability to
suid_dumpable when UIDs, GIDs, or capabilities change between
current_cred() and rootns->cred:

kernel/ptrace.c:task_still_dumpable() {
	...
	exec_state = task_exec_state_rcu(task);
	if (READ_ONCE(exec_state->dumpable) == TASK_DUMPABLE_OWNER)
		return true;
	return ptrace_has_cap(exec_state->user_ns, mode);
}

When a dumpable privileged daemon spawns a child into a less-privileged
rootns, unprivileged tasks in that rootns matching rootns->cred can attach
via ptrace() or access /proc/<pid>/mem and /proc/<pid>/fd/* before the
child calls execve().  Because clone3_args_valid() also allows CLONE_VM |
CLONE_VFORK with CLONE_INTO_ROOTNS, the child shares current->mm with the
parent daemon, allowing tasks in the target rootns to modify the parent
daemon's live memory.

> diff --git a/kernel/fork.c b/kernel/fork.c
> index 99e22b3cd213e..978605de8428e 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c

[ ... ]

> @@ -2566,7 +2583,7 @@ __latent_entropy struct task_struct *copy_process(
>  			attach_pid(p, PIDTYPE_TGID);
>  			attach_pid(p, PIDTYPE_PGID);
>  			attach_pid(p, PIDTYPE_SID);
> -			copy_rootns(p, NULL);
> +			copy_rootns(p, clone_args_rootns(args));
>  			__this_cpu_inc(process_counts);
>  		} else {
>  			current->signal->nr_threads++;

[Severity: High]
Can forking into a rootns PID namespace via setns(CLONE_NEWPID) without
CLONE_INTO_ROOTNS cause a refcount underflow or use-after-free on struct
rootns?

create_rootns() sets ns->pid_ns_for_children->rootns = c without taking a
reference on c and activates the namespace in ns_tree, while put_rootns()
frees c without clearing pid_ns_for_children->rootns.  A caller with
CAP_SYS_ADMIN can open an nsfs fd for pid_ns_for_children via
open_by_handle_at(FD_NSFS_ROOT, ...), call setns(pidns_fd, CLONE_NEWPID),
and fork() PID 1 without CLONE_INTO_ROOTNS.  Because clone_args_rootns() is
NULL, copy_rootns() returns early without calling get_rootns(c), but when
that PID 1 exits, zap_pid_ns_processes() -> exit_rootns() still
dereferences pid_ns->rootns and calls put_rootns(c):

kernel/rootns.c:copy_rootns() {
	if (likely(!c || READ_ONCE(c->state) != ROOTNS_FORKING))
		return;

	scoped_guard(spinlock, &c->lock) {
		get_rootns(c);
		WRITE_ONCE(c->init_pid, get_task_pid(tsk, PIDTYPE_TGID));
		WRITE_ONCE(c->state, ROOTNS_RUNNING);
	}
	wake_up_all(&c->waitq);
}

If the rootns fd was already closed, exit_rootns() dereferences freed
memory; if the rootns fd is still open, exit_rootns() drops the fd's
reference and frees c prematurely.

[Severity: Medium]
Can a concurrent rootns_kill() or close() with ROOTNS_KILL_ON_CLOSE miss
the init task because copy_rootns() transitions rootns->state to
ROOTNS_RUNNING before attach_pid(p, PIDTYPE_PID)?

CPU0                              CPU1
copy_process()
  attach_pid(p, PIDTYPE_TGID)
  copy_rootns(p, ...)
    c->init_pid = ...
    c->state = ROOTNS_RUNNING
                                  SYSCALL_DEFINE2(rootns_kill, ...)
                                    rootns_kill_members()
                                      kill_pid(pid, sig, 0)
                                        pid_task(pid, PIDTYPE_PID)
                                        --> NULL (-ESRCH)
  attach_pid(p, PIDTYPE_PID)

Neither __rootns_kill() nor rootns_kill_members() takes tasklist_lock, so a
signal sent in this window sees ROOTNS_RUNNING and PIDTYPE_TGID attached,
but kill_pid() looks up PIDTYPE_PID and fails with -ESRCH.  Calling
copy_rootns() after attach_pid(p, PIDTYPE_PID) avoids this window.

> diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
> index 8a3c6d4e94e2a..78a1e1fb2cfd7 100644
> --- a/kernel/nsproxy.c
> +++ b/kernel/nsproxy.c
> @@ -166,12 +167,18 @@ struct nsproxy *create_new_namespaces(u64 flags,
>   * called from clone.  This now handles copy for nsproxy and all
>   * namespaces therein.
>   */
> -int copy_namespaces(u64 flags, struct task_struct *tsk)
> +int copy_namespaces(u64 flags, struct task_struct *tsk,
> +		    struct rootns *rootns)
>  {
>  	struct nsproxy *old_ns = tsk->nsproxy;
>  	struct user_namespace *user_ns = task_cred_xxx(tsk, user_ns);
>  	struct nsproxy *new_ns;
>  
> +	if (rootns) {
> +		get_nsproxy(rootns->ns);
> +		tsk->nsproxy = rootns->ns;
> +		return 0;
> +	}

[Severity: Medium]
Does returning early here skip time namespace VDSO setup when rootns->ns
has a different time namespace from the caller?

In copy_process(), copy_mm() duplicates current->mm (including any faulted
[vvar] mappings for old_ns->time_ns) before copy_namespaces() runs.
Returning early without calling timens_commit() or timens_on_fork() leaves
the child's [vvar] mapping pointing at the parent's time namespace until
execve(), and when CLONE_VM | CLONE_VFORK is used, allows tasks sharing an
mm_struct to have different time namespaces.

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


  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 [this message]
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
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-222161@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