From 8907b0e96a1d7d3b090982abc6d9eb396eb467f0 Mon Sep 17 00:00:00 2001 From: Amir Goldstein Date: Thu, 11 Apr 2024 18:59:08 +0300 Subject: [PATCH v2] fsnotify: fix UAF from FS_ERROR event on a shutting down filesystem Protect against use after free when filesystem calls fsnotify_sb_error() during fs shutdown. fsnotify_sb_error() may be called without an s_active reference, so use RCU to synchronize access to fsnotify_sb_info() in this case. Reported-by: syzbot+5e3f9b2a67b45f16d4e6@syzkaller.appspotmail.com Suggested-by: Hillf Danton Link: https://lore.kernel.org/linux-fsdevel/20240413014033.1722-1-hdanton@sina.com/ Fixes: 07a3b8d0bf72 ("fsnotify: lazy attach fsnotify_sb_info state to sb") Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 17 ++++++++++++++--- include/linux/fsnotify_backend.h | 4 ++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index 2ae965ef37e8..310c8e845482 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -103,7 +103,8 @@ void fsnotify_sb_delete(struct super_block *sb) WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_CONTENT)); WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_PRE_CONTENT)); - kfree(sbinfo); + WRITE_ONCE(sb->s_fsnotify_info, NULL); + kfree_rcu(sbinfo, rcu); } /* @@ -506,6 +507,7 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, struct dentry *moved; int inode2_type; int ret = 0; + bool sb_active_ref = !(mask & FS_EVENTS_POSS_ON_SHUTDOWN); __u32 test_mask, marks_mask; if (path) @@ -535,8 +537,10 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, * However, if we do not walk the lists, we do not have to do * SRCU because we have no references to any objects and do not * need SRCU to keep them "alive". + * We need RCU read side to keep sbinfo "alive" for events possible + * during shutdown (e.g. FS_ERROR). */ - if ((!sbinfo || !sbinfo->sb_marks) && + if ((!sbinfo || (sb_active_ref && !sbinfo->sb_marks)) && (!mnt || !mnt->mnt_fsnotify_marks) && (!inode || !inode->i_fsnotify_marks) && (!inode2 || !inode2->i_fsnotify_marks)) @@ -562,11 +566,18 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, return 0; iter_info.srcu_idx = srcu_read_lock(&fsnotify_mark_srcu); - + /* + * For events possible during shutdown (e.g. FS_ERROR) we may not hold + * an s_active reference on sb, so we need to dereference sbinfo with + * rcu_read_lock() held. + */ + rcu_read_lock(); + sbinfo = fsnotify_sb_info(sb); if (sbinfo) { iter_info.marks[FSNOTIFY_ITER_TYPE_SB] = fsnotify_first_mark(&sbinfo->sb_marks); } + rcu_read_unlock(); if (mnt) { iter_info.marks[FSNOTIFY_ITER_TYPE_VFSMOUNT] = fsnotify_first_mark(&mnt->mnt_fsnotify_marks); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 7f1ab8264e41..f11047125522 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -97,6 +97,9 @@ */ #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD) +/* Events that could be generated while fs is shutting down */ +#define FS_EVENTS_POSS_ON_SHUTDOWN (FS_ERROR) + /* Events that can be reported to backends */ #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \ FS_EVENTS_POSS_ON_CHILD | \ @@ -488,6 +491,7 @@ struct fsnotify_mark_connector { */ struct fsnotify_sb_info { struct fsnotify_mark_connector __rcu *sb_marks; + struct rcu_head rcu; /* * Number of inode/mount/sb objects that are being watched in this sb. * Note that inodes objects are currently double-accounted. -- 2.34.1