packfile: bound delta header size reads
PATCH 1/1 packfile: bound delta header size reads
get_size_from_delta() inflates up to 20 bytes into stack-allocated delta_head[] and passes delta_head + sizeof(delta_head) to get_delta_hdr_size() twice, even when Z_STREAM_END stopped after fewer than 20 bytes. Meanwhile, get_delta_hdr_size() uses a do-while loop that unconditionally dereferences *data before checking data < top and returns a partial size when a varint's continuation bit is still set at top.
On a corrupt local packfile, a delta inflating to fewer than 20 bytes reads uninitialized stack bytes, and a 20-byte header whose bytes all have 0x80 set leaves data == top after the base-size read so the second get_delta_hdr_size() call reads 1 byte past delta_head[20].
Replace get_delta_hdr_size() with decode_delta_hdr_size(), returning -1 on EOF, unterminated varints, or size_t shift overflow so neither get_size_from_delta() nor patch_delta() treats a truncated varint as a zero-byte size, and bound top by stream.total_out.
--- delta.h | 31 +++++++++++---- packfile.c | 11 ++++-- patch-delta.c | 6 +-- t/t5300-pack-object.sh | 86 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 14 deletions(-)
delta.h +23 -8
@@ -85,20 +85,35 @@ void *patch_delta(const void *src_buf, size_t src_size,/* * This must be called twice on the delta data buffer, first to get the * expected source buffer size, and again to get the target buffer size. +* Returns 0 on success and -1 on truncated or overflowing varints. */ -static inline size_t get_delta_hdr_size (const unsigned char **datap, -const unsigned char *top) +static inline int decode_delta_hdr_size (const unsigned char **datap, +const unsigned char *top, +size_t *sizep) {const unsigned char *data = *datap;size_t cmd, size =0 ; -int i =0 ; -do { +unsigned int i =0 ; + +while (data < top) { cmd = *data++; - size |=st_left_shift (cmd &0x7f , i); - i +=7 ; - }while (cmd &0x80 && data < top); +if (i >=bitsizeof (size_t)) { +if (cmd &0x7f ) +return -1 ; + }else { +if (unsigned_left_shift_overflows (cmd &0x7f , i)) +return -1 ; + size |= (cmd &0x7f ) << i; + i +=7 ; + } +if (!(cmd &0x80 )) { + *datap = data; + *sizep = size; +return 0 ; + } + } *datap = data; -return size; +return -1 ; }#endif
packfile.c +8 -3
@@ -890,8 +890,9 @@ size_t get_size_from_delta(struct packed_git *p, struct pack_window **w_curs, off_t curpos) { -const unsigned char *data; +const unsigned char *data, *top;unsigned char delta_head[20 ], *in; +size_t size;git_zstream stream;int st;
@@ -933,12 +934,16 @@ size_t get_size_from_delta(struct packed_git *p,* the result size. */ data = delta_head; + top = delta_head + stream.total_out;/* ignore base size */ -get_delta_hdr_size (&data, delta_head+sizeof (delta_head)); +if (decode_delta_hdr_size (&data, top, &size) <0 ) +return 0 ;/* Read the result size */ -return get_delta_hdr_size (&data, delta_head+sizeof (delta_head)); +if (decode_delta_hdr_size (&data, top, &size) <0 ) +return 0 ; +return size; } int unpack_object_header(struct packed_git *p,
patch-delta.c +3 -3
@@ -27,12 +27,12 @@ void *patch_delta(const void *src_buf, size_t src_size, top = (const unsigned char *) delta_buf + delta_size;/* make sure the orig file size matches what we expect */ - size =get_delta_hdr_size (&data, top); -if (size != src_size) +if (decode_delta_hdr_size (&data, top, &size) <0 || size != src_size)return NULL ;/* now the result size */ - size =get_delta_hdr_size (&data, top); +if (decode_delta_hdr_size (&data, top, &size) <0 ) +return NULL ; dst_buf =xmallocz (size); out = dst_buf;
t/t5300-pack-object.sh +86 -0
90 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog