mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach
@ 2026-12-02 17:57 Jiayi Zhang
  2026-12-03 10:14 ` Jan Kara
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-02 17:57 UTC (permalink / raw)
  To: Jan Kara, Amir Goldstein
  Cc: linux-fsdevel, linux-kernel, syzbot+f2d61c1b2357c03fb539, syzkaller-bugs

fsnotify_detach_connector_from_object() clears the fsnotify mask of the
inode, mount, superblock or mount namespace with a plain store. The
mask is read without locks: __fsnotify_parent() and fsnotify() read it
with READ_ONCE(), and __fsnotify_recalc_mask() writes it with
WRITE_ONCE() for that reason. KCSAN reports the plain store:

  BUG: KCSAN: data-race in __fsnotify_parent / fsnotify_detach_connector_from_object

  write to 0xffff888101690808 of 4 bytes by task 3372 on cpu 1:
   fsnotify_detach_connector_from_object+0x1e3/0x370 fs/notify/mark.c:409
   fsnotify_put_mark+0x1d8/0x590 fs/notify/mark.c:476
   __do_sys_inotify_rm_watch fs/notify/inotify/inotify_user.c:796 [inline]

  read (marked) to 0xffff888101690808 of 4 bytes by task 3017 on cpu 0:
   fsnotify_object_watched fs/notify/fsnotify.c:152 [inline]
   __fsnotify_parent+0xfd/0x3a0 fs/notify/fsnotify.c:202

The race has no functional effect: the store writes a constant zero to
an aligned u32, and a reader that still sees the old mask goes on to
look up the marks as before. Commit 35ceae44742e ("fsnotify: Avoid
data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
marked the readers and the store in __fsnotify_recalc_mask() but not
these four stores. Use WRITE_ONCE() for them too. KCSAN reports the plain store only with
CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n, as on syzbot.

Fixes: 35ceae44742e ("fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
Reported-by: syzbot+f2d61c1b2357c03fb539@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=f2d61c1b2357c03fb539
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
The syzbot report is in syzbot's moderation queue and was not sent to
the lists; the Closes: link shows it.

The syzbot reproducer only mounts an image; the race comes from
inotify users running on the syzbot machines. Tested in QEMU instead
with a small program that runs open() and inotify_add_watch()/
inotify_rm_watch() on one file in parallel threads, with KCSAN on and
unrelated races filtered: without the patch 1 of 5 instances reported
this race first; with the patch none of 10 did. Both runs also report
other races in fsnotify (for example fsnotify_free_mark() against
fsnotify_handle_inode_event(), and the plain read of the connector
pointer in fsnotify()), which this patch does not address.

AI tools assisted with the analysis, the code and the changelog.

 fs/notify/mark.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index cb07f42..d24941b 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -406,18 +406,18 @@ static void *fsnotify_detach_connector_from_object(
 
 	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
 		inode = fsnotify_conn_inode(conn);
-		inode->i_fsnotify_mask = 0;
+		WRITE_ONCE(inode->i_fsnotify_mask, 0);
 		fsnotify_untrack_connector(conn);
 
 		/* Unpin inode when detaching from connector */
 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
 			inode = NULL;
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
-		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_mount(conn)->mnt_fsnotify_mask, 0);
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
-		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_sb(conn)->s_fsnotify_mask, 0);
 	} else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
-		fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0;
+		WRITE_ONCE(fsnotify_conn_mntns(conn)->n_fsnotify_mask, 0);
 	}
 
 	rcu_assign_pointer(*connp, NULL);
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach
  2026-12-02 17:57 [PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach Jiayi Zhang
@ 2026-12-03 10:14 ` Jan Kara
  0 siblings, 0 replies; 2+ messages in thread
From: Jan Kara @ 2026-12-03 10:14 UTC (permalink / raw)
  To: Jiayi Zhang
  Cc: Jan Kara, Amir Goldstein, linux-fsdevel, linux-kernel,
	syzbot+f2d61c1b2357c03fb539, syzkaller-bugs

On Wed 02-12-26 17:57:45, Jiayi Zhang wrote:
> fsnotify_detach_connector_from_object() clears the fsnotify mask of the
> inode, mount, superblock or mount namespace with a plain store. The
> mask is read without locks: __fsnotify_parent() and fsnotify() read it
> with READ_ONCE(), and __fsnotify_recalc_mask() writes it with
> WRITE_ONCE() for that reason. KCSAN reports the plain store:
> 
>   BUG: KCSAN: data-race in __fsnotify_parent / fsnotify_detach_connector_from_object
> 
>   write to 0xffff888101690808 of 4 bytes by task 3372 on cpu 1:
>    fsnotify_detach_connector_from_object+0x1e3/0x370 fs/notify/mark.c:409
>    fsnotify_put_mark+0x1d8/0x590 fs/notify/mark.c:476
>    __do_sys_inotify_rm_watch fs/notify/inotify/inotify_user.c:796 [inline]
> 
>   read (marked) to 0xffff888101690808 of 4 bytes by task 3017 on cpu 0:
>    fsnotify_object_watched fs/notify/fsnotify.c:152 [inline]
>    __fsnotify_parent+0xfd/0x3a0 fs/notify/fsnotify.c:202
> 
> The race has no functional effect: the store writes a constant zero to
> an aligned u32, and a reader that still sees the old mask goes on to
> look up the marks as before. Commit 35ceae44742e ("fsnotify: Avoid
> data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
> marked the readers and the store in __fsnotify_recalc_mask() but not
> these four stores. Use WRITE_ONCE() for them too. KCSAN reports the plain store only with
> CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n, as on syzbot.
> 
> Fixes: 35ceae44742e ("fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()")
> Reported-by: syzbot+f2d61c1b2357c03fb539@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=f2d61c1b2357c03fb539
> Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>

There are many more place where mask modifications are problematic
(actually not only theoretically with KCSAN but even practically). This
needs more thought and planning than this band aid.

								Honza

> ---
> The syzbot report is in syzbot's moderation queue and was not sent to
> the lists; the Closes: link shows it.
> 
> The syzbot reproducer only mounts an image; the race comes from
> inotify users running on the syzbot machines. Tested in QEMU instead
> with a small program that runs open() and inotify_add_watch()/
> inotify_rm_watch() on one file in parallel threads, with KCSAN on and
> unrelated races filtered: without the patch 1 of 5 instances reported
> this race first; with the patch none of 10 did. Both runs also report
> other races in fsnotify (for example fsnotify_free_mark() against
> fsnotify_handle_inode_event(), and the plain read of the connector
> pointer in fsnotify()), which this patch does not address.
> 
> AI tools assisted with the analysis, the code and the changelog.
> 
>  fs/notify/mark.c |    8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/notify/mark.c b/fs/notify/mark.c
> index cb07f42..d24941b 100644
> --- a/fs/notify/mark.c
> +++ b/fs/notify/mark.c
> @@ -406,18 +406,18 @@ static void *fsnotify_detach_connector_from_object(
>  
>  	if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
>  		inode = fsnotify_conn_inode(conn);
> -		inode->i_fsnotify_mask = 0;
> +		WRITE_ONCE(inode->i_fsnotify_mask, 0);
>  		fsnotify_untrack_connector(conn);
>  
>  		/* Unpin inode when detaching from connector */
>  		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_IREF))
>  			inode = NULL;
>  	} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
> -		fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
> +		WRITE_ONCE(fsnotify_conn_mount(conn)->mnt_fsnotify_mask, 0);
>  	} else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
> -		fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
> +		WRITE_ONCE(fsnotify_conn_sb(conn)->s_fsnotify_mask, 0);
>  	} else if (conn->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
> -		fsnotify_conn_mntns(conn)->n_fsnotify_mask = 0;
> +		WRITE_ONCE(fsnotify_conn_mntns(conn)->n_fsnotify_mask, 0);
>  	}
>  
>  	rcu_assign_pointer(*connp, NULL);
> -- 
> 2.53.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 10:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 17:57 [PATCH] fsnotify: use WRITE_ONCE() when clearing the object mask on detach Jiayi Zhang
2026-12-03 10:14 ` Jan Kara

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®