ewah: validate rlw bounds in ewah_read_mmap
PATCH 1/1 ewah: validate rlw bounds in ewah_read_mmap
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.
--- ewah/ewah_io.c | 43 +++++++++++++---- ewah/ewah_rlw.c | 17 +++++-- t/t5310-pack-bitmaps.sh | 103 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 13 deletions(-)
ewah/ewah_io.c +33 -10
@@ -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; }
ewah/ewah_rlw.c +14 -3
@@ -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;
t/t5310-pack-bitmaps.sh +103 -0
109 lines not on this page · raw message
Signature · 1 line
2.56.1.750.gf70dc682cf-goog