All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>
Cc: linux-mm@kvack.org, linux-security-module@vger.kernel.org,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Ben Segall" <bsegall@google.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"David Hildenbrand" <david@kernel.org>,
	"David Howells" <dhowells@redhat.com>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Ingo Molnar" <mingo@redhat.com>, "Jan Kara" <jack@suse.cz>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Kees Cook" <kees@kernel.org>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Manfred Spraul" <manfred@colorfullife.com>,
	"Mel Gorman" <mgorman@suse.de>, "Michal Hocko" <mhocko@suse.com>,
	"Mike Rapoport" <rppt@kernel.org>, "Neal Gompa" <neal@gompa.dev>,
	"Paul Moore" <paul@paul-moore.com>,
	"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Vlastimil Babka" <vbabka@kernel.org>
Subject: Re: [RFC PATCH 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace
Date: Wed, 18 Nov 2026 05:09:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-220138@kernel.org> (raw)
In-Reply-To: <20261117050841.3289652-4-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] rootns: UAF and refcount imbalance in exit_rootns() via
  unreferenced pid_ns->rootns
- [High] rootns: unreferenced pid_ns->rootns pointer in exit_rootns()
  causes UAF
- [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 257575ec869d3f0e55e6c1a6f99ef3b0751d2a44
Author: Jarkko Sakkinen <jarkko@kernel.org>

rootns: Track init lifecycle via copy_process() and pid_namespace

This commit adds rootns lifecycle tracking hooks to copy_process(),
create_new_namespaces(), and zap_pid_ns_processes() to record the
bootstrapped init task's PID and capture its exit status and resource usage
when the PID namespace terminates.

> 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) publish
c->init_pid and ROOTNS_RUNNING before pid->tasks[PIDTYPE_PID] is populated?

CPU0                                    CPU1
copy_process()
  attach_pid(p, PIDTYPE_TGID)
  copy_rootns(p, c)
    spin_lock(&c->lock)
    c->init_pid = get_task_pid(p, ...)
    c->state = ROOTNS_RUNNING
    spin_unlock(&c->lock)
                                        __rootns_kill()
                                          spin_lock(&c->lock)
                                          pid = get_pid(c->init_pid)
                                          spin_unlock(&c->lock)
                                          kill_pid(pid, sig, priv)
                                            ...
                                              pid_task(pid, PIDTYPE_PID)
                                              --> NULL (-ESRCH)
  attach_pid(p, PIDTYPE_PID)

__rootns_kill() does not take tasklist_lock, so if a signal is sent during
this window (for example from rootns_release() when ROOTNS_KILL_ON_CLOSE is
set), kill_pid() fails with -ESRCH and drops the signal, leaving the
container init process running.

> 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 race with
exit_rootns() to overwrite ROOTNS_DEAD back to ROOTNS_RUNNING?

In commit "rootns: Add rootns_create() system call", create_rootns() sets
ns->pid_ns_for_children->rootns = c and marks the PID namespace active in
pid_ns_tree before rootns_enter() is called. A host task with
CAP_SYS_ADMIN can open that PID namespace via open_by_handle_at(), join it
via setns(CLONE_NEWPID), and fork() PID 1 without bootstrapping c. Because
copy_process() calls copy_rootns() for any thread_group_leader(p) rather
than checking is_child_reaper(pid), a later rootns_enter() spawns PID 2 and
commits it as c->init_pid.

Furthermore, copy_rootns() checks c->state == ROOTNS_FORKING outside
c->lock without re-checking under the lock:

CPU0                                    CPU1
copy_rootns(tsk, c)
  READ_ONCE(c->state) == ROOTNS_FORKING
                                        zap_pid_ns_processes(pid_ns)
                                          exit_rootns(pid_ns, me)
                                            spin_lock(&c->lock)
                                            c->state = ROOTNS_DEAD
                                            spin_unlock(&c->lock)
                                            put_rootns(c)
  spin_lock(&c->lock)
  get_rootns(c)
  c->init_pid = ...
  c->state = ROOTNS_RUNNING  <-- overwrites ROOTNS_DEAD
  spin_unlock(&c->lock)

Checking is_child_reaper(pid) and re-checking c->state == ROOTNS_FORKING
under c->lock would prevent both issues.

> +
> +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 an unheld reference on c when pid_ns was entered
without copy_rootns()?

In commit "rootns: Add rootns_create() system call", create_rootns() sets
ns->pid_ns_for_children->rootns = c without taking a reference on c; the
matching get_rootns(c) only happens in copy_rootns() when bootstrapping in
ROOTNS_FORKING state. If a privileged host process enters pid_ns via
open_by_handle_at() and setns(CLONE_NEWPID) and forks a child reaper,
copy_rootns() is called with c == NULL and never increments c->usage. When
that child reaper exits, zap_pid_ns_processes() -> exit_rootns() sees
pid_ns->rootns == c and calls put_rootns(c), dropping the open rootns file
descriptor's own reference and freeing c while the file descriptor is still
open.

[Severity: High]
Can exit_rootns() dereference a dangling pid_ns->rootns pointer after the
rootns file descriptor has been closed?

Because create_rootns() stores an unreferenced c in pid_ns->rootns and
put_rootns() frees c without clearing pid_ns->rootns, any reference that
outlives the rootns file descriptor before rootns_enter() runs leaves
pid_ns->rootns pointing to freed memory. If a process holds an nsfs file
descriptor to pid_ns, closes the rootns file descriptor (freeing c), and
then enters pid_ns via setns(CLONE_NEWPID), forks, and exits,
zap_pid_ns_processes() -> exit_rootns() dereferences the freed c and
double-frees it via put_rootns(c). Assigning pid_ns->rootns = get_rootns(c)
in copy_rootns() when the bootstrap init task is attached would avoid
storing an unreferenced pointer in create_rootns().

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


  reply	other threads:[~2026-11-18  5:09 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:08 [RFC PATCH 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-17  5:08 ` [RFC PATCH 01/13] nsproxy: Pass nsproxy to create_new_namespaces() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 02/13] rootns: Add core infrastructure for root namespaces Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 03/13] rootns: Track init lifecycle via copy_process() and pid_namespace Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot [this message]
2026-11-17  5:08 ` [RFC PATCH 04/13] rootns: Add rootns_create() system call Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 05/13] rootns: Add rootns_wait() " Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 06/13] rootns: Add rootns_kill() " Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 07/13] rootns: Add rootns_enter() " Jarkko Sakkinen
2026-11-17 17:42   ` Andy Lutomirski
2026-11-17 20:03     ` Jarkko Sakkinen
2026-11-17 20:53       ` Andy Lutomirski
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 08/13] fs: Support FSCONFIG_SET_ROOTNS in fsconfig() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 09/13] fs: Support installing rootns mount trees via move_mount() Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 10/13] arch: Wire up rootns system calls Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 11/13] docs: Document root namespace uAPI Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 12/13] selftests/rootns: Add root namespace test suite Jarkko Sakkinen
2026-11-18  5:09   ` sashiko-bot
2026-11-17  5:08 ` [RFC PATCH 13/13] MAINTAINERS: Add entry for ROOT NAMESPACE Jarkko Sakkinen
2026-11-17 11:10 ` [RFC PATCH 00/13] Namespace for namespace roots Jarkko Sakkinen
2026-11-17 17:44 ` Andy Lutomirski
2026-11-17 18:48   ` Jarkko Sakkinen
2026-11-17 19:10     ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-220138@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=corbet@lwn.net \
    --cc=dave@stgolabs.net \
    --cc=david@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=jack@suse.cz \
    --cc=jarkko@kernel.org \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=neal@gompa.dev \
    --cc=paul@paul-moore.com \
    --cc=peterz@infradead.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sergeh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.