resize2fs: prevent block group descriptors from overflowing the first bg

For 1k block file systems, resizing a file system larger than
1073610752 blocks will result in the size of the block group
descriptors to be so large that it will overlap with the backup
superblock in block group #1.   This problem can be reproduced via:

    mke2fs -t ext4 /tmp/foo.img 200M
    resize2fs /tmp/foo.img 1T
    e2fsck -f /tmp/foo.img

https://github.com/tytso/e2fsprogs/issues/50

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
This commit is contained in:
Theodore Ts'o 2020-10-02 14:47:25 -04:00
parent b1a780ed7b
commit 21bad2b679

View File

@ -269,6 +269,8 @@ int main (int argc, char ** argv)
long sysval;
int len, mount_flags;
char *mtpt, *undo_file = NULL;
dgrp_t new_group_desc_count;
unsigned long new_desc_blocks;
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
@ -528,6 +530,18 @@ int main (int argc, char ** argv)
exit(1);
}
}
new_group_desc_count = ext2fs_div64_ceil(new_size -
fs->super->s_first_data_block,
EXT2_BLOCKS_PER_GROUP(fs->super));
new_desc_blocks = ext2fs_div_ceil(new_group_desc_count,
EXT2_DESC_PER_BLOCK(fs->super));
if ((new_desc_blocks + fs->super->s_first_data_block) >
EXT2_BLOCKS_PER_GROUP(fs->super)) {
com_err(program_name, 0,
_("New size results in too many block group "
"descriptors.\n"));
exit(1);
}
if (!force && new_size < min_size) {
com_err(program_name, 0,