[RFC PATCH v5 16/25] fork: Support CLONE_INTO_ROOTNS in clone3()
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Sun Nov 22 00:10:57 UTC 2026
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 at 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
More information about the Linux-security-module-archive
mailing list