OSDN Git Service

ext4_utils: fix crash when filesystem size is too small
authorJin Qian <jinqian@google.com>
Sat, 25 Mar 2017 00:01:54 +0000 (17:01 -0700)
committerJin Qian <jinqian@google.com>
Sat, 25 Mar 2017 19:49:06 +0000 (19:49 +0000)
make_ext4fs crashes if size is less than block size or less than
header size (super block + descriptors + bitmaps). Check the size
and return errors.

Test: run make_ext4fs with small sizes
Bug: 36576677
Change-Id: I61d92e280cdf290054fadda5a045fc839c6fd8fe

ext4_utils/ext4_utils.c
ext4_utils/make_ext4fs.c

index 550181f..07a9211 100644 (file)
@@ -222,6 +222,9 @@ void ext4_create_fs_aux_info()
        if (ext4_bg_has_super_block(aux_info.groups - 1))
                last_header_size += 1 + aux_info.bg_desc_blocks +
                        info.bg_desc_reserve_blocks;
+       if (aux_info.groups <= 1 && last_group_size < last_header_size) {
+               critical_error("filesystem size too small");
+       }
        if (last_group_size > 0 && last_group_size < last_header_size) {
                aux_info.groups--;
                aux_info.len_blocks -= last_group_size;
index b84db9b..58069f3 100644 (file)
@@ -753,17 +753,17 @@ int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out
        if (info.len <= 0)
                info.len = get_file_size(fd);
 
-       if (info.len <= 0) {
-               fprintf(stderr, "Need size of filesystem\n");
-               return EXIT_FAILURE;
-       }
-
        if (info.block_size <= 0)
                info.block_size = compute_block_size();
 
        /* Round down the filesystem length to be a multiple of the block size */
        info.len &= ~((u64)info.block_size - 1);
 
+       if (info.len <= 0) {
+               fprintf(stderr, "filesystem size too small\n");
+               return EXIT_FAILURE;
+       }
+
        if (info.journal_blocks == 0)
                info.journal_blocks = compute_journal_blocks();