Skip to content

Commit

Permalink
ext2: Add more validity checks for inode counts
Browse files Browse the repository at this point in the history
Add checks verifying number of inodes stored in the superblock matches
the number computed from number of inodes per group. Also verify we have
at least one block worth of inodes per group. This prevents crashes on
corrupted filesystems.

Reported-by: [email protected]
Signed-off-by: Jan Kara <[email protected]>
  • Loading branch information
jankara committed Jul 26, 2022
1 parent bd6e21a commit fa78f33
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions fs/ext2/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,10 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
sbi->s_frags_per_group);
goto failed_mount;
}
if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
sbi->s_inodes_per_group > sb->s_blocksize * 8) {
ext2_msg(sb, KERN_ERR,
"error: #inodes per group too big: %lu",
"error: invalid #inodes per group: %lu",
sbi->s_inodes_per_group);
goto failed_mount;
}
Expand All @@ -1071,6 +1072,13 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
le32_to_cpu(es->s_first_data_block) - 1)
/ EXT2_BLOCKS_PER_GROUP(sb)) + 1;
if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group !=
le32_to_cpu(es->s_inodes_count)) {
ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu",
le32_to_cpu(es->s_inodes_count),
(u64)sbi->s_groups_count * sbi->s_inodes_per_group);
goto failed_mount;
}
db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
EXT2_DESC_PER_BLOCK(sb);
sbi->s_group_desc = kmalloc_array(db_count,
Expand Down

0 comments on commit fa78f33

Please sign in to comment.