Linux EXT4 FS development
 help / color / mirror / Atom feed
From: Peng Hao <flyingpenghao@gmail.com>
To: jack@suse.com
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 1/2] ext2: Reject a superblock with an invalid s_first_ino
Date: Tue, 24 Nov 2026 19:17:07 +0800	[thread overview]
Message-ID: <20261124111708.24204-1-flyingpeng@tencent.com> (raw)

ext2_fill_super() takes s_first_ino from a revision 1 superblock without
checking it.  ext2_get_inode() and ext2_nfs_get_inode() only reject
inode numbers below EXT2_FIRST_INO(sb), so with s_first_ino == 0 an
inode number of 0 gets through, and (ino - 1) wraps around when the
block group is computed.  On such an image, opening a file handle with
inode number 0 (open_by_handle_at(), or an NFS client if the filesystem
is exported) triggers:

  block_group >= groups_count - block_group = -1, groups_count = 2
  WARNING: balloc.c:51 at ext2_get_group_desc+0x5b/0x90 [ext2]
   ext2_get_inode+0x9c/0x160 [ext2]
   ext2_iget+0x63/0x4b0 [ext2]

Depending on s_inodes_per_group, the truncated group number may also
land inside the filesystem, so that some other inode is read in place of
inode 0.  Values from 1 to 10 make the reserved inodes reachable through
file handles.

ext4 rejects s_first_ino < EXT4_GOOD_OLD_FIRST_INO at mount time since
commit c37e9e013469 ("ext4: add more inode number paranoia checks").  Do
the same for ext2.  As s_first_ino is an int, this also rejects values
of 0x80000000 and above, which turn negative and make every inode except
the root inode look reserved.

Signed-off-by: Peng Hao <flyingpeng@tencent.com>
---
 fs/ext2/super.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/ext2/super.c b/fs/ext2/super.c
index a40f530872a4..8a4412231394 100644
--- a/fs/ext2/super.c
+++ b/fs/ext2/super.c
@@ -1014,6 +1014,11 @@ static int ext2_fill_super(struct super_block *sb, struct fs_context *fc)
 	} else {
 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
+		if (sbi->s_first_ino < EXT2_GOOD_OLD_FIRST_INO) {
+			ext2_msg(sb, KERN_ERR, "error: invalid first ino: %u",
+				 le32_to_cpu(es->s_first_ino));
+			goto failed_mount;
+		}
 		if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
 		    !is_power_of_2(sbi->s_inode_size) ||
 		    (sbi->s_inode_size > blocksize)) {
-- 
2.43.7


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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 11:17 Peng Hao [this message]
2026-11-24 11:17 ` [PATCH 2/2] ext2: Set the superblock UUID Peng Hao
2026-11-24 11:22 ` [PATCH 1/2] ext2: Reject a superblock with an invalid s_first_ino sashiko-bot

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=20261124111708.24204-1-flyingpeng@tencent.com \
    --to=flyingpenghao@gmail.com \
    --cc=jack@suse.com \
    --cc=linux-ext4@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