Skip to content

Commit 13e0b0d

Browse files
peffgitster
authored andcommitted
use_pack: handle signed off_t overflow
A v2 pack index file can specify an offset within a packfile of up to 2^64-1 bytes. On a system with a signed 64-bit off_t, we can represent only up to 2^63-1. This means that a corrupted .idx file can end up with a negative offset in the pack code. Our bounds-checking use_pack function looks for too-large offsets, but not for ones that have wrapped around to negative. Let's do so, which fixes an out-of-bounds access demonstrated in t5313. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47fe3f6 commit 13e0b0d

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

sha1_file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ unsigned char *use_pack(struct packed_git *p,
10411041
die("packfile %s cannot be accessed", p->pack_name);
10421042
if (offset > (p->pack_size - 20))
10431043
die("offset beyond end of packfile (truncated pack?)");
1044+
if (offset < 0)
1045+
die("offset before end of packfile (broken .idx?)");
10441046

10451047
if (!win || !in_window(win, offset)) {
10461048
if (win)

t/t5313-pack-bounds-checks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ test_expect_success 'bogus offset into v2 extended table' '
136136
test_must_fail git index-pack --verify $pack
137137
'
138138

139-
test_expect_failure 'bogus offset inside v2 extended table' '
139+
test_expect_success 'bogus offset inside v2 extended table' '
140140
# We need two objects here, so we can plausibly require
141141
# an extended table (if the first object were larger than 2^31).
142142
do_pack "$object $(git rev-parse HEAD)" --index-version=2 &&

0 commit comments

Comments
 (0)