Skip to contentSkip to conversation

KCSAN: data-race in __se_sys_inotify_add_watch / fsnotify (2)

syzbot New syzbot 2 participants 2 messages Dec 4

Subsystems (3)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

Hello,

syzbot found the following issue on:

HEAD commit:    0810bd27d7bb Merge tag 'uml-fixes-7.4-rc6' of git://git.ke..
git tree:       upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=1491ed03580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=12de098a40e11adc
dashboard link: https://syzkaller.appspot.com/bug?extid=21acc08140e9f736ec77
compiler:       Debian clang version 22.1.8 (1~deb13u5), Debian LLD 22.1.8

Unfortunately, I don't have any reproducer for this issue yet.

Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/958a9a0914a2/disk-0810bd27.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/ec2ac86f1ee0/vmlinux-0810bd27.xz
kernel image: https://storage.googleapis.com/syzbot-assets/545506d0be3f/bzImage-0810bd27.xz

AI-generated root cause summary:
A KCSAN data race occurs on fsnotify_mark::mask (__u32) between inotify_update_existing_watch() in fs/notify/inotify/inotify_user.c and lockless SRCU event delivery in send_to_group() in fs/notify/fsnotify.c. inotify_update_existing_watch() updates fsn_mark->mask under fsn_mark->lock using two plain writes (fsn_mark->mask = 0; followed by fsn_mark->mask |= ...;), while send_to_group() runs under fsnotify_mark_srcu without holding fsn_mark->lock and reads mark->mask via a plain access. As a result, concurrent event delivery can race with the mask update and even observe the intermediate zeroed mask, potentially dropping an inotify event.

full analysis: https://syzkaller.appspot.com/x/ai_root_cause?x=21acc08140e9f736ec77

IMPORTANT: if you fix the issue, please add the following tag to the commit:

Reported-by [email protected]

==================================================================

BUG: KCSAN: data-race in __se_sys_inotify_add_watch / fsnotify

write to 0xffff888103bb00f0 of 4 bytes by task 5315 on cpu 0:

 inotify_update_existing_watch fs/notify/inotify/inotify_user.c:558 [inline]
 inotify_update_watch fs/notify/inotify/inotify_user.c:632 [inline]
 __do_sys_inotify_add_watch fs/notify/inotify/inotify_user.c:769 [inline]
 __se_sys_inotify_add_watch+0x2ea/0x720 fs/notify/inotify/inotify_user.c:717
 __x64_sys_inotify_add_watch+0x43/0x50 fs/notify/inotify/inotify_user.c:717
 x64_sys_call+0xa19/0x2550 arch/x86/include/generated/asm/syscalls_64.h:255
 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 0xffff888103bb00f0 of 4 bytes by task 5316 on cpu 1:

 send_to_group fs/notify/fsnotify.c:361 [inline]
 fsnotify+0xf70/0x18b0 fs/notify/fsnotify.c:597
 fsnotify_parent include/linux/fsnotify.h:99 [inline]
 fsnotify_path include/linux/fsnotify.h:113 [inline]
 fsnotify_file include/linux/fsnotify.h:127 [inline]
 fsnotify_open include/linux/fsnotify.h:450 [inline]
 vfs_open_consume+0x1ee/0x210 fs/open.c:1133
 do_open fs/namei.c:4851 [inline]
 path_openat+0xc16/0x11e0 fs/namei.c:5016
 do_file_open+0x16c/0x290 fs/namei.c:5045
 do_sys_openat2+0xa1/0x140 fs/open.c:1443
 do_sys_open fs/open.c:1449 [inline]
 __do_sys_openat fs/open.c:1465 [inline]
 __se_sys_openat fs/open.c:1460 [inline]
 __x64_sys_openat+0xf2/0x120 fs/open.c:1460
 x64_sys_call+0x1f72/0x2550 arch/x86/include/generated/asm/syscalls_64.h:258
 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: 5316 Comm: syz.0.348 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

---
This report is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

#1 lore raw

PATCH 1/1 inotify: avoid transient zero mask when updating an existing watch

From: Brahmajit Das <[email protected]>

inotify_update_existing_watch() updates fsn_mark->mask under fsn_mark->lock in two steps when replacing the mask:

	fsn_mark->mask = 0;
	fsn_mark->mask |= inotify_arg_to_mask(inode, arg);

send_to_group() reads mark->mask locklessly under fsnotify_mark_srcu, so a concurrent event can see the intermediate zero mask. The event is then dropped even if the watch reports it in both the old and new state. KCSAN reports this as a data race.

Build the new mask and flags in local variables and publish each with a single WRITE_ONCE(). Annotate the lockless read in send_to_group() with READ_ONCE().

There is no reproducer. The change was build-tested with KCSAN enabled but not runtime-tested.

This patch was developed with help from an AI coding assistant.

---
 fs/notify/fsnotify.c             |  2 +-
 fs/notify/inotify/inotify_user.c | 23 +++++++++++++++++------
 2 files changed, 18 insertions(+), 7 deletions(-)
fs/notify/fsnotify.c +1 -1
index 8657bb94dfa8..449d8bca27ee 100644
@@ -358,7 +358,7 @@ static int send_to_group(__u32 mask, const void *data, int data_type,
 	/* Are any of the group marks interested in this event? */
 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
 		group = mark->group;
-		marks_mask |= mark->mask;
+		marks_mask |= READ_ONCE(mark->mask);
 		marks_ignore_mask |=
 			fsnotify_effective_ignore_mask(mark, is_dir, type);
 	}
fs/notify/inotify/inotify_user.c +17 -6
index 5f19c24ec187..aa0fde88013e 100644
@@ -541,6 +541,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	struct inotify_inode_mark *i_mark;
 	int replace = !(arg & IN_MASK_ADD);
 	int create = (arg & IN_MASK_CREATE);
+	unsigned int flags;
+	__u32 mask;
 	int ret;
 
 	fsn_mark = fsnotify_find_inode_mark(inode, group);
@@ -554,12 +556,21 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
 
 	spin_lock(&fsn_mark->lock);
-	if (replace) {
-		fsn_mark->mask = 0;
-		fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
-	}
-	fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
-	fsn_mark->flags |= inotify_arg_to_flags(arg);
+	/*
+	 * Event delivery reads the mark mask locklessly under SRCU, so build
+	 * the new values locally and publish each with a single store.
+	 * Otherwise a concurrent reader could see a transient zero mask and
+	 * drop an event the watch was meant to report in both its old and
+	 * new state.
+	 */
+	mask = replace ? 0 : fsn_mark->mask;
+	flags = fsn_mark->flags;
+	if (replace)
+		flags &= ~INOTIFY_MARK_FLAGS;
+	mask |= inotify_arg_to_mask(inode, arg);
+	flags |= inotify_arg_to_flags(arg);
+	WRITE_ONCE(fsn_mark->mask, mask);
+	WRITE_ONCE(fsn_mark->flags, flags);
 	spin_unlock(&fsn_mark->lock);
 
 	fsnotify_recalc_mask(fsn_mark->connector);
Signature · 1 line
2.56.1