Skip to content

Bound Candid reply decoding with a quota (ICPBB-438) - #132

Merged
aterga merged 1 commit into
mainfrom
security/candid-reply-decoding-quota
Aug 18, 2026
Merged

aterga merged 1 commit into
mainfrom
security/candid-reply-decoding-quota

Conversation

@aterga

@aterga aterga commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Problem (ICPBB-438)

Every canister-reply decode in src/calls.rs used candid's default DecoderConfig (decoding_quota: None), so nothing bounded the number of decoded elements. A zero-byte element type makes this a ~2M× amplifier:

DIDL 01 6d 7f 01 00 <leb128 count>   # vec null, one arg, attacker-chosen count

A ~13-byte reply can declare tens of millions of null elements (and far more via nesting). Decoding it drives a multi-gigabyte allocation → handle_alloc_error/OOM abort, which drops every concurrent user's in-memory session/token/grant. It's uncatchable: on_deep_stack recovers panics (resume_unwind), and its CWE-674 depth guard doesn't apply because the reply is depth-1. Any authenticated user with a canister they control can hold the service down with one 13-byte reply per restart. It's the element-count axis of the same class #37/#82/#87/#108 already hardened along the depth axis.

grep -rn "DecoderConfig|decoding_quota|skipping_quota" src/ → no matches, confirming the gap.

Fix

A shared reply_decoder_config() builds a candid::DecoderConfig with a decoding and skipping quota, applied at all five decode sites: decode_reply (type-less), the typed decode_bytes_with_did, parse_execute_reply, decode_text_reply, and from_bytes_with_types.

  • Absolute bound. candid charges the quota per decoded value, so nesting or padding the wire can't raise it. An over-budget reply returns the existing "reply is not decodable" error instead of aborting.
  • Verified it fires before allocating. null isn't an exact primitive, so vec null takes the generic seq path, where Compound::next_element_seed charges add_cost(3) per element before decoding it and there's no Vec::with_capacity(len) pre-sizing — so the quota trips after ~quota/3 elements, bounding a rejected bomb's transient allocation to ~tens of MB.
  • Quota (REPLY_DECODING_QUOTA = 3_000_000) leaves realistic replies untouched (a reply with >~1M decoded values would render to unusable megabytes of text anyway); tunable.

Test

decode_reply_rejects_a_vec_null_bomb_instead_of_aborting builds the 13-byte vec null × 40M bomb and asserts the type-less decode returns the error path rather than aborting. Full lib suite green (176 tests); no new clippy warnings.

Note

This is the fix #37/#82/#87/#108 would have made along this axis; those were proactive in-house hardening of the same "untrusted canister reply aborts the process" class.

…ICPBB-438)

Every canister-reply decode used candid's default `DecoderConfig` (no
`decoding_quota`), so nothing bounded the number of decoded elements. A
zero-byte element type lets a ~13-byte reply declare tens of millions of
elements (`vec null` × 4e7, and far more via nesting); decoding it drives a
multi-gigabyte allocation and an UNCATCHABLE `handle_alloc_error`/OOM abort
that drops every concurrent user's session. `on_deep_stack` recovers panics,
not aborts, so its CWE-674 depth guard does not catch this element-count axis
(the reply is depth-1).

Route all five reply-decode sites (`decode_reply`, the typed
`decode_bytes_with_did`, `parse_execute_reply`, `decode_text_reply`, and
`from_bytes_with_types`) through a shared `DecoderConfig` carrying a decoding
and skipping quota. candid charges the quota per decoded value, so the bound
is absolute (nesting/padding can't raise it) and an over-budget reply returns
the ordinary "reply is not decodable" error instead of aborting. The quota is
sized so a rejected bomb's transient allocation stays in the tens of MB while
realistic replies are untouched. Completes the untrusted-reply hardening line
of #37/#82/#87/#108 (which bounded the depth axis) along the element-count axis.

Co-Authored-By: Claude Opus 5 <[email protected]>
@aterga
aterga requested review from a team and a balanced review from Copilot August 18, 2026 10:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Candid decoding quotas to prevent allocation-amplification attacks from malicious canister replies.

Changes:

  • Introduces shared decoding and skipping quotas.
  • Applies quotas across typed and type-less reply decoders.
  • Adds a regression test for compact vec null allocation bombs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@aterga
aterga merged commit a0512ba into main Aug 18, 2026
8 checks passed
aterga added a commit that referenced this pull request Aug 18, 2026
Second review round on rows_to_table (r3803651957): the width cap only
ran on the first row, but every row still copied ALL of its cells into an
intermediate `Vec<(String, String)>` (rendering each value) before
alignment. A reply with a narrow first row and a later row carrying tens
of thousands of junk cells could therefore still force a large transient
allocation — bounded only implicitly by the #132 decode quota, a
cross-module invariant that is easy to break.

Make the bound explicit and local. The first row establishes the distinct
column set (refused if it exceeds MAX_OQL_COLUMNS, detected with a plain
counter so no full-width Vec is built). Every row — including later,
attacker-widened ones — is then aligned by rendering ONLY the cells whose
name is a known column, via a `col_pos` lookup. Intermediate allocation is
now O(columns) per row regardless of how many junk cells a row carries;
unknown cells cost a hashmap lookup and allocate nothing.

Also adds exact-boundary tests (256 accepted / 257 refused columns; 1000
materialized / 1001 truncated rows) and a wide-later-row test that asserts
a 5,000-junk-cell second row aligns to the 1-column set without widening.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
aterga added a commit that referenced this pull request Aug 18, 2026
…85) (#136)

* Cap OQL table densification to bound reply-driven memory (ICPBB-384/385)

`rows_to_table` builds a dense `Vec<Vec<String>>` sized `columns × rows`,
where the column set is taken from the first row's cell names and every
subsequent row is aligned to it. Both dimensions came straight from an
attacker-controllable canister reply with no cap: a compact reply whose
first row declares tens of thousands of columns and that then carries
thousands of (even empty) rows densifies to hundreds of MB of owned
`String`s out of an ~80 KB wire payload — a memory-amplification OOM.

The decode quota added in #132 does not catch this: the decoded Candid
tree stays small; the blow-up is in our own alignment loop, not candid's
decoder.

Cap the materialized table at MAX_OQL_COLUMNS (256) × MAX_OQL_ROWS
(1000). Excess first-row columns are dropped and excess rows are not
materialized; either case sets a `truncated` flag that `extract_oql`
folds into `has_more`, so the agent sees the page is incomplete and can
page with offset rather than treating a truncated table as the full
result set. Real OQL pages are far smaller than these bounds.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* Refuse over-wide OQL tables instead of conflating them with has_more

Addresses the PR review: the first cut folded BOTH row and column
truncation into `has_more`. But `has_more` contractually means "more rows
remain — page with a higher `offset`" (calls.rs:294), and the dropped
columns are chosen by the query's `select`, not by `offset`. So a final
page wider than the column cap would report `has_more = true` at every
offset — trapping the agent in an infinite paging loop while still
silently losing the extra columns.

Separate the two truncation reasons:

  * Row truncation (> MAX_OQL_ROWS) stays pageable and still raises
    `has_more` — a higher `offset` genuinely returns those rows.
  * A first row wider than MAX_OQL_COLUMNS is now REFUSED
    (`OqlResult::TooManyColumns`), and the tool tells the agent to narrow
    its `select` rather than to page. Refusing at the first row also
    bounds the dense `cols × rows` allocation before any row is
    materialized, so the ICPBB-384/385 memory cap is unchanged.

`rows_to_table` returns a `TableOutcome` that carries the two reasons
apart; `extract_oql` maps them to the right `OqlResult`. Tests split into
refuse-wide and page-tall cases, each asserting the internal outcome and
the public mapping (a wide reply must NOT become a has_more table; a tall
reply must raise has_more even over the canister's own hasMore = false).
The rewrite also drops the earlier fixture whose row count was off by one.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* Bound per-row OQL alignment allocation, not just the final table

Second review round on rows_to_table (r3803651957): the width cap only
ran on the first row, but every row still copied ALL of its cells into an
intermediate `Vec<(String, String)>` (rendering each value) before
alignment. A reply with a narrow first row and a later row carrying tens
of thousands of junk cells could therefore still force a large transient
allocation — bounded only implicitly by the #132 decode quota, a
cross-module invariant that is easy to break.

Make the bound explicit and local. The first row establishes the distinct
column set (refused if it exceeds MAX_OQL_COLUMNS, detected with a plain
counter so no full-width Vec is built). Every row — including later,
attacker-widened ones — is then aligned by rendering ONLY the cells whose
name is a known column, via a `col_pos` lookup. Intermediate allocation is
now O(columns) per row regardless of how many junk cells a row carries;
unknown cells cost a hashmap lookup and allocate nothing.

Also adds exact-boundary tests (256 accepted / 257 refused columns; 1000
materialized / 1001 truncated rows) and a wide-later-row test that asserts
a 5,000-junk-cell second row aligns to the 1-column set without widening.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants