[RFC PATCH 07/13] rootns: Add rootns_enter() system call

sashiko-bot at kernel.org sashiko-bot at kernel.org
Wed Nov 18 05:09:49 UTC 2026


Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] cred: privilege escalation and NNP bypass in
  may_enter_rootns_creds()
- [High] cred: missing dumpability reset in copy_creds() on rootns entry
- [High] rootns: refcount underflow and UAF of rootns via setns() on
  pid_ns
- [High] rootns: sys_rootns_enter() calls kernel_clone() without arch
  fork wrappers
- [Medium] nsproxy: missing timens_commit() in copy_namespaces() on
  rootns entry
- [Medium] fork: stale rootns->root used in copy_fs() after pivot_root()
- [Medium] rootns: rootns_may_setns() rejects setns() to inherited
  rootns namespaces
--

commit 2b3b027946b3cddbb69540b1d520fd1496bf137c
Author: Jarkko Sakkinen <jarkko at kernel.org>

rootns: Add rootns_enter() system call

This commit introduces the rootns_enter() system call to fork a child
process directly into a configured rootns, along with credential checks
and namespace provenance tracking for setns().

> diff --git a/kernel/cred.c b/kernel/cred.c
> index d01df40a9a386..97d334db7758a 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 (task_no_new_privs(current) || 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 process bypass PR_SET_NO_NEW_PRIVS, regain dropped credentials, or
escape an LSM domain through may_enter_rootns_creds()?

When may_enter_rootns_creds() succeeds, copy_creds() replaces the child's
entire struct cred with dup_creds(rootns->cred).  The early return runs
before the task_no_new_privs(current) check and only compares euid, egid,
and cred_cap_issubset(old, target).  For a ROOTNS_USER namespace where
old->euid is target->user_ns->owner, cred_cap_issubset() returns true
even if old dropped capabilities or set PR_SET_NO_NEW_PRIVS, letting the
child gain full capabilities in target->user_ns.

Even in the same user namespace, the early return does not check real,
saved, or fs UIDs/GIDs, group_info, cap_effective, cap_bset, securebits,
or the LSM security blob.  A process that creates a rootns and later
drops real/saved UIDs, clears supplementary groups, drops bounding set
bits, or restricts itself with Landlock can call rootns_enter() to spawn
a child with the original unrestricted rootns->cred.

In the second branch, CAP_SETUID and CAP_SETGID are checked only against
target->user_ns->parent without checking old->user_ns or verifying that
target's UIDs, GIDs, and supplementary groups are mapped into
target->user_ns->parent.  Conversely, when old->user_ns == target->user_ns,
a caller holding CAP_SYS_ADMIN, CAP_SETUID, and CAP_SETGID in old->user_ns
is rejected if its euid or egid differs from target.

> @@ -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: High]
Does switching credentials to rootns->cred in copy_creds() leave
p->exec_state->dumpable set to TASK_DUMPABLE_OWNER?

In copy_process(), copy_exec_state() copies current->exec_state->dumpable
into p->exec_state before copy_creds() runs.  Unlike commit_creds(),
copy_creds() does not reset dumpable to suid_dumpable when UIDs, GIDs, or
capabilities change.  When a privileged daemon calls rootns_enter() to
fork into a rootns owned by an unprivileged user, processes inside the
target user namespace pass task_still_dumpable() and can ptrace the child
before execve(), reading the daemon's duplicated memory or stealing its
open file descriptors via pidfd_getfd().

> diff --git a/kernel/fork.c b/kernel/fork.c
> index 99e22b3cd213e..5e23d36b7ac3e 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
[ ... ]
> @@ -1671,6 +1683,10 @@ 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) {
> +		set_fs_root(tsk->fs, &rootns->root);
> +		set_fs_pwd(tsk->fs, &rootns->root);
> +	}
>  	return 0;
>  }

[Severity: Medium]
If a process inside a running rootns changes the root mount with
pivot_root() or overmounts "/", will subsequent rootns_enter() calls get
a stale root path here?

rootns->root holds the initial mount installed by MOVE_MOUNT_T_ROOTNS_ROOT
and is not updated by pivot_root().  Setting tsk->fs root and pwd to
&rootns->root gives newly entering tasks the old root mount instead of
the current root of rootns->ns->mnt_ns.

> diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
> index a76837b3ee332..0c6407757d896 100644
> --- a/kernel/nsproxy.c
> +++ b/kernel/nsproxy.c
> @@ -169,13 +169,21 @@ struct nsproxy *create_new_namespaces(u64 flags,
>  /*
>   * called from clone.  This now handles copy for nsproxy and all
>   * namespaces therein.
> + *
> + * Namespaces derived from a rootns member inherit the same rootns domain.
>   */
> -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 without calling timens_commit() leave the child
with the parent's VDSO vvar mappings when entering a rootns in a
different time namespace?

copy_mm() runs before copy_namespaces() and duplicates the caller's vvar
PTEs into tsk->mm.  When old_ns->time_ns differs from rootns->ns->time_ns,
skipping timens_commit() leaves the old vvar mappings in place until
execve(), so vDSO clock_gettime() in the child reads the wrong time
namespace offsets.

> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index f59b2fe9910de..e286c1e753b71 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
[ ... ]
> +static atomic64_t rootns_id = ATOMIC64_INIT(0);
> +
> +static void rootns_tag_namespaces(struct rootns *c, const struct nsproxy *old)
> +{
> +	struct nsproxy *ns = c->ns;
> +
> +	if (c->cred->user_ns != current_user_ns())
> +		c->cred->user_ns->ns.rootns_id = c->id;
> +	from_mnt_ns(ns->mnt_ns)->rootns_id = c->id;
> +	ns->pid_ns_for_children->ns.rootns_id = c->id;
> +	if (ns->uts_ns != old->uts_ns)
> +		ns->uts_ns->ns.rootns_id = c->id;
> +	if (ns->ipc_ns != old->ipc_ns)
> +		ns->ipc_ns->ns.rootns_id = c->id;
> +	if (ns->cgroup_ns != old->cgroup_ns)
> +		ns->cgroup_ns->ns.rootns_id = c->id;
> +	if (ns->net_ns != old->net_ns)
> +		ns->net_ns->ns.rootns_id = c->id;
> +}
> +
> +bool rootns_may_setns(struct rootns *rootns, struct ns_common *ns)
> +{
> +	if (!ns)
> +		return false;
> +	if (!rootns || is_current_namespace(ns))
> +		return true;
> +
> +	return ns->rootns_id == rootns->id;
> +}

[Severity: Medium]
Does rootns_may_setns() reject intra-rootns setns() back to a namespace
that the rootns inherited at creation time?

rootns_tag_namespaces() only sets c->id on newly created namespaces, so
any namespace inherited from the creator (such as time_ns, or UTS, IPC,
cgroup, net, or user namespaces omitted from rootns_create() flags) keeps
its previous rootns_id.  If a task inside the rootns unshares one of
those namespace types, is_current_namespace(ns) becomes false for the
rootns's initial namespace and ns->rootns_id != rootns->id, causing
setns() back to PID 1's namespace to fail with -EPERM.

> @@ -249,6 +308,7 @@ static struct rootns *create_rootns(unsigned int flags)
>  	c->ns = ns;
>  	ns->rootns = c;
>  	ns->pid_ns_for_children->rootns = c;
> +	rootns_tag_namespaces(c, current->nsproxy);
>  	nsproxy_ns_active_get(ns);
>  
>  	return c;

[Severity: High]
Can an external task joining ns->pid_ns_for_children via setns() trigger
a refcount underflow and use-after-free on struct rootns?

create_rootns() sets ns->pid_ns_for_children->rootns = c without taking a
reference on c, and marks ns->pid_ns_for_children active in the namespace
tree via nsproxy_ns_active_get(ns).  The reference on c that pairs with
exit_rootns()'s put_rootns(c) is only acquired in copy_rootns() when PID 1
is forked through sys_rootns_enter() in state ROOTNS_FORKING:

kernel/rootns.c:copy_rootns() {
	if (likely(!c || READ_ONCE(c->state) != ROOTNS_FORKING))
		return;

	scoped_guard(spinlock, &c->lock) {
		get_rootns(c);
...
}

Because rootns_may_setns() returns true when task_rootns(current) is NULL,
a process outside any rootns can open ns->pid_ns_for_children from nsfs,
call setns(fd, CLONE_NEWPID), and fork() PID 1 with args->rootns == NULL.
copy_rootns() then skips get_rootns(c), but when that child exits,
zap_pid_ns_processes() -> exit_rootns() still calls put_rootns(c),
dropping the rootns fd's reference and freeing c while the fd is still
open (or dereferencing a dangling pid_ns->rootns if the fd was already
closed, since put_rootns() does not clear pid_ns->rootns).

> @@ -455,3 +515,85 @@ SYSCALL_DEFINE2(rootns_kill, int, rootnsfd, int, sig)
[ ... ]
> +SYSCALL_DEFINE1(rootns_enter, int, rootnsfd)
> +{
[ ... ]
> +	args.rootns = rootns;
> +	ret = kernel_clone(&args);

[Severity: High]
Is calling kernel_clone() directly from a regular C syscall safe on
architectures that require fork/clone entry wrappers?

On alpha, m68k, parisc, mips, arc, nios2, and openrisc, fork and clone
syscalls enter through assembly wrappers that allocate a switch_stack or
save callee-saved registers before calling C code, which copy_thread()
then reads from the stack.  On sparc, the wrapper must flush register
windows via synchronize_user_stack(), and copy_thread() checks whether
UREG_G1 is __NR_clone3 to return 0 in the child instead of the parent's
PID.  Calling kernel_clone() from sys_rootns_enter() bypasses those paths
and corrupts the child's register state or return value on those
architectures.

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



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