f2fs: fix use-after-free of quota file names on failed remount
Subsystems (2)
PATCH v2 1/2 f2fs: fix use-after-free of quota file names on failed remount
From: Zhan Xusheng <[email protected]>
f2fs_show_options() prints the journalled quota file names without any lock that excludes remount, and a failed remount frees the current names before restoring the old ones. Reading /proc/mounts at that moment is a use-after-free.
Read the names under rcu_read_lock() and free them only after a grace period.
---
v2: Added Chao's Reviewed-by.
fs/f2fs/super.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
fs/f2fs/super.c +19 -11
@@ -2325,6 +2325,7 @@ static inline void f2fs_show_quota_options(struct seq_file *seq, {#ifdef CONFIG_QUOTA struct f2fs_sb_info *sbi =F2FS_SB (sb); +char *qf_name;if (F2FS_OPTION (sbi).s_jquota_fmt) {char *fmtname ="" ;
@@ -2343,17 +2344,20 @@ static inline void f2fs_show_quota_options(struct seq_file *seq, seq_printf(seq, ",jqfmt=%s", fmtname); } -if (F2FS_OPTION (sbi).s_qf_names[USRQUOTA ]) -seq_show_option (seq,"usrjquota" , -F2FS_OPTION (sbi).s_qf_names[USRQUOTA ]); +/* Remount frees replaced names only after a grace period */ +rcu_read_lock (); + qf_name =READ_ONCE (F2FS_OPTION (sbi).s_qf_names[USRQUOTA ]); +if (qf_name) +seq_show_option (seq,"usrjquota" , qf_name); -if (F2FS_OPTION (sbi).s_qf_names[GRPQUOTA ]) -seq_show_option (seq,"grpjquota" , -F2FS_OPTION (sbi).s_qf_names[GRPQUOTA ]); + qf_name =READ_ONCE (F2FS_OPTION (sbi).s_qf_names[GRPQUOTA ]); +if (qf_name) +seq_show_option (seq,"grpjquota" , qf_name); -if (F2FS_OPTION (sbi).s_qf_names[PRJQUOTA ]) -seq_show_option (seq,"prjjquota" , -F2FS_OPTION (sbi).s_qf_names[PRJQUOTA ]); + qf_name =READ_ONCE (F2FS_OPTION (sbi).s_qf_names[PRJQUOTA ]); +if (qf_name) +seq_show_option (seq,"prjjquota" , qf_name); +rcu_read_unlock ();#endif }
@@ -3151,8 +3155,12 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)#ifdef CONFIG_QUOTA F2FS_OPTION (sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;for (i =0 ; i <MAXQUOTAS ; i++) { -kfree (F2FS_OPTION (sbi).s_qf_names[i]); -F2FS_OPTION (sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i]; +char *qf_name =F2FS_OPTION (sbi).s_qf_names[i]; + +/* Pairs with READ_ONCE() in f2fs_show_quota_options() */ +smp_store_release (&F2FS_OPTION (sbi).s_qf_names[i], + org_mount_opt.s_qf_names[i]); +kfree_rcu_mightsleep (qf_name); }#endif sbi->mount_opt = org_mount_opt;
base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8
Signature · 1 line
2.43.0
PATCH v2 2/2 f2fs: free the quota file name that remount drops
From: Zhan Xusheng <[email protected]>
Remounting with an empty journalled quota option such as "usrjquota=" clears the name, but f2fs_apply_quota_options() overwrites the old string without freeing it, so every such remount leaks it.
Free it after a grace period, as f2fs_show_options() may still be reading it.
---
v2: Corrected the Fixes tag (Chao), added Chao's Reviewed-by.
fs/f2fs/super.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
fs/f2fs/super.c +5 -2
@@ -1646,7 +1646,7 @@ static void f2fs_apply_quota_options(struct fs_context *fc,struct f2fs_fs_context *ctx = fc->fs_private;struct f2fs_sb_info *sbi =F2FS_SB (sb);bool quota_feature =f2fs_sb_has_quota_ino (sbi); -char *qname; +char *qname, *old;int i;if (quota_feature)
@@ -1662,7 +1662,10 @@ static void f2fs_apply_quota_options(struct fs_context *fc, GFP_KERNEL | __GFP_NOFAIL);set_opt (sbi,QUOTA ); } -F2FS_OPTION (sbi).s_qf_names[i] = qname; + old =F2FS_OPTION (sbi).s_qf_names[i]; +/* Pairs with READ_ONCE() in f2fs_show_quota_options() */ +smp_store_release (&F2FS_OPTION (sbi).s_qf_names[i], qname); +kfree_rcu_mightsleep (old); }if (ctx->spec_mask & F2FS_SPEC_jqfmt)
Signature · 1 line
2.43.0
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 8 lines · On Mon, 2 Nov 2026 10:36:46 +0800 you wrote:
From: Zhan Xusheng <[email protected]>
f2fs_show_options()prints the journalled quota file names without any lock that excludes remount, and a failed remount frees the current names before restoring the old ones. Reading/proc/mountsat that moment is a use-after-free.[...]
Here is the summary with links:
- [f2fs-dev,v2,1/2] f2fs: fix use-after-free of quota file names on failed remount
https://git.kernel.org/jaegeuk/f2fs/c/441baab7f06a - [f2fs-dev,v2,2/2] f2fs: free the quota file name that remount drops https://git.kernel.org/jaegeuk/f2fs/c/f0d620265cbe
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html