Bounds-check HuffmanEncoderV2 tree loading against corrupted input - #134
Open
alexey-milovidov wants to merge 1 commit into
Open
Bounds-check HuffmanEncoderV2 tree loading against corrupted input#134alexey-milovidov wants to merge 1 commit into
alexey-milovidov wants to merge 1 commit into
Conversation
loadAsDFSOrder read the tree header and the DFS bitstream from the compressed data without validating them against the available bytes (the remaining_length parameter was accepted but never used), so corrupted input could read past the end of the buffer and reserve an untrusted amount: - require the fixed-size header to fit in remaining_length; - reject a node count larger than the available bits (each node consumes at least one bit), which also avoids the tree.n << 1 overflow in reserve; - bound the DFS bit index against the available bytes; and - decrement remaining_length by the bytes actually consumed. Found while integrating SZ3 into ClickHouse and fuzzing the decompressor: ClickHouse/ClickHouse#108788
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.
Problem
HuffmanEncoderV2::loadAsDFSOrderreconstructs the Huffman tree from the compressed data without validating any of it against the available bytes. Theremaining_lengthparameter is accepted but never used. On corrupted or adversarial input (e.g. when fuzzing the decompressor) this leads to:tree.ht.reserve(tree.n << 1)with an untrustedtree.n— an unbounded allocation, andtree.n << 1can overflow;readBit(bytes, i++)with no upper bound oni, reading past the end of the buffer.Changes
Bounds checks only; valid data is unaffected (the checks fire only on input that would otherwise read out of bounds or allocate an untrusted amount):
usemp/mbft,offset,n,maxval) to fit inremaining_length;tree.n << 1overflow inreserve;remaining_lengthby the bytes actually consumed (the original code advanced the pointer but never updated the length).Notes
This hardens the
loadpath (which has a length).decode()does not currently receive a buffer length, so fully bounding its bit-reading loops would require threading a length through theEncoderInterface::decodesignature; that is left out of this change.Context
Found while integrating SZ3 as an experimental compression codec in ClickHouse and fuzzing the decompressor: ClickHouse/ClickHouse#108788