mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] f2fs: flush s_error_work on early mount failures
@ 2026-11-30 17:06 Kirill Vanzha
  0 siblings, 0 replies; only message in thread
From: Kirill Vanzha @ 2026-11-30 17:06 UTC (permalink / raw)
  To: jaegeuk, chao
  Cc: frank.li, linux-f2fs-devel, linux-kernel, Kirill Vanzha, stable

f2fs_fill_super() initializes s_error_work early, but the only
flush_work() on the mount failure paths sits at the stop_ckpt_thread
label.  Failure exits that jump to labels located after it in source
order skip the flush while still freeing raw_super and sbi, a pending
f2fs_record_error_work() can run afterwards and touch freed memory.

A crafted image with a single-segment checkpoint area and zero-length
SIT/NAT/SSA areas passes sanity_check_area_boundary(), which makes
main_blkaddr == cp_blkaddr + 512.  f2fs_get_valid_checkpoint() then
reads checkpoint pack 2 at cp_blkaddr + 512,
f2fs_is_valid_blkaddr(META_GENERIC) rejects the address and calls
f2fs_handle_error(ERROR_INVALID_BLKADDR), which schedules
s_error_work.  Both checkpoint packs are unusable, the mount fails
with -EFSCORRUPTED, and the cleanup frees raw_super/sbi with the work
still queued.

Flush s_error_work at free_sb_buf before raw_super and sbi are freed.
Also move INIT_WORK() above read_raw_super_block(), the
sb_set_blocksize() failure path jumps to free_sb_buf before the
current INIT_WORK() site flushing a never-initialized work.  On these
early failure paths no background threads are running and the error
worker does not requeue itself, so a plain flush_work() is sufficient
and preserves pending error recording.

Found by Linux Verification Center (linuxtesting.org) with Syzkaller.

Fixes: ca8b201f2854 ("f2fs: fix to avoid potential deadlock")
Cc: stable@vger.kernel.org
Signed-off-by: Kirill Vanzha <vanzha.kirill@gmail.com>
---
The patch is based on the bugfix/common branch of the f2fs development
tree:
https://git.kernel.org/pub/scm/linux/kernel/git/chao/linux.git/log/?h=bugfix/common
 fs/f2fs/super.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 0cbcc1bc9c42..e300e5445641 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -5245,6 +5245,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	init_f2fs_rwsem(&sbi->quota_sem);
 	init_waitqueue_head(&sbi->cp_wait);
 	spin_lock_init(&sbi->error_lock);
+	INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
 
 	for (i = 0; i < NR_INODE_TYPE; i++) {
 		INIT_LIST_HEAD(&sbi->inode_list[i]);
@@ -5268,7 +5269,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 		goto free_sb_buf;
 	}
 
-	INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
 	memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
 	memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
 
@@ -5702,6 +5702,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	/* no need to free dummy_enc_policy, we just keep it in ctx when failed */
 	swap(F2FS_CTX_INFO(ctx).dummy_enc_policy, F2FS_OPTION(sbi).dummy_enc_policy);
 free_sb_buf:
+	/*
+	 * s_error_work may be scheduled on early error paths that bypass
+	 * stop_ckpt_thread's flush, make sure it is flushed before
+	 * freeing raw_super
+	 */
+	flush_work(&sbi->s_error_work);
 	kfree(raw_super);
 free_sbi:
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
-- 
2.56.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-30 17:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 17:06 [PATCH] f2fs: flush s_error_work on early mount failures Kirill Vanzha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®