Skip to contentSkip to conversation

fsnotify: fix UAF from FS_ERROR event on a shutting down filesystem

PATCH In review Amir Goldstein 3 participants 3 messages Apr 16, 2024 – Apr 17, 2024

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author reviewer
#0 lore raw

PATCH 1/1 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]
Fixes 07a3b8d0bf72 ("fsnotify: lazy attach fsnotify_sb_info state to sb")
---
 fs/notify/fsnotify.c             | 6 +++++-
 fs/super.c                       | 1 +
 include/linux/fsnotify_backend.h | 4 ++++
 3 files changed, 10 insertions(+), 1 deletion(-)
fs/notify/fsnotify.c +5 -1
index 2ae965ef37e8..ff69ae24c4e8 100644
@@ -103,7 +103,11 @@ 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);
+}
+
+void fsnotify_sb_free(struct super_block *sb)
+{
+	kfree(sb->s_fsnotify_info);
 }
 
 /*
fs/super.c +1 -0
index 69ce6c600968..b72f1d288e95 100644
@@ -274,6 +274,7 @@ static void destroy_super_work(struct work_struct *work)
 {
 	struct super_block *s = container_of(work, struct super_block,
 							destroy_work);
+	fsnotify_sb_free(s);
 	security_sb_free(s);
 	put_user_ns(s->s_user_ns);
 	kfree(s->s_subtype);
include/linux/fsnotify_backend.h +4 -0
index 7f1ab8264e41..4dd6143db271 100644
@@ -576,6 +576,7 @@ extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data
 extern void __fsnotify_inode_delete(struct inode *inode);
 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
 extern void fsnotify_sb_delete(struct super_block *sb);
+extern void fsnotify_sb_free(struct super_block *sb);
 extern u32 fsnotify_get_cookie(void);
 
 static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
@@ -880,6 +881,9 @@ static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
 static inline void fsnotify_sb_delete(struct super_block *sb)
 {}
 
+static inline void fsnotify_sb_free(struct super_block *sb)
+{}
+
 static inline void fsnotify_update_flags(struct dentry *dentry)
 {}
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 11 lines · Amir Goldstein

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]
Fixes 07a3b8d0bf72 ("fsnotify: lazy attach fsnotify_sb_info state to sb")

Thanks! Added to my tree.

Honza

Quoted 60 lines
---
 fs/notify/fsnotify.c             | 6 +++++-
 fs/super.c                       | 1 +
 include/linux/fsnotify_backend.h | 4 ++++
 3 files changed, 10 insertions(+), 1 deletion(-)
fs/notify/fsnotify.c +5 -1
index 2ae965ef37e8..ff69ae24c4e8 100644
@@ -103,7 +103,11 @@ 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);
+}
+
+void fsnotify_sb_free(struct super_block *sb)
+{
+	kfree(sb->s_fsnotify_info);
 }
 
 /*
fs/super.c +1 -0
index 69ce6c600968..b72f1d288e95 100644
@@ -274,6 +274,7 @@ static void destroy_super_work(struct work_struct *work)
 {
 	struct super_block *s = container_of(work, struct super_block,
 							destroy_work);
+	fsnotify_sb_free(s);
 	security_sb_free(s);
 	put_user_ns(s->s_user_ns);
 	kfree(s->s_subtype);
include/linux/fsnotify_backend.h +4 -0
index 7f1ab8264e41..4dd6143db271 100644
@@ -576,6 +576,7 @@ extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data
 extern void __fsnotify_inode_delete(struct inode *inode);
 extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
 extern void fsnotify_sb_delete(struct super_block *sb);
+extern void fsnotify_sb_free(struct super_block *sb);
 extern u32 fsnotify_get_cookie(void);
 
 static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
@@ -880,6 +881,9 @@ static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
 static inline void fsnotify_sb_delete(struct super_block *sb)
 {}
 
+static inline void fsnotify_sb_free(struct super_block *sb)
+{}
+
 static inline void fsnotify_update_flags(struct dentry *dentry)
 {}
 
Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Amir Goldstein
Quoted 6 lines · Amir Goldstein

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.

Quoted 6 lines
Reported-by [email protected]
Fixes 07a3b8d0bf72 ("fsnotify: lazy attach fsnotify_sb_info state to sb")

---