Verify block checksums in multithreaded XZ decompression

perfloop-oss/lzma-rust2 · Uncatalogued correctness

https://perfloop.ai/t/oss/case_2wsstgkzhm

Verdict

VERIFIED · settled 2026-11-25 · pull request opened as hasenbanck/lzma-rust2#153

What happened: The check fails before this change and passes with it.

Hypothesis

The multithreaded XZ reader never verifies block integrity checksums during decompression, so corrupted block payloads or corrupted checksums silently succeed and can return corrupted bytes to callers. This affects all non-None XZ check types including the default CRC64, as well as CRC32 and SHA-256. Real-world corruption prevalence is unmeasured, but XZ integrity verification is a core format requirement already enforced by the single-threaded decoders and liblzma.

`XzReaderMt::scan_blocks` parses the stream header's `CheckType` and passes it into `worker_thread_logic` and then `decompress_xz_block` at `src/xz/reader_mt.rs:506-544`. Inside `decompress_xz_block`, `check_type` is only used at line 513 to compute `checksum_size` and slice the trailing bytes off `block_data`. After `chain_reader` decodes the payload into `decompressed_data` and checks its length against `uncompressed_size`, the function returns `Ok(decompressed_data)` directly without hashing `decompressed_data` with `ChecksumCalculator` or comparing it against the trailing checksum slice `&block_data[block_data.len() - checksum_size..]`. By contrast, `XzReader::verify_block_checksum` (`src/xz/reader.rs:251-285`), `XzStream::process_block_checksum` (`src/xz/reader.rs:781-795`), and `liblzma::read::XzDecoder` all verify the block checksum and return `InvalidData` on mismatch.

A bounded exhaustive single-byte mutation search over valid single-block XZ streams (13-byte stored, 16-byte stored, and 256-byte compressed payloads across `Crc32`, `Crc64`, and `Sha256`) found 309 corrupted blocks accepted by `XzReaderMt` that both `liblzma` and `XzReader` rejected, including 90 cases of silent data corruption where `XzReaderMt` returned altered plaintext bytes. The reduced deterministic replay test `cargo test --test xz_mt_block_checksum_replay -- --exact xz_reader_mt_rejects_corrupted_payload_and_checksum` flips one bit in the stored LZMA2 payload (byte 27) and one bit in the trailing checksum across `Crc32`, `Crc64`, and `Sha256`; it fails with exit 101 on the current code because `XzReaderMt` returns `Ok([10, 48, 85, ...])` instead of an error while both `liblzma` and `XzReader` reject the input.

The smallest independent proof target is to pass this replay test and the wider single-byte mutation differential check across `CheckType::Crc32`, `CheckType::Crc64`, and `CheckType::Sha256` (including multi-block streams and filter chains) while keeping `CheckType::None` and all valid streams passing. The attached test file placement is provisional proof support.

Upstream open pull requests on `hasenbanck/lzma-rust2` were checked (`upstreamWork` returned no open PRs), and recent commits on `src/xz/reader_mt.rs` (`d956bae`, `aee6a0f`, `2646b82`, `1e05e7b`) harden malformed-stream handling rather than intentionally omitting checksum verification. Prior-finding searches (`hyp_8j1w7tw5a1`, `hyp_c5zd577xqm`) cover queueing and payload-copy performance in `XzReaderMt` and explicitly leave checksum policy unchanged.

Change to test: Compute the block integrity check specified by `check_type` over the decoded block output in `decompress_xz_block` and verify it against the block's trailing checksum bytes, returning `InvalidData` on mismatch. Preserve `CheckType::None` behavior, valid stream decoding, and existing uncompressed-size bounds.

Where it lives

perfloop-oss/lzma-rust2 · src/xz/reader_mt.rs

Evidence

The check fails before this change and passes with it: `XzReaderMt rejects CRC32, CRC64 and SHA-256 block integrity mismatches reported by XzReader and rejected by liblzma in the bounded single-byte mutation fixtures, including multiple blocks and filter chains; intact fixtures and BCJ samples decode unchanged, and None remains unchecked.`

Multi-block CRC64 XZ decode · 9 sample pairs

metric baseline candidate paired median change confidence range required result
ns/decode 211461082 240198771 +14.4% (+30432778) +14338321 to +36355838 loss ≤ 0.26 × |baseline median| PASSED

Multi-block CRC32 XZ decode · 20 sample pairs

metric baseline candidate paired median change confidence range required result
ns/decode 213379687 244329287 +15.1% (+32116094) +20930033 to +42454707 loss ≤ 0.21 × |baseline median| PASSED

Multi-block SHA-256 XZ decode · 40 sample pairs

metric baseline candidate paired median change confidence range required result
ns/decode 222137042 231926482 +4.4% (+9762923) +424246 to +18100757 loss ≤ 0.09 × |baseline median| PASSED

One-byte CRC64 XZ decode · 30 sample pairs

metric baseline candidate paired median change confidence range required result
ns/decode 157193 160141 +3042 −14027 to +10411 loss ≤ 0.09 × |baseline median| PASSED

Checks: 6 of 6 passed. Verification: no defect found.

Timeline