[RFC PATCH v4 07/14] fork: Support CLONE_INTO_ROOTNS in clone3()

sashiko-bot at kernel.org sashiko-bot at kernel.org
Fri Nov 20 22:44:37 UTC 2026


Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] rootns: rootns_finish_fork() rewrites copy_process() -EINVAL
  to -EBUSY
- [Low] rootns: unreachable PIDNS_ADDING check in rootns_prepare_fork()
--

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

> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index ce012a07e9828..865689eed69cb 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
> @@ -423,3 +424,138 @@ SYSCALL_DEFINE1(rootns, unsigned int, flags)
>  	return fd;
>  }

[ ... ]

> +int rootns_prepare_fork(struct kernel_clone_args *args)
> +{

[ ... ]

> +	for (;;) {
> +		bool wait = false;
> +
> +		scoped_guard(spinlock, &rootns->lock) {
> +			switch (rootns->state) {
> +			case ROOTNS_INIT:
> +				if (READ_ONCE(pid_ns->child_reaper))
> +					return -EBUSY;
> +				if (!(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING))
> +					return -ESRCH;

[Severity: Low]
Can the PIDNS_ADDING check under ROOTNS_INIT ever be reached?

PIDNS_ADDING is only cleared by disable_pid_allocation() in
zap_pid_ns_processes() when the PID namespace's init process exits, and
pid_ns->child_reaper is never reset to NULL afterwards.  Because
pid_ns->child_reaper is non-NULL whenever PIDNS_ADDING is cleared, testing
child_reaper first always returns -EBUSY here instead of -ESRCH.

> +				WRITE_ONCE(rootns->state, ROOTNS_FORKING);
> +				bootstrapping = true;
> +				break;

[ ... ]

> +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 (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)
> +			ret = -EBUSY;
> +	}

[Severity: Medium]
Does this rewrite argument validation failures in copy_process() from
-EINVAL to -EBUSY during bootstrap?

rootns_prepare_fork() runs before copy_process(), where companion flags
allowed by CLONE_INTO_ROOTNS_FLAGS (such as CLONE_PIDFD_AUTOKILL without
CLONE_PIDFD, or CLONE_AUTOREAP with a non-zero exit_signal) and
copy_thread() are validated:

kernel/fork.c:copy_process() {
    ...
	if (clone_flags & CLONE_AUTOREAP) {
		if (clone_flags & CLONE_THREAD)
			return ERR_PTR(-EINVAL);
		if (clone_flags & CLONE_PARENT)
			return ERR_PTR(-EINVAL);
		if (args->exit_signal)
			return ERR_PTR(-EINVAL);
	}
    ...
}

When the initial bootstrap clone3(CLONE_INTO_ROOTNS) call with
set_tid_size == 0 fails one of those checks with -EINVAL,
rootns_finish_fork() treats it as an alloc_pid() collision and returns
-EBUSY to userspace instead of -EINVAL.

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



More information about the Linux-security-module-archive mailing list