Repository navigation
Conversation
|
Thanks for you submission @Subodh-17. We've recently embarked on a campaign to reduce the number of PRs that address this type of accounting error (#12325), and this PR fall squarely into that category. How would you feel about me rolling this up into a larger PR, properly attributed? Doing so would help reduce pressure on reviewers (such as myself). |
|
Hi Ed (@etseidl), That sounds great to me! I completely understand the review overhead with the broader #12325 overflow campaign, and I'm very happy to have this rolled up into your larger Parquet PR. Either cherry-picking the commit or adding a Please feel free to tag me when the consolidated PR is ready—I’d be glad to help test or review the compression portions. I will leave this PR open for now so you can easily reference or cherry-pick the branch, or let me know if you’d prefer me to close it once your PR is up. Thanks again! |
|
Thanks @Subodh-17! Let's convert this PR to draft then. I'll ping you when the omnibus PR is up. |
Which issue does this PR close?
Closes #12181.
Rationale for this change
In
parquet::compression,GZipCodec::decompressandBrotliCodec::decompressread decompressed data to the end of the input stream usingread_to_end(output_buf), without enforcing the page header's declareduncompressed_size.GZipCodecexplicitly suppressed and ignored_uncompress_size.BrotliCodecuseduncompress_sizesolely as an internal buffer size hint.While
parquet::file::serialized_reader::decode_pagechecks thatdecompressed.len() == uncompressed_page_sizeafter decompression completes, that post-check occurs too late to prevent memory exhaustion during decompression. An untrusted or corrupted Parquet page declaring a small uncompressed size (e.g. 1 KB) with a compressed payload that expands into gigabytes forcesread_to_endto repeatedly reallocate the heap, causing an uncatchable Out-Of-Memory (OOM) process abort.What changes are included in this PR?
uncompress_sizeisSome(max_size), the decompressor stream is wrapped using.take(probe_limit).probe_limitis computed as(max_size as u64).saturating_add(1). Reading up tomax_size + 1allows the decoder to disambiguate an exact-match stream from an over-inflated stream, and enables GZip (MultiGzDecoder) to parse and validate the 8-byte trailer (CRC-32 and ISIZE).bytes_read > max_size, decompression immediately halts,output_bufis truncated back to its pre-call length (initial_len), and a descriptiveParquetError::Generalerror is returned.Err(e)),output_bufis similarly truncated toinitial_len, preserving preexisting caller buffer data (such as uncompressed definition and repetition levels in DataPageV2).max_size.clamp(1, BROTLI_DEFAULT_BUFFER_SIZE)(capped at 4,096 bytes) to prevent large intermediate buffer pre-allocations from untrusted headers.output_buf.reserve(max_size)insideCodec::decompress. Buffers grow dynamically viaread_to_endup toprobe_limit, preventing immediate OOM panics when a corrupted header declares an enormous size (e.g. 100 GB).uncompress_sizeisNone, falls back to unboundedread_to_endwith buffer rollback on error, preserving existing behavior for unversioned callers.Codec::decompressdocstring to clarify thatuncompress_sizeacts as a safety bound rather than guaranteeing exact eager memory allocation.Scope Exclusions
LZ4_HADOOP(LZ4Codec),LZ4_RAW,Snappy, orZSTD. It does not claim to resolve every theoretical decompression DoS vector across all formats.Are these changes tested?
Yes. Unit and integration tests were added in
parquet/src/compression.rs:test_bounded_decompression_suite: Evaluates 10 scenarios across all 10 valid GZip levels (0..=9) and all 12 valid Brotli levels (0..=11):None)usize::MAXprobe limit without overflow)test_gzip_truncated_compressed_stream: Dedicated regression test covering 4 truncation scenarios (header only, half body, truncated 8-byte trailer, unbounded mode).test_brotli_truncated_compressed_stream: Dedicated regression test covering 4 truncation scenarios (1-byte header, half body, truncated near end, unbounded mode).Verified Test Results
cargo test -p parquet --lib compression::tests: 10 passed, 0 failed.cargo test -p parquet --lib file::serialized_reader: 48 passed, 0 failed (includingtest_multi_gz).cargo test -p parquet --lib file::reader: 2 passed, 0 failed.cargo clippy -p parquet --features flate2,brotli -- -D warnings: 0 warnings.cargo fmt --check -- parquet/src/compression.rs: clean (0 diffs).Are there any significant user-facing changes?
No breaking public API changes. Legitimate Parquet pages are unaffected because their decompressed size already matches the page header's uncompressed size. Pages exceeding their declared size fail early with a clear
ParquetError::Generalerror rather than exhausting system memory.AI usage disclosure
AI assistance (Claude / DeepMind Antigravity) was used to assist in identifying edge cases, structuring the parameterized test matrix, and drafting documentation. All source changes, arithmetic safety bounds, and test cases were independently reviewed, traced, and verified against the Rust standard library and Arrow-rs codebase by the author.