Skip to content

Commit e087c0d

Browse files
committed
delta: fix overflow when computing limit
When checking whether a delta base offset and length fit into the base we have in memory already, we can trigger an overflow which breaks the check. This would subsequently result in us reading memory from out of bounds of the base. The issue is easily fixed by checking for overflow when adding `off` and `len`, thus guaranteeting that we are never indexing beyond `base_len`. This corresponds to the git patch 8960844a7 (check patch_delta bounds more carefully, 2006-04-07), which adds these overflow checks. Reported-by: Riccardo Schirone <rschiron@redhat.com>
1 parent 2459781 commit e087c0d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/delta.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ int git_delta_apply(
566566
unsigned char cmd = *delta++;
567567
if (cmd & 0x80) {
568568
/* cmd is a copy instruction; copy from the base. */
569-
size_t off = 0, len = 0;
569+
size_t off = 0, len = 0, end;
570570

571571
#define ADD_DELTA(o, shift) { if (delta < delta_end) (o) |= ((unsigned) *delta++ << shift); else goto fail; }
572572
if (cmd & 0x01) ADD_DELTA(off, 0UL);
@@ -580,8 +580,10 @@ int git_delta_apply(
580580
if (!len) len = 0x10000;
581581
#undef ADD_DELTA
582582

583-
if (base_len < off + len || res_sz < len)
583+
if (GIT_ADD_SIZET_OVERFLOW(&end, off, len) ||
584+
base_len < end || res_sz < len)
584585
goto fail;
586+
585587
memcpy(res_dp, base + off, len);
586588
res_dp += len;
587589
res_sz -= len;

0 commit comments

Comments
 (0)