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 Mikhalitsyn" <alex@amutable.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Christian Brauner" <brauner@kernel.org>,
	"David Howells" <dhowells@redhat.com>, "Jan Kara" <jack@suse.cz>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Kees Cook" <kees@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Paul Moore" <paul@paul-moore.com>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Zygmunt Krynicki" <zygmunt.krynicki@canonical.com>
Subject: Re: [RFC PATCH v5 16/25] fork: Support CLONE_INTO_ROOTNS in clone3()
Date: Sun, 22 Nov 2026 00:10:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-227551@kernel.org> (raw)
In-Reply-To: <20261121000844.1863324-17-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] rootns: leaf-only PIDNS_ADDING check returns -ENOMEM on dead
  ancestor pid_ns
--

commit 62f2477b1da0630c69ec1a2d1d679d2b7228b9f8
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 or running
root namespace.

> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index 5ae975604571f..6fb49c20cc2df 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
> @@ -36,6 +36,7 @@
>  #include <uapi/linux/wait.h>
>  #include <linux/file.h>
>  #include <linux/fs.h>
> +#include <linux/fs_struct.h>
>  #include <linux/anon_inodes.h>
>  #include <linux/rootns.h>
>  #include <linux/sched/signal.h>
> @@ -426,3 +427,142 @@ SYSCALL_DEFINE1(rootns, unsigned int, flags)
>  	return fd;
>  }
>  
[ ... ]
> +int rootns_prepare_fork(struct kernel_clone_args *args)
> +{
> +	struct file *file __free(fput) = fget(args->rootns_fd);
> +	struct path root __free(path_put) = {};
> +	struct pid_namespace *pid_ns;
> +	bool bootstrapping = false;
> +	struct rootns *rootns;
> +	int ret;
> +
> +	if (!file)
> +		return -EBADF;
> +	if (!is_rootns_file(file))
> +		return -EINVAL;
> +
> +	rootns = file->private_data;
> +	if (!ns_capable(rootns->user_ns, CAP_SYS_ADMIN) ||
> +	    !ns_capable(rootns->user_ns, CAP_SYS_CHROOT))
> +		return -EPERM;
> +	if (current_chrooted() && !ns_capable(current_user_ns(), CAP_SYS_CHROOT))
> +		return -EPERM;
> +
> +	pid_ns = rootns->ns->pid_ns_for_children;
> +	if (!pidns_is_ancestor(pid_ns, task_active_pid_ns(current)))
> +		return -EXDEV;
> +
> +	ret = mnt_ns_get_root(rootns->ns->mnt_ns, &root);
> +	if (ret)
> +		return ret;
> +
> +	for (;;) {
> +		bool wait = false;
> +
> +		scoped_guard(spinlock, &rootns->lock) {
> +			switch (rootns->state) {
> +			case ROOTNS_INIT:
> +				if (!(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING))
> +					return -ESRCH;
> +				if (READ_ONCE(pid_ns->child_reaper))
> +					return -EBUSY;
> +				WRITE_ONCE(rootns->state, ROOTNS_FORKING);
> +				bootstrapping = true;
> +				break;
> +			case ROOTNS_FORKING:
> +				wait = true;
> +				break;
> +			case ROOTNS_RUNNING:
> +				if (!(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING))
> +					return -ESRCH;
> +				break;
> +			case ROOTNS_DEAD:
> +				return -ESRCH;
> +			}
> +		}
> +		if (!wait)
> +			break;
> +
> +		ret = wait_event_killable(rootns->waitq,
> +					  READ_ONCE(rootns->state) != ROOTNS_FORKING);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	args->rootns_file = no_free_ptr(file);
> +	args->rootns = rootns;
> +	args->rootns_bootstrapping = bootstrapping;
> +	return 0;
> +}
> +
> +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)) {
> +		if (args->rootns_bootstrapping)
> +			wake_up_all(&rootns->waitq);
> +		if (current->nsproxy->time_ns != rootns->ns->time_ns)
> +			timens_commit(p, rootns->ns->time_ns);
> +		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);
> +		if (ret == -EINVAL && !args->set_tid_size &&
> +		    (READ_ONCE(pid_ns->child_reaper) ||
> +		     (READ_ONCE(pid_ns->pid_allocated) & ~PIDNS_ADDING)))
> +			ret = -EBUSY;
> +	}
> +	if ((ret == -ENOMEM || ret == -EBUSY) &&
> +	    !(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING))
> +		return -ESRCH;
> +
> +	return ret;
> +}

[Severity: Medium]
Can clone3(CLONE_INTO_ROOTNS) return -ENOMEM instead of -ESRCH when an
intermediate ancestor PID namespace is tearing down?

rootns_prepare_fork() allows callers in any ancestor active PID namespace,
but both rootns_prepare_fork() and rootns_finish_fork() only check
PIDNS_ADDING on the leaf pid_ns.  If the target rootns was created inside
an intermediate PID namespace whose init exits while the leaf pid_ns still
has PIDNS_ADDING set (for example, when the nested rootns has not been
bootstrapped yet), rootns_prepare_fork() proceeds into copy_process(),
alloc_pid() fails with -ENOMEM when it walks the ancestor PID namespaces,
and rootns_finish_fork() leaves the error as -ENOMEM.

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


  reply	other threads:[~2026-11-22  0:11 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  0:08 [RFC PATCH v5 00/25] rootns: fd-bound process closures Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 01/25] userns: Factor out idmap validation and commit helpers Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 02/25] userns: Add userns_set_idmap() system call Jarkko Sakkinen
2026-11-21 16:09   ` Jarkko Sakkinen
2026-11-21 16:49     ` Jarkko Sakkinen
2026-11-21 16:53       ` Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 03/25] arch: Wire up " Jarkko Sakkinen
2026-11-21 15:58   ` Jarkko Sakkinen
2026-11-21 18:12     ` Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 04/25] mount: Add mnt_ns_get_root() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 05/25] mount: Enforce mount_too_revealing() for foreign mntns Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 06/25] mount: Support MOVE_MOUNT_T_MNTNS in move_mount() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 07/25] mount: Support MOVE_MOUNT_CLONE with MOVE_MOUNT_T_MNTNS Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 08/25] userns: Factor out install_user_ns_cred() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 09/25] nsfs: Add nsproxy_open_namespace() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 10/25] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 11/25] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 12/25] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-21  1:32   ` Jarkko Sakkinen
2026-11-21 18:08     ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 13/25] rootns: Add rootns() system call Jarkko Sakkinen
2026-11-21 19:10   ` Jarkko Sakkinen
2026-11-21 19:12     ` Jarkko Sakkinen
2026-11-21 19:54       ` Jarkko Sakkinen
2026-11-21 20:16         ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 14/25] rootns: Add ROOTNS_WAIT ioctl Jarkko Sakkinen
2026-11-21  0:17   ` Jarkko Sakkinen
2026-11-21  0:18     ` Jarkko Sakkinen
2026-11-21  1:09       ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 15/25] rootns: Add ROOTNS_KILL ioctl Jarkko Sakkinen
2026-11-21  1:19   ` Jarkko Sakkinen
2026-11-21 11:24     ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 16/25] fork: Support CLONE_INTO_ROOTNS in clone3() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot [this message]
2026-11-21  0:08 ` [RFC PATCH v5 17/25] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-22  0:10   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 18/25] proc: Implement ->set_rootns() fs_context operation Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 19/25] ipc/mqueue: " Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 20/25] security: Add rootns LSM hooks Jarkko Sakkinen
2026-11-21 11:47   ` Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 21/25] bpf: Register " Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 22/25] arch: Wire up rootns() system call Jarkko Sakkinen
2026-11-22  0:11   ` sashiko-bot
2026-11-21  0:08 ` [RFC PATCH v5 23/25] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 24/25] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-21  0:08 ` [RFC PATCH v5 25/25] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-21 17:21   ` 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-227551@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alex@amutable.com \
    --cc=brauner@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dhowells@redhat.com \
    --cc=jack@suse.cz \
    --cc=jarkko@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=kees@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=neal@gompa.dev \
    --cc=paul@paul-moore.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sergeh@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --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