mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Henry Martin <bsdhenrymartin@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Zhang Qiao <zhangqiao22@huawei.com>, Tejun Heo <tj@kernel.org>,
	linux-kernel@vger.kernel.org,
	Henry Martin <bsdhenrymartin@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] sched/autogroup: Fix task_group use-after-free when fork races autogroup_move_group()
Date: Mon, 23 Nov 2026 16:13:52 +0800	[thread overview]
Message-ID: <20261123081352.3570164-1-bsdhenrymartin@gmail.com> (raw)

sched_cgroup_fork() reads signal->autogroup->tg without ->siglock, before
the new thread is linked into signal->thread_head.  A concurrent
autogroup_move_group() misses it in for_each_thread(), drops the last
kref of the old autogroup and frees its task_group, which the new thread
then keeps enqueueing into:

BUG: KASAN: slab-use-after-free in enqueue_task_fair+0x1075/0x33b0
Write of size 8 at addr ffff888101007940 by task poc/20125
CPU: 2 UID: 1000 PID: 20125 Comm: poc
Call Trace:
 kasan_check_range+0x100/0x1b0
 enqueue_task_fair+0x1075/0x33b0
 enqueue_task+0x70/0x170
 ttwu_do_activate+0xe9/0x590
 try_to_wake_up+0x69e/0x1110
 hrtimer_wakeup+0x39/0x50
Allocated by task 20119:
 sched_create_group+0x1a/0x90
 sched_autogroup_create_attach+0x5e/0x310
 ksys_setsid+0x2f6/0x410
Freed by task 0:
 kmem_cache_free+0x1dc/0x3c0
 rcu_core+0x570/0x1850
Second to last potentially related work creation:
 autogroup_move_group+0x2ac/0x3d0
 sched_autogroup_create_attach+0x167/0x310

Re-sync the new thread right after it is linked into thread_head, while
->siglock is still held: a concurrent move_group() then either already
installed the new autogroup, which the re-sync picks up, or is still
blocked on ->siglock and will see this thread in its for_each_thread()
sweep.  The re-sync compares pointers only; p->sched_task_group may
already be freed and must not be dereferenced.

This issue was discovered by Tencent CodeBuddy Security.

Cc: stable@vger.kernel.org
Fixes: 4ef0c5c6b5ba ("kernel/sched: Fix sched_fork() access an invalid sched_task_group")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 include/linux/sched/autogroup.h |  4 ++++
 kernel/fork.c                   |  1 +
 kernel/sched/core.c             | 29 +++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+)

diff --git a/include/linux/sched/autogroup.h b/include/linux/sched/autogroup.h
index 704391cc1d20..edb1d5c022b2 100644
--- a/include/linux/sched/autogroup.h
+++ b/include/linux/sched/autogroup.h
@@ -6,11 +6,13 @@ struct signal_struct;
 struct task_struct;
 struct task_group;
 struct seq_file;
+struct kernel_clone_args;
 
 #ifdef CONFIG_SCHED_AUTOGROUP
 extern void sched_autogroup_create_attach(struct task_struct *p);
 extern void sched_autogroup_detach(struct task_struct *p);
 extern void sched_autogroup_fork(struct signal_struct *sig);
+extern void sched_cgroup_fork_sync(struct task_struct *p, struct kernel_clone_args *kargs);
 extern void sched_autogroup_exit(struct signal_struct *sig);
 extern void sched_autogroup_exit_task(struct task_struct *p);
 #ifdef CONFIG_PROC_FS
@@ -21,6 +23,8 @@ extern int proc_sched_autogroup_set_nice(struct task_struct *p, int nice);
 static inline void sched_autogroup_create_attach(struct task_struct *p) { }
 static inline void sched_autogroup_detach(struct task_struct *p) { }
 static inline void sched_autogroup_fork(struct signal_struct *sig) { }
+static inline void sched_cgroup_fork_sync(struct task_struct *p,
+					  struct kernel_clone_args *kargs) { }
 static inline void sched_autogroup_exit(struct signal_struct *sig) { }
 static inline void sched_autogroup_exit_task(struct task_struct *p) { }
 #endif
diff --git a/kernel/fork.c b/kernel/fork.c
index 5efceb8d58a7..da6a59a260a0 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2574,6 +2574,7 @@ __latent_entropy struct task_struct *copy_process(
 			task_join_group_stop(p);
 			list_add_tail_rcu(&p->thread_node,
 					  &p->signal->thread_head);
+			sched_cgroup_fork_sync(p, args);
 		}
 		attach_pid(p, PIDTYPE_PID);
 		nr_threads++;
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5ed660f099a5..3d911ae0f147 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4975,6 +4975,35 @@ int sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs)
 	return scx_fork(p, kargs);
 }
 
+#ifdef CONFIG_SCHED_AUTOGROUP
+/*
+ * sched_cgroup_fork() pinned @p to the autogroup that signal->autogroup
+ * pointed at back then, without ->siglock.  A concurrent
+ * autogroup_move_group() that ran before @p became visible in thread_head
+ * could not see it in for_each_thread() and destroyed that autogroup,
+ * leaving @p->sched_task_group dangling.
+ *
+ * Now that @p is visible, such a move_group() either already ran (and
+ * missed @p) or is serialized behind the ->siglock its sweep needs.  In the
+ * former case @p is still tied to the destroyed task_group and must be
+ * re-synced.  @p->sched_task_group is only compared here, never
+ * dereferenced: it may already be freed.
+ */
+void sched_cgroup_fork_sync(struct task_struct *p,
+			    struct kernel_clone_args *kargs)
+{
+	struct task_group *tg = p->sched_task_group;
+
+	/* Only the autogroup path can leave @p on a destroyed task_group. */
+	if (kargs->cset->subsys[cpu_cgrp_id] != &root_task_group.css)
+		return;
+	if (tg == &root_task_group || tg == p->signal->autogroup->tg)
+		return;
+
+	sched_move_task(p, true);
+}
+#endif /* CONFIG_SCHED_AUTOGROUP */
+
 void sched_cancel_fork(struct task_struct *p)
 {
 	scx_cancel_fork(p);
-- 
2.53.0


                 reply	other threads:[~2026-11-23  8:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261123081352.3570164-1-bsdhenrymartin@gmail.com \
    --to=bsdhenrymartin@gmail.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=stable@vger.kernel.org \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=zhangqiao22@huawei.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®