Linux-f2fs-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Chao Yu via Linux-f2fs-devel <linux-f2fs-devel@lists.sourceforge.net>
To: jaegeuk@kernel.org
Cc: linux-f2fs-devel@lists.sourceforge.net
Subject: [f2fs-dev] [PATCH 2/2] fsck.f2fs: add bounds checking for orphan entry_count
Date: Tue, 16 Jun 2026 09:48:02 +0000	[thread overview]
Message-ID: <20260616094802.3107281-2-chao@kernel.org> (raw)
In-Reply-To: <20260616094802.3107281-1-chao@kernel.org>

In fsck_chk_orphan_node(), entry_count is read directly from the on-disk
orphan block footer. If an attacker or corrupted filesystem sets this to an
excessive value (e.g., 0xFFFFFFFF), it can cause a massive loop leading to
out-of-bounds memory reads and out-of-bounds writes into the newly allocated
orphan repair block.

Fix this by ensuring entry_count does not exceed F2FS_ORPHANS_PER_BLOCK. If
an invalid entry_count is encountered, safely reset it to 0 and write the
repaired orphan block to disk when running with auto-fix enabled.

Signed-off-by: Chao Yu <chao@kernel.org>
---
 fsck/fsck.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fsck/fsck.c b/fsck/fsck.c
index e679357..66cc1c5 100644
--- a/fsck/fsck.c
+++ b/fsck/fsck.c
@@ -2275,6 +2275,17 @@ int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
 
 		ASSERT(ret >= 0);
 		entry_count = le32_to_cpu(F2FS_ORPHAN_BLOCK_FOOTER(orphan_blk)->entry_count);
+		if (entry_count > F2FS_ORPHANS_PER_BLOCK) {
+			ASSERT_MSG("wrong orphan entry_count: %u", entry_count);
+			entry_count = 0;
+			if (f2fs_dev_is_writable() && c.fix_on) {
+				FIX_MSG("reset orphan entry_count to 0");
+				F2FS_ORPHAN_BLOCK_FOOTER(new_blk)->entry_count = 0;
+				ret = dev_write_block(new_blk, start_blk + i,
+						      WRITE_LIFE_NONE);
+				ASSERT(ret >= 0);
+			}
+		}
 
 		for (j = 0; j < entry_count; j++) {
 			nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
-- 
2.49.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

      reply	other threads:[~2026-06-16  9:48 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-16  9:48 [f2fs-dev] [PATCH 1/2] fsck.f2fs: sanity check i_inline_xattr_size correctly Chao Yu via Linux-f2fs-devel
2026-06-16  9:48 ` Chao Yu via Linux-f2fs-devel [this message]

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=20260616094802.3107281-2-chao@kernel.org \
    --to=linux-f2fs-devel@lists.sourceforge.net \
    --cc=chao@kernel.org \
    --cc=jaegeuk@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