From 03acc1578274556131d41caecc15f35a31b35370 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 9 Nov 2017 13:07:37 +1100 Subject: [PATCH] Add some more length and distance checks. --- gen/c/std/flate.c | 15 +++++++++++++++ std/flate/decode_huffman_fast.puffs | 23 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 57f19b4a..30b68697 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2138,6 +2138,11 @@ label_0_continue:; v_n_bits -= v_table_entry_n_bits; } else { } + if (v_length > 258) { + status = + PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; + goto exit; + } if (v_n_bits < 15) { { uint8_t t_6 = *b_rptr_src++; @@ -2213,6 +2218,11 @@ label_0_continue:; v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } + if (v_distance <= 0) { + status = + PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; + goto exit; + } v_n_copied = 0; while (true) { if (((uint64_t)(v_distance)) > @@ -2238,6 +2248,11 @@ label_0_continue:; if (v_length > v_hdist) { v_length -= v_hdist; v_hlen = v_hdist; + if (v_length > 258) { + status = + PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; + goto exit; + } } else { v_hlen = v_length; v_length = 0; diff --git a/std/flate/decode_huffman_fast.puffs b/std/flate/decode_huffman_fast.puffs index 4a0ef818..eb801abe 100644 --- a/std/flate/decode_huffman_fast.puffs +++ b/std/flate/decode_huffman_fast.puffs @@ -182,6 +182,9 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { } else { assert in.src.available() >= 6 } + if length > 258 { + return error "internal error: inconsistent Huffman decoder state" + } // Ensure that we have at least 15 bits of input. if n_bits < 15 { @@ -262,12 +265,17 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { bits >>= table_entry_n_bits n_bits -= table_entry_n_bits } + if distance <= 0 { + return error "internal error: inconsistent Huffman decoder state" + } var n_copied u32 // The "while true { etc; break }" is a redundant version of "etc", but // its presence minimizes the diff between decode_huffman_fast and // decode_huffman_slow. while true, + pre length <= 258, + pre distance > 0, pre in.dst.available() >= 258, inv in.dst.is_marked(), { @@ -281,9 +289,14 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { if length > hdist { length -= hdist hlen = hdist + // TODO: this if check should be redundant. + if length > 258 { + return error "internal error: inconsistent Huffman decoder state" + } } else { hlen = length length = 0 + assert length <= 258 } if this.history_index < hdist { return error "bad distance" @@ -299,6 +312,8 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { // "etc", but its presence minimizes the diff between // decode_huffman_fast and decode_huffman_slow. while true, + inv length <= 258, + inv distance > 0, inv in.dst.available() >= 258, inv in.dst.is_marked(), { @@ -324,9 +339,15 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { } } // Once again, redundant but explicit assertions. + assert length <= 258 + assert distance > 0 + assert (distance as u64) <= in.dst.since_mark().length() assert in.dst.available() >= 258 assert in.dst.is_marked() - assert (distance as u64) <= in.dst.since_mark().length() + + // We can therefore prove: + assert (length as u64) <= 258 + assert (length as u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258) // Copy from in.dst. in.dst.copy_from_history32(distance:distance, length:length)