Repository navigation
fix: bound metadata parsing before allocation - #99
Conversation
The 1.1.0 reader accepted an uncompressed metadata size up to 4 GiB and allocated it before decompression; block metadata was then pre-sized from a declared count at about 216 B per entry, so a small compressed archive could drive 14 GiB+ of allocations. The legacy layout had no fixed cap at all. Add a per-archive metadata budget (--max-metadata-size, default 1 GiB, 0 disables), enforced in ReadPackHeaderOnly before any metadata buffer is allocated, for every archive reader. Reject compressed metadata above the LZ4 int limit, decompress into uninitialized buffers so a failed stream does not commit the declared size to RSS, grow the block vector while parsing instead of resize(count), and reject empty blocks so the minimum serialized block size bounds the count. No format change; archive bytes are unchanged. Closes #74
blocksmith314
left a comment
There was a problem hiding this comment.
Review
The direction is right: the budget is checked before metadata allocation, the block vector grows incrementally, uninitialized buffers avoid committing failed reads to RSS, and legacy metadata is covered. But one validation gap remains, so I would not merge yet.
metadata_compressed_size is not bounded
ReadPackHeaderOnly() checks:
metadata_original_size > max_metadata_size_- for compressed metadata,
metadata_original_size > INT_MAX
but never checks metadata_compressed_size.
RestoreBlocksFromPack() then does:
auto stored = std::make_unique_for_overwrite<char[]>(static_cast<size_t>(metadata_compressed_size_));before decompression. A crafted header can set metadata_compressed_size > metadata_original_size (the ratio check original / compressed > 255 becomes 0 and does not reject), and metadata_compressed_size is only bounded by the archive file size. That bypasses --max-metadata-size for the compressed buffer.
Also, compress::Decompress() narrows the compressed size to int:
LZ4_decompress_safe(src, dst, static_cast<int>(compressed_size),
static_cast<int>(dst_capacity));The new check covers dst_capacity (metadata_original_size) but not the compressed input size. A compressed metadata size above INT_MAX still reaches the narrowing. Valid pack stores compressed metadata only when it is smaller than the raw metadata, so this can be rejected outright.
Suggested checks:
if (type != kNone) {
if (metadata_compressed_size >= metadata_original_size) reject; // pack never writes equal/expanded compressed metadata
if (metadata_compressed_size > max_metadata_size_) reject;
if (metadata_compressed_size > INT_MAX) reject;
}and fix the existing error text, which currently says "compressed metadata size" while checking metadata_original_size.
Tests
MetadataBudget.RejectsCompressedMetadataAboveLz4Limitsets original = 2^31 and compressed = 1, so it tests the original-size branch, not a large compressed input.- Add a fixture where
compressed_size > original_sizeand a small--max-metadata-size; assert it is rejected before the compressed buffer is allocated. - Add a case where
compressed_size > INT_MAXwith a valid original size if practical.
Verified
Release ctest 183/183, ASan ctest 183/183, legacy 1.0.0 archives stat OK, CI 5/5 green.
The rest (empty-block rejection, 137-byte minimum, incremental block vector, uninitialized metadata buffers, CLI/docs) looks good.
ReadPackHeaderOnly only bounded the uncompressed metadata size. A crafted header could set metadata_compressed_size above the budget (and above the LZ4 int limit) while keeping original_size small; the ratio check then became zero and the stored buffer was still allocated up to the archive-size bound. Require compressed metadata to be strictly smaller than its original size (the packer's raw fallback otherwise), apply the metadata budget to the stored size, and check both sizes against the LZ4 int limit. Fix the error text that named the compressed size while checking the original. Refs #74
blocksmith314
left a comment
There was a problem hiding this comment.
Re-reviewed 7a5e89b.
The previous gap is fixed:
metadata_compressed_sizenow gets the same--max-metadata-sizebudget as the uncompressed size- both LZ4 inputs are checked against
INT_MAX - compressed metadata must be strictly smaller than the original (matching pack's fallback rule)
- the error messages now distinguish stored/uncompressed sizes
- new tests cover stored-size budget and equal-size rejection
Verified locally: Release ctest 185/185, ASan ctest 185/185, legacy 1.0.0 archives stat OK, CI 5/5 green.
No blockers from my side.
Minor notes, not blocking:
- README/README_CN and the
doc/file_formatline still say the limit applies to the uncompressed metadata; it now also applies to the stored compressed buffer. The CLI help already says "stored and uncompressed", so align the prose. MetadataBudget.RejectsStoredMetadataAboveBudgetusescompressed > originalas well as over-budget; a separate case withcompressed < originalbutcompressed > budgetwould isolate the stored-budget branch.- There is still no direct test for
compressed_size > INT_MAX; the code path is there, but the current LZ4 test exercises the original-size branch.
Summary
--max-metadata-size SIZE(default 1 GiB,0disables) tounpack,tree,ls,dumpandstat; readers reject oversized metadata inReadPackHeaderOnly, before allocating either metadata buffer or the block metadata vector.intlimit.data_block_metas_incrementally as blocks parse instead of pre-sizing to the declared count, and reject empty blocks; the minimum valid block is now 137 bytes.Tests
ctest: 185/185 passedctest: 185/185 passedMetadataBudget.*(original budget, zero disables, stored budget, LZ4 original limit, compressed-not-smaller),DataBlobMeta.RejectsEmptyBlock,ArgParserTest.CommandMaxMetadataSize.Performance gate
4dab1f3->7a5e89b): unpack +0.6% wall time / -0.6% peak RSS.origin/main(a6f9634), 10 unpack runs: +4.0% wall time, within the 5% threshold; same-code A/A control -1.0%. No attributable regression.Closes #74