Repository navigation
fix(file_store): reject an entry length prefix larger than the file - #2313
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:
|
|
Hi @wangzhengzhuo05, |
|
Thanks @nymius — understood, closing as you recommend. With For the record, the local evidence from this branch (in case it is useful when the migration lands):
Happy to re-target this against the |
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.