[PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs

From: Shakeel Butt

Date: Sun Nov 29 2026 - 13:35:56 EST


A process forked while cgroup.kill is walking its parent's cgroup can
survive the kill:

kill fork
---- ----
kill_seq++
cgroup_can_fork() reads kill_seq
iterator returns the parent
and moves on
copy_process() sees no SIGKILL yet
and commits to the fork
send SIGKILL to the parent
cgroup_post_fork() links the child
behind the iterator; kill_seq is
unchanged, so the child lives

The fork read kill_seq after the kill started, so the check from commit
b69bb476dee9 ("cgroup: fix race between fork and cgroup.kill") misses it.
The window is the gap between the iterator step and send_sig(), and
preemption can stretch it.

Make kill_seq odd while __cgroup_kill() walks the tasks. Kill the child if
kill_seq was odd when the fork read it, or if it changed since. A walk that
ended before the read has already sent SIGKILL to the parent, so the fork
fails. A walk that starts after the child is linked finds the child.
CLONE_INTO_CGROUP forks hold cgroup_mutex across both reads, so they always
see an even, unchanged kill_seq.

Reproduced in a VM with a sleep between the iterator and send_sig(): the
child survived in 5 of 5 runs, and in 0 of 5 with this patch.

Fixes: b69bb476dee9 ("cgroup: fix race between fork and cgroup.kill")
Cc: stable@xxxxxxxxxxxxxxx # v5.14+
Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
include/linux/cgroup-defs.h | 5 +++--
kernel/cgroup/cgroup.c | 15 ++++++++++++++-
2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index cbfe8937647c..0dae54b033e9 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -528,8 +528,9 @@ struct cgroup {
int nr_threaded_children; /* # of live threaded child cgroups */

/*
- * Sequence number for cgroup.kill. Incremented with both cgroup_mutex
- * and css_set_lock held. Readers hold either one.
+ * Sequence number for cgroup.kill, odd while a kill is walking the
+ * cgroup's tasks. Incremented with both cgroup_mutex and css_set_lock
+ * held. Readers hold either one.
*/
unsigned int kill_seq;

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index f6df19241772..3d56f7417fb6 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4300,6 +4300,14 @@ static void __cgroup_kill(struct cgroup *cgrp)

lockdep_assert_held(&cgroup_mutex);

+ /*
+ * kill_seq is odd while this walk is in progress. The walk can signal
+ * a forking parent after copy_process() passed its
+ * fatal_signal_pending() check, and can pass the parent before the
+ * child is linked. cgroup_post_fork() therefore kills the child if
+ * kill_seq was odd when cgroup_css_set_fork() sampled it, or if it
+ * changed since.
+ */
spin_lock_irq(&css_set_lock);
cgrp->kill_seq++;
spin_unlock_irq(&css_set_lock);
@@ -4317,6 +4325,10 @@ static void __cgroup_kill(struct cgroup *cgrp)
send_sig(SIGKILL, task, 0);
}
css_task_iter_end(&it);
+
+ spin_lock_irq(&css_set_lock);
+ cgrp->kill_seq++;
+ spin_unlock_irq(&css_set_lock);
}

static void cgroup_kill(struct cgroup *cgrp)
@@ -7179,7 +7191,8 @@ void cgroup_post_fork(struct task_struct *child,
* child down right after we finished preparing it for
* userspace.
*/
- kill = kargs->kill_seq != cgrp_kill_seq;
+ kill = kargs->kill_seq != cgrp_kill_seq ||
+ (kargs->kill_seq & 1);
}

spin_unlock_irq(&css_set_lock);
--
2.53.1-Meta