Skip to content

Commit 795bb82

Browse files
amir73iljankara
authored andcommitted
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. Move freeing of sb->s_fsnotify_info to destroy_super_work(), because it may be accessed from fs shutdown context. Reported-by: [email protected] Suggested-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/linux-fsdevel/20240416173211.4lnmgctyo4jn5fha@quack3/ Fixes: 07a3b8d ("fsnotify: lazy attach fsnotify_sb_info state to sb") Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]> Message-Id: <[email protected]>
1 parent a5e57b4 commit 795bb82

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

fs/notify/fsnotify.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ void fsnotify_sb_delete(struct super_block *sb)
103103
WARN_ON(fsnotify_sb_has_priority_watchers(sb, FSNOTIFY_PRIO_CONTENT));
104104
WARN_ON(fsnotify_sb_has_priority_watchers(sb,
105105
FSNOTIFY_PRIO_PRE_CONTENT));
106-
kfree(sbinfo);
106+
}
107+
108+
void fsnotify_sb_free(struct super_block *sb)
109+
{
110+
kfree(sb->s_fsnotify_info);
107111
}
108112

109113
/*

fs/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ static void destroy_super_work(struct work_struct *work)
274274
{
275275
struct super_block *s = container_of(work, struct super_block,
276276
destroy_work);
277+
fsnotify_sb_free(s);
277278
security_sb_free(s);
278279
put_user_ns(s->s_user_ns);
279280
kfree(s->s_subtype);

include/linux/fsnotify_backend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data
576576
extern void __fsnotify_inode_delete(struct inode *inode);
577577
extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
578578
extern void fsnotify_sb_delete(struct super_block *sb);
579+
extern void fsnotify_sb_free(struct super_block *sb);
579580
extern u32 fsnotify_get_cookie(void);
580581

581582
static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
@@ -880,6 +881,9 @@ static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
880881
static inline void fsnotify_sb_delete(struct super_block *sb)
881882
{}
882883

884+
static inline void fsnotify_sb_free(struct super_block *sb)
885+
{}
886+
883887
static inline void fsnotify_update_flags(struct dentry *dentry)
884888
{}
885889

0 commit comments

Comments
 (0)