mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/3] f2fs: fix dentry recovery and MAIN_AREA validation
@ 2026-10-26 10:50 Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 1/3] f2fs: reject non-directory parent inode during dentry recovery Cheng Yongkang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Cheng Yongkang @ 2026-10-26 10:50 UTC (permalink / raw)
  To: Jaegeuk Kim, Chao Yu; +Cc: linux-f2fs-devel, linux-kernel

v3:
- Add the missing Fixes trailers to 1/3 and 3/3; 2/3 already carried its
  Fixes tag in v2.
- Add Cc: stable@vger.kernel.org to each fix.  No code change from v2.

v2:
- Rebase patch 1/3 onto current f2fs dev.  recover_dentry() now takes a
  struct f2fs_cached_block and sets dir from fsync_entry->inode; the
  S_ISDIR() check is unchanged.
- Patches 2/3 and 3/3 are unchanged.

This series fixes three independent issues found while validating crafted
f2fs images:

1. reject a non-directory i_pino during dentry recovery;
2. reject MAIN_AREA boundary underflow in the alignment fix;
3. refresh the superblock CRC after that alignment fix.

Each patch is a single logical change and compiles on its own.  All three
were validated in QEMU on the KASAN kernel: baseline trigger, patched
no-trigger or clean rejection, and a normal filesystem mount/write/fsck
regression.  The baseline source and build were restored and
hash-verified after each run.  The series applies cleanly to f2fs dev
(fd106ada4dee).

Cheng Yongkang (3):
  f2fs: reject non-directory parent inode during dentry recovery
  f2fs: reject MAIN_AREA boundary underflow
  f2fs: refresh superblock CRC after MAIN_AREA alignment fix

 fs/f2fs/recovery.c |  7 +++++++
 fs/f2fs/super.c    | 13 +++++++++++++
 2 files changed, 20 insertions(+)

--
2.34.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/3] f2fs: reject non-directory parent inode during dentry recovery
  2026-10-26 10:50 [PATCH v3 0/3] f2fs: fix dentry recovery and MAIN_AREA validation Cheng Yongkang
@ 2026-10-26 10:50 ` Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 2/3] f2fs: reject MAIN_AREA boundary underflow Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 3/3] f2fs: refresh superblock CRC after MAIN_AREA alignment fix Cheng Yongkang
  2 siblings, 0 replies; 4+ messages in thread
From: Cheng Yongkang @ 2026-10-26 10:50 UTC (permalink / raw)
  To: Jaegeuk Kim, Chao Yu; +Cc: linux-f2fs-devel, linux-kernel, stable

recover_dentry() takes i_pino from the on-disk inode and uses the
resulting inode as the directory for __f2fs_find_entry() and
f2fs_add_dentry().  A crafted image can set i_pino to a regular or
special inode; the recovery code does not check S_ISDIR() and then
writes a dentry into a non-directory inode, extending its i_size and
changing its directory-only metadata.

Reject the record with -EFSCORRUPTED and mark the filesystem for fsck:

  F2FS-fs (loop0): recover_dentry: ino 4 is not a directory
  F2FS-fs (loop0): Cannot recover all fsync data errno=-117

Fixes: d624c96fb324 ("f2fs: add recovery routines for roll-forward")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
 fs/f2fs/recovery.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/f2fs/recovery.c b/fs/f2fs/recovery.c
index 54b524660..3e8054324 100644
--- a/fs/f2fs/recovery.c
+++ b/fs/f2fs/recovery.c
@@ -209,6 +209,13 @@ static int recover_dentry(struct inode *inode, struct f2fs_cached_block *entry,
 	}
 
 	dir = fsync_entry->inode;
+	if (!S_ISDIR(dir->i_mode)) {
+		f2fs_err(F2FS_I_SB(inode),
+			 "%s: ino %u is not a directory", __func__, pino);
+		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
+		err = -EFSCORRUPTED;
+		goto out;
+	}
 	err = init_recovered_filename(dir, inode, raw_inode, &fname, &usr_fname);
 	if (err)
 		goto out;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 2/3] f2fs: reject MAIN_AREA boundary underflow
  2026-10-26 10:50 [PATCH v3 0/3] f2fs: fix dentry recovery and MAIN_AREA validation Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 1/3] f2fs: reject non-directory parent inode during dentry recovery Cheng Yongkang
@ 2026-10-26 10:50 ` Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 3/3] f2fs: refresh superblock CRC after MAIN_AREA alignment fix Cheng Yongkang
  2 siblings, 0 replies; 4+ messages in thread
From: Cheng Yongkang @ 2026-10-26 10:50 UTC (permalink / raw)
  To: Jaegeuk Kim, Chao Yu; +Cc: linux-f2fs-devel, linux-kernel, stable

sanity_check_area_boundary() computes

	main_end_blkaddr - segment0_blkaddr

as u64 when main_end_blkaddr < seg_end_blkaddr.  A crafted superblock
can make main_end_blkaddr smaller than segment0_blkaddr, so the
subtraction underflows and cpu_to_le32() truncates the result; the
function persists a wrapped segment_count (0xfffffffd in the
crafted image) and the next mount rejects the filesystem.  The
same path can also leave the error workqueue dereferencing freed
superblock state:

  BUG: KASAN: slab-use-after-free in f2fs_commit_super

Reject the underflowing geometry before the subtraction.

Fixes: fd694733d523 ("f2fs: cover large section in sanity check of super")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
 fs/f2fs/super.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index ea2d218..b90831e 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -4004,6 +4004,12 @@ static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
 		return true;
 	}
 
+	if (main_end_blkaddr < segment0_blkaddr) {
+		f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu)",
+			  main_blkaddr, main_end_blkaddr);
+		return true;
+	}
+
 	if (main_end_blkaddr > seg_end_blkaddr) {
 		f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
 			  main_blkaddr, seg_end_blkaddr,
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 3/3] f2fs: refresh superblock CRC after MAIN_AREA alignment fix
  2026-10-26 10:50 [PATCH v3 0/3] f2fs: fix dentry recovery and MAIN_AREA validation Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 1/3] f2fs: reject non-directory parent inode during dentry recovery Cheng Yongkang
  2026-10-26 10:50 ` [PATCH v3 2/3] f2fs: reject MAIN_AREA boundary underflow Cheng Yongkang
@ 2026-10-26 10:50 ` Cheng Yongkang
  2 siblings, 0 replies; 4+ messages in thread
From: Cheng Yongkang @ 2026-10-26 10:50 UTC (permalink / raw)
  To: Jaegeuk Kim, Chao Yu; +Cc: linux-f2fs-devel, linux-kernel, stable

sanity_check_area_boundary() fixes the on-disk segment_count through
__f2fs_commit_super(..., false), which does not refresh the superblock
checksum.  segment_count is covered by the checksum, so the next mount
fails with -EFSCORRUPTED after the alignment fix.

Recompute the CRC over the modified superblock before writing it.  Use the
raw superblock's feature bit: sanity_check_area_boundary() runs from
read_raw_super_block(), before sbi->raw_super is assigned.

Fixes: fd694733d523 ("f2fs: cover large section in sanity check of super")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
 fs/f2fs/super.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index b90831e..7dcf15e 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -4023,6 +4023,13 @@ static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
 		raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
 				segment0_blkaddr) >> log_blocks_per_seg);
 
+		if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
+			u32 crc;
+
+			crc = f2fs_crc32(raw_super, offsetof(struct f2fs_super_block, crc));
+			raw_super->crc = cpu_to_le32(crc);
+		}
+
 		if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
 			set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
 			res = "internally";
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-26 10:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-26 10:50 [PATCH v3 0/3] f2fs: fix dentry recovery and MAIN_AREA validation Cheng Yongkang
2026-10-26 10:50 ` [PATCH v3 1/3] f2fs: reject non-directory parent inode during dentry recovery Cheng Yongkang
2026-10-26 10:50 ` [PATCH v3 2/3] f2fs: reject MAIN_AREA boundary underflow Cheng Yongkang
2026-10-26 10:50 ` [PATCH v3 3/3] f2fs: refresh superblock CRC after MAIN_AREA alignment fix Cheng Yongkang

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®