Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796363625; b=2mbdpGeS3FkJ6ugZwnNGQ2vCKn76iZFTDIj+H/HzvtxrldrrYYrKhtbhY/XfRGFZtEv1 1h79ijlN4P4wLRfpc9etN+eJDu9eIpmlr8T0oxHAaqTSZhXQlGDIcqYO9WqtFidnVTPgv WfuPqXglMBv++ki/+pMupCQuPPLdObcHgRnkOH1SHfRCPKrjvBdYyut3007Jr4P/+8SOT es0ymAuCFm/Cl1In9AGXlEOahj2v0IzJ4KagseaiR8oiJX+vHaBcHfZ6V6snUAXrOeZSS mcg1m7nnXRrW5DX98/I2rjHpTZTAaozv66l0NclL7foDCVH9M8cDzks5vOWWqZJY+hg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796363625; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=Yfw14PcBJeoRrwyRq5OAbn0ne6iirK9mNMLCfJzZ7ZM=; b=rDQCQOdGMl+VUqm8Yc45jI/L+kfhs6SLQbni9uzDOrgxr5bcKbJXxPJWV3SI8sg369jI sQ8br0hEXNZ9uSvQ9HHEjKnpb39EETomGSY81rHSkIsYteLx98xvPqJI5RPexjhrDChme mHiZu8T/J5q4OhZ+pqFoWRGHtOoIY2xGFPCza+QERYc7zMPOUDme2RQukWhLnyacEbqkE 95rg5eaEUMZG9uI+XEWqsAkQBMLNOVmylWbp+YHyLfh73kqzOlgqaVDfkJi3mC3qWjEV1 YPJFe1YeJQ4kdMplug4fEOs1vHfywbhr9DC2Yp9TH6FVKpvgoFBH2qicy62d+brYnkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=NhjVtVKy; arc=none smtp.remote-ip=74.125.229.9 Received: from mail-dy2-f9.google.com (mail-dy2-f9.google.com [74.125.229.9]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0460606A322 for ; Fri, 4 Dec 2026 05:53:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F0460606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=NhjVtVKy Received: by mail-dy2-f9.google.com with SMTP id 5a478bee46e88-38754910f39so69588eec.1 for ; Thu, 03 Dec 2026 21:53:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796363624; x=1796968424; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Yfw14PcBJeoRrwyRq5OAbn0ne6iirK9mNMLCfJzZ7ZM=; b=NhjVtVKyvM9LpJtk9gRn39Ldy3Qn4XzI2gtavgpq1Ek5CtA3mvNQIJXyPeT9jlo8Ex J9lBRishLOlNW4TJuEtO8rAtOu6/w0SjK4uRiXKiY2T7LvD0DUwsjI745n4n6rG1QFex 7gLlUSX4XVNOLp+XrqsAkuP2nHdyl88ZoiWWCinVpNk8MQIo86n33dtAD89eMO18q/32 bxvMkUstkBdFe+13du3v76eiQgeT4Fhj17XIjuYCn4dJcv8LBuVZldlq3/hQuhmVOGrN 4csD9UmQ6COGmjv6a3h+gKhIueviMMGVW7SHXdVrEv6OEA9XCd6nlDywf1tEoIl1WBC/ Jy+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796363624; x=1796968424; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Yfw14PcBJeoRrwyRq5OAbn0ne6iirK9mNMLCfJzZ7ZM=; b=1RV9mazZKnSLNoYSVX3wuVdSiuvA3w75viokgml2iGnyT+kABparGmejcNaH9fXH2j JCQ10jIV7DGkxFLzGmPZs8ZmXTfltJNrwkWYQ8Y1XAYBEK7CNe+RHW/QUMoHNv/0ewWi 2Q9PnqLnDsOzILSesJ7Tcc1RaM80TFFCe87nmVOT3JoXJ8bAi4K8AVu7PeidQQlN7Q3h yccwyyl6QkOFmot6qs+T4B7qoH/bUjrjc8eDztdg+YK9SYbmjoehsrRJqlSzhzWsx/3R 1HyYR0zqsh0BhFPtqTuH+UeQBz9Fm7Lj/rJtg51+B7Ghk9asvau1rcBFUl3xK39zqaaH dEqg== X-Forwarded-Encrypted: i=1; AL+hErHWmzai8TBMeaYco8wVsKgc661C4fn59CVmvEwdJEY3FxjV5N7LFaFku/LRzs0Gq4YJG2RKtY1XObH16l5T@vger.kernel.org X-Gm-Message-State: APuU4Z9HI+TcAtQ3YrNP24bFHNNpgTXkyqZuWhRD8i6bCHy5v1t0QCQU I4RT/ytHdJOGFB0ivK8ZtCGtE3q5YYopbCNchgS7BKGC1eswyOukkLtujpj7CHKMfy8= X-Gm-Gg: AZFdTZfx9Q3RQlsE/9Ecc7FdQa4/9Zf1hurHLri0S9NL0/aY8QzoJeEPPeAsKwJJ9hP WekcvDJB5wGM0ZWBvJP9Ckp/ldw7H44x4k97FF1uwntpLLTRJtSXAYBeXQOt1VJYWa42OEzMOZW 5ma/4cKBBwGfFC9a3hPmthjf1gEl1beunYA4fnKKXENBT9Li6HykOyFRa7qT+4jAklDrjgQDMoH DN6662i+AyA1ggcT4qF4F4dGjTgMkKXWuWiWUsDDlc5rItdeiGz3HT94KcRUNNNwHMVEyilSEQg B5rnZl4KjQakM8pf4ezL5VERfnr4VPoFpebVVVJ55ucR9g4ej6HbxIyDPJuDkts/zpnuvApy1eW QREXMsJ3xndy45egWqHTpc9H815mptmBBATRdzrZY4ZqrSfmuGycnJY/zT6i9SEaVr6xVHVFcZ/ Yv/mbd2H97QqDLMFtcJ9sVUSXxnQT6cZsPl6gemhdpK1BsoJSW9lsxK19f40yNEczqAeN960HZL qGVzFEYSf/l5Al3ycbG3x1mAofHt3sjpjXw1cffw+rWkXGaz/N4niD02VZjmevZjoIduSV2T38X s6SGl96yNelUmHTsXA/Hm9XYvaJQT1p/god42GTh0AxM2R/9VuAt4jYR X-Received: by 2002:a05:7022:d45:b0:1b5:8967:b255 with SMTP id a92af1059eb24-1b5d711252fmr1263804c88.28.1796363623854; Thu, 03 Dec 2026 21:53:43 -0800 (PST) Received: from jiayi-vm-sprl-server17.local ([155.69.195.24]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d643dbd2sm4206206c88.6.2026.12.03.21.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 21:53:43 -0800 (PST) From: Jiayi Zhang To: Jan Kara , Amir Goldstein Cc: Christian Brauner , Alexander Viro , Miklos Szeredi , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+f968afcac117a84b7f14@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com Subject: [PATCH v2] fsnotify: use rcu_access_pointer() to test the mark connector pointers Date: Fri, 4 Dec 2026 05:53:35 +0000 Message-ID: <20261204055335.1900438-1-zhangjiayi1014@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-fsdevel@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 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 (found by syzbot): 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. struct fsnotify_mark_connector is an incomplete type in most users of fs/mount.h, and rcu_access_pointer() expands to typeof(*p), which GCC 8 rejects for an incomplete type. So keep mnt_notify_add() as a thin inline wrapper there and move the test of the marks into __mnt_notify_add() in fs/namespace.c. Fixes: 08991e83b728 ("fsnotify: Free fsnotify_mark_connector when there is no mark attached") Link: https://syzkaller.appspot.com/bug?extid=f968afcac117a84b7f14 Signed-off-by: Jiayi Zhang --- v2: - Move the test of the marks out of mnt_notify_add() in fs/mount.h into __mnt_notify_add() in fs/namespace.c, as Jan suggested, so fs/mount.h needs no fsnotify internals. v1 failed to build with GCC 8 in every file that includes fs/mount.h without the definition of the connector (reported by the kernel test robot: https://lore.kernel.org/oe-kbuild-all/20261203083353.XuNYnFsf-lkp@intel.com/). - Replace Reported-by:/Closes: with Link:. The syzbot title also covers the i_fsnotify_mask stores in fsnotify_detach_connector_from_object() (see below), which this patch does not change, so it should not close the syzbot bug. v1:https://lore.kernel.org/all/20261202181338.4162502-1-zhangjiayi1014@gmail.com/ Build test of v2: x86_64 defconfig with AUTOFS_FS, FANOTIFY and AUDITSYSCALL, GCC 8.5.0, make W=1 fs/ kernel/auditsc.o: no errors and no warnings in the changed files. With v1 the same build fails in 19 files (fs/autofs/*, fs/stat.c, fs/pidfs.c, ...) with the error the robot reported. The syzbot report is in syzbot's moderation queue and was not sent to the lists; the 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 and is unchanged (auditsc.c includes linux/fsnotify_backend.h). This conflicts with patch 5 of Amir's series "Partition the fanotify watchers by type" (v3), which moves the n_fsnotify_marks test out of fsnotify() into fsnotify_mnt(). If that series goes in first, I will rebase on top of it. syzbot has no reproducer. v1 was 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 this patch does not change; with this patch alone the instances stopped at that pair after 98 to 159 seconds. The runtime test was not repeated for v2: the change is in mnt_notify_add(), which the test program does not call. AI tools assisted with the analysis, the code and the changelog. fs/mount.h | 9 +++------ fs/namespace.c | 10 ++++++++++ fs/notify/fsnotify.c | 12 ++++++------ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/fs/mount.h b/fs/mount.h index 4d23cad..5259a4b 100644 --- a/fs/mount.h +++ b/fs/mount.h @@ -238,17 +238,14 @@ static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns) } #ifdef CONFIG_FSNOTIFY +void __mnt_notify_add(struct mount *m); + static inline void mnt_notify_add(struct mount *m) { /* queued already under this namespace_sem hold */ 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)) - list_add_tail(&m->to_notify, ¬ify_list); - else - m->prev_ns = m->mnt_ns; + __mnt_notify_add(m); } #else static inline void mnt_notify_add(struct mount *m) diff --git a/fs/namespace.c b/fs/namespace.c index aecd05a..f01d9d8 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -1716,6 +1716,16 @@ int may_umount(struct vfsmount *mnt) EXPORT_SYMBOL(may_umount); #ifdef CONFIG_FSNOTIFY +void __mnt_notify_add(struct mount *m) +{ + /* Optimize the case where there are no watches */ + 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, ¬ify_list); + else + m->prev_ns = m->mnt_ns; +} + static void mnt_notify(struct mount *p) { if (!p->prev_ns && p->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