* [PATCH] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()
@ 2024-07-15 12:36 Jan Kara
2024-07-15 12:56 ` Jan Kara
0 siblings, 1 reply; 2+ messages in thread
From: Jan Kara @ 2024-07-15 12:36 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Dmitry Vyukov, Jan Kara,
syzbot+701037856c25b143f1ad
When __fsnotify_recalc_mask() recomputes the mask on the watched object,
the compiler can "optimize" the code to perform partial updates to the
mask (including zeroing it at the beginning). Thus places checking
the object mask without conn->lock such as fsnotify_object_watched()
could see invalid states of the mask. Make sure the mask update is
performed by one memory store using WRITE_ONCE().
Reported-by: syzbot+701037856c25b143f1ad@syzkaller.appspotmail.com
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Link: https://lore.kernel.org/all/CACT4Y+Zk0ohwwwHSD63U2-PQ=UuamXczr1mKBD6xtj2dyYKBvA@mail.gmail.com
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/notify/mark.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
I plan to merge this fix through my tree.
diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index c3eefa70633c..74a8a8ed42ff 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -245,7 +245,11 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
!(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
want_iref = true;
}
- *fsnotify_conn_mask_p(conn) = new_mask;
+ /*
+ * We use WRITE_ONCE() to prevent silly compiler optimizations from
+ * confusing readers not holding conn->lock with partial updates.
+ */
+ WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask);
return fsnotify_update_iref(conn, want_iref);
}
--
2.35.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched()
2024-07-15 12:36 [PATCH] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched() Jan Kara
@ 2024-07-15 12:56 ` Jan Kara
0 siblings, 0 replies; 2+ messages in thread
From: Jan Kara @ 2024-07-15 12:56 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Dmitry Vyukov, Jan Kara,
syzbot+701037856c25b143f1ad
On Mon 15-07-24 14:36:10, Jan Kara wrote:
> When __fsnotify_recalc_mask() recomputes the mask on the watched object,
> the compiler can "optimize" the code to perform partial updates to the
> mask (including zeroing it at the beginning). Thus places checking
> the object mask without conn->lock such as fsnotify_object_watched()
> could see invalid states of the mask. Make sure the mask update is
> performed by one memory store using WRITE_ONCE().
>
> Reported-by: syzbot+701037856c25b143f1ad@syzkaller.appspotmail.com
> Reported-by: Dmitry Vyukov <dvyukov@google.com>
> Link: https://lore.kernel.org/all/CACT4Y+Zk0ohwwwHSD63U2-PQ=UuamXczr1mKBD6xtj2dyYKBvA@mail.gmail.com
> Signed-off-by: Jan Kara <jack@suse.cz>
Apparently my brain is still in vacation mode and although this will
probably be good enough to stop the data race from happening, it will not
be probably good enough for KCSAN as WRITE_ONCE() should better be paired
with READ_ONCE(). I'll send v2.
Honza
> ---
> fs/notify/mark.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> I plan to merge this fix through my tree.
>
> diff --git a/fs/notify/mark.c b/fs/notify/mark.c
> index c3eefa70633c..74a8a8ed42ff 100644
> --- a/fs/notify/mark.c
> +++ b/fs/notify/mark.c
> @@ -245,7 +245,11 @@ static void *__fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
> !(mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF))
> want_iref = true;
> }
> - *fsnotify_conn_mask_p(conn) = new_mask;
> + /*
> + * We use WRITE_ONCE() to prevent silly compiler optimizations from
> + * confusing readers not holding conn->lock with partial updates.
> + */
> + WRITE_ONCE(*fsnotify_conn_mask_p(conn), new_mask);
>
> return fsnotify_update_iref(conn, want_iref);
> }
> --
> 2.35.3
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-15 12:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-15 12:36 [PATCH] fsnotify: Avoid data race between fsnotify_recalc_mask() and fsnotify_object_watched() Jan Kara
2024-07-15 12:56 ` Jan Kara
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox