{"thread":{"id":"66881","subject":"[PATCH] ewah: validate rlw bounds in ewah_read_mmap","startedAt":"2026-11-19T19:22:37Z","lastAt":"2026-11-19T19:22:37Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558874","messageId":"20261119192237.2666510-1-dmnk@google.com","threadId":"66881","inReplyTo":null,"subject":"[PATCH] ewah: validate rlw bounds in ewah_read_mmap","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:22:37Z","receivedAt":"2026-11-19T19:22:37Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"ewah_read_mmap() trusts the serialized word count, RLW offset, and\nper-header literal word counts after reading a bitmap from disk. A\ncorrupt .bitmap file with buffer_size == 0, an oversized buffer_size,\nan rlw offset past buffer_size, or a running-length header whose\nliteral_words exceeds the remaining words in self->buffer causes memory\nleaks, huge pre-check reallocations, or out-of-bounds heap reads in\newah_each_bit(), rlwit_init(), next_word(), and rlwit_discharge().\n\nValidate non-zero buffer_size and available mmap length before mutating\nself or growing self->buffer, verify that every RLW header's literal\nword count fits within self->buffer_size and that the serialized rlw\noffset points to the final RLW header, and clamp next_word() /\nrlwit_init() against the buffer size as defense in depth.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n ewah/ewah_io.c          |  43 +++++++++++++----\n ewah/ewah_rlw.c         |  17 +++++--\n t/t5310-pack-bitmaps.sh | 103 ++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 150 insertions(+), 13 deletions(-)\n\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nindex da005523b0..551aec22d3 100644\n--- a/ewah/ewah_io.c\n+++ b/ewah/ewah_io.c\n@@ -21,6 +21,7 @@\n \n #include \"git-compat-util.h\"\n #include \"ewok.h\"\n+#include \"ewok_rlw.h\"\n #include \"strbuf.h\"\n \n int ewah_serialize_to(struct ewah_bitmap *self,\n@@ -93,21 +94,32 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)\n {\n \tconst uint8_t *ptr = map;\n \tsize_t data_len;\n-\tsize_t i;\n+\tsize_t i, pos, last_rlw = 0;\n+\tuint32_t bit_size, buffer_size, rlw_offset;\n \n \tif (len < sizeof(uint32_t))\n \t\treturn error(\"corrupt ewah bitmap: eof before bit size\");\n-\tself->bit_size = get_be32(ptr);\n+\tbit_size = get_be32(ptr);\n \tptr += sizeof(uint32_t);\n \tlen -= sizeof(uint32_t);\n \n \tif (len < sizeof(uint32_t))\n \t\treturn error(\"corrupt ewah bitmap: eof before length\");\n-\tself->buffer_size = self->alloc_size = get_be32(ptr);\n+\tbuffer_size = get_be32(ptr);\n \tptr += sizeof(uint32_t);\n \tlen -= sizeof(uint32_t);\n \n-\tREALLOC_ARRAY(self->buffer, self->alloc_size);\n+\tif (!buffer_size)\n+\t\treturn error(\"corrupt ewah bitmap: empty buffer\");\n+\n+\tif (buffer_size > len / sizeof(eword_t))\n+\t\treturn error(\"corrupt ewah bitmap: eof in data \"\n+\t\t\t     \"(%\"PRIuMAX\" bytes short)\",\n+\t\t\t     (uintmax_t)buffer_size * sizeof(eword_t) - len);\n+\n+\tself->bit_size = bit_size;\n+\tself->buffer_size = buffer_size;\n+\tALLOC_GROW(self->buffer, self->buffer_size, self->alloc_size);\n \n \t/*\n \t * Copy the raw data for the bitmap as a whole chunk;\n@@ -115,11 +127,7 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)\n \t * the endianness conversion in a separate pass to ensure\n \t * we're loading 8-byte aligned words.\n \t */\n-\tdata_len = st_mult(self->buffer_size, sizeof(eword_t));\n-\tif (len < data_len)\n-\t\treturn error(\"corrupt ewah bitmap: eof in data \"\n-\t\t\t     \"(%\"PRIuMAX\" bytes short)\",\n-\t\t\t     (uintmax_t)(data_len - len));\n+\tdata_len = (size_t)buffer_size * sizeof(eword_t);\n \tmemcpy(self->buffer, ptr, data_len);\n \tptr += data_len;\n \tlen -= data_len;\n@@ -129,9 +137,24 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)\n \n \tif (len < sizeof(uint32_t))\n \t\treturn error(\"corrupt ewah bitmap: eof before rlw\");\n-\tself->rlw = self->buffer + get_be32(ptr);\n+\trlw_offset = get_be32(ptr);\n \tptr += sizeof(uint32_t);\n \tlen -= sizeof(uint32_t);\n \n+\tif (rlw_offset >= self->buffer_size)\n+\t\treturn error(\"corrupt ewah bitmap: rlw offset out of bounds\");\n+\n+\tfor (pos = 0; pos < self->buffer_size; ) {\n+\t\teword_t literal_words = rlw_get_literal_words(&self->buffer[pos]);\n+\t\tlast_rlw = pos;\n+\t\tif (literal_words > self->buffer_size - 1 - pos)\n+\t\t\treturn error(\"corrupt ewah bitmap: literal words out of bounds\");\n+\t\tpos += 1 + (size_t)literal_words;\n+\t}\n+\tif (rlw_offset != last_rlw)\n+\t\treturn error(\"corrupt ewah bitmap: invalid rlw offset\");\n+\n+\tself->rlw = self->buffer + rlw_offset;\n+\n \treturn ptr - (const uint8_t *)map;\n }\ndiff --git a/ewah/ewah_rlw.c b/ewah/ewah_rlw.c\nindex 76b4c6c19e..9db47ab6f7 100644\n--- a/ewah/ewah_rlw.c\n+++ b/ewah/ewah_rlw.c\n@@ -25,13 +25,20 @@\n \n static inline int next_word(struct rlw_iterator *it)\n {\n+\tsize_t avail;\n+\teword_t literal_words;\n+\n \tif (it->pointer >= it->size)\n \t\treturn 0;\n \n+\tavail = it->size - it->pointer - 1;\n \tit->rlw.word = &it->buffer[it->pointer];\n-\tit->pointer += rlw_get_literal_words(it->rlw.word) + 1;\n+\tliteral_words = rlw_get_literal_words(it->rlw.word);\n+\tif (literal_words > avail)\n+\t\tliteral_words = avail;\n+\tit->pointer += (size_t)literal_words + 1;\n \n-\tit->rlw.literal_words = rlw_get_literal_words(it->rlw.word);\n+\tit->rlw.literal_words = literal_words;\n \tit->rlw.running_len = rlw_get_running_len(it->rlw.word);\n \tit->rlw.running_bit = rlw_get_run_bit(it->rlw.word);\n \tit->rlw.literal_word_offset = 0;\n@@ -45,7 +52,11 @@ void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *from_ewah)\n \tit->size = from_ewah->buffer_size;\n \tit->pointer = 0;\n \n-\tnext_word(it);\n+\tif (!next_word(it)) {\n+\t\tmemset(&it->rlw, 0, sizeof(it->rlw));\n+\t\tit->literal_word_start = 0;\n+\t\treturn;\n+\t}\n \n \tit->literal_word_start = rlwit_literal_words(it) +\n \t\tit->rlw.literal_word_offset;\ndiff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh\nindex b75a35707c..7679166c37 100755\n--- a/t/t5310-pack-bitmaps.sh\n+++ b/t/t5310-pack-bitmaps.sh\n@@ -566,6 +566,109 @@ test_bitmap_cases () {\n \t\t    test_cmp expect actual\n \t\t)\n \t'\n+\n+\ttest_expect_success 'reject ewah bitmap with invalid buffer_size, rlw, or literal words' '\n+\t\trm -fr repo &&\n+\t\tgit init repo &&\n+\t\ttest_when_finished \"rm -fr repo\" &&\n+\t\t(\n+\t\t\tcd repo &&\n+\t\t\tgit config pack.writeBitmapLookupTable '\"$writeLookupTable\"' &&\n+\t\t\ttest_commit base &&\n+\t\t\tgit repack -adb &&\n+\t\t\tbitmap=\"$(ls .git/objects/pack/pack-*.bitmap)\" &&\n+\t\t\tchmod +w \"$bitmap\" &&\n+\t\t\tcp \"$bitmap\" \"$bitmap.orig\" &&\n+\t\t\talgo=$(test_oid algo) &&\n+\n+\t\t\t# Corrupt the first type bitmap buffer_size to 0\n+\t\t\t\"$PERL_PATH\" -MDigest::SHA=sha1,sha256 -e '\\''\n+\t\t\t\tmy ($file, $algo) = @ARGV;\n+\t\t\t\topen my $fh, \"+<:raw\", $file or die $!;\n+\t\t\t\tlocal $/;\n+\t\t\t\tmy $data = <$fh>;\n+\t\t\t\tmy $hsz = ($algo eq \"sha256\") ? 32 : 20;\n+\t\t\t\tmy $off = 12 + $hsz;\n+\t\t\t\tsubstr($data, $off + 4, 4) = pack(\"N\", 0);\n+\t\t\t\tmy $sum = ($algo eq \"sha256\") ?\n+\t\t\t\t\tsha256(substr($data, 0, -$hsz)) :\n+\t\t\t\t\tsha1(substr($data, 0, -$hsz));\n+\t\t\t\tsubstr($data, -$hsz, $hsz) = $sum;\n+\t\t\t\tseek $fh, 0, 0 or die $!;\n+\t\t\t\tprint $fh $data or die $!;\n+\t\t\t\tclose $fh or die $!;\n+\t\t\t'\\'' \"$bitmap\" \"$algo\" &&\n+\t\t\ttest_must_fail git rev-list --test-bitmap HEAD 2>err &&\n+\t\t\ttest_grep \"corrupt ewah bitmap: empty buffer\" err &&\n+\n+\t\t\t# Corrupt the first type bitmap buffer_size to 0xffffffff\n+\t\t\tcp \"$bitmap.orig\" \"$bitmap\" &&\n+\t\t\tchmod +w \"$bitmap\" &&\n+\t\t\t\"$PERL_PATH\" -MDigest::SHA=sha1,sha256 -e '\\''\n+\t\t\t\tmy ($file, $algo) = @ARGV;\n+\t\t\t\topen my $fh, \"+<:raw\", $file or die $!;\n+\t\t\t\tlocal $/;\n+\t\t\t\tmy $data = <$fh>;\n+\t\t\t\tmy $hsz = ($algo eq \"sha256\") ? 32 : 20;\n+\t\t\t\tmy $off = 12 + $hsz;\n+\t\t\t\tsubstr($data, $off + 4, 4) = pack(\"N\", 0xffffffff);\n+\t\t\t\tmy $sum = ($algo eq \"sha256\") ?\n+\t\t\t\t\tsha256(substr($data, 0, -$hsz)) :\n+\t\t\t\t\tsha1(substr($data, 0, -$hsz));\n+\t\t\t\tsubstr($data, -$hsz, $hsz) = $sum;\n+\t\t\t\tseek $fh, 0, 0 or die $!;\n+\t\t\t\tprint $fh $data or die $!;\n+\t\t\t\tclose $fh or die $!;\n+\t\t\t'\\'' \"$bitmap\" \"$algo\" &&\n+\t\t\ttest_must_fail git rev-list --test-bitmap HEAD 2>err &&\n+\t\t\ttest_grep \"corrupt ewah bitmap: eof in data\" err &&\n+\n+\t\t\t# Corrupt the first type bitmap rlw offset past buffer_size\n+\t\t\tcp \"$bitmap.orig\" \"$bitmap\" &&\n+\t\t\tchmod +w \"$bitmap\" &&\n+\t\t\t\"$PERL_PATH\" -MDigest::SHA=sha1,sha256 -e '\\''\n+\t\t\t\tmy ($file, $algo) = @ARGV;\n+\t\t\t\topen my $fh, \"+<:raw\", $file or die $!;\n+\t\t\t\tlocal $/;\n+\t\t\t\tmy $data = <$fh>;\n+\t\t\t\tmy $hsz = ($algo eq \"sha256\") ? 32 : 20;\n+\t\t\t\tmy $off = 12 + $hsz;\n+\t\t\t\tmy $words = unpack(\"N\", substr($data, $off + 4, 4));\n+\t\t\t\tsubstr($data, $off + 8 + $words * 8, 4) = pack(\"N\", $words + 64);\n+\t\t\t\tmy $sum = ($algo eq \"sha256\") ?\n+\t\t\t\t\tsha256(substr($data, 0, -$hsz)) :\n+\t\t\t\t\tsha1(substr($data, 0, -$hsz));\n+\t\t\t\tsubstr($data, -$hsz, $hsz) = $sum;\n+\t\t\t\tseek $fh, 0, 0 or die $!;\n+\t\t\t\tprint $fh $data or die $!;\n+\t\t\t\tclose $fh or die $!;\n+\t\t\t'\\'' \"$bitmap\" \"$algo\" &&\n+\t\t\ttest_must_fail git rev-list --test-bitmap HEAD 2>err &&\n+\t\t\ttest_grep \"corrupt ewah bitmap\" err &&\n+\n+\t\t\t# Corrupt the first RLW header literal_words count past buffer_size\n+\t\t\tcp \"$bitmap.orig\" \"$bitmap\" &&\n+\t\t\tchmod +w \"$bitmap\" &&\n+\t\t\t\"$PERL_PATH\" -MDigest::SHA=sha1,sha256 -e '\\''\n+\t\t\t\tmy ($file, $algo) = @ARGV;\n+\t\t\t\topen my $fh, \"+<:raw\", $file or die $!;\n+\t\t\t\tlocal $/;\n+\t\t\t\tmy $data = <$fh>;\n+\t\t\t\tmy $hsz = ($algo eq \"sha256\") ? 32 : 20;\n+\t\t\t\tmy $off = 12 + $hsz;\n+\t\t\t\tsubstr($data, $off + 8, 8) = pack(\"NN\", 0x00001000, 0x00000000);\n+\t\t\t\tmy $sum = ($algo eq \"sha256\") ?\n+\t\t\t\t\tsha256(substr($data, 0, -$hsz)) :\n+\t\t\t\t\tsha1(substr($data, 0, -$hsz));\n+\t\t\t\tsubstr($data, -$hsz, $hsz) = $sum;\n+\t\t\t\tseek $fh, 0, 0 or die $!;\n+\t\t\t\tprint $fh $data or die $!;\n+\t\t\t\tclose $fh or die $!;\n+\t\t\t'\\'' \"$bitmap\" \"$algo\" &&\n+\t\t\ttest_must_fail git rev-list --test-bitmap HEAD 2>err &&\n+\t\t\ttest_grep \"corrupt ewah bitmap\" err\n+\t\t)\n+\t'\n }\n \n test_bitmap_cases\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}