mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kirill Vanzha <vanzha.kirill@gmail.com>
To: jaegeuk@kernel.org, chao@kernel.org
Cc: frank.li@vivo.com, linux-f2fs-devel@lists.sourceforge.net,
	linux-kernel@vger.kernel.org,
	Kirill Vanzha <vanzha.kirill@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] f2fs: flush s_error_work on early mount failures
Date: Mon, 30 Nov 2026 20:06:11 +0300	[thread overview]
Message-ID: <20261130170615.150247-2-vanzha.kirill@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-30 17:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130170615.150247-2-vanzha.kirill@gmail.com \
    --to=vanzha.kirill@gmail.com \
    --cc=chao@kernel.org \
    --cc=frank.li@vivo.com \
    --cc=jaegeuk@kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®