From 8ebc2061e099e38654a6dd74243d5f8a55a6c84a Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Tue, 20 Oct 2026 09:44:25 -0400 Subject: [PATCH] igzip: reject inflate headers whose long codes do not fit The inflate decode tables keep codes longer than the short table index in long_code_lookup, which has 80 entries for the distance code and 1264 for the lit/len code. igzip_lib.h sizes it for the codes, entries that repeat codes shorter than the longest code of their group, and entries that no code uses, which only incomplete codes have. setup_dynamic_header() only rejects oversubscribed codes, so a header can define an incomplete code, and the bound on unused entries is too small: it allows 21 for the distance code, but a 15-bit code alone in the last group leaves 31 entries unused. An incomplete distance code can then need 86 entries: 30 codes, 25 repeats, and 31 unused entries. make_inflate_huff_code_dist() writes them without a check: it clears up to 12 bytes past long_code_lookup, which are the block_state, dict_length, and bfinal fields of inflate_state. With the stream added to the standard vectors, bfinal is cleared, and inflate returns ISAL_END_INPUT instead of finishing the final block. Before writing each group of long codes, check that it fits, and return ISAL_INVALID_BLOCK if it does not. Complete codes always fit: a complete distance code needs at most 48 entries. zlib rejects incomplete codes as invalid, except a lit/len or distance code of a single code of length 1 and an empty distance code. None of these has long codes, so the streams this rejects are not valid for zlib either. The lit/len table gets the same check, since its size was worked out the same way, though no lit/len code was found that needs more than 1208 of its 1264 entries. Signed-off-by: Parker Loflin --- igzip/igzip_inflate.c | 52 +++++++++++++++++++++++++++------------ igzip/inflate_std_vects.h | 7 +++++- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/igzip/igzip_inflate.c b/igzip/igzip_inflate.c index 6a0dbf31..f3a66f71 100644 --- a/igzip/igzip_inflate.c +++ b/igzip/igzip_inflate.c @@ -382,8 +382,9 @@ static int inline index_to_sym(int index) { return (index != 513) ? index : 512; /* Sets result to the inflate_huff_code corresponding to the huffcode defined by * the lengths in huff_code_table,where count is a histogram of the appearance - * of each code length */ -static void + * of each code length. Returns ISAL_INVALID_BLOCK if the long codes do not fit + * in long_code_lookup, and 0 otherwise. */ +static int make_inflate_huff_code_lit_len(struct inflate_huff_code_large *result, struct huff_code *huff_code_table, uint32_t table_length, uint16_t *count_total, uint32_t *code_list, uint32_t multisym) @@ -414,7 +415,7 @@ make_inflate_huff_code_lit_len(struct inflate_huff_code_large *result, if (code_list_len == 0) { memset(result->short_code_lookup, 0, sizeof(result->short_code_lookup)); - return; + return 0; } /* Determine the length of the first code */ @@ -571,6 +572,10 @@ make_inflate_huff_code_lit_len(struct inflate_huff_code_large *result, } } + if (long_code_lookup_length + (1 << (max_length - ISAL_DECODE_LONG_BITS)) > + ISAL_HUFF_CODE_LARGE_LONG_ALIGNED) + return ISAL_INVALID_BLOCK; + memset(&result->long_code_lookup[long_code_lookup_length], 0x00, sizeof(*result->long_code_lookup) * ((uint64_t) 1 << (max_length - ISAL_DECODE_LONG_BITS))); @@ -596,12 +601,17 @@ make_inflate_huff_code_lit_len(struct inflate_huff_code_large *result, LARGE_FLAG_BIT; long_code_lookup_length += 1 << (max_length - ISAL_DECODE_LONG_BITS); } + + return 0; } -static void inline make_inflate_huff_code_dist(struct inflate_huff_code_small *result, - struct huff_code *huff_code_table, - uint32_t table_length, uint16_t *count, - uint32_t max_symbol) +/* Like make_inflate_huff_code_lit_len(), for the distance code. The long codes + * of an incomplete distance code can need more entries than long_code_lookup + * has. */ +static int inline make_inflate_huff_code_dist(struct inflate_huff_code_small *result, + struct huff_code *huff_code_table, + uint32_t table_length, uint16_t *count, + uint32_t max_symbol) { uint32_t i, j, k; uint32_t *long_code_list; @@ -632,7 +642,7 @@ static void inline make_inflate_huff_code_dist(struct inflate_huff_code_small *r code_list_len = count_total[16]; if (code_list_len == 0) { memset(result->short_code_lookup, 0, sizeof(result->short_code_lookup)); - return; + return 0; } for (i = 0; i < table_length; i++) { @@ -708,6 +718,10 @@ static void inline make_inflate_huff_code_dist(struct inflate_huff_code_small *r } } + if (long_code_lookup_length + (1 << (max_length - ISAL_DECODE_SHORT_BITS)) > + ISAL_HUFF_CODE_SMALL_LONG_ALIGNED) + return ISAL_INVALID_BLOCK; + memset(&result->long_code_lookup[long_code_lookup_length], 0x00, 2 * (1 << (max_length - ISAL_DECODE_SHORT_BITS))); @@ -739,6 +753,8 @@ static void inline make_inflate_huff_code_dist(struct inflate_huff_code_small *r SMALL_FLAG_BIT; long_code_lookup_length += 1 << (max_length - ISAL_DECODE_SHORT_BITS); } + + return 0; } static void inline make_inflate_huff_code_header(struct inflate_huff_code_small *result, @@ -989,14 +1005,16 @@ static int inline setup_static_header(struct inflate_state *state) set_codes(dist_code, DIST_LEN + 2, dist_count); - make_inflate_huff_code_lit_len(&state->lit_huff_code, lit_code, LIT_LEN_ELEMS, lit_count, - code_list, multisym); + if (make_inflate_huff_code_lit_len(&state->lit_huff_code, lit_code, LIT_LEN_ELEMS, + lit_count, code_list, multisym)) + return ISAL_INVALID_BLOCK; if (state->hist_bits && state->hist_bits < 15) max_dist = 2 * state->hist_bits; - make_inflate_huff_code_dist(&state->dist_huff_code, dist_code, DIST_LEN + 2, dist_count, - max_dist); + if (make_inflate_huff_code_dist(&state->dist_huff_code, dist_code, DIST_LEN + 2, dist_count, + max_dist)) + return ISAL_INVALID_BLOCK; #endif state->block_state = ISAL_BLOCK_CODED; @@ -1378,15 +1396,17 @@ static int inline setup_dynamic_header(struct inflate_state *state) if (state->hist_bits && state->hist_bits < 15) max_dist = 2 * state->hist_bits; - make_inflate_huff_code_dist(&state->dist_huff_code, &lit_and_dist_huff[LIT_LEN], DIST_LEN, - dist_count, max_dist); + if (make_inflate_huff_code_dist(&state->dist_huff_code, &lit_and_dist_huff[LIT_LEN], + DIST_LEN, dist_count, max_dist)) + return ISAL_INVALID_BLOCK; if (set_and_expand_lit_len_huffcode(lit_and_dist_huff, LIT_LEN, lit_count, lit_expand_count, code_list)) return ISAL_INVALID_BLOCK; - make_inflate_huff_code_lit_len(&state->lit_huff_code, lit_and_dist_huff, LIT_LEN_ELEMS, - lit_count, code_list, multisym); + if (make_inflate_huff_code_lit_len(&state->lit_huff_code, lit_and_dist_huff, LIT_LEN_ELEMS, + lit_count, code_list, multisym)) + return ISAL_INVALID_BLOCK; state->block_state = ISAL_BLOCK_CODED; diff --git a/igzip/inflate_std_vects.h b/igzip/inflate_std_vects.h index 9a970906..63666b12 100644 --- a/igzip/inflate_std_vects.h +++ b/igzip/inflate_std_vects.h @@ -801,6 +801,10 @@ uint8_t std_vect_150[] = { 0x1c, 0xe5, 0x03, 0x97, 0x00, 0x80, 0xff, 0xff, 0xa5, 0x10, 0x6d, 0x6d, 0xa5, 0xa5, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }; +/* An incomplete distance code that needs 86 entries in long_code_lookup, which has 80 */ +uint8_t std_vect_151[] = { 0x05, 0xfd, 0x81, 0x01, 0x00, 0x80, 0x61, 0x18, 0xb6, 0xed, 0xbf, 0x35, + 0x48, 0x49, 0x92, 0xe4, 0x24, 0x49, 0x92, 0x24, 0x49, 0x92, 0xb4, 0x05 }; + struct vect_result { uint8_t *vector; int vector_length; @@ -958,5 +962,6 @@ struct vect_result std_vect_array[] = { { std_vect_147, sizeof(std_vect_147), ISAL_INVALID_BLOCK }, { std_vect_148, sizeof(std_vect_148), ISAL_INVALID_BLOCK }, { std_vect_149, sizeof(std_vect_149), ISAL_INVALID_BLOCK }, - { std_vect_150, sizeof(std_vect_150), ISAL_INVALID_BLOCK } + { std_vect_150, sizeof(std_vect_150), ISAL_INVALID_BLOCK }, + { std_vect_151, sizeof(std_vect_151), ISAL_INVALID_BLOCK } };