[RFC PATCH v2 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Wed Nov 18 23:15:08 UTC 2026
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] rootns: unpaired pid_ns->rootns and refcount in copy_rootns()
and exit_rootns()
- [Medium] rootns: copy_rootns() publishes init_pid before
attach_pid(PIDTYPE_PID)
- [Medium] rootns: missing is_child_reaper() and locked state checks in
copy_rootns()
--
commit ad1de852195b8caa7fd9ea3eac031f0716f9fc81
Author: Jarkko Sakkinen <jarkko at kernel.org>
rootns: Track init lifecycle via copy_process() and pid_namespace
This commit adds copy_rootns() and exit_rootns() hooks to copy_process()
and zap_pid_ns_processes() to record the root namespace's init PID and
capture its exit status and rusage when the PID namespace shuts down.
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 5efceb8d58a78..99e22b3cd213e 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
[ ... ]
> @@ -2565,6 +2566,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);
> __this_cpu_inc(process_counts);
> } else {
> current->signal->nr_threads++;
[Severity: Medium]
Does calling copy_rootns() before attach_pid(p, PIDTYPE_PID) allow
__rootns_kill() to miss the new init process?
__rootns_kill() synchronizes only on rootns->lock (not tasklist_lock) and
calls kill_pid(), which looks up pid_task(pid, PIDTYPE_PID):
CPU0 CPU1
copy_process()
write_lock_irq(&tasklist_lock)
attach_pid(p, PIDTYPE_TGID)
copy_rootns(p, c)
lock(&c->lock)
c->init_pid = get_task_pid(p, PIDTYPE_TGID)
c->state = ROOTNS_RUNNING
unlock(&c->lock)
rootns_release()
__rootns_kill(c, SIGKILL, true)
kill_pid(pid, SIGKILL, true)
pid_task(pid, PIDTYPE_PID)
--> NULL, returns -ESRCH
attach_pid(p, PIDTYPE_PID)
If a ROOTNS_KILL_ON_CLOSE file descriptor is closed during that window,
the SIGKILL is dropped and the container init process escapes termination.
> diff --git a/kernel/rootns.c b/kernel/rootns.c
> index e75d918b93ec5..e3b203322ae23 100644
> --- a/kernel/rootns.c
> +++ b/kernel/rootns.c
[ ... ]
> @@ -105,3 +106,42 @@ bool is_rootns_file(struct file *file)
> {
> return file->f_op == &rootns_fops;
> }
> +
> +void copy_rootns(struct task_struct *tsk, struct rootns *c)
> +{
> + 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);
> +}
[Severity: Medium]
Can copy_rootns() record a non-reaper task as c->init_pid or overwrite
ROOTNS_DEAD back to ROOTNS_RUNNING?
If a caller with may_see_all_namespaces() opens the root namespace's PID
namespace via open_by_handle_at(FD_NSFS_ROOT, ...), enters it with
setns(CLONE_NEWPID), and forks PID 1 before bootstrap, c->state stays
ROOTNS_READY. Because copy_process() calls copy_rootns() for every
thread_group_leader(p) rather than checking is_child_reaper(pid), a later
bootstrap clone spawns PID 2 and commits it as c->init_pid.
In addition, c->state == ROOTNS_FORKING is only checked before acquiring
c->lock. If that external child reaper exits while bootstrap is in
flight, exit_rootns() sets c->state to ROOTNS_DEAD under c->lock, and
copy_rootns() can then overwrite c->state back to ROOTNS_RUNNING.
> +
> +void exit_rootns(struct pid_namespace *pid_ns, struct task_struct *tsk)
> +{
> + struct rootns *c = pid_ns->rootns;
> + struct task_struct *leader;
> + struct rusage rusage;
> + int status;
> +
> + if (!c)
> + return;
> +
> + leader = tsk->group_leader;
> + status = (tsk->signal->flags & SIGNAL_GROUP_EXIT) ?
> + tsk->signal->group_exit_code : leader->exit_code;
> + getrusage(leader, RUSAGE_BOTH, &rusage);
> +
> + scoped_guard(spinlock, &c->lock) {
> + c->exit_code = status;
> + c->rusage = rusage;
> + WRITE_ONCE(c->state, ROOTNS_DEAD);
> + }
> +
> + pid_ns->rootns = NULL;
> + wake_up_poll(&c->waitq, EPOLLHUP);
> + put_rootns(c);
> +}
[Severity: High]
Can exit_rootns() drop a reference that copy_rootns() never took, or
dereference a freed struct rootns via pid_ns->rootns?
copy_rootns() acquires the reference dropped by exit_rootns(), but it
never sets pid_ns->rootns = c. Even when create_rootns() later sets
ns->pid_ns_for_children->rootns = c without taking a reference,
put_rootns() frees c without clearing pid_ns->rootns.
If pid_ns is opened via open_by_handle_at(FD_NSFS_ROOT, ...) and the
rootns file descriptor is closed before bootstrap, c is freed while
pid_ns->rootns still points to it. Forking and exiting a child reaper in
pid_ns via setns(CLONE_NEWPID) then runs zap_pid_ns_processes() ->
exit_rootns(), causing a use-after-free and double put_rootns(c) (or
dropping the open control file descriptor's own reference if it was not
closed yet).
Setting task_active_pid_ns(tsk)->rootns = get_rootns(c) in copy_rootns()
instead of at creation time would keep pid_ns->rootns paired with the
reference dropped in exit_rootns().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117231352.3816603-1-jarkko@kernel.org?part=3
More information about the Linux-security-module-archive
mailing list