Skip to content

Commit 81c4c5c

Browse files
peffgitster
authored andcommitted
packfile: detect overflow in .idx file size checks
In load_idx(), we check that the .idx file is sized appropriately for the number of objects it claims to have. We recently fixed the case where the number of objects caused our expected size to overflow a 32-bit unsigned int, and we switched to size_t. On a 64-bit system, this is fine; our size_t covers any expected size. On a 32-bit system, though, it won't. The file may claim to have 2^31 objects, which will overflow even a size_t. This doesn't hurt us at all for a well-formed idx file. A 32-bit system would already have failed to mmap such a file, since it would be too big. But an .idx file which _claims_ to have 2^31 objects but is actually much smaller would fool our check. This is a broken file, and for the most part we don't care that much what happens. But: - it's a little friendlier to notice up front "woah, this file is broken" than it is to get nonsense results - later access of the data assumes that the loading function sanity-checked that we have at least enough bytes for the regular object-id table. A malformed .idx file could lead to an out-of-bounds read. So let's use our overflow-checking functions to make sure that we're not fooled by a malformed file. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bb4542 commit 81c4c5c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

packfile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
148148
* - hash of the packfile
149149
* - file checksum
150150
*/
151-
if (idx_size != 4 * 256 + (size_t)nr * (hashsz + 4) + hashsz + hashsz)
151+
if (idx_size != st_add(4 * 256 + hashsz + hashsz, st_mult(nr, hashsz + 4)))
152152
return error("wrong index v1 file size in %s", path);
153153
} else if (version == 2) {
154154
/*
@@ -164,10 +164,10 @@ int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
164164
* variable sized table containing 8-byte entries
165165
* for offsets larger than 2^31.
166166
*/
167-
size_t min_size = 8 + 4*256 + (size_t)nr*(hashsz + 4 + 4) + hashsz + hashsz;
167+
size_t min_size = st_add(8 + 4*256 + hashsz + hashsz, st_mult(nr, hashsz + 4 + 4));
168168
size_t max_size = min_size;
169169
if (nr)
170-
max_size += ((size_t)nr - 1)*8;
170+
max_size = st_add(max_size, st_mult(nr - 1, 8));
171171
if (idx_size < min_size || idx_size > max_size)
172172
return error("wrong index v2 file size in %s", path);
173173
if (idx_size != min_size &&

0 commit comments

Comments
 (0)