Repository navigation
fix(mmr): reject non-canonical mmr_size in proof verification - #974
Conversation
The peak helpers round a non-canonical MMR size (2, 5, 6, ...) down to the last valid MMR below it, so an honest proof relabelled with such a size recomputed the same root. `u64::MAX` maps to 2^63 leaves, whose last leaf index passed the range check and then overflowed `leaf_index_to_mmr_size` (a panic under overflow checks, a wrapped position without them) before the proof was bound to a trusted root. Add `checked_mmr_size_to_leaf_count`, the single definition of a canonical size (`2 * leaves - popcount(leaves) == mmr_size`, checked arithmetic), and enforce it in `calculate_peaks_hashes` (so every `MerkleProof` root computation), `verify_incremental`, `calculate_root_with_new_leaf`, `MmrTreeProof::verify` / `verify_and_get_root` and `MmrTreeProof::generate`. The state-sync `validate_mmr_size` now delegates to it. Not version-gated: every size a real MMR reaches is canonical, so no honest proof changes. Closes #692 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (6)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe change adds checked canonical MMR size validation. Proof APIs and replication synchronization reject non-canonical sizes before position arithmetic. Unit and integration tests cover forged sizes, including ChangesMMR size validation
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to MMR proof and replication handling now reject malformed sizes before unsafe arithmetic, with no remaining concrete merge-blocking risk identified. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 07deaa3)
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #974 +/- ##
===========================================
+ Coverage 92.93% 92.95% +0.01%
===========================================
Files 331 331
Lines 104225 104450 +225
===========================================
+ Hits 96864 97088 +224
- Misses 7361 7362 +1
🚀 New features to boost your workflow:
|
Issue
Closes #692.
MMR proof verification trusted the
mmr_sizea proof carries. Two things went wrong with that:get_peaks/get_peak_mapround a size no MMR can have (2, 5, 6, 9, ...) down to the last valid MMR below it. Sizes 5 and 6 share their peak set with size 4, and size 2 shares its with size 1, so an honest proof relabelled with one of those sizes recomputed the honest root and was accepted.mmr_size_to_leaf_count(u64::MAX)is2^63, so leaf index2^63 - 1passed theidx < leaf_countrange check and then hit2 * leaves_countinleaf_index_to_mmr_size. That is a panic under overflow checks and a wrapped position without them. In GroveDB the size comes from the parent element carried by the proof, so this ran before the element was bound to the trusted root.Both were reproduced at runtime before the fix (tests written first):
No GroveDB root forgery follows from (1).
verify_mmr_lower_layerrequires the proof's size to equal the element's size, and the element bytes are hash-bound, so a relabelled size changes the derived root. The fix makes the MMR crate's own verifier sound instead of relying on that outer check.What was done
checked_mmr_size_to_leaf_count(mmr_size) -> Option<u64>ingrovedb-merkle-mountain-range. It is the single definition of a canonical size:2 * leaf_count - popcount(leaf_count) == mmr_size, computed with checked arithmetic. That arithmetic also rejectsu64::MAX, the only size whose leaf count (2^63) has a last index that cannot be converted. Every index below a returned count is safe forleaf_index_to_pos/leaf_index_to_mmr_size.calculate_peaks_hashes, which covers everyMerkleProofroot computation:calculate_root,verify, and both sizes incalculate_root_with_new_leafverify_incrementalcalculate_root_with_new_leaf, up front fornew_mmr_size(InvalidInput, since it is a caller argument)MmrTreeProof::verify/verify_and_get_root(InvalidProof)MmrTreeProof::generate(InvalidInput)validate_mmr_sizeingrovedb/src/replication/non_merk_sync.rshad its own copy of this check. It now delegates to the shared helper.helper.rs. Theget_peaksdoc comment had drifted ontohash_count_for_root_bagging, and the arithmetic-safety notes pointed callers at the unchecked helper.Not version-gated. Every size a real MMR reaches is canonical, so no honest proof or stored tree changes behaviour. The bulk-append chunk MMR derives its expected size from
leaf_count_to_mmr_size(completed_chunks)withcompleted_chunks <= 2^63 - 1, which is always canonical, so CommitmentTree proofs are unaffected.Tests
helper.rsu64::MAX - 64(2^63 - 1leaves) and its last index converts cleanlyu64::MAX, is refusedproof.rsverifyandverify_and_get_rootu64::MAXwith index2^63 - 1returnsInvalidProofinstead of panickingMerkleProof::calculate_root,calculate_root_with_new_leaf,verify_incrementalandMmrTreeProof::generateeach rejectgrovedb/src/tests/non_merk_completeness_budget_tests.rs: a forged parentMmrTreeelement declaringu64::MAX,u64::MAX - 1, 2, 5 or 6 is refused byGroveDb::verify_querywith a boundedInvalidProof.Test, lint and format runs:
cargo test -p grovedb -p grovedb-bulk-append-tree -p grovedb-merkle-mountain-range: all pass (grovedb 3490 passed / 8 ignored, MMR 128, bulk 120)cargo clippy -p grovedb-merkle-mountain-range -p grovedb --all-targets -- -D warnings: cleancargo fmt: clean🤖 Generated with Claude Code
Summary by CodeRabbit