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 --- 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