Skip to contentSkip to conversation

exec: annotate lockless tsk->comm update with data_race()

PATCH New Xiange Pi 2 participants 2 messages Nov 24 – Nov 25

Subsystems (3)
lore
Share & tools
lore

Xiange Pi

author
#0 lore raw

PATCH 1/1 exec: annotate lockless tsk->comm update with data_race()

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.

---
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(-)
fs/exec.c +10 -2
index a5269b5e00df..4b03bb2fcb95 100644
@@ -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);
 }
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] exec: data_race() in __set_task_comm() still triggers KCSAN on readers --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] exec: data_race() in __set_task_comm() still triggers KCSAN on readers
--

commit a04284363b9bce90bc6367728fc880cc5b2fd84a
Author: Xiange Pi <[email protected]>

exec: annotate lockless tsk->comm update with data_race()

This commit wraps the memcpy() and memset() calls in __set_task_comm() with data_race() to suppress KCSAN warnings on lockless updates to tsk->comm.

Quoted 10 lines
fs/exec.c +0 -2
index 8d2b14e1d455c..9a47cb27ce5d0 100644
@@ -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);

fs/exec.c void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec) · line 1112

Quoted 10 lines
fs/exec.c +10 -0
+	/*
+	 * 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));

[Severity: Low]
Does annotating only the writer in __set_task_comm() actually suppress KCSAN reports for unannotated lockless readers of tsk->comm?

data_race() disables KCSAN only on the writer thread via __kcsan_disable_current(). Unannotated readers such as strlen() in tracepoints or get_task_comm() still set up read watchpoints on tsk->comm in kcsan_setup_watchpoint(). When a concurrent __set_task_comm() modifies a watched byte to a different value, kcsan_found_watchpoint() on the writer returns early without consuming the watchpoint, and the reader then reports an unknown-origin data race under the default CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=y:

kernel/kcsan/core.c:kcsan_found_watchpoint() {
	if (!kcsan_is_enabled(ctx))
		return;
	...
	/*
	 * If the other thread does not want to ignore the access, and there was
	 * a value change as a result of this thread's operation, we will still
	 * generate a report of unknown origin.
	 *
	 * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
	 */
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1