[RFC PATCH v2 07/13] fork: Support CLONE_INTO_ROOTNS in clone3()

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 23:15:17 UTC 2026


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 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 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



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