Skip to content

fix: bound metadata parsing before allocation - #99

Merged
blocksmith314 merged 2 commits into
mainfrom
fix/74-metadata-budget
Nov 25, 2026
Merged

blocksmith314 merged 2 commits into
mainfrom
fix/74-metadata-budget

Conversation

@blocksmith314

@blocksmith314 blocksmith314 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Add --max-metadata-size SIZE (default 1 GiB, 0 disables) to unpack, tree, ls, dump and stat; readers reject oversized metadata in ReadPackHeaderOnly, before allocating either metadata buffer or the block metadata vector.
  • Bound both the stored and uncompressed metadata sizes; require compressed metadata to be strictly smaller than its original size (the packer uses the raw/kNone fallback otherwise), and reject either size above the LZ4 int limit.
  • Decompress into uninitialized buffers instead of zero-filling the declared size, so a failed stream does not commit it to RSS.
  • Grow 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.
  • Apply the same budget to the legacy 1.0.0 metadata path.
  • No archive-format change.

Tests

  • Release ctest: 185/185 passed
  • ASan ctest: 185/185 passed
  • New tests: MetadataBudget.* (original budget, zero disables, stored budget, LZ4 original limit, compressed-not-smaller), DataBlobMeta.RejectsEmptyBlock, ArgParserTest.CommandMaxMetadataSize.

Performance gate

  • Review-fix A/B (4dab1f3 -> 7a5e89b): unpack +0.6% wall time / -0.6% peak RSS.
  • Against origin/main (a6f9634), 10 unpack runs: +4.0% wall time, within the 5% threshold; same-code A/A control -1.0%. No attributable regression.
  • Initial both-mode gate: pack -1.9% / -0.2% RSS; unpack -3.8% / -0.6% RSS.

Closes #74

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 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.RejectsCompressedMetadataAboveLz4Limit sets 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_size and a small --max-metadata-size; assert it is rejected before the compressed buffer is allocated.
  • Add a case where compressed_size > INT_MAX with 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 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 7a5e89b.

The previous gap is fixed:

  • metadata_compressed_size now gets the same --max-metadata-size budget 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_format line 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.RejectsStoredMetadataAboveBudget uses compressed > original as well as over-budget; a separate case with compressed < original but compressed > budget would 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.

@blocksmith314
blocksmith314 merged commit 7e4a23d into main Nov 25, 2026
5 checks passed
@blocksmith314
blocksmith314 deleted the fix/74-metadata-budget branch November 25, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[security] Bug: metadata parsing can allocate several GiB from a small archive

1 participant