Root Cause Analysis
The KCSAN report identifies a data race on the 4-byte mask field (__u32) of struct fsnotify_mark (defined in include/linux/fsnotify_backend.h) between an inotify watch update and concurrent filesystem event delivery.
When inotify_add_watch() updates an existing watch on an inode, it calls:
__do_sys_inotify_add_watch() -> inotify_update_watch() -> inotify_update_existing_watch().
inotify_update_watch() holds group->mark_mutex (fsnotify_group_lock(group)), and inotify_update_existing_watch() acquires fsn_mark->lock before updating fsn_mark->mask and fsn_mark->flags:
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);
spin_unlock(&fsn_mark->lock);
Two issues exist here:
- When
replace is true (!(arg & IN_MASK_ADD)), fsn_mark->mask is updated in two separate stepsβfirst zeroed at line 558 (fsn_mark->mask = 0;), and then populated with the new mask at line 561 (fsn_mark->mask |= inotify_arg_to_mask(inode, arg);).
- Both writes to
fsn_mark->mask (as well as the writes to fsn_mark->flags) are plain memory accesses rather than WRITE_ONCE() of a single computed value.
During openat() (vfs_open_consume() -> fsnotify_open() -> fsnotify()), fsnotify() enters an SRCU read-side critical section (srcu_read_lock(&fsnotify_mark_srcu)), which protects the lifetime of struct fsnotify_mark and its connector, and iterates over attached marks without acquiring group->mark_mutex or mark->lock.
Inside the loop, fsnotify() calls send_to_group() (fs/notify/fsnotify.c:361), which reads mark->mask via a plain access:
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
group = mark->group;
marks_mask |= mark->mask;
marks_ignore_mask |=
fsnotify_effective_ignore_mask(mark, is_dir, type);
}
(The same lockless plain read pattern on mark->mask also occurs in fsnotify_iter_select_report_types() at fs/notify/fsnotify.c:442 and fsnotify_handle_inode_event() at fs/notify/fsnotify.c:272, and analogously for mark->flags at fs/notify/fsnotify.c:267 and fs/notify/inotify/inotify_fsnotify.c:132.)
3. Violated Invariant & Mechanism
Although fsnotify_mark::mask is documented as protected by [mark->lock, group->mark_mutex] for writers, the fsnotify() event-delivery path intentionally reads mark->mask locklessly under fsnotify_mark_srcu. Because inotify_update_existing_watch() performs non-WRITE_ONCE() storesβand explicitly stores 0 before storing the new mask when replacing a watchβand send_to_group() performs a plain read without READ_ONCE(), KCSAN flags the concurrent unsynchronized 4-byte access. In addition, a concurrent fsnotify() call can observe the transient fsn_mark->mask == 0 state between lines 558 and 561 and spuriously drop an event that both the old and new watch masks requested.
Computing the new mask (and flags) in a local variable and publishing it with a single WRITE_ONCE() in inotify_update_existing_watch(), paired with READ_ONCE() on the lockless SRCU read paths in fs/notify/fsnotify.c (and fs/notify/inotify/inotify_fsnotify.c), resolves both the data race and the transient zero-mask window.