[PATCH] inotify: avoid transient zero mask when updating an existing watch
From: listout
Date: Fri Dec 04 2026 - 02:19:05 EST
From: Brahmajit Das <bdas@xxxxxxx>
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.
Reported-by: syzbot+21acc08140e9f736ec77@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=21acc08140e9f736ec77
Signed-off-by: Brahmajit Das <bdas@xxxxxxx>
---
fs/notify/fsnotify.c | 2 +-
fs/notify/inotify/inotify_user.c | 23 +++++++++++++++++------
2 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 8657bb94dfa8..449d8bca27ee 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -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);
}
diff --git a/fs/notify/inotify/inotify_user.c b/fs/notify/inotify/inotify_user.c
index 5f19c24ec187..aa0fde88013e 100644
--- a/fs/notify/inotify/inotify_user.c
+++ b/fs/notify/inotify/inotify_user.c
@@ -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);
--
2.56.1