Skip to contentSkip to conversation

fsnotify: use rcu_access_pointer() to test the mark connector pointers

PATCH New Jiayi Zhang 1 participant 1 message Dec 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fsnotify: use rcu_access_pointer() to test the mark connector pointers

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")

---
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/[email protected]/).
- 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/[email protected]/

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(-)
fs/mount.h +3 -6
index 4d23cad..5259a4b 100644
@@ -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, &notify_list);
-	else
-		m->prev_ns = m->mnt_ns;
+	__mnt_notify_add(m);
 }
 #else
 static inline void mnt_notify_add(struct mount *m)
fs/namespace.c +10 -0
index aecd05a..f01d9d8 100644
@@ -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, &notify_list);
+	else
+		m->prev_ns = m->mnt_ns;
+}
+
 static void mnt_notify(struct mount *p)
 {
 	if (!p->prev_ns && p->mnt_ns) {
fs/notify/fsnotify.c +6 -6
index 8657bb9..a496220 100644
@@ -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);
Signature · 1 line
2.53.0