]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ext2: Add sanity checks for group and filesystem size
authorJan Kara <jack@suse.cz>
Wed, 14 Sep 2022 15:24:42 +0000 (17:24 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 24 Oct 2022 07:56:54 +0000 (09:56 +0200)
commit d766f2d1e3e3bd44024a7f971ffcf8b8fbb7c5d2 upstream.

Add sanity check that filesystem size does not exceed the underlying
device size and that group size is big enough so that metadata can fit
into it. This avoid trying to mount some crafted filesystems with
extremely large group counts.

Reported-by: syzbot+0f2f7e65a3007d39539f@syzkaller.appspotmail.com
Reported-by: kernel test robot <oliver.sang@intel.com> # Test fixup
CC: stable@vger.kernel.org
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext2/super.c

index cdffa2a041af869e8d768dde657c20dd008b1ac2..b3232845d0c447a093fb45b891006d613c544684 100644 (file)
@@ -1053,6 +1053,13 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                        sbi->s_blocks_per_group);
                goto failed_mount;
        }
+       /* At least inode table, bitmaps, and sb have to fit in one group */
+       if (sbi->s_blocks_per_group <= sbi->s_itb_per_group + 3) {
+               ext2_msg(sb, KERN_ERR,
+                       "error: #blocks per group smaller than metadata size: %lu <= %lu",
+                       sbi->s_blocks_per_group, sbi->s_inodes_per_group + 3);
+               goto failed_mount;
+       }
        if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
                ext2_msg(sb, KERN_ERR,
                        "error: #fragments per group too big: %lu",
@@ -1066,9 +1073,14 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                        sbi->s_inodes_per_group);
                goto failed_mount;
        }
+       if (sb_bdev_nr_blocks(sb) < le32_to_cpu(es->s_blocks_count)) {
+               ext2_msg(sb, KERN_ERR,
+                        "bad geometry: block count %u exceeds size of device (%u blocks)",
+                        le32_to_cpu(es->s_blocks_count),
+                        (unsigned)sb_bdev_nr_blocks(sb));
+               goto failed_mount;
+       }
 
-       if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
-               goto cantfind_ext2;
        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;