Skip to content

Commit 9514b0b

Browse files
peffgitster
authored andcommitted
patch-delta: handle truncated copy parameters
When we see a delta command instructing us to copy bytes from the base, we have to read the offset and size from the delta stream. We do this without checking whether we're at the end of the stream, meaning we may read past the end of the buffer. In practice this isn't exploitable in any interesting way because: 1. Deltas are always in packfiles, so we have at least a 20-byte trailer that we'll end up reading. 2. The worst case is that we try to perform a nonsense copy from the base object into the result, based on whatever was in the pack stream next. In most cases this will simply fail due to our bounds-checks against the base or the result. But even if you carefully constructed a pack stream for which it succeeds, it wouldn't perform any delta operation that you couldn't have simply included in a non-broken form. But obviously it's poor form to read past the end of the buffer we've been given. Unfortunately there's no easy way to do a single length check, since the number of bytes we need depends on the number of bits set in the initial command byte. So we'll just check each byte as we parse. We can hide the complexity in a macro; it's ugly, but not as ugly as writing out each individual conditional. Signed-off-by: Jeff King <[email protected]> Reviewed-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa72f90 commit 9514b0b

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

patch-delta.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@ void *patch_delta(const void *src_buf, unsigned long src_size,
4040
cmd = *data++;
4141
if (cmd & 0x80) {
4242
unsigned long cp_off = 0, cp_size = 0;
43-
if (cmd & 0x01) cp_off = *data++;
44-
if (cmd & 0x02) cp_off |= (*data++ << 8);
45-
if (cmd & 0x04) cp_off |= (*data++ << 16);
46-
if (cmd & 0x08) cp_off |= ((unsigned) *data++ << 24);
47-
if (cmd & 0x10) cp_size = *data++;
48-
if (cmd & 0x20) cp_size |= (*data++ << 8);
49-
if (cmd & 0x40) cp_size |= (*data++ << 16);
43+
#define PARSE_CP_PARAM(bit, var, shift) do { \
44+
if (cmd & (bit)) { \
45+
if (data >= top) \
46+
goto bad_length; \
47+
var |= ((unsigned) *data++ << (shift)); \
48+
} } while (0)
49+
PARSE_CP_PARAM(0x01, cp_off, 0);
50+
PARSE_CP_PARAM(0x02, cp_off, 8);
51+
PARSE_CP_PARAM(0x04, cp_off, 16);
52+
PARSE_CP_PARAM(0x08, cp_off, 24);
53+
PARSE_CP_PARAM(0x10, cp_size, 0);
54+
PARSE_CP_PARAM(0x20, cp_size, 8);
55+
PARSE_CP_PARAM(0x40, cp_size, 16);
56+
#undef PARSE_CP_PARAM
5057
if (cp_size == 0) cp_size = 0x10000;
5158
if (unsigned_add_overflows(cp_off, cp_size) ||
5259
cp_off + cp_size > src_size ||

t/t5303-pack-corruption-resilience.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ test_expect_success \
364364
#
365365
# Note that the literal byte is necessary to get past the uninteresting minimum
366366
# delta size check.
367-
test_expect_failure \
367+
test_expect_success \
368368
'apply delta with truncated copy parameters' \
369369
'printf "\5\5\1X\221" > truncated_copy_delta &&
370370
echo base >base &&

0 commit comments

Comments
 (0)