{"thread":{"id":"66882","subject":"[PATCH] packfile: bound delta header size reads","startedAt":"2026-11-19T19:22:42Z","lastAt":"2026-11-19T19:22:42Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558875","messageId":"20261119192242.2669774-1-dmnk@google.com","threadId":"66882","inReplyTo":null,"subject":"[PATCH] packfile: bound delta header size reads","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:22:42Z","receivedAt":"2026-11-19T19:22:42Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"get_size_from_delta() inflates up to 20 bytes into stack-allocated\ndelta_head[] and passes delta_head + sizeof(delta_head) to\nget_delta_hdr_size() twice, even when Z_STREAM_END stopped after fewer\nthan 20 bytes. Meanwhile, get_delta_hdr_size() uses a do-while loop that\nunconditionally dereferences *data before checking data < top and\nreturns a partial size when a varint's continuation bit is still set at\ntop.\n\nOn a corrupt local packfile, a delta inflating to fewer than 20 bytes\nreads uninitialized stack bytes, and a 20-byte header whose bytes all\nhave 0x80 set leaves data == top after the base-size read so the second\nget_delta_hdr_size() call reads 1 byte past delta_head[20].\n\nReplace get_delta_hdr_size() with decode_delta_hdr_size(), returning -1\non EOF, unterminated varints, or size_t shift overflow so neither\nget_size_from_delta() nor patch_delta() treats a truncated varint as a\nzero-byte size, and bound top by stream.total_out.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n delta.h                | 31 +++++++++++----\n packfile.c             | 11 ++++--\n patch-delta.c          |  6 +--\n t/t5300-pack-object.sh | 86 ++++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 120 insertions(+), 14 deletions(-)\n\ndiff --git a/delta.h b/delta.h\nindex 42a211905d..9a7c0ad496 100644\n--- a/delta.h\n+++ b/delta.h\n@@ -85,20 +85,35 @@ void *patch_delta(const void *src_buf, size_t src_size,\n /*\n  * This must be called twice on the delta data buffer, first to get the\n  * expected source buffer size, and again to get the target buffer size.\n+ * Returns 0 on success and -1 on truncated or overflowing varints.\n  */\n-static inline size_t get_delta_hdr_size(const unsigned char **datap,\n-\t\t\t\t\tconst unsigned char *top)\n+static inline int decode_delta_hdr_size(const unsigned char **datap,\n+\t\t\t\t\tconst unsigned char *top,\n+\t\t\t\t\tsize_t *sizep)\n {\n \tconst unsigned char *data = *datap;\n \tsize_t cmd, size = 0;\n-\tint i = 0;\n-\tdo {\n+\tunsigned int i = 0;\n+\n+\twhile (data < top) {\n \t\tcmd = *data++;\n-\t\tsize |= st_left_shift(cmd & 0x7f, i);\n-\t\ti += 7;\n-\t} while (cmd & 0x80 && data < top);\n+\t\tif (i >= bitsizeof(size_t)) {\n+\t\t\tif (cmd & 0x7f)\n+\t\t\t\treturn -1;\n+\t\t} else {\n+\t\t\tif (unsigned_left_shift_overflows(cmd & 0x7f, i))\n+\t\t\t\treturn -1;\n+\t\t\tsize |= (cmd & 0x7f) << i;\n+\t\t\ti += 7;\n+\t\t}\n+\t\tif (!(cmd & 0x80)) {\n+\t\t\t*datap = data;\n+\t\t\t*sizep = size;\n+\t\t\treturn 0;\n+\t\t}\n+\t}\n \t*datap = data;\n-\treturn size;\n+\treturn -1;\n }\n \n #endif\ndiff --git a/packfile.c b/packfile.c\nindex b32b4cd75d..bb6d0219ac 100644\n--- a/packfile.c\n+++ b/packfile.c\n@@ -890,8 +890,9 @@ size_t get_size_from_delta(struct packed_git *p,\n \t\t\t   struct pack_window **w_curs,\n \t\t\t   off_t curpos)\n {\n-\tconst unsigned char *data;\n+\tconst unsigned char *data, *top;\n \tunsigned char delta_head[20], *in;\n+\tsize_t size;\n \tgit_zstream stream;\n \tint st;\n \n@@ -933,12 +934,16 @@ size_t get_size_from_delta(struct packed_git *p,\n \t * the result size.\n \t */\n \tdata = delta_head;\n+\ttop = delta_head + stream.total_out;\n \n \t/* ignore base size */\n-\tget_delta_hdr_size(&data, delta_head+sizeof(delta_head));\n+\tif (decode_delta_hdr_size(&data, top, &size) < 0)\n+\t\treturn 0;\n \n \t/* Read the result size */\n-\treturn get_delta_hdr_size(&data, delta_head+sizeof(delta_head));\n+\tif (decode_delta_hdr_size(&data, top, &size) < 0)\n+\t\treturn 0;\n+\treturn size;\n }\n \n int unpack_object_header(struct packed_git *p,\ndiff --git a/patch-delta.c b/patch-delta.c\nindex 42199fa956..3e29e81f4e 100644\n--- a/patch-delta.c\n+++ b/patch-delta.c\n@@ -27,12 +27,12 @@ void *patch_delta(const void *src_buf, size_t src_size,\n \ttop = (const unsigned char *) delta_buf + delta_size;\n \n \t/* make sure the orig file size matches what we expect */\n-\tsize = get_delta_hdr_size(&data, top);\n-\tif (size != src_size)\n+\tif (decode_delta_hdr_size(&data, top, &size) < 0 || size != src_size)\n \t\treturn NULL;\n \n \t/* now the result size */\n-\tsize = get_delta_hdr_size(&data, top);\n+\tif (decode_delta_hdr_size(&data, top, &size) < 0)\n+\t\treturn NULL;\n \tdst_buf = xmallocz(size);\n \n \tout = dst_buf;\ndiff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh\nindex e5ce478484..972671abfc 100755\n--- a/t/t5300-pack-object.sh\n+++ b/t/t5300-pack-object.sh\n@@ -766,4 +766,90 @@ test_expect_success '--path-walk thin pack' '\n \tgit -C server index-pack --fix-thin --stdin <out.pack\n '\n \n+test_expect_success 'delta header decoding rejects truncated and unterminated varints' '\n+\ttest_when_finished \"rm -rf delta-bounds\" &&\n+\tgit init delta-bounds &&\n+\t(\n+\t\tcd delta-bounds &&\n+\t\tprintf \"hello\" >delta_base &&\n+\t\tprintf \"\\005\\201\\200\\200\" >corrupt_delta &&\n+\t\ttest_must_fail test-tool delta -p delta_base corrupt_delta delta_out &&\n+\t\t: >empty_base &&\n+\t\tprintf \"\\200\\200\\200\\200\" >corrupt_empty_delta &&\n+\t\ttest_must_fail test-tool delta -p empty_base corrupt_empty_delta delta_out &&\n+\n+\t\ttest-tool genrandom base 4096 >base &&\n+\t\tcat base >file &&\n+\t\ttest-tool genrandom oldtail 512 >>file &&\n+\t\tgit add file &&\n+\t\tgit commit -m base &&\n+\t\tcat base >file &&\n+\t\ttest-tool genrandom newtail 1024 >>file &&\n+\t\tgit add file &&\n+\t\tgit commit -m delta &&\n+\t\tgit -c pack.writeReverseIndex=false repack -ad --window=10 --depth=10 &&\n+\t\tidx=$(ls .git/objects/pack/pack-*.idx) &&\n+\t\tpack=${idx%.idx}.pack &&\n+\t\tchmod +w \"$pack\" \"$idx\" &&\n+\t\tcp \"$pack\" \"$pack.orig\" &&\n+\t\tcp \"$idx\" \"$idx.orig\" &&\n+\t\tdelta_line=$(git verify-pack -v \"$idx\" | awk '\\''NF >= 7 && $2 == \"blob\" { print $1, $5; exit }'\\'') &&\n+\t\tdelta_oid=${delta_line% *} &&\n+\t\toffset=${delta_line#* } &&\n+\t\ttest -n \"$delta_oid\" &&\n+\t\ttest -n \"$offset\" &&\n+\t\talgo=$(git rev-parse --show-object-format) &&\n+\n+\t\tfor mode in unterm oob trunc\n+\t\tdo\n+\t\t\tcp \"$pack.orig\" \"$pack\" &&\n+\t\t\tcp \"$idx.orig\" \"$idx\" &&\n+\t\t\tchmod +w \"$pack\" \"$idx\" &&\n+\t\t\t\"$PERL_PATH\" -MCompress::Zlib=compress -MDigest::SHA=sha1,sha256 -e '\\''\n+\t\t\t\tmy ($pfile, $ifile, $off, $algo, $mode) = @ARGV;\n+\t\t\t\tmy $hsz = ($algo eq \"sha256\") ? 32 : 20;\n+\t\t\t\tmy $hash = ($algo eq \"sha256\") ? \\&sha256 : \\&sha1;\n+\t\t\t\tlocal $/;\n+\n+\t\t\t\topen my $pfh, \"+<:raw\", $pfile or die $!;\n+\t\t\t\tmy $pdata = <$pfh>;\n+\t\t\t\tmy $p = $off;\n+\t\t\t\tmy $c = ord(substr($pdata, $p++, 1));\n+\t\t\t\tmy $type = ($c >> 4) & 7;\n+\t\t\t\twhile ($c & 0x80) { $c = ord(substr($pdata, $p++, 1)); }\n+\t\t\t\tif ($type == 6) {\n+\t\t\t\t\t$c = ord(substr($pdata, $p++, 1));\n+\t\t\t\t\twhile ($c & 0x80) { $c = ord(substr($pdata, $p++, 1)); }\n+\t\t\t\t} elsif ($type == 7) {\n+\t\t\t\t\t$p += $hsz;\n+\t\t\t\t} else {\n+\t\t\t\t\tdie \"expected delta object, got type $type\";\n+\t\t\t\t}\n+\t\t\t\tmy $raw = ($mode eq \"unterm\") ?\n+\t\t\t\t\tpack(\"C*\", 0x81, (0x80) x 8, 0x00, 0xaa, (0x80) x 9) :\n+\t\t\t\t\t($mode eq \"oob\") ?\n+\t\t\t\t\tpack(\"C*\", (0x80) x 20) :\n+\t\t\t\t\tpack(\"C*\", 0x05);\n+\t\t\t\tmy $z = compress($raw);\n+\t\t\t\tdie \"compressed stream too large\" if $p + length($z) > length($pdata) - $hsz;\n+\t\t\t\tsubstr($pdata, $p, length($z)) = $z;\n+\t\t\t\tmy $psum = $hash->(substr($pdata, 0, -$hsz));\n+\t\t\t\tsubstr($pdata, -$hsz, $hsz) = $psum;\n+\t\t\t\tseek $pfh, 0, 0 or die $!;\n+\t\t\t\tprint $pfh $pdata or die $!;\n+\t\t\t\tclose $pfh or die $!;\n+\n+\t\t\t\topen my $ifh, \"+<:raw\", $ifile or die $!;\n+\t\t\t\tmy $idata = <$ifh>;\n+\t\t\t\tsubstr($idata, -2 * $hsz, $hsz) = $psum;\n+\t\t\t\tsubstr($idata, -$hsz, $hsz) = $hash->(substr($idata, 0, -$hsz));\n+\t\t\t\tseek $ifh, 0, 0 or die $!;\n+\t\t\t\tprint $ifh $idata or die $!;\n+\t\t\t\tclose $ifh or die $!;\n+\t\t\t'\\'' \"$pack\" \"$idx\" \"$offset\" \"$algo\" \"$mode\" &&\n+\t\t\ttest_must_fail git cat-file -s \"$delta_oid\" || return 1\n+\t\tdone\n+\t)\n+'\n+\n test_done\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}