From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796545030; b=J4RVqxnB99JxHh1S9JoMoLBnbRkm+7qcRdkJ8H3htiOPTPPABq4NIVuVn3XNgsHBLYWM +ejEbe78BtJf+p3koIwdssD0/Qx07qksZxQHvHbpfhsj5ENPqIBGHy9nBefkfY3tbBO71 1Z2sfR+DbNr/NstLj+psV0cvSXrtc2mH3tZdjBhm5szoFTiJMFMrjL0buv7c6xnQstS7o etIzii0etQJZzmC+q6M6080ckDhDFx19ySCeUCw/j4QRHnPXfhynADG2BXrfR8baBjeq3 x6DGZOB6QL0yZuRskCVvpDiBga4FP7O3B9GjOvGTwe2m2pxbEDMNuAKMhtWOnfRU6tQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796545030; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=+xTIOVeJRR6eLGQ5Ezx5LSXQ4yA8h+Zh/hdgsZmT8bM=; b=K4bAHpGmn4kUjZdhBkn753xqiJAKRCU8FGub1K1c1ZX/0Q1URPDeWgz2mbyDHxzsTnfE DdYTHQ2VS4cXVxRAMVNKTq4tOWhrFnb1nlnr3qmVP4POSBqkaDTgHIPf0eU5F6Ve4xuik F+qijL+1HBPYnql75Swe6QtkhTQvUvs3zuAsVrb28/G1xBHUIsjYqjge8F8r2jBhu1Vxf zv14AKTyQiV/8/KVHWE1YKuZuAe7BNfWlZVUUnWOEr0vWAL76bJ4rKXQS+L4FoA5XeU7S lBFLWp9V6+7pamAbeX8snXhC8oOiFLRcFTDJtK2b6OKybdSXP/OE8SVSRJ6ZUTbWfSg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CYG3RGvp; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98A2C606A333 for ; Sun, 6 Dec 2026 08:17:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 98A2C606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CYG3RGvp DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796545029; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+xTIOVeJRR6eLGQ5Ezx5LSXQ4yA8h+Zh/hdgsZmT8bM=; b=CYG3RGvppS/p298HGMfrNAvs4vzp26FOcBg7Fh8aEkPTkpQhuc3i13m22JmC3QFX9R0/yi GNwQF1y42vrtjyWJrAYeHfzy4IxawUVL0xPXv+NMr4xQoqqkYVzOYAs/C0vn7zvjtrt3Sj sKOcf+NRglUvWmxlb3g9E2ZJjb3H1LQ= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-378-xaaAQ2a-NwySEnrbGcq4gg-1; Sun, 6 Dec 2026 08:17:04 +0000 X-MC-Unique: xaaAQ2a-NwySEnrbGcq4gg-1 X-Mimecast-MFC-AGG-ID: xaaAQ2a-NwySEnrbGcq4gg_1796545023 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 914FA18003FD; Sun, 6 Dec 2026 08:17:03 +0000 (UTC) Received: from snishika-thinkpadp16vgen1.tokyo.csb (headnet03.pony-001.prod.rdu2.dc.redhat.com [10.4.224.30]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0DA971800543; Sun, 6 Dec 2026 08:17:00 +0000 (UTC) From: Seiji Nishikawa To: Jan Kara Cc: Amir Goldstein , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Seiji Nishikawa Subject: [PATCH v2 2/3] fsnotify: clear the ignore mask under mark->lock Date: Sun, 6 Dec 2026 17:15:57 +0900 Message-ID: <20261206081558.1952451-2-snishika@redhat.com> In-Reply-To: <20261206080650.1945355-1-snishika@redhat.com> References: <20261206080650.1945355-1-snishika@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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 Link: https://lore.kernel.org/all/pitzz3qt6efds6xqkowtaybzwh5tsd2arn3en263qqccs5wb3s@rrky5c2aggkq/ Signed-off-by: Seiji Nishikawa --- 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