Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Oleg Nesterov" <oleg@redhat.com>, "Chris Mason" <mason@kernel.org>
Cc: brauner@kernel.org, ast@kernel.org, daniel@iogearbox.net,
	andrii@kernel.org, bpf@vger.kernel.org,
	"Felix Kuehling" <Felix.Kuehling@amd.com>,
	amd-gfx@lists.freedesktop.org, akpm@linux-foundation.org,
	peterz@infradead.org, mingo@redhat.com, kees@kernel.org,
	viro@zeniv.linux.org.uk, jack@suse.cz, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com, bsingharora@gmail.com,
	eddyz87@gmail.com, memxor@gmail.com, yonghong.song@linux.dev,
	martin.lau@linux.dev, song@kernel.org, jolsa@kernel.org,
	emil@etsalapatis.com, ihor.solodrai@linux.dev,
	sched-ext@lists.linux.dev, linux-mm@kvack.org
Subject: Re: [PATCH] introduce signal_struct->group_leader
Date: Sun, 06 Dec 2026 16:23:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-252531@kernel.org> (raw)
In-Reply-To: <axWGOnzsTXMjcyRj@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] fork: use-after-free of signal_struct in
  css_task_iter_advance()
- [Medium] fork: __put_task_struct() tears down creds and LSM blob on
  live group_leader
- [Low] sched: missing __rcu annotation and lockdep check for
  signal->group_leader
--

commit 727c8222d6d617a9bd77b3792d903c2fbaf25924
Author: Oleg Nesterov <oleg@redhat.com>

introduce signal_struct->group_leader

This commit adds a group_leader pointer to struct signal_struct along with a
task_group_leader() helper so callers can access the thread group leader
under RCU even after it has exited.  It defers free_task() on
signal_struct->group_leader until put_signal_struct() drops the last
signal_struct reference and moves cgroup_task_free() into free_task().

> diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
> index 0adb51ab15428..ce697ce8612e0 100644
> --- a/include/linux/sched/signal.h
> +++ b/include/linux/sched/signal.h
> @@ -99,6 +99,9 @@ struct signal_struct {
>  	int			quick_threads;
>  	struct list_head	thread_head;
>  
> +	// rcu-safe but ->usage can be 0
> +	struct task_struct      *group_leader;
> +
>  	wait_queue_head_t	wait_chldexit;	/* for wait4() */
>  
>  	/* current thread group signal load-balancing target: */
> @@ -256,6 +259,12 @@ struct signal_struct {
>  						 */
>  } __randomize_layout;
>  
> +static inline struct task_struct *task_group_leader(const struct task_struct *p)
> +{
> +	// assert same_thread_group() || rcu || tasklist
> +	return rcu_dereference_raw(p->signal->group_leader);
> +}
> +
>  /*
>   * Bits in flags field of signal_struct.
>   */

[Severity: Low]
Should signal_struct->group_leader be annotated with __rcu?

de_thread(), copy_signal(), and __put_task_struct() access it via
rcu_assign_pointer(), RCU_INIT_POINTER(), and rcu_access_pointer(), which all
invoke rcu_check_sparse() and will trigger sparse address-space warnings
without __rcu.  Adding __rcu will also require RCU_INITIALIZER(&init_task) in
init_signals and rcu_access_pointer() or rcu_dereference_protected() instead
of bare READ_ONCE() in put_signal_struct().

Using rcu_dereference_raw() in task_group_leader() also bypasses sparse and
CONFIG_PROVE_RCU checks for the documented locking rule.  Moving
task_group_leader() after same_thread_group() with matching const parameters
would allow using rcu_dereference_check(p->signal->group_leader,
same_thread_group(p, current) || lockdep_tasklist_lock_is_held()).

[ ... ]

> diff --git a/kernel/fork.c b/kernel/fork.c
> index e9e52fa81c9d1..29a4f5f266e32 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -532,6 +532,10 @@ void put_task_stack(struct task_struct *tsk)
>  
>  void free_task(struct task_struct *tsk)
>  {
> +	// false if called by bad_fork_free:
> +	if (tsk->exit_state)
> +		cgroup_task_free(tsk);
> +
>  #ifdef CONFIG_SECCOMP
>  	WARN_ON_ONCE(tsk->seccomp.filter);
>  #endif
> @@ -759,26 +763,33 @@ static inline void free_signal_struct(struct signal_struct *sig)
>  
>  static inline void put_signal_struct(struct signal_struct *sig)
>  {
> -	if (refcount_dec_and_test(&sig->sigcnt))
> +	if (refcount_dec_and_test(&sig->sigcnt)) {
> +		// can't do this in free_signal_struct()
> +		free_task(READ_ONCE(sig->group_leader));
>  		free_signal_struct(sig);
> +	}
>  }
>  
>  void __put_task_struct(struct task_struct *tsk)
>  {
> +	bool free;
> +
>  	WARN_ON(!tsk->exit_state);
>  	WARN_ON(refcount_read(&tsk->usage));
>  	WARN_ON(tsk == current);
>  
>  	unwind_task_free(tsk);
>  	io_uring_free(tsk);
> -	cgroup_task_free(tsk);
>  	task_numa_free(tsk, true);
>  	security_task_free(tsk);
>  	exit_creds(tsk);
>  	delayacct_tsk_free(tsk);
> -	put_signal_struct(tsk->signal);
>  	sched_core_free(tsk);

[Severity: Medium]
Can __put_task_struct() tear down credentials and the LSM security blob while
signal_struct->group_leader is still reachable via task_group_leader()?

When the group leader's usage drops to zero while a sub-thread still keeps
signal_struct alive (sig->sigcnt > 1), free_task() is deferred, but
__put_task_struct() still runs exit_creds(), security_task_free(),
unwind_task_free(), and sched_core_free() on the leader.

exit_creds() clears real_cred and cred to NULL with plain stores, so standard
RCU helpers like get_task_cred() hit BUG_ON(!cred) and task_uid() or
task_cred_xxx() NULL-dereference.  security_task_free() also frees
task->security without an RCU grace period, and unwind_task_free() and
sched_core_free() leave dangling pointers in unwind_info.cache and
core_cookie.

Deferring the full __put_task_struct() teardown for
signal_struct->group_leader until put_signal_struct() drops sig->sigcnt to
zero would keep the leader intact for the entire lifetime of signal_struct.

> -	free_task(tsk);
> +	// Explain why this check can't be false positive/negative
> +	free = rcu_access_pointer(tsk->signal->group_leader) != tsk;
> +	put_signal_struct(tsk->signal);
> +	if (free)
> +		free_task(tsk);
>  }

[Severity: High]
Can put_signal_struct() free signal_struct before cgroup_task_free() unlinks
a demoted group leader from cset->dying_tasks?

When a group leader L exits while sub-thread T is still alive,
do_cgroup_task_dead() adds L to cset->dying_tasks.  If T then calls execve(),
de_thread() updates sig->group_leader to T and calls release_task(L) without
unlinking L from cset->dying_tasks.

If L remains pinned until after T exits and runs __put_task_struct(T), then
__put_task_struct(L) sees free == true and calls put_signal_struct(sig)
before free_task(L):

CPU0 (releasing former leader L)   CPU1 (reading cgroup.procs)
__put_task_struct(L)
  free = (sig->group_leader != L)
  put_signal_struct(sig)
    free_task(T)
    free_signal_struct(sig)
      kmem_cache_free(..., sig)
                                   css_task_iter_advance()
                                     /* visits L on cset->dying_tasks */
                                     atomic_read(&L->signal->live)
                                       <-- use-after-free
  if (free)
    free_task(L)
      cgroup_task_free(L)
        list_del_init(&L->cg_list)

Because css_task_iter_advance() checks atomic_read(&task->signal->live) on
dying_tasks entries before checking thread_group_leader(), a concurrent
cgroup.procs read triggers a slab use-after-free on signal_cachep.  Saving
sig = tsk->signal and running cgroup_task_free(tsk) (or free_task(tsk) when
free is true) before put_signal_struct(sig) would close this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/axWGOnzsTXMjcyRj@redhat.com?part=1


  reply	other threads:[~2026-12-06 16:23 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 15:35   ` Oleg Nesterov
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:36   ` sashiko-bot
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
2026-12-05 13:12 ` [PATCH v2 5/8] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-05 13:12 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:40   ` sashiko-bot
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 15:29   ` Oleg Nesterov
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 15:24   ` Oleg Nesterov
2026-12-06 11:00     ` Oleg Nesterov
2026-12-06 12:34       ` Chris Mason
2026-12-06 14:39         ` Oleg Nesterov
2026-12-06 15:09           ` Chris Mason
2026-12-05 15:59 ` [PATCH] change next_thread() to use list_next_entry_circular() Oleg Nesterov
2026-12-05 16:11   ` sashiko-bot
2026-12-05 16:19   ` Oleg Nesterov
2026-12-05 17:46   ` Chris Mason
2026-12-05 18:17     ` Oleg Nesterov
2026-12-06 15:58 ` [PATCH] introduce signal_struct->group_leader Oleg Nesterov
2026-12-06 16:23   ` sashiko-bot [this message]
2026-12-06 16:27   ` Oleg Nesterov

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-252531@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Felix.Kuehling@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=bsingharora@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=yonghong.song@linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox