mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xiange Pi <xiange.pi@intel.com>
To: Kees Cook <kees@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
	linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] exec: annotate lockless tsk->comm update with data_race()
Date: Tue, 24 Nov 2026 16:38:48 +0800	[thread overview]
Message-ID: <20261124083848.1430527-1-xiange.pi@intel.com> (raw)

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


                 reply	other threads:[~2026-11-24  8:50 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=20261124083848.1430527-1-xiange.pi@intel.com \
    --to=xiange.pi@intel.com \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=viro@zeniv.linux.org.uk \
    /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®