mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] exec: annotate lockless tsk->comm update with data_race()
@ 2026-11-24  8:38 Xiange Pi
  0 siblings, 0 replies; only message in thread
From: Xiange Pi @ 2026-11-24  8:38 UTC (permalink / raw)
  To: Kees Cook, Alexander Viro, Christian Brauner
  Cc: Jan Kara, linux-mm, linux-fsdevel, linux-kernel

KCSAN reports a data race between prctl(PR_SET_NAME) and a lockless
reader of tsk->comm such as the sched_stat_runtime tracepoint:

 BUG: KCSAN: data-race in __set_task_comm / strlen

 read to 0xffff88800d1787d8 of 1 bytes by task 3929 on cpu 1:
  strlen+0x19/0x40
  trace_event_raw_event_sched_stat_runtime+0x5a/0x150
  update_se+0x14f/0x3b0
  ...
 write to 0xffff88800d1787d8 of 10 bytes by task 3417 on cpu 0:
  __set_task_comm+0x78/0x180
  __se_sys_prctl+0xcb3/0x13b0

The race is benign by design: since commit 3a3f61ce5e0b ("exec: Make
sure task->comm is always NUL-terminated") the last byte of comm is
never overwritten with a non-NUL value, so lockless readers see at worst
a mix of old and new name. Annotate the writes in __set_task_comm()
with data_race() to tell KCSAN that the lockless access is intentional.
As nearly all updates of comm go through this function, this covers the
lockless readers without having to annotate each of them.

Reported-by: syzbot+98baf51f5cc36dcbe2f7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=98baf51f5cc36dcbe2f7
Signed-off-by: Xiange Pi <xiange.pi@intel.com>
---
Test environment:
  - Baseline: torvalds/linux.git (git://git.kernel.org/pub/scm/linux/
    kernel/git/torvalds/linux.git) commit fc1c25014ff8, with and
    without this patch
  - Kernel config: the .config provided on the syzbot bug page
    (CONFIG_KCSAN=y)
  - Method: QEMU guest, running syz-execprog and syz-executor with the
    reproducer in the background, plus a script that enables the
    sched_stat_runtime tracepoint
  - Without the patch: "KCSAN: data-race in __set_task_comm / strlen"
    reproduced in every run
  - With the patch: not reproduced in 10 out of 10 runs

 fs/exec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index a5269b5e00df..4b03bb2fcb95 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1100,8 +1100,16 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 	size_t len = strnlen(buf, sizeof(tsk->comm) - 1);
 
 	trace_task_rename(tsk, buf);
-	memcpy(tsk->comm, buf, len);
-	memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len);
+	/*
+	 * tsk->comm is read locklessly by many users (e.g. tracepoints,
+	 * printk). comm[TASK_COMM_LEN - 1] is never written with a non-NUL
+	 * byte, so concurrent readers see a bounded, NUL-terminated string;
+	 * at worst a mix of the old and new name. Mark the update as
+	 * intentionally racy.
+	 */
+	data_race(memcpy(tsk->comm, buf, len));
+	/* Same as above: lockless readers tolerate a partially updated comm. */
+	data_race(memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len));
 	perf_event_comm(tsk, exec);
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-24  8:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24  8:38 [PATCH] exec: annotate lockless tsk->comm update with data_race() Xiange Pi

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®