All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>,
	"David Howells" <dhowells@redhat.com>
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>,
	"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	"Ben Segall" <bsegall@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Jan Kara" <jack@suse.cz>,
	"John Johansen" <john.johansen@canonical.com>,
	"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>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Vincenzo Frascino" <vincenzo.frascino@arm.com>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: Re: [RFC PATCH v3 07/13] fork: Support CLONE_INTO_ROOTNS in clone3()
Date: Thu, 19 Nov 2026 19:36:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-223949@kernel.org> (raw)
In-Reply-To: <20261118193539.240065-8-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] cred: missing dumpability reset across userns without CLONE_VM
  in copy_creds()
- [Medium] fork: missing CAP_SYS_CHROOT check and umask reset to init_fs
  in copy_fs()
- [Medium] nsproxy: shared mm and timens vvar mismatch in
  copy_namespaces()
- [Low] rootns: wrong errno on external PID 1 race during bootstrap in
  copy_process()
--

commit 9762bc2570e709528c82312b63771aa4f8d6c6d8
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 spawn a
child process directly into a target root namespace via clone3().

> diff --git a/kernel/cred.c b/kernel/cred.c
> index d01df40a9a386..5bc3d455141b5 100644
> --- a/kernel/cred.c
> +++ b/kernel/cred.c
[ ... ]
> @@ -287,6 +294,28 @@ int copy_creds(struct task_struct *p, u64 clone_flags)
>  	if (!new)
>  		return -ENOMEM;
>  
> +	if (rootns) {
> +		const struct cred *old = current_cred();
> +
> +		if (!ns_capable(rootns->user_ns, CAP_SYS_ADMIN)) {
> +			ret = -EPERM;
> +			goto error_put;
> +		}
> +		if (new->user_ns != rootns->user_ns) {
> +			ret = install_user_ns_cred(new, rootns->user_ns);
> +			if (ret < 0)
> +				goto error_put;
> +		}
> +		if (!cred_cap_issubset(old, new) ||
> +		    ((clone_flags & CLONE_VM) && old->user_ns != new->user_ns)) {
> +			struct task_exec_state *exec_state;
> +
> +			exec_state = rcu_dereference_protected(p->exec_state, true);
> +			if (exec_state != &init_task_exec_state)
> +				WRITE_ONCE(exec_state->dumpable, suid_dumpable);
> +		}
> +	}

[Severity: High]
Should exec_state->dumpable be reset whenever old->user_ns != new->user_ns,
even when CLONE_VM is not set?

When a dumpable daemon (TASK_DUMPABLE_OWNER) creates a rootns and spawns a
child into it without CLONE_VM, old->euid owns rootns->user_ns, so
cred_cap_issubset(old, new) returns true:

kernel/cred.c:cred_cap_issubset() {
    ...
	for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
		if ((set_ns == subset_ns->parent)  &&
		    uid_eq(subset_ns->owner, set->euid))
			return true;
	}
    ...
}

Because CLONE_VM is clear, p->exec_state->dumpable remains
TASK_DUMPABLE_OWNER while p->real_cred->user_ns is switched to
rootns->user_ns.  In __ptrace_may_access(), a process in rootns->user_ns
with CAP_SYS_PTRACE passes ptrace_has_cap(tcred->user_ns, mode), and
task_still_dumpable() returns true on TASK_DUMPABLE_OWNER without checking
ptrace_has_cap(exec_state->user_ns, mode).  Until the child changes UID or
calls execve(), an unprivileged container root process can attach via
ptrace or read /proc/<pid>/mem and /proc/<pid>/fd/* while the child still
holds the parent daemon's forked memory, O_CLOEXEC descriptors, and host
UID.

> diff --git a/kernel/fork.c b/kernel/fork.c
> index 08fc28ad1eedf..f5b98eea9ce92 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
[ ... ]
> @@ -1649,7 +1672,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk,
>  		if (current->nsproxy->mnt_ns != &init_mnt_ns)
>  			return -EINVAL;
>  		fs = userspace_init_fs;
> -	} else if (flags & COPY_FS_IDLE) {
> +	} else if (flags & COPY_FS_IDLE || rootns) {
>  		fs = &init_fs;
>  	} else {
>  		fs = current->fs;
> @@ -1671,6 +1694,20 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk,
>  	tsk->real_fs = tsk->fs = copy_fs_struct(fs);
>  	if (!tsk->fs)
>  		return -ENOMEM;
> +	if (rootns) {
> +		struct path root __free(path_put) = {};
> +		int err;
> +
> +		err = mnt_ns_get_root(rootns->ns->mnt_ns, &root);
> +		if (err) {
> +			free_fs_struct(tsk->fs);
> +			tsk->real_fs = NULL;
> +			tsk->fs = NULL;
> +			return err;
> +		}
> +		set_fs_root(tsk->fs, &root);
> +		set_fs_pwd(tsk->fs, &root);
> +	}
>  	return 0;
>  }

[Severity: Medium]
Does copying init_fs when rootns is set discard current->fs->umask, and
should changing tsk->fs->root require CAP_SYS_CHROOT in rootns->user_ns?

Selecting init_fs causes copy_fs_struct() to initialize tsk->fs->umask from
init_fs.umask (0022) instead of inheriting the parent's umask.  In
addition, unlike mntns_install() on setns(CLONE_NEWNS), neither
rootns_prepare_fork() nor copy_creds() checks ns_capable(rootns->user_ns,
CAP_SYS_CHROOT).  A chrooted process already running in rootns->user_ns
that has dropped CAP_SYS_CHROOT while retaining CAP_SYS_ADMIN can call
clone3(CLONE_INTO_ROOTNS) to spawn a child with fs->root reset to the mount
namespace root and escape its chroot.

[ ... ]

> @@ -2510,6 +2548,12 @@ __latent_entropy struct task_struct *copy_process(
>  		goto bad_fork_core_free;
>  	}
>  
> +	if (unlikely(clone_args_rootns_bootstrapping(args) &&
> +		     !is_child_reaper(pid))) {
> +		retval = -EBUSY;
> +		goto bad_fork_core_free;
> +	}
> +
>  	/* Let kill terminate clone/fork in the middle */
>  	if (fatal_signal_pending(current)) {
>  		retval = -EINTR;
> diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
> index 8a3c6d4e94e2a..ec8b6fac7fbf1 100644
> --- a/kernel/nsproxy.c
> +++ b/kernel/nsproxy.c
> @@ -166,12 +167,21 @@ 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) {
> +		if (old_ns->time_ns != rootns->ns->time_ns)
> +			timens_commit_fork(tsk, rootns->ns->time_ns,
> +					   flags & CLONE_VM);
> +		get_nsproxy(rootns->ns);
> +		tsk->nsproxy = rootns->ns;
> +		return 0;
> +	}

[Severity: Medium]
Can switching tsk->nsproxy->time_ns under CLONE_VM | CLONE_VFORK install the
child time namespace's vvar page into the parent's shared mm_struct?

When clone3() is called with CLONE_INTO_ROOTNS | CLONE_VM | CLONE_VFORK and
rootns has a different time namespace, timens_commit_fork() skips
vdso_join_timens() because shared_vm is true, while tsk->nsproxy is switched
immediately to rootns->ns (where time_ns == time_ns_for_children).  The
parent and the vfork child now share current->mm while having different
nsproxy->time_ns pointers.

If VDSO_TIME_PAGE_OFFSET in the shared mm's [vvar] VMA has not been faulted
in yet and the child calls a VDSO time function before execve(),
vvar_fault() calls find_timens_vvar_page(), which sees vma->vm_mm ==
current->mm and maps rootns->ns->time_ns->vvar_page into the shared page
tables.  Because time_ns already equals time_ns_for_children in rootns->ns,
exec_task_namespaces() on the child's later execve() is a no-op, leaving
the parent's mm permanently mapped to the child time namespace's vvar page.
Conversely, if the parent already faulted [vvar] before clone3(), the child
observes the parent's time namespace through VDSO and rootns->ns->time_ns
through syscalls until execve().

Also, calling timens_set_vvar_page() in copy_namespaces() before fallible
steps in copy_process() (such as alloc_pid() and cgroup_can_fork())
permanently sets rootns->ns->time_ns->frozen_offsets = true even when the
fork fails.

> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index 32bb8ef5ed584..cfc33c243dba4 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
[ ... ]
> +int rootns_finish_fork(struct kernel_clone_args *args, struct task_struct *p)
> +{
> +	struct file *file __free(fput) = args->rootns_file;
> +	struct rootns *rootns = args->rootns;
> +	struct pid_namespace *pid_ns = rootns->ns->pid_ns_for_children;
> +	int ret;
> +
> +	args->rootns_file = NULL;
> +	args->rootns = NULL;
> +	if (!IS_ERR(p))
> +		return 0;
> +
> +	ret = PTR_ERR(p);
> +	if (args->rootns_bootstrapping) {
> +		scoped_guard(spinlock, &rootns->lock) {
> +			if (rootns->state == ROOTNS_FORKING)
> +				WRITE_ONCE(rootns->state, ROOTNS_INIT);
> +		}
> +		wake_up_all(&rootns->waitq);
> +	} else if (ret == -ENOMEM &&
> +		   !(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING)) {
> +		return -ESRCH;
> +	}
> +
> +	return ret;
> +}

[Severity: Low]
Can a bootstrap race with an external fork into pid_ns return -EINVAL or
-ENOMEM instead of the documented -EBUSY or -ESRCH?

If an external process enters pid_ns and allocates PID 1 in alloc_pid()
after rootns_prepare_fork() checks READ_ONCE(pid_ns->child_reaper) but
before setting child_reaper under tasklist_lock, the bootstrapping task's
alloc_pid() allocates nr == 2 while child_reaper is still NULL and fails
with -EINVAL, never reaching the !is_child_reaper(pid) check in
copy_process().  Similarly, if that external PID 1 exits and clears
PIDNS_ADDING before the bootstrapping task finishes copy_process(),
copy_process() returns -ENOMEM, and rootns_finish_fork() only translates
-ENOMEM to -ESRCH in the !args->rootns_bootstrapping branch.

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

  reply	other threads:[~2026-11-19 19:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 19:35 [RFC PATCH v3 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-18 19:35 ` [RFC PATCH v3 01/13] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 02/13] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 04/13] rootns: Add rootns_create() system call Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 05/13] rootns: Add rootns_wait() " Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 06/13] rootns: Add rootns_kill() " Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 07/13] fork: Support CLONE_INTO_ROOTNS in clone3() Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot [this message]
2026-11-18 19:35 ` [RFC PATCH v3 08/13] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 09/13] fs: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-11-19 19:36   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 10/13] arch: Wire up rootns system calls Jarkko Sakkinen
2026-11-19 19:37   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 11/13] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-19 19:37   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 12/13] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-19 19:37   ` sashiko-bot
2026-11-18 19:35 ` [RFC PATCH v3 13/13] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-18 19:54 ` [RFC PATCH v3 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-18 20:42   ` Jarkko Sakkinen
2026-11-18 21:22     ` Jarkko Sakkinen
2026-11-19  4:43     ` Jarkko Sakkinen
2026-11-19  6:21       ` Jarkko Sakkinen
2026-11-18 22:36 ` Vlastimil Babka (SUSE)
2026-11-18 23:33   ` 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-223949@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=anna-maria@linutronix.de \
    --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=frederic@kernel.org \
    --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=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vincenzo.frascino@arm.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.