Commit 4ec11028 authored by Theodore Ts'o's avatar Theodore Ts'o

ext4: Add sanity checks for the superblock before mounting the filesystem

This avoids insane superblock configurations that could lead to kernel
oops due to null pointer derefences.

http://bugzilla.kernel.org/show_bug.cgi?id=12371

Thanks to David Maciejak at Fortinet's FortiGuard Global Security
Research Team who discovered this bug independently (but at
approximately the same time) as Thiemo Nagel, who submitted the patch.
Signed-off-by: default avatarThiemo Nagel <thiemo.nagel@ph.tum.de>
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
Cc: stable@kernel.org
parent b3881f74
...@@ -2041,8 +2041,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) ...@@ -2041,8 +2041,8 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
const char *descr; const char *descr;
int ret = -EINVAL; int ret = -EINVAL;
int blocksize; int blocksize;
int db_count; unsigned int db_count;
int i; unsigned int i;
int needs_recovery, has_huge_files; int needs_recovery, has_huge_files;
int features; int features;
__u64 blocks_count; __u64 blocks_count;
...@@ -2331,20 +2331,30 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) ...@@ -2331,20 +2331,30 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
if (EXT4_BLOCKS_PER_GROUP(sb) == 0) if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
goto cantfind_ext4; goto cantfind_ext4;
/* ensure blocks_count calculation below doesn't sign-extend */ /*
if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) < * It makes no sense for the first data block to be beyond the end
le32_to_cpu(es->s_first_data_block) + 1) { * of the filesystem.
printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, " */
"first data block %u, blocks per group %lu\n", if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
ext4_blocks_count(es), printk(KERN_WARNING "EXT4-fs: bad geometry: first data"
le32_to_cpu(es->s_first_data_block), "block %u is beyond end of filesystem (%llu)\n",
EXT4_BLOCKS_PER_GROUP(sb)); le32_to_cpu(es->s_first_data_block),
ext4_blocks_count(es));
goto failed_mount; goto failed_mount;
} }
blocks_count = (ext4_blocks_count(es) - blocks_count = (ext4_blocks_count(es) -
le32_to_cpu(es->s_first_data_block) + le32_to_cpu(es->s_first_data_block) +
EXT4_BLOCKS_PER_GROUP(sb) - 1); EXT4_BLOCKS_PER_GROUP(sb) - 1);
do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
printk(KERN_WARNING "EXT4-fs: groups count too large: %u "
"(block count %llu, first data block %u, "
"blocks per group %lu)\n", sbi->s_groups_count,
ext4_blocks_count(es),
le32_to_cpu(es->s_first_data_block),
EXT4_BLOCKS_PER_GROUP(sb));
goto failed_mount;
}
sbi->s_groups_count = blocks_count; sbi->s_groups_count = blocks_count;
db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
EXT4_DESC_PER_BLOCK(sb); EXT4_DESC_PER_BLOCK(sb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment