[PATCH] fsnotify: use rcu_access_pointer() to test the mark connector pointers

From: Jiayi Zhang

Date: Wed Dec 02 2026 - 13:13:57 EST


fsnotify() skips objects without marks by testing sb_marks,
mnt_fsnotify_marks, i_fsnotify_marks and n_fsnotify_marks for NULL
before it takes SRCU, and fsnotify_mnt() and mnt_notify_add() do the
same for n_fsnotify_marks. These are __rcu pointers that
fsnotify_detach_connector_from_object() clears with
rcu_assign_pointer() when the last mark goes away, but the tests read
them with plain loads. KCSAN reports:

BUG: KCSAN: data-race in fsnotify / fsnotify_detach_connector_from_object

write (marked) to 0xffff88810024bbb8 of 8 bytes by task 4110 on cpu 1:
fsnotify_detach_connector_from_object+0x319/0x370 fs/notify/mark.c:423
fsnotify_put_mark+0x1d8/0x590 fs/notify/mark.c:476
__do_sys_inotify_rm_watch fs/notify/inotify/inotify_user.c:796 [inline]
...

read to 0xffff88810024bbb8 of 8 bytes by task 11515 on cpu 0:
fsnotify+0x339/0x18b0 fs/notify/fsnotify.c:542
__fsnotify_parent+0x2f8/0x3a0 fs/notify/fsnotify.c:241
fsnotify_parent include/linux/fsnotify.h:96 [inline]
...

The race has no functional effect: the tests only compare the pointers
with NULL to skip the SRCU section, and a stale value only means one
event is or is not delivered to a mark that is being added or removed
at that moment. The plain loads still let the compiler tear or reload
them, and KCSAN reports them.

Use rcu_access_pointer(), the accessor for testing an __rcu pointer
without dereferencing it, for these tests.

Fixes: 08991e83b728 ("fsnotify: Free fsnotify_mark_connector when there is no mark attached")
Reported-by: syzbot+f968afcac117a84b7f14@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=f968afcac117a84b7f14
Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
---
The syzbot report is in syzbot's moderation queue and was not sent to
the lists; the Closes: link shows it. kernel/auditsc.c has the same
plain test of i_fsnotify_marks in two places; that change is sent to
the audit maintainers separately.

syzbot has no reproducer. Tested in QEMU (2 CPUs, the syzbot config,
kernel 8d7fe9885c62) with a test program in which threads open() and
close() one file while other threads add and remove an inotify watch
on it, with KCSAN switched on at run time and a report filter for the
functions on both sides. Without the patch all 5 instances reported a
race under this title within about 90 seconds, 4 of them this pair
(fsnotify.c:542 against mark.c:423). The same title also covers the
i_fsnotify_mask stores in fsnotify_detach_connector_from_object(),
which "fsnotify: use WRITE_ONCE() when clearing the object mask on
detach" (sent the same day) fixes; with this patch alone the instances
stopped at that pair after 98 to 159 seconds. With both patches none
of 5 instances reported anything in 10 minutes each.

AI tools assisted with the analysis, the code and the changelog.

fs/mount.h | 4 ++--
fs/notify/fsnotify.c | 12 ++++++------
2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/fs/mount.h b/fs/mount.h
index 4d23cad..b4a019f 100644
--- a/fs/mount.h
+++ b/fs/mount.h
@@ -244,8 +244,8 @@ static inline void mnt_notify_add(struct mount *m)
if (!list_empty(&m->to_notify))
return;
/* Optimize the case where there are no watches */
- if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
- (m->prev_ns && m->prev_ns->n_fsnotify_marks))
+ if ((m->mnt_ns && rcu_access_pointer(m->mnt_ns->n_fsnotify_marks)) ||
+ (m->prev_ns && rcu_access_pointer(m->prev_ns->n_fsnotify_marks)))
list_add_tail(&m->to_notify, &notify_list);
else
m->prev_ns = m->mnt_ns;
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 8657bb9..a496220 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -537,11 +537,11 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
* SRCU because we have no references to any objects and do not
* need SRCU to keep them "alive".
*/
- if ((!sbinfo || !sbinfo->sb_marks) &&
- (!mnt || !mnt->mnt_fsnotify_marks) &&
- (!inode || !inode->i_fsnotify_marks) &&
- (!inode2 || !inode2->i_fsnotify_marks) &&
- (!mnt_data || !mnt_data->ns->n_fsnotify_marks))
+ if ((!sbinfo || !rcu_access_pointer(sbinfo->sb_marks)) &&
+ (!mnt || !rcu_access_pointer(mnt->mnt_fsnotify_marks)) &&
+ (!inode || !rcu_access_pointer(inode->i_fsnotify_marks)) &&
+ (!inode2 || !rcu_access_pointer(inode2->i_fsnotify_marks)) &&
+ (!mnt_data || !rcu_access_pointer(mnt_data->ns->n_fsnotify_marks)))
return 0;

if (sb)
@@ -710,7 +710,7 @@ void fsnotify_mnt(__u32 mask, struct mnt_namespace *ns, struct vfsmount *mnt)
* This is an optimization as well as making sure fsnotify_init() has
* been called.
*/
- if (!ns->n_fsnotify_marks)
+ if (!rcu_access_pointer(ns->n_fsnotify_marks))
return;

fsnotify(mask, &data, FSNOTIFY_EVENT_MNT, NULL, NULL, NULL, 0);
--
2.53.0