Skip to content

Commit 0924869

Browse files
peffgitster
authored andcommitted
midx: check size of object offset chunk
The object offset chunk has one fixed-size entry for each object in the midx. But since we don't check its size, we may access out-of-bounds memory if we see a corrupt or malicious midx file. Sine the entries are fixed-size, the total length can be known up-front, and we can just check it while parsing the chunk (this is similar to what we do when opening pack idx files, which contain a similar offset table). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c9b9fef commit 0924869

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

midx.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ static int midx_read_oid_lookup(const unsigned char *chunk_start,
8888
return 0;
8989
}
9090

91+
static int midx_read_object_offsets(const unsigned char *chunk_start,
92+
size_t chunk_size, void *data)
93+
{
94+
struct multi_pack_index *m = data;
95+
m->chunk_object_offsets = chunk_start;
96+
97+
if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
98+
error(_("multi-pack-index object offset chunk is the wrong size"));
99+
return 1;
100+
}
101+
return 0;
102+
}
103+
91104
struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
92105
{
93106
struct multi_pack_index *m = NULL;
@@ -164,7 +177,7 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
164177
die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
165178
if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
166179
die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
167-
if (pair_chunk_unsafe(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets))
180+
if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
168181
die(_("multi-pack-index required object offsets chunk missing or corrupted"));
169182

170183
pair_chunk_unsafe(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets);

t/t5319-multi-pack-index.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,4 +1108,14 @@ test_expect_success 'reader handles unaligned chunks' '
11081108
test_cmp expect.err err
11091109
'
11101110

1111+
test_expect_success 'reader notices too-small object offset chunk' '
1112+
corrupt_chunk OOFF clear 00000000 &&
1113+
test_must_fail git log 2>err &&
1114+
cat >expect <<-\EOF &&
1115+
error: multi-pack-index object offset chunk is the wrong size
1116+
fatal: multi-pack-index required object offsets chunk missing or corrupted
1117+
EOF
1118+
test_cmp expect err
1119+
'
1120+
11111121
test_done

0 commit comments

Comments
 (0)