The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Bradley Morgan <include@grrlz.net>
To: bsingharora@gmail.com, akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] taskstats: copy signal->stats under siglock in taskstats_exit
Date: Tue,  4 Aug 2026 09:34:00 +0000	[thread overview]
Message-ID: <20260804093400.3922-1-include@grrlz.net> (raw)

taskstats_exit() copies tsk->signal->stats into the exit reply
without taking any lock. Every other writer of this struct holds
sighand->siglock before touching it, and this copy does not.

The copy happens on the last thread of a thread group that exits.
group_dead being 1 only says that every thread has dropped
signal->live, it does not say how far the other threads got in
do_exit(). One of them can still be inside fill_tgid_exit() adding
its counters to the struct while the last thread copies it out, so
the copy can read the struct in the middle of an update.

The commit that added the copy assumed no locking was needed because
the group was dead:

	/* No locking needed for tsk->signal->stats since
	   group is dead */

but at that point the other threads have not necessarily finished
their exit path.

cpu0 (thread A, not last)        cpu1 (thread B, last)
===========================      ==============================
atomic_dec(&signal->live)        atomic_dec(&signal->live) -> 0
group_dead = 0                   group_dead = 1
...                              taskstats_exit(tsk, 1)
taskstats_exit(tsk, 0)           fill_tgid_exit(tsk) [siglock]
fill_tgid_exit(tsk)              memcpy(stats, signal->stats)
  spin_lock(siglock)               reads ac_utime (new)
  stats->ac_utime += x             reads ac_stime (old)
  stats->ac_stime += y             torn snapshot -> netlink
  spin_unlock(siglock)

The listeners receive a partially updated tgid snapshot, with some
fields from before the concurrent update and some from after. There
is no crash or splat, which is likely why this went unnoticed since
2006. A userspace model of the same shape, writer under a lock and
a lockless memcpy reader, produces millions of torn reads in a few
seconds.

Take siglock around the copy like every other access does. sighand
is still alive here because taskstats_exit() runs before
exit_notify(), and fill_tgid_exit() already takes this same lock
earlier in this function.

Fixes: ad4ecbcba728 ("[PATCH] delay accounting taskstats interface send tgid once")
Signed-off-by: Bradley Morgan <include@grrlz.net>
---
 kernel/taskstats.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/taskstats.c b/kernel/taskstats.c
index f31df72f0e9d..9a48827e22bc 100644
--- a/kernel/taskstats.c
+++ b/kernel/taskstats.c
@@ -590,6 +590,7 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
 	struct sk_buff *rep_skb;
 	size_t size;
 	int is_thread_group;
+	unsigned long flags;
 
 	if (!family_registered)
 		return;
@@ -635,7 +636,10 @@ void taskstats_exit(struct task_struct *tsk, int group_dead)
 	if (!stats)
 		goto err;
 
+	/* This was racy before, copy the stats under siglock. */
+	spin_lock_irqsave(&tsk->sighand->siglock, flags);
 	memcpy(stats, tsk->signal->stats, sizeof(*stats));
+	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
 	stats->version = TASKSTATS_VERSION;
 
 send:
-- 
2.47.3


                 reply	other threads:[~2026-08-04  9:34 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=20260804093400.3922-1-include@grrlz.net \
    --to=include@grrlz.net \
    --cc=akpm@linux-foundation.org \
    --cc=bsingharora@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /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