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 --- 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