[PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock

From: Seiji Nishikawa

Date: Sun Dec 06 2026 - 03:17:12 EST


When a file is modified, send_to_group() clears the ignore mask of each
mark that does not have FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY. It does
this without mark->lock. On the other hand, fanotify_mark_add_to_mask()
sets the ignore mask and then sets this flag, both under mark->lock.

A modify event can come between these two writes:

CPU 0 CPU 1
fanotify_mark_add_to_mask()
spin_lock(&mark->lock)
mark->ignore_mask |= mask
send_to_group() for FS_MODIFY
mark->flags does not have
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
mark->ignore_mask = 0
fanotify_mark_update_flags()
mark->flags |=
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
spin_unlock(&mark->lock)

After this, mark->flags has FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY but
mark->ignore_mask stays 0, so the events that the user asked to ignore
are delivered.

The clear of the ignore mask can also be lost when it races with an
update of the ignore mask in fanotify_mark_add_to_mask() or
fanotify_mark_remove_from_mask(). "mark->ignore_mask |= mask" reads the
ignore mask and then writes the new value, and send_to_group() can come
between the read and the write:

CPU 0 CPU 1
fanotify_mark_add_to_mask()
spin_lock(&mark->lock)
mark->ignore_mask |= mask
old = mark->ignore_mask
send_to_group() for FS_MODIFY
mark->flags does not have
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
mark->ignore_mask = 0
mark->ignore_mask = old | mask
spin_unlock(&mark->lock)

The old ignore mask that send_to_group() cleared is written back, so
the events that it ignored are still ignored after the file is modified.
"mark->ignore_mask &= ~mask" in fanotify_mark_remove_from_mask() has the
same problem.

This was reproduced on v7.4-rc5 with the syzbot reproducer of the bug
fixed by the previous patch, slightly changed with the assistance of an
LLM. One thread adds an ignore mask with FAN_MARK_IGNORED_SURV_MODIFY,
reads it back from fdinfo and removes the mark, in a loop. Another
thread writes to the same file. The program prints "lost: N of M", where
N is the number of loops where the ignore mask read back was 0, and M is
the number of all loops. Each run is 5 seconds, 3 runs:

lost: 2321 of 354297
lost: 1674 of 356127
lost: 1378 of 367778

So without this patch, in every run, the ignore mask that was just added
with FAN_MARK_IGNORED_SURV_MODIFY was already 0 in more than 1000 loops,
because a modify event cleared it.

With this patch:

lost: 0 of 339212
lost: 0 of 245721
lost: 0 of 363328

Clear the ignore mask under mark->lock, and check the flag again under
the lock. Take the lock only when the lockless checks show that there is
an ignore mask to clear, so the modify path does not take it for marks
without an ignore mask.

Fixes: e898386146de ("fsnotify: clear ignored mask on modify")
Reported-by: Jan Kara <jack@xxxxxxx>
Link: https://lore.kernel.org/all/pitzz3qt6efds6xqkowtaybzwh5tsd2arn3en263qqccs5wb3s@rrky5c2aggkq/
Signed-off-by: Seiji Nishikawa <snishika@xxxxxxxxxx>
---
fs/notify/fsnotify.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index cc323f9e4..b082b4e34 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -349,9 +349,23 @@ static int send_to_group(__u32 mask, const void *data, int data_type,
/* clear ignored on inode modification */
if (mask & FS_MODIFY) {
fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
- if (!(READ_ONCE(mark->flags) &
+ /* The flag is never cleared once set */
+ if (READ_ONCE(mark->flags) &
+ FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
+ continue;
+ /* Most marks have no ignore mask to clear */
+ if (!READ_ONCE(mark->ignore_mask))
+ continue;
+ /*
+ * The ignore mask and the mark flags are updated
+ * under mark->lock, so check the flag again and clear
+ * the ignore mask under that lock.
+ */
+ spin_lock(&mark->lock);
+ if (!(mark->flags &
FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
- mark->ignore_mask = 0;
+ WRITE_ONCE(mark->ignore_mask, 0);
+ spin_unlock(&mark->lock);
}
}

--
2.43.0