Skip to content

fix(proof): bound bulk-append extraction before the root is bound - #990

Merged
QuantumExplorer merged 4 commits into
developfrom
fix/bulk-append-verify-resource-bound
Sep 23, 2026
Merged

QuantumExplorer merged 4 commits into
developfrom
fix/bulk-append-verify-resource-bound

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

This is an incomplete fix of #856 (PR #872). The 2026-09-23 audit found it, and it has not been filed separately.

A malicious proof supplier could make a BulkAppendTree / CommitmentTree proof verifier allocate gigabytes and run billions of iterations before the proof was bound to any trusted root. Dash Platform's shielded-note verification is exposed: rs-drive-proof-verifier, verify_shielded_encrypted_notes. It runs verify_query with limit: None over a window of up to 8192 positions, before the tenderdash root check, so a malicious DAPI node could run wallets and light clients out of memory. CommitmentTree is live on mainnet, and the V1 verifier runs under every grove version.

How the bug works:

  • Unbound inputs. verify_bulk_append_lower_layer takes total_count and height from the element carried in the proof, which is not yet authenticated.
  • Extraction before binding. It calls verify_and_compute_root, which computes a root and compares it with nothing, then extracts values. The parent combine_hash check ran only afterwards. The root itself is authenticated only when the caller compares the returned root with a trusted one.
  • Tiny blobs, huge counts. A fixed-format chunk blob [01][count = 2^20][entry_size = 0] is 9 bytes. The payload check passes (0 == 0), so deserialize_chunk_blob builds 1,048,576 empty entries, about 24 MB.
  • No per-blob bound in values_in_range. It had no count bound tied to chunk_item_count and no dedup across blobs. Under a forged height = 1, each blob's positions [2i, 2i + 2^20) overlap the query window, so every blob contributes the whole window.
  • A wrong premise. The [audit][P05] Broad non-Merk proof checks allocate heavily before parent metadata is authenticated #856 comment claimed extracted values were bounded by proof bytes, which this blob format makes false.

Reproduced against develop. A forged envelope under 4 KB (64 such blobs, an 8192-position window, no limit) made GroveDb::verify_query return 520,256 duplicate rows under a root nobody holds. Completeness passed because the overlapping blobs covered the window. The damage grows linearly with the blob count.

What was done?

This change touches only the client-side verifier. Honest proofs never trigger the new refusals, so there is no GroveVersion gate.

  1. Exact chunk count, checked without per-entry allocation. The new completed_chunk_entries(blob, expected_count, wanted) refuses a completed-chunk blob whose entry count is not exactly 2^height. For the fixed format it reads the header count. For the variable format it walks the length prefixes once, through a walker shared with the general decoder. It copies only the wanted entries, so touching one position of a chunk costs one entry. deserialize_completed_chunk_blob is the all-entries wrapper. Compaction always writes exactly 2^height entries, so honest chunks pass, including chunks of zero-sized entries, which are appendable.
  2. Completeness first, then limit-aware extraction by interval.
    • BulkAppendTreeProofResult::proved_position_spans() returns the positions the proof's chunk indices and buffer entries cover, without decoding anything. It also refuses chunk indices that repeat, descend or reach buffer_start, and buffer entries that repeat or pass total_count. The GroveDB glue checks completeness against these spans (PositionIntervals::missing_from_spans) before extracting, so an incomplete forged proof is refused in O(intervals + carried chunks).
    • values_in_ranges_directed(ranges, left_to_right, limit) then walks the query's exact intervals in its direction and stops at the row limit. It skips untouched chunks and decodes only the entries it returns. values_in_range, values_in_ranges and the standalone verify_against_query all use it.
    • The GroveDB glue passes the query's exact PositionIntervals instead of a bounding span, which removes extract_range_from_query_items.
    • Without these two steps the exact count alone still let a broad query over a forged total_count extract 2^height values per carried chunk: a 1 KB proof of 64 honest-shaped zero-size chunks at height 16 gave about 4M values. A bad chunk blob in a proof result is now reported as InvalidProof, in the crate and in GroveDB alike.
  3. Bind before reporting. verify_bulk_append_lower_layer is split into decode_bulk_append_lower_layer and report_bulk_append_entries.
    • Each dispatch arm now binds its root to the parent row's committed value hash exactly once, through ParentCommitment.
    • The BulkAppendTree and CommitmentTree layers bind before decoding any chunk blob. For CommitmentTree the child hash is ct_state(sinsemilla, bulk_root). This matches what verify_against_query already did.
    • The MMR and dense layers bind before reporting any row. Their leaves and entries are decoded while computing the root, which the proof bytes bound.
    • The V1 trunk verifier shares ParentCommitment::check and keeps its own error text. V0 is untouched.
    • This is defense in depth. A forger can always make the parent row consistent with a forged lower root, since that row is authenticated only by the caller's final root check. Items 1 and 2 provide the resource bound, and the ParentCommitment doc says so.
  4. Comments. The misleading [audit][P05] Broad non-Merk proof checks allocate heavily before parent metadata is authenticated #856 comment is corrected. MMR and dense extraction are genuinely bounded by proof bytes: MMR leaves are canonical and one per index, and dense entries reject duplicate and out-of-range positions. Their comments now say why.

How Has This Been Tested?

  • Crate-boundary tests, in grovedb-bulk-append-tree/tests/forged_chunk_blob_resource_bound.rs, using only the public API:
    • The audit reproductions now assert refusal: T1 (a 9-byte blob), T2 (a single forged leaf) and T3 (64 leaves over an 8192-position window).
    • verify_against_query refuses the same blob.
    • An 8192-row window over 256 height-16 chunks extracts from the two honest chunks it touches. The other 254 are garbage, which proves they are never decoded.
  • Unit tests in chunk.rs, proof/tests.rs and position_intervals.rs cover:
    • exact-count decoding of both formats, with too many and too few entries, truncation, and copying only the wanted entries;
    • disjoint, unsorted and overlapping ranges, and extraction in both directions under a limit;
    • a limit met inside one chunk never decoding the garbage chunks beyond it;
    • proved_position_spans without decoding, and malformed result shapes;
    • missing_from_spans matching missing_from exactly.
  • GroveDB envelope tests in grovedb/src/tests/non_merk_completeness_budget_tests.rs:
    • forged_chunk_blob_counts_refused_before_expansion covers BulkAppendTree and CommitmentTree with a complete forged proof. On develop a forged proof of this shape was accepted with duplicate rows: 520,256 at total_count = 8192.
    • incomplete_forged_chunks_refused_before_decoding refuses by completeness before touching the carried garbage chunks.
    • limited_query_decodes_only_what_it_reports runs a RangeFull query with limit 2, from either end, over 64 chunks of which 63 are garbage.
    • lower_layer_root_bound_before_rows_are_extracted covers bulk, CommitmentTree and MMR. It fails on develop, where the completeness error fires before the binding.
    • element_only_subset_query_binds_mmr_and_dense_lower_layers covers the root-only bind of MMR and dense layers.
    • honest_bulk_queries_across_chunks_and_buffer passes on both. It covers disjoint keys and ranges across chunks and the buffer, both directions, a limit, and both variable-format and zero-size chunks.
  • Existing suites (merged with current develop):
    • cargo test -p grovedb-bulk-append-tree and cargo test -p grovedb --lib pass: 134 unit and 5 integration tests in the crate, and 3,501 passed, 0 failed, 8 ignored in GroveDB, including the slow CommitmentTree cost tests.
    • cargo clippy -p grovedb -p grovedb-bulk-append-tree -- -D warnings, cargo fmt --all -- --check and cargo check -p grovedb --no-default-features --features verify are clean.

Breaking Changes

None. Honest proofs verify as before. The refusals, and errors that now come from the binding or the completeness check rather than a later step, only affect forged proofs. values_in_range keeps its signature. It now reports a bad chunk blob as InvalidProof rather than CorruptedData, because a proof result is proof input. values_in_ranges, values_in_ranges_directed, proved_position_spans, completed_chunk_entries and deserialize_completed_chunk_blob are new public items.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

The V1 BulkAppendTree / CommitmentTree lower-layer verifier took
total_count and height from element bytes carried in the proof, computed
an unbound root, and extracted values before its caller compared the root
with a trusted one. A 9-byte fixed-format chunk blob can declare 2^20
zero-sized entries, and under a forged height of 1 every blob's positions
overlapped the query window, so a proof of a few KB made the verifier
decode 2^20 entries per blob and return one row per blob per queried
position (520,256 duplicate rows for 64 blobs over an 8192-position
window). Incomplete fix of #856 (#872).

- deserialize_completed_chunk_blob refuses a completed-chunk blob whose
  entry count is not exactly 2^height, before allocating. A resource
  bound, not the soundness length check rejected in #688 / #729.
- BulkAppendTreeProofResult::values_in_ranges extracts only the query's
  position intervals, skips chunks no interval touches without decoding
  them, and refuses repeated, descending or buffer-reaching chunk indices.
  The GroveDB glue passes the exact PositionIntervals instead of a
  bounding span.
- The MMR, dense, bulk and commitment-tree lower layers bind their
  computed root to the parent row's value hash before extracting rows.

Client-side verifier only; honest proofs never trigger the refusals, so
no GroveVersion gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit 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.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 1d1456aa-c4f2-4564-aba7-9bbfa77da837

📥 Commits

Reviewing files that changed from the base of the PR and between 2d92ac4 and d8d9661.

📒 Files selected for processing (10)
  • CHANGELOG.md
  • grovedb-bulk-append-tree/src/chunk.rs
  • grovedb-bulk-append-tree/src/lib.rs
  • grovedb-bulk-append-tree/src/proof/mod.rs
  • grovedb-bulk-append-tree/src/proof/tests.rs
  • grovedb-bulk-append-tree/tests/forged_chunk_blob_resource_bound.rs
  • grovedb/src/operations/proof/position_intervals.rs
  • grovedb/src/operations/proof/verify.rs
  • grovedb/src/tests/non_merk_completeness_budget_tests.rs
  • grovedb/src/tests/proof_coverage_tests.rs

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 23, 2026
@thepastaclaw

thepastaclaw commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

🕓 Queued for automated review — 5th in line, estimated start in ~1.4 h (commit 46d76bf)
Estimated review time once started: ~35 min (two-phase automated review; median of recent runs).

  • Request priority review — click to move this review to the front of the queue.

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.16995% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.04%. Comparing base (2d92ac4) to head (d8d9661).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
grovedb/src/operations/proof/verify.rs 88.43% 17 Missing ⚠️
grovedb-bulk-append-tree/src/chunk.rs 94.79% 9 Missing ⚠️
grovedb-bulk-append-tree/src/proof/mod.rs 99.48% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #990      +/-   ##
===========================================
+ Coverage    92.95%   93.04%   +0.08%     
===========================================
  Files          331      331              
  Lines       104500   104804     +304     
===========================================
+ Hits         97138    97513     +375     
+ Misses        7362     7291      -71     
Components Coverage Δ
grovedb-core 91.36% <91.09%> (+0.13%) ⬆️
merk 93.99% <ø> (+0.01%) ⬆️
storage 91.94% <ø> (ø)
commitment-tree 95.62% <ø> (ø)
mmr 95.43% <ø> (ø)
bulk-append-tree 93.72% <97.28%> (+0.94%) ⬆️
element 97.18% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

QuantumExplorer and others added 3 commits September 24, 2026 01:42
…action

Code-review follow-up. The exact chunk count still let a broad query over
a forged total_count extract 2^height values per carried chunk (a 1 KB
proof gave about 4M values) before completeness or the row limit applied.

- Completeness is checked first, on the spans the proof's chunk indices
  and buffer entries cover (proved_position_spans, missing_from_spans),
  without decoding anything.
- values_in_ranges_directed walks the query's intervals in its direction,
  stops at the row limit, and copies only the entries it returns
  (completed_chunk_entries). Variable-format blobs are parsed once, via a
  walker shared with the general decoder.
- A bad chunk blob in a proof result is reported as InvalidProof.
- Each lower-layer arm binds its root to the parent exactly once; the
  MMR, dense, bulk and commitment-tree verifiers bind before reporting
  any row. The V1 trunk verifier shares ParentCommitment::check.
- Tests: incomplete forged chunks refused before decoding, limited
  queries decode only what they report, deterministic signals instead of
  1 s timing asserts, shared blob and CT-root helpers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pend-verify-resource-bound

# Conflicts:
#	CHANGELOG.md
A subset query that stops at an MMR or dense tree consumes the lower
layer for its root alone. Pin that the honest element verifies there and
that a lower root the parent row does not commit to is refused; codecov
flagged the dense root-only bind as uncovered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@QuantumExplorer
QuantumExplorer merged commit b479ad1 into develop Sep 23, 2026
11 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/bulk-append-verify-resource-bound branch September 23, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-bots Waiting for the review bots to report on this head

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants