[RFC PATCH v3 07/13] fork: Support CLONE_INTO_ROOTNS in clone3()
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Thu Nov 19 19:36:54 UTC 2026
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] cred: missing dumpability reset across userns without CLONE_VM
in copy_creds()
- [Medium] fork: missing CAP_SYS_CHROOT check and umask reset to init_fs
in copy_fs()
- [Medium] nsproxy: shared mm and timens vvar mismatch in
copy_namespaces()
- [Low] rootns: wrong errno on external PID 1 race during bootstrap in
copy_process()
--
commit 9762bc2570e709528c82312b63771aa4f8d6c6d8
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/cred.c b/kernel/cred.c
> index d01df40a9a386..5bc3d455141b5 100644
> --- a/kernel/cred.c
> +++ b/kernel/cred.c
[ ... ]
> @@ -287,6 +294,28 @@ int copy_creds(struct task_struct *p, u64 clone_flags)
> if (!new)
> return -ENOMEM;
>
> + if (rootns) {
> + const struct cred *old = current_cred();
> +
> + if (!ns_capable(rootns->user_ns, CAP_SYS_ADMIN)) {
> + ret = -EPERM;
> + goto error_put;
> + }
> + if (new->user_ns != rootns->user_ns) {
> + ret = install_user_ns_cred(new, rootns->user_ns);
> + if (ret < 0)
> + goto error_put;
> + }
> + if (!cred_cap_issubset(old, new) ||
> + ((clone_flags & CLONE_VM) && old->user_ns != new->user_ns)) {
> + struct task_exec_state *exec_state;
> +
> + exec_state = rcu_dereference_protected(p->exec_state, true);
> + if (exec_state != &init_task_exec_state)
> + WRITE_ONCE(exec_state->dumpable, suid_dumpable);
> + }
> + }
[Severity: High]
Should exec_state->dumpable be reset whenever old->user_ns != new->user_ns,
even when CLONE_VM is not set?
When a dumpable daemon (TASK_DUMPABLE_OWNER) creates a rootns and spawns a
child into it without CLONE_VM, old->euid owns rootns->user_ns, so
cred_cap_issubset(old, new) returns true:
kernel/cred.c:cred_cap_issubset() {
...
for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
if ((set_ns == subset_ns->parent) &&
uid_eq(subset_ns->owner, set->euid))
return true;
}
...
}
Because CLONE_VM is clear, p->exec_state->dumpable remains
TASK_DUMPABLE_OWNER while p->real_cred->user_ns is switched to
rootns->user_ns. In __ptrace_may_access(), a process in rootns->user_ns
with CAP_SYS_PTRACE passes ptrace_has_cap(tcred->user_ns, mode), and
task_still_dumpable() returns true on TASK_DUMPABLE_OWNER without checking
ptrace_has_cap(exec_state->user_ns, mode). Until the child changes UID or
calls execve(), an unprivileged container root process can attach via
ptrace or read /proc/<pid>/mem and /proc/<pid>/fd/* while the child still
holds the parent daemon's forked memory, O_CLOEXEC descriptors, and host
UID.
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 08fc28ad1eedf..f5b98eea9ce92 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
[ ... ]
> @@ -1649,7 +1672,7 @@ static int copy_fs(u64 clone_flags, struct task_struct *tsk,
> if (current->nsproxy->mnt_ns != &init_mnt_ns)
> return -EINVAL;
> fs = userspace_init_fs;
> - } else if (flags & COPY_FS_IDLE) {
> + } else if (flags & COPY_FS_IDLE || rootns) {
> fs = &init_fs;
> } else {
> fs = current->fs;
> @@ -1671,6 +1694,20 @@ 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) {
> + struct path root __free(path_put) = {};
> + int err;
> +
> + err = mnt_ns_get_root(rootns->ns->mnt_ns, &root);
> + if (err) {
> + free_fs_struct(tsk->fs);
> + tsk->real_fs = NULL;
> + tsk->fs = NULL;
> + return err;
> + }
> + set_fs_root(tsk->fs, &root);
> + set_fs_pwd(tsk->fs, &root);
> + }
> return 0;
> }
[Severity: Medium]
Does copying init_fs when rootns is set discard current->fs->umask, and
should changing tsk->fs->root require CAP_SYS_CHROOT in rootns->user_ns?
Selecting init_fs causes copy_fs_struct() to initialize tsk->fs->umask from
init_fs.umask (0022) instead of inheriting the parent's umask. In
addition, unlike mntns_install() on setns(CLONE_NEWNS), neither
rootns_prepare_fork() nor copy_creds() checks ns_capable(rootns->user_ns,
CAP_SYS_CHROOT). A chrooted process already running in rootns->user_ns
that has dropped CAP_SYS_CHROOT while retaining CAP_SYS_ADMIN can call
clone3(CLONE_INTO_ROOTNS) to spawn a child with fs->root reset to the mount
namespace root and escape its chroot.
[ ... ]
> @@ -2510,6 +2548,12 @@ __latent_entropy struct task_struct *copy_process(
> goto bad_fork_core_free;
> }
>
> + if (unlikely(clone_args_rootns_bootstrapping(args) &&
> + !is_child_reaper(pid))) {
> + retval = -EBUSY;
> + goto bad_fork_core_free;
> + }
> +
> /* Let kill terminate clone/fork in the middle */
> if (fatal_signal_pending(current)) {
> retval = -EINTR;
> diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
> index 8a3c6d4e94e2a..ec8b6fac7fbf1 100644
> --- a/kernel/nsproxy.c
> +++ b/kernel/nsproxy.c
> @@ -166,12 +167,21 @@ 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) {
> + if (old_ns->time_ns != rootns->ns->time_ns)
> + timens_commit_fork(tsk, rootns->ns->time_ns,
> + flags & CLONE_VM);
> + get_nsproxy(rootns->ns);
> + tsk->nsproxy = rootns->ns;
> + return 0;
> + }
[Severity: Medium]
Can switching tsk->nsproxy->time_ns under CLONE_VM | CLONE_VFORK install the
child time namespace's vvar page into the parent's shared mm_struct?
When clone3() is called with CLONE_INTO_ROOTNS | CLONE_VM | CLONE_VFORK and
rootns has a different time namespace, timens_commit_fork() skips
vdso_join_timens() because shared_vm is true, while tsk->nsproxy is switched
immediately to rootns->ns (where time_ns == time_ns_for_children). The
parent and the vfork child now share current->mm while having different
nsproxy->time_ns pointers.
If VDSO_TIME_PAGE_OFFSET in the shared mm's [vvar] VMA has not been faulted
in yet and the child calls a VDSO time function before execve(),
vvar_fault() calls find_timens_vvar_page(), which sees vma->vm_mm ==
current->mm and maps rootns->ns->time_ns->vvar_page into the shared page
tables. Because time_ns already equals time_ns_for_children in rootns->ns,
exec_task_namespaces() on the child's later execve() is a no-op, leaving
the parent's mm permanently mapped to the child time namespace's vvar page.
Conversely, if the parent already faulted [vvar] before clone3(), the child
observes the parent's time namespace through VDSO and rootns->ns->time_ns
through syscalls until execve().
Also, calling timens_set_vvar_page() in copy_namespaces() before fallible
steps in copy_process() (such as alloc_pid() and cgroup_can_fork())
permanently sets rootns->ns->time_ns->frozen_offsets = true even when the
fork fails.
> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index 32bb8ef5ed584..cfc33c243dba4 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
[ ... ]
> +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))
> + 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);
> + } else if (ret == -ENOMEM &&
> + !(READ_ONCE(pid_ns->pid_allocated) & PIDNS_ADDING)) {
> + return -ESRCH;
> + }
> +
> + return ret;
> +}
[Severity: Low]
Can a bootstrap race with an external fork into pid_ns return -EINVAL or
-ENOMEM instead of the documented -EBUSY or -ESRCH?
If an external process enters pid_ns and allocates PID 1 in alloc_pid()
after rootns_prepare_fork() checks READ_ONCE(pid_ns->child_reaper) but
before setting child_reaper under tasklist_lock, the bootstrapping task's
alloc_pid() allocates nr == 2 while child_reaper is still NULL and fails
with -EINVAL, never reaching the !is_child_reaper(pid) check in
copy_process(). Similarly, if that external PID 1 exits and clears
PIDNS_ADDING before the bootstrapping task finishes copy_process(),
copy_process() returns -ENOMEM, and rootns_finish_fork() only translates
-ENOMEM to -ESRCH in the !args->rootns_bootstrapping branch.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118193539.240065-1-jarkko@kernel.org?part=7
More information about the Linux-security-module-archive
mailing list