Skip to content

Commit 902bc91

Browse files
committed
Fix OOB write in BuildHuffmanTable.
First, BuildHuffmanTable is called to check if the data is valid. If it is and the table is not big enough, more memory is allocated. This will make sure that valid (but unoptimized because of unbalanced codes) streams are still decodable. Bug: chromium:1479274 Change-Id: I31c36dbf3aa78d35ecf38706b50464fd3d375741
1 parent 7ba44f8 commit 902bc91

File tree

4 files changed

+129
-43
lines changed

4 files changed

+129
-43
lines changed

src/dec/vp8l_dec.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ static int ReadHuffmanCodeLengths(
262262
int symbol;
263263
int max_symbol;
264264
int prev_code_len = DEFAULT_CODE_LENGTH;
265-
HuffmanCode table[1 << LENGTHS_TABLE_BITS];
265+
HuffmanTables tables;
266266

267-
if (!VP8LBuildHuffmanTable(table, LENGTHS_TABLE_BITS,
268-
code_length_code_lengths,
269-
NUM_CODE_LENGTH_CODES)) {
267+
if (!VP8LHuffmanTablesAllocate(1 << LENGTHS_TABLE_BITS, &tables) ||
268+
!VP8LBuildHuffmanTable(&tables, LENGTHS_TABLE_BITS,
269+
code_length_code_lengths, NUM_CODE_LENGTH_CODES)) {
270270
goto End;
271271
}
272272

@@ -286,7 +286,7 @@ static int ReadHuffmanCodeLengths(
286286
int code_len;
287287
if (max_symbol-- == 0) break;
288288
VP8LFillBitWindow(br);
289-
p = &table[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
289+
p = &tables.curr_segment->start[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK];
290290
VP8LSetBitPos(br, br->bit_pos_ + p->bits);
291291
code_len = p->value;
292292
if (code_len < kCodeLengthLiterals) {
@@ -309,14 +309,16 @@ static int ReadHuffmanCodeLengths(
309309
ok = 1;
310310

311311
End:
312+
VP8LHuffmanTablesDeallocate(&tables);
312313
if (!ok) return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR);
313314
return ok;
314315
}
315316

316317
// 'code_lengths' is pre-allocated temporary buffer, used for creating Huffman
317318
// tree.
318319
static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
319-
int* const code_lengths, HuffmanCode* const table) {
320+
int* const code_lengths,
321+
HuffmanTables* const table) {
320322
int ok = 0;
321323
int size = 0;
322324
VP8LBitReader* const br = &dec->br_;
@@ -367,8 +369,7 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
367369
VP8LMetadata* const hdr = &dec->hdr_;
368370
uint32_t* huffman_image = NULL;
369371
HTreeGroup* htree_groups = NULL;
370-
HuffmanCode* huffman_tables = NULL;
371-
HuffmanCode* huffman_table = NULL;
372+
HuffmanTables* huffman_tables = &hdr->huffman_tables_;
372373
int num_htree_groups = 1;
373374
int num_htree_groups_max = 1;
374375
const int max_alphabet_size =
@@ -378,6 +379,10 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
378379
int* mapping = NULL;
379380
int ok = 0;
380381

382+
// Check the table has been 0 initialized (through InitMetadata).
383+
assert(huffman_tables->root.start == NULL);
384+
assert(huffman_tables->curr_segment == NULL);
385+
381386
if (allow_recursion && VP8LReadBits(br, 1)) {
382387
// use meta Huffman codes.
383388
const int huffman_precision = VP8LReadBits(br, 3) + 2;
@@ -429,16 +434,15 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
429434

430435
code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
431436
sizeof(*code_lengths));
432-
huffman_tables = (HuffmanCode*)WebPSafeMalloc(num_htree_groups * table_size,
433-
sizeof(*huffman_tables));
434437
htree_groups = VP8LHtreeGroupsNew(num_htree_groups);
435438

436-
if (htree_groups == NULL || code_lengths == NULL || huffman_tables == NULL) {
439+
if (htree_groups == NULL || code_lengths == NULL ||
440+
!VP8LHuffmanTablesAllocate(num_htree_groups * table_size,
441+
huffman_tables)) {
437442
VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY);
438443
goto Error;
439444
}
440445

441-
huffman_table = huffman_tables;
442446
for (i = 0; i < num_htree_groups_max; ++i) {
443447
// If the index "i" is unused in the Huffman image, just make sure the
444448
// coefficients are valid but do not store them.
@@ -463,19 +467,20 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
463467
int max_bits = 0;
464468
for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
465469
int alphabet_size = kAlphabetSize[j];
466-
htrees[j] = huffman_table;
467470
if (j == 0 && color_cache_bits > 0) {
468471
alphabet_size += (1 << color_cache_bits);
469472
}
470-
size = ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_table);
473+
size =
474+
ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables);
475+
htrees[j] = huffman_tables->curr_segment->curr_table;
471476
if (size == 0) {
472477
goto Error;
473478
}
474479
if (is_trivial_literal && kLiteralMap[j] == 1) {
475-
is_trivial_literal = (huffman_table->bits == 0);
480+
is_trivial_literal = (htrees[j]->bits == 0);
476481
}
477-
total_size += huffman_table->bits;
478-
huffman_table += size;
482+
total_size += htrees[j]->bits;
483+
huffman_tables->curr_segment->curr_table += size;
479484
if (j <= ALPHA) {
480485
int local_max_bits = code_lengths[0];
481486
int k;
@@ -510,14 +515,13 @@ static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
510515
hdr->huffman_image_ = huffman_image;
511516
hdr->num_htree_groups_ = num_htree_groups;
512517
hdr->htree_groups_ = htree_groups;
513-
hdr->huffman_tables_ = huffman_tables;
514518

515519
Error:
516520
WebPSafeFree(code_lengths);
517521
WebPSafeFree(mapping);
518522
if (!ok) {
519523
WebPSafeFree(huffman_image);
520-
WebPSafeFree(huffman_tables);
524+
VP8LHuffmanTablesDeallocate(huffman_tables);
521525
VP8LHtreeGroupsFree(htree_groups);
522526
}
523527
return ok;
@@ -1352,7 +1356,7 @@ static void ClearMetadata(VP8LMetadata* const hdr) {
13521356
assert(hdr != NULL);
13531357

13541358
WebPSafeFree(hdr->huffman_image_);
1355-
WebPSafeFree(hdr->huffman_tables_);
1359+
VP8LHuffmanTablesDeallocate(&hdr->huffman_tables_);
13561360
VP8LHtreeGroupsFree(hdr->htree_groups_);
13571361
VP8LColorCacheClear(&hdr->color_cache_);
13581362
VP8LColorCacheClear(&hdr->saved_color_cache_);
@@ -1666,7 +1670,7 @@ int VP8LDecodeImage(VP8LDecoder* const dec) {
16661670

16671671
if (dec == NULL) return 0;
16681672

1669-
assert(dec->hdr_.huffman_tables_ != NULL);
1673+
assert(dec->hdr_.huffman_tables_.root.start != NULL);
16701674
assert(dec->hdr_.htree_groups_ != NULL);
16711675
assert(dec->hdr_.num_htree_groups_ > 0);
16721676

src/dec/vp8li_dec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef struct {
5151
uint32_t* huffman_image_;
5252
int num_htree_groups_;
5353
HTreeGroup* htree_groups_;
54-
HuffmanCode* huffman_tables_;
54+
HuffmanTables huffman_tables_;
5555
} VP8LMetadata;
5656

5757
typedef struct VP8LDecoder VP8LDecoder;

src/utils/huffman_utils.c

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,24 @@ static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
177177
if (num_open < 0) {
178178
return 0;
179179
}
180-
if (root_table == NULL) continue;
181180
for (; count[len] > 0; --count[len]) {
182181
HuffmanCode code;
183182
if ((key & mask) != low) {
184-
table += table_size;
183+
if (root_table != NULL) table += table_size;
185184
table_bits = NextTableBitSize(count, len, root_bits);
186185
table_size = 1 << table_bits;
187186
total_size += table_size;
188187
low = key & mask;
189-
root_table[low].bits = (uint8_t)(table_bits + root_bits);
190-
root_table[low].value = (uint16_t)((table - root_table) - low);
188+
if (root_table != NULL) {
189+
root_table[low].bits = (uint8_t)(table_bits + root_bits);
190+
root_table[low].value = (uint16_t)((table - root_table) - low);
191+
}
192+
}
193+
if (root_table != NULL) {
194+
code.bits = (uint8_t)(len - root_bits);
195+
code.value = (uint16_t)sorted[symbol++];
196+
ReplicateValue(&table[key >> root_bits], step, table_size, code);
191197
}
192-
code.bits = (uint8_t)(len - root_bits);
193-
code.value = (uint16_t)sorted[symbol++];
194-
ReplicateValue(&table[key >> root_bits], step, table_size, code);
195198
key = GetNextKey(key, len);
196199
}
197200
}
@@ -211,25 +214,83 @@ static int BuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
211214
((1 << MAX_CACHE_BITS) + NUM_LITERAL_CODES + NUM_LENGTH_CODES)
212215
// Cut-off value for switching between heap and stack allocation.
213216
#define SORTED_SIZE_CUTOFF 512
214-
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
217+
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
215218
const int code_lengths[], int code_lengths_size) {
216-
int total_size;
219+
const int total_size =
220+
BuildHuffmanTable(NULL, root_bits, code_lengths, code_lengths_size, NULL);
217221
assert(code_lengths_size <= MAX_CODE_LENGTHS_SIZE);
218-
if (root_table == NULL) {
219-
total_size = BuildHuffmanTable(NULL, root_bits,
220-
code_lengths, code_lengths_size, NULL);
221-
} else if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
222+
if (total_size == 0 || root_table == NULL) return total_size;
223+
224+
if (root_table->curr_segment->curr_table + total_size >=
225+
root_table->curr_segment->start + root_table->curr_segment->size) {
226+
// If 'root_table' does not have enough memory, allocate a new segment.
227+
// The available part of root_table->curr_segment is left unused because we
228+
// need a contiguous buffer.
229+
const int segment_size = root_table->curr_segment->size;
230+
struct HuffmanTablesSegment* next =
231+
(HuffmanTablesSegment*)WebPSafeMalloc(1, sizeof(*next));
232+
if (next == NULL) return 0;
233+
// Fill the new segment.
234+
// We need at least 'total_size' but if that value is small, it is better to
235+
// allocate a big chunk to prevent more allocations later. 'segment_size' is
236+
// therefore chosen (any other arbitrary value could be chosen).
237+
next->size = total_size > segment_size ? total_size : segment_size;
238+
next->start =
239+
(HuffmanCode*)WebPSafeMalloc(next->size, sizeof(*next->start));
240+
if (next->start == NULL) {
241+
WebPSafeFree(next);
242+
return 0;
243+
}
244+
next->curr_table = next->start;
245+
next->next = NULL;
246+
// Point to the new segment.
247+
root_table->curr_segment->next = next;
248+
root_table->curr_segment = next;
249+
}
250+
if (code_lengths_size <= SORTED_SIZE_CUTOFF) {
222251
// use local stack-allocated array.
223252
uint16_t sorted[SORTED_SIZE_CUTOFF];
224-
total_size = BuildHuffmanTable(root_table, root_bits,
225-
code_lengths, code_lengths_size, sorted);
226-
} else { // rare case. Use heap allocation.
253+
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
254+
code_lengths, code_lengths_size, sorted);
255+
} else { // rare case. Use heap allocation.
227256
uint16_t* const sorted =
228257
(uint16_t*)WebPSafeMalloc(code_lengths_size, sizeof(*sorted));
229258
if (sorted == NULL) return 0;
230-
total_size = BuildHuffmanTable(root_table, root_bits,
231-
code_lengths, code_lengths_size, sorted);
259+
BuildHuffmanTable(root_table->curr_segment->curr_table, root_bits,
260+
code_lengths, code_lengths_size, sorted);
232261
WebPSafeFree(sorted);
233262
}
234263
return total_size;
235264
}
265+
266+
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables) {
267+
// Have 'segment' point to the first segment for now, 'root'.
268+
HuffmanTablesSegment* const root = &huffman_tables->root;
269+
huffman_tables->curr_segment = root;
270+
// Allocate root.
271+
root->start = (HuffmanCode*)WebPSafeMalloc(size, sizeof(*root->start));
272+
if (root->start == NULL) return 0;
273+
root->curr_table = root->start;
274+
root->next = NULL;
275+
root->size = size;
276+
return 1;
277+
}
278+
279+
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables) {
280+
HuffmanTablesSegment *current, *next;
281+
if (huffman_tables == NULL) return;
282+
// Free the root node.
283+
current = &huffman_tables->root;
284+
next = current->next;
285+
WebPSafeFree(current->start);
286+
current->start = NULL;
287+
current->next = NULL;
288+
current = next;
289+
// Free the following nodes.
290+
while (current != NULL) {
291+
next = current->next;
292+
WebPSafeFree(current->start);
293+
WebPSafeFree(current);
294+
current = next;
295+
}
296+
}

src/utils/huffman_utils.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ typedef struct {
4343
// or non-literal symbol otherwise
4444
} HuffmanCode32;
4545

46+
// Contiguous memory segment of HuffmanCodes.
47+
typedef struct HuffmanTablesSegment {
48+
HuffmanCode* start;
49+
// Pointer to where we are writing into the segment. Starts at 'start' and
50+
// cannot go beyond 'start' + 'size'.
51+
HuffmanCode* curr_table;
52+
// Pointer to the next segment in the chain.
53+
struct HuffmanTablesSegment* next;
54+
int size;
55+
} HuffmanTablesSegment;
56+
57+
// Chained memory segments of HuffmanCodes.
58+
typedef struct HuffmanTables {
59+
HuffmanTablesSegment root;
60+
// Currently processed segment. At first, this is 'root'.
61+
HuffmanTablesSegment* curr_segment;
62+
} HuffmanTables;
63+
64+
// Allocates a HuffmanTables with 'size' contiguous HuffmanCodes. Returns 0 on
65+
// memory allocation error, 1 otherwise.
66+
int VP8LHuffmanTablesAllocate(int size, HuffmanTables* huffman_tables);
67+
void VP8LHuffmanTablesDeallocate(HuffmanTables* const huffman_tables);
68+
4669
#define HUFFMAN_PACKED_BITS 6
4770
#define HUFFMAN_PACKED_TABLE_SIZE (1u << HUFFMAN_PACKED_BITS)
4871

@@ -78,9 +101,7 @@ void VP8LHtreeGroupsFree(HTreeGroup* const htree_groups);
78101
// the huffman table.
79102
// Returns built table size or 0 in case of error (invalid tree or
80103
// memory error).
81-
// If root_table is NULL, it returns 0 if a lookup cannot be built, something
82-
// > 0 otherwise (but not the table size).
83-
int VP8LBuildHuffmanTable(HuffmanCode* const root_table, int root_bits,
104+
int VP8LBuildHuffmanTable(HuffmanTables* const root_table, int root_bits,
84105
const int code_lengths[], int code_lengths_size);
85106

86107
#ifdef __cplusplus

0 commit comments

Comments
 (0)