read: bound zstd-decompressed size to the declared size#951
Open
scadastrangelove wants to merge 1 commit into
Open
read: bound zstd-decompressed size to the declared size#951scadastrangelove wants to merge 1 commit into
scadastrangelove wants to merge 1 commit into
Conversation
CompressedData::decompress reserves try_reserve_exact(size) for the header-declared uncompressed size, but the Zstandard branch's read_to_end grows the buffer to the actual decoded length, ignoring that reservation (the Zlib branch stays bounded by its reserved capacity via decompress_vec). A section declaring a small ch_size but carrying a stream that expands to gigabytes therefore allocates the full decoded size before the trailing size != decompressed.len() check can reject it. Bound the zstd read to size (+1 so the existing size check still rejects an over-long stream), so the allocation can no longer exceed the declared size. Discovered by the rust-in-peace security pipeline (https://github.com/scadastrangelove/rust-in-peace/).
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.
Fixes #950.
The Zstandard branch of
CompressedData::decompressgrowsdecompressedpast thetry_reserve_exact(size)reservation that the Zlib branch respects (decompress_vecstays within its reserved capacity,read_to_enddoes not). So a section declaring a smallch_sizecan allocate the full decoded size of a large-expanding zstd stream before the trailingsize != decompressed.len()check rejects it.This bounds the zstd read to
size(plus one byte, so the existing size check still rejects an over-long stream), so the allocation can no longer exceed the declared uncompressed size.Verified against
main:cargo testpasses (25 lib + 7 integration);Discovered by the rust-in-peace security pipeline.