Skip to content

Commit 4bc6d43

Browse files
peffgitster
authored andcommitted
commit-graph: handle overflow in chunk_size checks
We check the size of chunks with fixed records by multiplying the width of each record by the number of commits in the file. Like: if (chunk_size != g->num_commits * GRAPH_DATA_WIDTH) If this multiplication overflows, we may not notice a chunk is too small (which could later lead to out-of-bound reads). In the current code this is only possible for the CDAT chunk, but the reasons are quite subtle. We compute g->num_commits by dividing the size of the OIDL chunk by the hash length (since it consists of a bunch of hashes). So we know that any size_t multiplication that uses a value smaller than the hash length cannot overflow. And the CDAT records are the only ones that are larger (the others are just 4-byte records). So it's worth fixing all of these, to make it clear that they're not subject to overflow (without having to reason about seemingly unrelated code). The obvious thing to do is add an st_mult(), like: if (chunk_size != st_mult(g->num_commits, GRAPH_DATA_WIDTH)) And that certainly works, but it has one downside: if we detect an overflow, we'll immediately die(). But the commit graph is an optional file; if we run into other problems loading it, we'll generally return an error and fall back to accessing the full objects. Using st_mult() means a malformed file will abort the whole process. So instead, we can do a division like this: if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits) where there's no possibility of overflow. We do lose a little bit of precision; due to integer division truncation we'd allow up to an extra GRAPH_DATA_WIDTH-1 bytes of data in the chunk. That's OK. Our main goal here is making sure we don't have too _few_ bytes, which would cause an out-of-bounds read (we could actually replace our "!=" with "<", but I think it's worth being a little pedantic, as a large mismatch could be a sign of other problems). I didn't add a test here. We'd need to generate a very large graph file in order to get g->num_commits large enough to cause an overflow. And a later patch in this series will use this same division technique in a way that is much easier to trigger in the tests. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4815c3c commit 4bc6d43

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

commit-graph.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static int graph_read_commit_data(const unsigned char *chunk_start,
344344
size_t chunk_size, void *data)
345345
{
346346
struct commit_graph *g = data;
347-
if (chunk_size != g->num_commits * GRAPH_DATA_WIDTH)
347+
if (chunk_size / GRAPH_DATA_WIDTH != g->num_commits)
348348
return error("commit-graph commit data chunk is wrong size");
349349
g->chunk_commit_data = chunk_start;
350350
return 0;
@@ -354,7 +354,7 @@ static int graph_read_generation_data(const unsigned char *chunk_start,
354354
size_t chunk_size, void *data)
355355
{
356356
struct commit_graph *g = data;
357-
if (chunk_size != g->num_commits * sizeof(uint32_t))
357+
if (chunk_size / sizeof(uint32_t) != g->num_commits)
358358
return error("commit-graph generations chunk is wrong size");
359359
g->chunk_generation_data = chunk_start;
360360
return 0;
@@ -364,7 +364,7 @@ static int graph_read_bloom_index(const unsigned char *chunk_start,
364364
size_t chunk_size, void *data)
365365
{
366366
struct commit_graph *g = data;
367-
if (chunk_size != g->num_commits * 4) {
367+
if (chunk_size / 4 != g->num_commits) {
368368
warning("commit-graph changed-path index chunk is too small");
369369
return -1;
370370
}

0 commit comments

Comments
 (0)