Skip to content

Commit 3a4b77c

Browse files
guaneryutytso
authored andcommitted
ext4: validate s_first_meta_bg at mount time
Ralf Spenneberg reported that he hit a kernel crash when mounting a modified ext4 image. And it turns out that kernel crashed when calculating fs overhead (ext4_calculate_overhead()), this is because the image has very large s_first_meta_bg (debug code shows it's 842150400), and ext4 overruns the memory in count_overhead() when setting bitmap buffer, which is PAGE_SIZE. ext4_calculate_overhead(): buf = get_zeroed_page(GFP_NOFS); <=== PAGE_SIZE buffer blks = count_overhead(sb, i, buf); count_overhead(): for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) { <=== j = 842150400 ext4_set_bit(EXT4_B2C(sbi, s++), buf); <=== buffer overrun count++; } This can be reproduced easily for me by this script: #!/bin/bash rm -f fs.img mkdir -p /mnt/ext4 fallocate -l 16M fs.img mke2fs -t ext4 -O bigalloc,meta_bg,^resize_inode -F fs.img debugfs -w -R "ssv first_meta_bg 842150400" fs.img mount -o loop fs.img /mnt/ext4 Fix it by validating s_first_meta_bg first at mount time, and refusing to mount if its value exceeds the largest possible meta_bg number. Reported-by: Ralf Spenneberg <ralf@os-t.de> Signed-off-by: Eryu Guan <guaneryu@gmail.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Andreas Dilger <adilger@dilger.ca>
1 parent d7614cc commit 3a4b77c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: fs/ext4/super.c

+9
Original file line numberDiff line numberDiff line change
@@ -3842,6 +3842,15 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
38423842
(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
38433843
db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
38443844
EXT4_DESC_PER_BLOCK(sb);
3845+
if (ext4_has_feature_meta_bg(sb)) {
3846+
if (le32_to_cpu(es->s_first_meta_bg) >= db_count) {
3847+
ext4_msg(sb, KERN_WARNING,
3848+
"first meta block group too large: %u "
3849+
"(group descriptor block count %u)",
3850+
le32_to_cpu(es->s_first_meta_bg), db_count);
3851+
goto failed_mount;
3852+
}
3853+
}
38453854
sbi->s_group_desc = ext4_kvmalloc(db_count *
38463855
sizeof(struct buffer_head *),
38473856
GFP_KERNEL);

0 commit comments

Comments
 (0)