]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pack-bitmap: bounds-check size of cache extension
authorJeff King <peff@peff.net>
Tue, 8 Dec 2020 22:03:24 +0000 (17:03 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 8 Dec 2020 22:48:15 +0000 (14:48 -0800)
A .bitmap file may have a "name hash cache" extension, which puts a
sequence of uint32_t values (one per object) at the end of the file.
When we see a flag indicating this extension, we blindly subtract the
appropriate number of bytes from our available length. However, if the
.bitmap file is too short, we'll underflow our length variable and wrap
around, thinking we have a very large length. This can lead to reading
out-of-bounds bytes while loading individual ewah bitmaps.

We can fix this by checking the number of available bytes when we parse
the header. The existing "truncated bitmap" test is now split into two
tests: one where we don't have this extension at all (and hence actually
do try to read a truncated ewah bitmap) and one where we realize
up-front that we can't even fit in the cache structure. We'll check
stderr in each case to make sure we hit the error we're expecting.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pack-bitmap.c
t/t5310-pack-bitmaps.sh

index fe5647e72e6b1d439568478070e21155a9df9de3..074d9ac8f247c8fd2f029c5d0f23b32207bfcd06 100644 (file)
@@ -153,14 +153,18 @@ static int load_bitmap_header(struct bitmap_index *index)
        /* Parse known bitmap format options */
        {
                uint32_t flags = ntohs(header->options);
+               size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
+               unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
 
                if ((flags & BITMAP_OPT_FULL_DAG) == 0)
                        return error("Unsupported options for bitmap index file "
                                "(Git requires BITMAP_OPT_FULL_DAG)");
 
                if (flags & BITMAP_OPT_HASH_CACHE) {
-                       unsigned char *end = index->map + index->map_size - the_hash_algo->rawsz;
-                       index->hashes = ((uint32_t *)end) - index->pack->num_objects;
+                       if (cache_size > index_end - index->map - header_size)
+                               return error("corrupted bitmap index file (too short to fit hash cache)");
+                       index->hashes = (void *)(index_end - cache_size);
+                       index_end -= cache_size;
                }
        }
 
index 8318781d2bbf6c345cda1fab26848771918f8a5a..e2c3907a68f2772033ef17ad3f1e3593efa354de 100755 (executable)
@@ -343,7 +343,8 @@ test_expect_success 'pack reuse respects --incremental' '
        test_must_be_empty actual
 '
 
-test_expect_success 'truncated bitmap fails gracefully' '
+test_expect_success 'truncated bitmap fails gracefully (ewah)' '
+       test_config pack.writebitmaphashcache false &&
        git repack -ad &&
        git rev-list --use-bitmap-index --count --all >expect &&
        bitmap=$(ls .git/objects/pack/*.bitmap) &&
@@ -352,7 +353,19 @@ test_expect_success 'truncated bitmap fails gracefully' '
        mv -f $bitmap.tmp $bitmap &&
        git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
        test_cmp expect actual &&
-       test_i18ngrep corrupt stderr
+       test_i18ngrep corrupt.ewah.bitmap stderr
+'
+
+test_expect_success 'truncated bitmap fails gracefully (cache)' '
+       git repack -ad &&
+       git rev-list --use-bitmap-index --count --all >expect &&
+       bitmap=$(ls .git/objects/pack/*.bitmap) &&
+       test_when_finished "rm -f $bitmap" &&
+       test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
+       mv -f $bitmap.tmp $bitmap &&
+       git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
+       test_cmp expect actual &&
+       test_i18ngrep corrupted.bitmap.index stderr
 '
 
 # have_delta <obj> <expected_base>