fix(file_store): reject an entry length prefix larger than the file - #2313
Open
wangzhengzhuo05 wants to merge 1 commit into
Open
wangzhengzhuo05 wants to merge 1 commit into
wangzhengzhuo05 wants to merge 1 commit into
Conversation
EntryIter::next deserialized each entry without a size limit, so a length prefix read from a corrupted store file was passed straight to the buffer allocation: a `String` length of `u64::MAX` panicked with `capacity overflow` inside `Store::load`. Bound each entry's deserialization by the bytes remaining in the file. bincode reports every length it reads to its `SizeLimit` and returns `ErrorKind::SizeLimit` before allocating, so the oversized prefix now surfaces as `StoreError::Bincode`. The bound cannot reject a well-formed store: an entry can never be larger than the bytes left to read.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2313 +/- ##
==========================================
+ Coverage 78.84% 78.85% +0.01%
==========================================
Files 31 31
Lines 6060 6073 +13
Branches 288 289 +1
==========================================
+ Hits 4778 4789 +11
- Misses 1203 1204 +1
- Partials 79 80 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
Hi @wangzhengzhuo05, |
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 #2306
What
EntryIter::nextdecoded each entry withbincode_options().deserialize_from(..)and no sizelimit, so a length prefix read from the file was passed straight to the buffer allocation. A
store file whose entry declares a
Stringlength ofu64::MAXpanicked insideStore::load:How
Each entry is now decoded with the deserializer bounded by the bytes remaining in the file,
multiplied by nine:
size_of::<u64>()(8 bytes) for every length literal it decodes, even when thevarint encoding occupies a single byte on disk, and then charges the payload length again. A
valid entry therefore charges at most
8 * bytes + bytes.nine times the bytes remaining — the bound cannot reject a store that fits in the file.
bincode::ErrorKind::SizeLimitbefore an allocation is attempted, and the existing error arm maps that to
StoreError::Bincode.remaining == 0(end of file) short-circuits to the existing clean-EOF return instead ofhanding the deserializer a zero limit.
Only
crates/file_store/src/entry_iter.rschanges;lib.rsandstore.rsare untouched.Tests
Added
crates/file_store/tests/test_oversized_entry_len.rs:load_returns_error_on_oversized_length_prefix— the reproduction from the issue:loadreturns an error instead of panicking.
valid_store_still_round_trips— create/append/load two changesets, so the bound is nottighter than a real store needs.
Mutation check: with this change reverted,
load_returns_error_on_oversized_length_prefixpanics withcapacity overflow; with the changeapplied, it passes.
Note on #2258
#2258 (replace bincode with postcard) also bounds the frame allocation and rewrites this same
file, so it supersedes this fix if it lands first. This change is scoped to the current framing
and is independently correct and testable today; I am happy to rebase or drop it in favour of
#2258 if that is the preferred path.
Implemented with AI assistance (OpenCode + muse-spark 1.3); the reproduction, the full crate
test suite, clippy, rustfmt and the mutation check above were run locally and reviewed before
submission.