add patch migrate-into-vfs-crypto-engine
[ext4-patch-queue.git] / validate-reserved_gdt_blocks-on-mount
blob69d57bc9486b7230bfbfafd68083d71d2e5337e7
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
14 ---
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
35 --- a/fs/ext4/super.c
36 +++ b/fs/ext4/super.c
37 @@ -3416,6 +3416,13 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
38                 goto failed_mount;
39         }
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));
45 +               goto failed_mount;
46 +       }
48         if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
49                 err = bdev_dax_supported(sb, blocksize);
50                 if (err)