KCSAN: data-race in do_writev / do_writev (4)

Status: moderation: reported on 2026/11/17 11:40
Subsystems: fs
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+b67e71ecceffb368f142@syzkaller.appspotmail.com
First crash: 3h07m, last: 3h07m
✨ AI cause bisection: candidate commits (most relevant first) (AI job):
  2be7d348fe92 Revert "vfs: properly and reliably lock f_pos in fdget_pos()"
  9c225f2655e3 vfs: atomic f_pos accesses as per POSIX
  438ab720c675 vfs: pass ppos=NULL to .read()/.write() of FMODE_STREAM files
  
✨ AI-generated root cause summary:

A data race occurs on file->f_pos during concurrent writev() operations on a shared open file description. In do_writev(), file_ppos() returns &file->f_pos for any file that is not marked FMODE_STREAM, causing do_writev() to perform plain, unmarked reads and writes to file->f_pos. However, fdget_pos() only acquires file->f_pos_lock when file_needs_f_pos_lock() returns true, which requires FMODE_ATOMIC_POS (only set on regular files and directories). For open file descriptions lacking both FMODE_ATOMIC_POS and FMODE_STREAMβ€”such as /dev/kmsg opened and shared across udevd processesβ€”concurrent calls to writev() access file->f_pos without synchronization, triggering a KCSAN data race report.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 666539eecdf4:

Root Cause Analysis

1. Raced Memory Location and Access Points

The reported KCSAN data race occurs on the 8-byte field struct file::f_pos (loff_t, defined at include/linux/fs.h:1280):

  • Read access: In do_writev() (fs/read_write.c:1081), the caller inspects the file position via pos = *ppos;, where ppos is obtained from file_ppos(fd_file(f)).
  • Write access: In do_writev() (fs/read_write.c:1086), after vfs_writev() returns, the updated position is written back using plain assignment: fd_file(f)->f_pos = pos;. (Analogous unmarked reads and writes exist in ksys_read(), ksys_write(), and do_readv().)

2. Why file->f_pos_lock Is Not Acquired

do_writev() uses CLASS(fd_pos, f)(fd), which calls fdget_pos(fd) (fs/file.c:1406–1416). fdget_pos() conditionally takes file->f_pos_lock:

struct fd fdget_pos(unsigned int fd)
{
	struct fd f = fdget(fd);
	struct file *file = fd_file(f);

	if (likely(file) && file_needs_f_pos_lock(file)) {
		f.word |= FDPUT_POS_UNLOCK;
		mutex_lock(&file->f_pos_lock);
	}
	return f;
}

file_needs_f_pos_lock(file) (fs/file.c:1377–1386) checks:

static inline bool file_needs_f_pos_lock(struct file *file)
{
	if (!(file->f_mode & FMODE_ATOMIC_POS))
		return false;
...

Per POSIX.1-2008 / SUSv4 Section XSI 2.9.7, do_dentry_open() (fs/open.c:968–969) sets FMODE_ATOMIC_POS only for regular files (S_ISREG(inode->i_mode)) and directories (S_ISDIR(inode->i_mode)). For other files (e.g., character devices, block devices), FMODE_ATOMIC_POS is not set. Consequently, file_needs_f_pos_lock() evaluates to false and f_pos_lock is omitted.

3. Why file_ppos() Still Exposes &file->f_pos

In fs/read_write.c:

static inline loff_t *file_ppos(struct file *file)
{
	return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
}

file_ppos() returns non-NULL (&file->f_pos) for any file where FMODE_STREAM is not set. Devices that implement custom seek or are not opened via stream_open() (for example, /dev/kmsg via kmsg_fops in kernel/printk/printk.c, which implements .llseek = devkmsg_llseek) do not have FMODE_STREAM set.

When two tasks share the same open file description lacking both FMODE_ATOMIC_POS and FMODE_STREAM (such as udevd and its worker threads/forked processes writing logs to an inherited /dev/kmsg file descriptor using writev()), neither process acquires file->f_pos_lock. Because both execute do_writev() concurrently, one task performs an unsynchronized read of file->f_pos at line 1081 while the other performs an unsynchronized write to file->f_pos at line 1086, triggering KCSAN.

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
f6fa75b8 assessment-kcsan Benign: βœ… ❓ KCSAN: data-race in do_writev / do_writev (4) 2026/11/17 11:39 2026/11/17 11:39 2026/11/17 12:26 a3e64cfc
89df59eb assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: βœ… UserNamespace: βœ… VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in do_writev / do_writev (4) 2026/11/17 11:31 2026/11/17 11:31 2026/11/17 11:39 a3e64cfc
a919d6dc bisect-cause ❓ KCSAN: data-race in do_writev / do_writev (4) 2026/11/17 11:31 2026/11/17 11:31 2026/11/17 12:52 a3e64cfc
▶ ▼ Similar bugs (3)
Kernel Title Rank πŸ›ˆ Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream KCSAN: data-race in do_writev / do_writev (2) fs 6 1 1573d 1573d 0/29 auto-closed as invalid on 2022/09/01 10:18
upstream KCSAN: data-race in do_writev / do_writev (3) fs 6 1 1053d 1053d 0/29 auto-obsoleted due to no activity on 2024/02/03 04:06
upstream KCSAN: data-race in do_writev / do_writev fs 6 1 1766d 1766d 0/29 auto-closed as invalid on 2022/02/20 05:25

Sample crash report:
==================================================================
BUG: KCSAN: data-race in do_writev / do_writev

write to 0xffff888101c6c128 of 8 bytes by task 3221 on cpu 0:
 do_writev+0x11c/0x210 fs/read_write.c:1086
 __do_sys_writev fs/read_write.c:1152 [inline]
 __se_sys_writev fs/read_write.c:1149 [inline]
 __x64_sys_writev+0x45/0x50 fs/read_write.c:1149
 x64_sys_call+0x23c9/0x2550 arch/x86/include/generated/asm/syscalls_64.h:21
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff888101c6c128 of 8 bytes by task 4959 on cpu 1:
 do_writev+0xc0/0x210 fs/read_write.c:1081
 __do_sys_writev fs/read_write.c:1152 [inline]
 __se_sys_writev fs/read_write.c:1149 [inline]
 __x64_sys_writev+0x45/0x50 fs/read_write.c:1149
 x64_sys_call+0x23c9/0x2550 arch/x86/include/generated/asm/syscalls_64.h:21
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 4959 Comm: udevd Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 10:14 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in do_writev / do_writev
* Struck through repros no longer work on HEAD.