Skip to content

Commit 9d2e330

Browse files
peffgitster
authored andcommitted
ewah_read_mmap: bounds-check mmap reads
The on-disk ewah format tells us how big the ewah data is, and we blindly read that much from the buffer without considering whether the mmap'd data is long enough, which can lead to out-of-bound reads. Let's make sure we have data available before reading it, both for the ewah header/footer as well as for the bit data itself. In particular: - keep our ptr/len pair in sync as we move through the buffer, and check it before each read - check the size for integer overflow (this should be impossible on 64-bit, as the size is given as a 32-bit count of 8-byte words, but is possible on a 32-bit system) - return the number of bytes read as an ssize_t instead of an int, again to prevent integer overflow - compute the return value using a pointer difference; this should yield the same result as the existing code, but makes it more obvious that we got our computations right The included test is far from comprehensive, as it just picks a static point at which to truncate the generated bitmap. But in practice this will hit in the middle of an ewah and make sure we're at least exercising this code. Reported-by: Luat Nguyen <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a42a58d commit 9d2e330

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

ewah/ewah_io.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,23 @@ int ewah_serialize_strbuf(struct ewah_bitmap *self, struct strbuf *sb)
122122
return ewah_serialize_to(self, write_strbuf, sb);
123123
}
124124

125-
int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
125+
ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
126126
{
127127
const uint8_t *ptr = map;
128+
size_t data_len;
128129
size_t i;
129130

131+
if (len < sizeof(uint32_t))
132+
return error("corrupt ewah bitmap: eof before bit size");
130133
self->bit_size = get_be32(ptr);
131134
ptr += sizeof(uint32_t);
135+
len -= sizeof(uint32_t);
132136

137+
if (len < sizeof(uint32_t))
138+
return error("corrupt ewah bitmap: eof before length");
133139
self->buffer_size = self->alloc_size = get_be32(ptr);
134140
ptr += sizeof(uint32_t);
141+
len -= sizeof(uint32_t);
135142

136143
REALLOC_ARRAY(self->buffer, self->alloc_size);
137144

@@ -141,15 +148,25 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
141148
* the endianness conversion in a separate pass to ensure
142149
* we're loading 8-byte aligned words.
143150
*/
144-
memcpy(self->buffer, ptr, self->buffer_size * sizeof(eword_t));
145-
ptr += self->buffer_size * sizeof(eword_t);
151+
data_len = st_mult(self->buffer_size, sizeof(eword_t));
152+
if (len < data_len)
153+
return error("corrupt ewah bitmap: eof in data "
154+
"(%"PRIuMAX" bytes short)",
155+
(uintmax_t)(data_len - len));
156+
memcpy(self->buffer, ptr, data_len);
157+
ptr += data_len;
158+
len -= data_len;
146159

147160
for (i = 0; i < self->buffer_size; ++i)
148161
self->buffer[i] = ntohll(self->buffer[i]);
149162

163+
if (len < sizeof(uint32_t))
164+
return error("corrupt ewah bitmap: eof before rlw");
150165
self->rlw = self->buffer + get_be32(ptr);
166+
ptr += sizeof(uint32_t);
167+
len -= sizeof(uint32_t);
151168

152-
return (3 * 4) + (self->buffer_size * 8);
169+
return ptr - (const uint8_t *)map;
153170
}
154171

155172
int ewah_deserialize(struct ewah_bitmap *self, int fd)

ewah/ewok.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int ewah_serialize_native(struct ewah_bitmap *self, int fd);
9191
int ewah_serialize_strbuf(struct ewah_bitmap *self, struct strbuf *);
9292

9393
int ewah_deserialize(struct ewah_bitmap *self, int fd);
94-
int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len);
94+
ssize_t ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len);
9595

9696
uint32_t ewah_checksum(struct ewah_bitmap *self);
9797

t/t5310-pack-bitmaps.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,17 @@ test_expect_success 'pack reuse respects --incremental' '
331331
git show-index <empty.idx >actual &&
332332
test_cmp expect actual
333333
'
334+
335+
test_expect_success 'truncated bitmap fails gracefully' '
336+
git repack -ad &&
337+
git rev-list --use-bitmap-index --count --all >expect &&
338+
bitmap=$(ls .git/objects/pack/*.bitmap) &&
339+
test_when_finished "rm -f $bitmap" &&
340+
head -c 512 <$bitmap >$bitmap.tmp &&
341+
mv -f $bitmap.tmp $bitmap &&
342+
git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
343+
test_cmp expect actual &&
344+
test_i18ngrep corrupt stderr
345+
'
346+
334347
test_done

0 commit comments

Comments
 (0)