From: Dominik Maier <dmnk@google.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Taylor Blau <me@ttaylorr.com>,
Patrick Steinhardt <ps@pks.im>,
Lidong Yan <502024330056@smail.nju.edu.cn>,
Abhradeep Chakraborty <chakrabortyabhradeep79@gmail.com>,
dmnk+artist@google.com, Dominik Maier <dmnk@google.com>
Subject: [PATCH] ewah: validate rlw bounds in ewah_read_mmap
Date: Thu, 19 Nov 2026 20:22:37 +0100 [thread overview]
Message-ID: <20261119192237.2666510-1-dmnk@google.com> (raw)
ewah_read_mmap() trusts the serialized word count, RLW offset, and
per-header literal word counts after reading a bitmap from disk. A
corrupt .bitmap file with buffer_size == 0, an oversized buffer_size,
an rlw offset past buffer_size, or a running-length header whose
literal_words exceeds the remaining words in self->buffer causes memory
leaks, huge pre-check reallocations, or out-of-bounds heap reads in
ewah_each_bit(), rlwit_init(), next_word(), and rlwit_discharge().
Validate non-zero buffer_size and available mmap length before mutating
self or growing self->buffer, verify that every RLW header's literal
word count fits within self->buffer_size and that the serialized rlw
offset points to the final RLW header, and clamp next_word() /
rlwit_init() against the buffer size as defense in depth.
Signed-off-by: Dominik Maier <dmnk@google.com>
---
ewah/ewah_io.c | 43 +++++++++++++----
ewah/ewah_rlw.c | 17 +++++--
t/t5310-pack-bitmaps.sh | 103 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 150 insertions(+), 13 deletions(-)
diff --git a/ewah/ewah_io.c b/ewah/ewah_io.c
index da005523b0..551aec22d3 100644
--- a/ewah/ewah_io.c
+++ b/ewah/ewah_io.c
@@ -21,6 +21,7 @@
#include "git-compat-util.h"
#include "ewok.h"
+#include "ewok_rlw.h"
#include "strbuf.h"
int ewah_serialize_to(struct ewah_bitmap *self,
@@ -93,21 +94,32 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
{
const uint8_t *ptr = map;
size_t data_len;
- size_t i;
+ size_t i, pos, last_rlw = 0;
+ uint32_t bit_size, buffer_size, rlw_offset;
if (len < sizeof(uint32_t))
return error("corrupt ewah bitmap: eof before bit size");
- self->bit_size = get_be32(ptr);
+ bit_size = get_be32(ptr);
ptr += sizeof(uint32_t);
len -= sizeof(uint32_t);
if (len < sizeof(uint32_t))
return error("corrupt ewah bitmap: eof before length");
- self->buffer_size = self->alloc_size = get_be32(ptr);
+ buffer_size = get_be32(ptr);
ptr += sizeof(uint32_t);
len -= sizeof(uint32_t);
- REALLOC_ARRAY(self->buffer, self->alloc_size);
+ if (!buffer_size)
+ return error("corrupt ewah bitmap: empty buffer");
+
+ if (buffer_size > len / sizeof(eword_t))
+ return error("corrupt ewah bitmap: eof in data "
+ "(%"PRIuMAX" bytes short)",
+ (uintmax_t)buffer_size * sizeof(eword_t) - len);
+
+ self->bit_size = bit_size;
+ self->buffer_size = buffer_size;
+ ALLOC_GROW(self->buffer, self->buffer_size, self->alloc_size);
/*
* Copy the raw data for the bitmap as a whole chunk;
@@ -115,11 +127,7 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
* the endianness conversion in a separate pass to ensure
* we're loading 8-byte aligned words.
*/
- data_len = st_mult(self->buffer_size, sizeof(eword_t));
- if (len < data_len)
- return error("corrupt ewah bitmap: eof in data "
- "(%"PRIuMAX" bytes short)",
- (uintmax_t)(data_len - len));
+ data_len = (size_t)buffer_size * sizeof(eword_t);
memcpy(self->buffer, ptr, data_len);
ptr += data_len;
len -= data_len;
@@ -129,9 +137,24 @@ ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
if (len < sizeof(uint32_t))
return error("corrupt ewah bitmap: eof before rlw");
- self->rlw = self->buffer + get_be32(ptr);
+ rlw_offset = get_be32(ptr);
ptr += sizeof(uint32_t);
len -= sizeof(uint32_t);
+ if (rlw_offset >= self->buffer_size)
+ return error("corrupt ewah bitmap: rlw offset out of bounds");
+
+ for (pos = 0; pos < self->buffer_size; ) {
+ eword_t literal_words = rlw_get_literal_words(&self->buffer[pos]);
+ last_rlw = pos;
+ if (literal_words > self->buffer_size - 1 - pos)
+ return error("corrupt ewah bitmap: literal words out of bounds");
+ pos += 1 + (size_t)literal_words;
+ }
+ if (rlw_offset != last_rlw)
+ return error("corrupt ewah bitmap: invalid rlw offset");
+
+ self->rlw = self->buffer + rlw_offset;
+
return ptr - (const uint8_t *)map;
}
diff --git a/ewah/ewah_rlw.c b/ewah/ewah_rlw.c
index 76b4c6c19e..9db47ab6f7 100644
--- a/ewah/ewah_rlw.c
+++ b/ewah/ewah_rlw.c
@@ -25,13 +25,20 @@
static inline int next_word(struct rlw_iterator *it)
{
+ size_t avail;
+ eword_t literal_words;
+
if (it->pointer >= it->size)
return 0;
+ avail = it->size - it->pointer - 1;
it->rlw.word = &it->buffer[it->pointer];
- it->pointer += rlw_get_literal_words(it->rlw.word) + 1;
+ literal_words = rlw_get_literal_words(it->rlw.word);
+ if (literal_words > avail)
+ literal_words = avail;
+ it->pointer += (size_t)literal_words + 1;
- it->rlw.literal_words = rlw_get_literal_words(it->rlw.word);
+ it->rlw.literal_words = literal_words;
it->rlw.running_len = rlw_get_running_len(it->rlw.word);
it->rlw.running_bit = rlw_get_run_bit(it->rlw.word);
it->rlw.literal_word_offset = 0;
@@ -45,7 +52,11 @@ void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *from_ewah)
it->size = from_ewah->buffer_size;
it->pointer = 0;
- next_word(it);
+ if (!next_word(it)) {
+ memset(&it->rlw, 0, sizeof(it->rlw));
+ it->literal_word_start = 0;
+ return;
+ }
it->literal_word_start = rlwit_literal_words(it) +
it->rlw.literal_word_offset;
diff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh
index b75a35707c..7679166c37 100755
--- a/t/t5310-pack-bitmaps.sh
+++ b/t/t5310-pack-bitmaps.sh
@@ -566,6 +566,109 @@ test_bitmap_cases () {
test_cmp expect actual
)
'
+
+ test_expect_success 'reject ewah bitmap with invalid buffer_size, rlw, or literal words' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ test_commit base &&
+ git repack -adb &&
+ bitmap="$(ls .git/objects/pack/pack-*.bitmap)" &&
+ chmod +w "$bitmap" &&
+ cp "$bitmap" "$bitmap.orig" &&
+ algo=$(test_oid algo) &&
+
+ # Corrupt the first type bitmap buffer_size to 0
+ "$PERL_PATH" -MDigest::SHA=sha1,sha256 -e '\''
+ my ($file, $algo) = @ARGV;
+ open my $fh, "+<:raw", $file or die $!;
+ local $/;
+ my $data = <$fh>;
+ my $hsz = ($algo eq "sha256") ? 32 : 20;
+ my $off = 12 + $hsz;
+ substr($data, $off + 4, 4) = pack("N", 0);
+ my $sum = ($algo eq "sha256") ?
+ sha256(substr($data, 0, -$hsz)) :
+ sha1(substr($data, 0, -$hsz));
+ substr($data, -$hsz, $hsz) = $sum;
+ seek $fh, 0, 0 or die $!;
+ print $fh $data or die $!;
+ close $fh or die $!;
+ '\'' "$bitmap" "$algo" &&
+ test_must_fail git rev-list --test-bitmap HEAD 2>err &&
+ test_grep "corrupt ewah bitmap: empty buffer" err &&
+
+ # Corrupt the first type bitmap buffer_size to 0xffffffff
+ cp "$bitmap.orig" "$bitmap" &&
+ chmod +w "$bitmap" &&
+ "$PERL_PATH" -MDigest::SHA=sha1,sha256 -e '\''
+ my ($file, $algo) = @ARGV;
+ open my $fh, "+<:raw", $file or die $!;
+ local $/;
+ my $data = <$fh>;
+ my $hsz = ($algo eq "sha256") ? 32 : 20;
+ my $off = 12 + $hsz;
+ substr($data, $off + 4, 4) = pack("N", 0xffffffff);
+ my $sum = ($algo eq "sha256") ?
+ sha256(substr($data, 0, -$hsz)) :
+ sha1(substr($data, 0, -$hsz));
+ substr($data, -$hsz, $hsz) = $sum;
+ seek $fh, 0, 0 or die $!;
+ print $fh $data or die $!;
+ close $fh or die $!;
+ '\'' "$bitmap" "$algo" &&
+ test_must_fail git rev-list --test-bitmap HEAD 2>err &&
+ test_grep "corrupt ewah bitmap: eof in data" err &&
+
+ # Corrupt the first type bitmap rlw offset past buffer_size
+ cp "$bitmap.orig" "$bitmap" &&
+ chmod +w "$bitmap" &&
+ "$PERL_PATH" -MDigest::SHA=sha1,sha256 -e '\''
+ my ($file, $algo) = @ARGV;
+ open my $fh, "+<:raw", $file or die $!;
+ local $/;
+ my $data = <$fh>;
+ my $hsz = ($algo eq "sha256") ? 32 : 20;
+ my $off = 12 + $hsz;
+ my $words = unpack("N", substr($data, $off + 4, 4));
+ substr($data, $off + 8 + $words * 8, 4) = pack("N", $words + 64);
+ my $sum = ($algo eq "sha256") ?
+ sha256(substr($data, 0, -$hsz)) :
+ sha1(substr($data, 0, -$hsz));
+ substr($data, -$hsz, $hsz) = $sum;
+ seek $fh, 0, 0 or die $!;
+ print $fh $data or die $!;
+ close $fh or die $!;
+ '\'' "$bitmap" "$algo" &&
+ test_must_fail git rev-list --test-bitmap HEAD 2>err &&
+ test_grep "corrupt ewah bitmap" err &&
+
+ # Corrupt the first RLW header literal_words count past buffer_size
+ cp "$bitmap.orig" "$bitmap" &&
+ chmod +w "$bitmap" &&
+ "$PERL_PATH" -MDigest::SHA=sha1,sha256 -e '\''
+ my ($file, $algo) = @ARGV;
+ open my $fh, "+<:raw", $file or die $!;
+ local $/;
+ my $data = <$fh>;
+ my $hsz = ($algo eq "sha256") ? 32 : 20;
+ my $off = 12 + $hsz;
+ substr($data, $off + 8, 8) = pack("NN", 0x00001000, 0x00000000);
+ my $sum = ($algo eq "sha256") ?
+ sha256(substr($data, 0, -$hsz)) :
+ sha1(substr($data, 0, -$hsz));
+ substr($data, -$hsz, $hsz) = $sum;
+ seek $fh, 0, 0 or die $!;
+ print $fh $data or die $!;
+ close $fh or die $!;
+ '\'' "$bitmap" "$algo" &&
+ test_must_fail git rev-list --test-bitmap HEAD 2>err &&
+ test_grep "corrupt ewah bitmap" err
+ )
+ '
}
test_bitmap_cases
--
2.56.1.750.gf70dc682cf-goog
reply other threads:[~2026-11-19 19:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119192237.2666510-1-dmnk@google.com \
--to=dmnk@google.com \
--cc=502024330056@smail.nju.edu.cn \
--cc=chakrabortyabhradeep79@gmail.com \
--cc=dmnk+artist@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=me@ttaylorr.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox