Repository navigation
fix(merk): validate ChunkOp bytes before trusting them - #975
Conversation
`ChunkOp::decode` mapped every traversal byte other than 1 to `false`, so one chunk id had many encodings, unlike `vec_bytes_as_traversal_instruction` which rejects non-binary bytes (#704). It also sized its buffers from the declared lengths before checking them, so a hostile length panicked on capacity overflow or reserved memory the input never backed (#904). - reject traversal bytes other than 0 and 1 - refuse a chunk id longer than any tree is deep (heights are `u8`) before allocating its buffer - cap the up-front op reservation; past it the vec grows as ops decode, so the allocation tracks the input actually consumed Production state sync does not go through this decoder (`decode_vec_ops` / `process_chunk`), so this is decoder hygiene with no consensus impact and needs no version gate. Closes #704 Closes #904 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Warning Review limit reachedNext included review available in 28 minutes. View limit detailsLimit details: You’ve used the included review currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
🕓 Queued for automated review — 2nd in line, estimated start in ~40 min (commit 01d0f00)
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #975 +/- ##
========================================
Coverage 92.95% 92.95%
========================================
Files 331 331
Lines 104450 104500 +50
========================================
+ Hits 97088 97138 +50
Misses 7362 7362
🚀 New features to boost your workflow:
|
Issue being fixed or feature implemented
Two hygiene findings in the
ChunkOpbyte decoder (merk/src/proofs/chunk/chunk_op.rs):v == 1_u8mapped every traversal byte other than1tofalse, so bytes like2or255were accepted and one chunk id had many encodings.vec_bytes_as_traversal_instructionalready rejects non-binary bytes; this decoder did not.usize::MAXpanicked withcapacity overflow(vec![0; len]/Vec::with_capacity(len)), and a large-but-valid one reserved memory the input never backed.As #904 notes, production state sync does not use this decoder (it goes through
decode_vec_opsandprocess_chunk;Restorer::process_multi_chunktakes already-materialized values). The only directChunkOp::decodecallers are tests. So this is decoder hygiene, not a demonstrated remote vulnerability.What was done?
0/1match; anything else returnsed::Error::UnexpectedByte(byte).MAX_CHUNK_ID_LENbefore the buffer is allocated. The bound is derived, not invented: Merk stores node heights asu8, somax_traversal_instruction_len(u8::MAX)= 254 is the longest instruction that can address a node in any tree. (max_traversal_instruction_lenbecame aconst fnfor this.)MAX_PREALLOCATED_OPS(1024). The reader is a genericRead, so remaining input is unknown and there is no principled cap on the op count itself; past the cap the vec grows as ops actually decode, so the allocation stays proportional to the input consumed. Valid chunks with more ops than the cap still decode.The encoder is untouched. Chunk producers already refuse over-long chunk ids (#883), so nothing legitimate is emitted that the decoder now rejects.
No
GroveVersiongate: the decoder is not on a consensus or production sync path, and the rejected inputs are ones no honest encoder produces.How Has This Been Tested?
cargo test -p grovedb-merk --lib chunk(79 pass),cargo clippy -p grovedb-merk -- -D warnings,cargo fmt --check.New/changed tests in
chunk_op.rs:test_chunk_op_decoding_rejects_non_binary_chunk_id_values— replaces the old test that pinned the canonicalizing behavior; bytes 2/3/128/255 now error.test_chunk_op_decoding_bounds_chunk_id_length_before_allocating—usize::MAXandMAX_CHUNK_ID_LEN + 1are refused with a typedInvalidDataerror.test_chunk_op_decoding_does_not_reserve_declared_op_count— a declaredusize::MAXop count returns an error.test_chunk_op_longest_chunk_id_round_trips,test_chunk_op_more_ops_than_preallocated_round_trip— bounded valid encodings still round-trip.Confirmed against the old decoder logic: the three rejection tests fail (two with a
capacity overflowpanic, one with the canonicalized decode), and pass with the fix.Breaking Changes
None for honest encodings.
ChunkOp::decodenow rejects non-binary traversal bytes and chunk ids longer than 254 steps, which it previously accepted.Closes #704
Closes #904
🤖 Generated with Claude Code