Repository navigation
Conversation
… does C indexes table[s - 257] and table[s] with no check, so a length symbol below 257 or a distance symbol of 30 or 31 reads past the table. The port returned 0. This reproduces C's out-of-bounds read and is not meant to be merged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Read the extra-bits tables without a bounds check, as C does
Problem
ZopfliGetLengthSymbolExtraBitsandZopfliGetDistSymbolExtraBits(C, Rust) look up a symbol's extra-bit count in a fixed table. C indexes the table directly,table[s - 257]for lengths andtable[s]for distances, with no bounds check. The port returns 0 for an out-of-range symbol. These are reached fromZopfliCalculateBlockSizewhen a store holds a command whose length symbol is below 257 (a length under 3) or whose distance symbol is 30 or 31 (a distance over 32768). For example:AutoType)Question
Which behaviour do we want: C's or the Rust port's? Specifically, for a length symbol below 257 or a distance symbol of 30/31, should the extra-bits lookup read past its table (like the C source), or return 0 (like the Rust port)?
This PR implements C's behaviour.
Fix
Both functions drop the range check and index the table unchecked, so an out-of-range symbol reads past it as C's
table[...]does:Replayed before and after (plain = x86-64, arm32 = 32-bit;
ZopfliCalculateBlockSize[AutoType]returns the block size as raw f64 bits):464437230009791283246342743853084180484634274385308418048(still differs)463195221675055513646218191175889715200.0(reads 0 past its table)The port now performs the same unchecked table read as C, leaving its safe
return 0. But the value it reads is whatever sits in memory past its table, which is not the memory past C's table and differs by architecture, so the result generally does not match C: on x86-64 case 1 still differs, and case 2's garbage differs on both. Only on the 32-bit build does case 1 land on the same value as C. This is the residual the note below is about: the patch enters C's out-of-bounds-read class but cannot match a value that depends on each program's own layout.(Case 2's range,
lstart > lend, is only reached once theZopfliCalculateBlockSizerange guard is also removed (#215); the numbers above are with both patches applied.)Downstream check. The normal path is untouched:
ZopfliGzipCompresson the repository'sREADMEgives the same 831-byte stream from the port before and after this patch, andgzip.decompressrestores the file. C's stream is also 831 bytes but differs in a few bytes, for a separate reason (#213).Notes for review: