mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Seiji Nishikawa <snishika@redhat.com>
To: Jan Kara <jack@suse.cz>
Cc: Amir Goldstein <amir73il@gmail.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Seiji Nishikawa <snishika@redhat.com>
Subject: [PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock
Date: Sun,  6 Dec 2026 17:15:57 +0900	[thread overview]
Message-ID: <20261206081558.1952451-2-snishika@redhat.com> (raw)
In-Reply-To: <20261206080650.1945355-1-snishika@redhat.com>

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@suse.cz>
Link: https://lore.kernel.org/all/pitzz3qt6efds6xqkowtaybzwh5tsd2arn3en263qqccs5wb3s@rrky5c2aggkq/
Signed-off-by: Seiji Nishikawa <snishika@redhat.com>
---
 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


  parent reply	other threads:[~2026-12-06  8:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  8:06 [PATCH v2 0/3] fsnotify: fix races on mark mask, flags and ignore mask Seiji Nishikawa
2026-12-06  8:15 ` [PATCH v2 1/3] inotify: do not expose a zero mask while replacing a watch Seiji Nishikawa
2026-12-06  8:15 ` Seiji Nishikawa [this message]
2026-12-06  8:15 ` [PATCH v2 3/3] fsnotify: annotate lockless accesses to mark masks and flags Seiji Nishikawa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206081558.1952451-2-snishika@redhat.com \
    --to=snishika@redhat.com \
    --cc=amir73il@gmail.com \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®