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