1 ext4: validate s_reserved_gdt_blocks on mount
3 If s_reserved_gdt_blocks is extremely large, it's possible for
4 ext4_init_block_bitmap(), which is called when ext4 sets up an
5 uninitialized block bitmap, to corrupt random kernel memory. Add the
6 same checks which e2fsck has --- it must never be larger than
7 blocksize / sizeof(__u32) --- and then add a backup check in
8 ext4_init_block_bitmap() in case the superblock gets modified after
9 the file system is mounted.
11 Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
12 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
13 Cc: stable@vger.kernel.org
15 fs/ext4/balloc.c | 3 +++
16 fs/ext4/super.c | 7 +++++++
17 2 files changed, 10 insertions(+)
19 diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
20 index 0b8105b..799a92b 100644
21 --- a/fs/ext4/balloc.c
22 +++ b/fs/ext4/balloc.c
23 @@ -208,6 +208,9 @@ static int ext4_init_block_bitmap(struct super_block *sb,
24 memset(bh->b_data, 0, sb->s_blocksize);
26 bit_max = ext4_num_base_meta_clusters(sb, block_group);
27 + if ((bit_max >> 3) >= bh->b_size)
28 + return -EFSCORRUPTED;
30 for (bit = 0; bit < bit_max; bit++)
31 ext4_set_bit(bit, bh->b_data);
33 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
34 index 5664ee6..13c49af7 100644
37 @@ -3416,6 +3416,13 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
41 + if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
42 + ext4_msg(sb, KERN_ERR,
43 + "Number of reserved GDT blocks insanely large: %d",
44 + le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
48 if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
49 err = bdev_dax_supported(sb, blocksize);