Skip to content

calls: bound type-less reply decode/render against deep nesting (CWE-674) - #108

Merged
aterga merged 1 commit into
mainfrom
claude/bound-reply-decode-recursion
Jul 30, 2026
Merged

aterga merged 1 commit into
mainfrom
claude/bound-reply-decode-recursion

Conversation

@aterga

@aterga aterga commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes a MEDIUM stack-exhaustion DoS (CWE-674): the type-less reply-decode paths decoded and rendered attacker-controlled Candid reply bytes on the ordinary worker thread (~2 MiB stack) with no depth guard, unlike the DID-typed path which already runs its decode + Display inside on_deep_stack (a 64 MiB stack).

The MCP client freely picks the target canister (tools.rs validates only that it's a well-formed Principal, no allowlist), so the reply bytes are fully attacker-controlled. candid's decoder, IDLArgs/IDLValue Display, the OQL tree walk (extract_oql/cell_scalar), and the decoded tree's recursive Drop all recurse once per nesting level with no bound. A recursive opt chain (type t = opt t → a few KB of opt tags on the wire) decodes to a tree deep enough to overflow the worker stack — an uncatchable process abort that drops every concurrent session.

Fix

Run the whole decode → walk → render → drop of each type-less path on the guarded deep stack, exactly as the DID path does, so the deep tree is created, walked, rendered, and dropped there and only the finished String/OqlResult crosses back:

  • decode_reply (calls.rs:702): wrap the type-less fallback. The DID path's on_deep_stack has already returned, so this does not nest.
  • decode_text_reply: wrap the whole decode + match + render.
  • parse_execute_reply: wrap the whole body (DID or type-less decode, then extract_oql/to_string) in one on_deep_stack, and make decode_args_with_did decode in place (it no longer takes its own on_deep_stack, so there's no nesting — the sole caller now provides the deep stack). This also moves the previously-unguarded extract_oql/cell_scalar walk and the IDLArgs Drop onto the deep stack.
  • management::format_status: guard the canister_status raw-Candid fallback too. Defense in depth — that reply is the management canister's own bounded record, not the target's arbitrary output, but no type-less reply decode should run unbounded on the worker stack.

Notes:

  • Drop matters: a deeply-nested IDLValue recurses on Drop, so the value must never cross back to the shallow stack. Each path now creates and drops the tree entirely inside on_deep_stack (the existing DID path already did this by returning only the String).
  • No nesting: on_deep_stack holds a bounded parse permit and must not nest; verified decode_args_with_did no longer calls it.
  • on_deep_stack returns None only if the parse thread can't be spawned; each caller then degrades to an explanatory string, never a parse on the small stack.

Testing

  • cargo test — all pass (151 + 8 + 1, 0 failed).
  • New regression test typeless_reply_paths_bound_deep_nesting: builds a deeply-nested opt reply and asserts decode_reply, decode_text_reply, and parse_execute_reply all render it on the deep stack. Depth is kept modest (candid's decode/Display is superlinear in depth, so CI stays fast); it exercises the guarded path end-to-end rather than trying to trigger an abort (which is uncatchable and can't be asserted on).

Checklist

  • Docs (comments) updated: each guarded site explains why the deep stack is required.
  • No secrets, credentials, or internal-only information are included.

🤖 Generated with Claude Code


Generated by Claude Code

…674)

The type-less reply paths decoded and rendered attacker-controlled Candid
reply bytes on the ordinary worker thread (~2 MiB stack) with no depth guard,
unlike the DID-typed path which already runs its decode + Display inside
on_deep_stack (a 64 MiB stack). candid's decoder, IDLArgs/IDLValue Display,
the OQL tree walk (extract_oql/cell_scalar), AND the decoded tree's recursive
Drop all recurse once per nesting level with no bound. The MCP client freely
picks the target canister (validated only as a well-formed Principal), so a
reply that is a recursive `opt` chain (`type t = opt t`: a few KB of `opt`
tags on the wire) decodes to a tree deep enough to overflow the worker stack,
an uncatchable process abort that drops every concurrent session.

Fix: run the whole decode-render-drop of each type-less path on the guarded
deep stack, exactly as the DID path does, so the deep tree is created, walked,
rendered, and dropped there and only the finished String/OqlResult crosses
back:

- decode_reply: wrap the type-less fallback (from_bytes + to_string). The DID
  path's on_deep_stack has already returned, so this does not nest.
- decode_text_reply: wrap the whole decode + match + render.
- parse_execute_reply: wrap the whole body (decode via DID or type-less
  fallback, then extract_oql / to_string) in one on_deep_stack, and make
  decode_args_with_did decode in place (it no longer takes its own
  on_deep_stack, so there is no nesting; its sole caller now provides the deep
  stack). This also moves the previously-unguarded extract_oql/cell_scalar walk
  and the IDLArgs Drop onto the deep stack.
- management::format_status: guard the canister_status raw-Candid fallback the
  same way. Defense in depth: that reply is the management canister's own
  bounded record, not the target's arbitrary output, but no type-less reply
  decode should run unbounded on the worker stack.

on_deep_stack returns None only if the parse thread can't be spawned; each
caller then degrades to an explanatory string, never a parse on the small
stack. Adds a regression test that a deeply-nested type-less reply decodes on
the deep stack across all three calls.rs paths.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym

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

This PR mitigates a stack-exhaustion DoS risk (CWE-674) by ensuring type-less Candid reply decoding (including subsequent tree walk, rendering, and recursive Drop) runs on the existing guarded “deep stack” thread rather than on the ~2 MiB worker stack, aligning behavior with the already-guarded DID-typed decode path.

Changes:

  • Wrap type-less reply decode/render paths (decode_reply, decode_text_reply, and parse_execute_reply) in on_deep_stack so deep recursion can’t abort the process.
  • Refactor decode_args_with_did to decode “in place” (no internal on_deep_stack) to avoid nested deep-stack usage and permit exhaustion.
  • Add a regression test covering deeply-nested typeless replies, and apply the same defense-in-depth guard to management canister_status raw-Candid fallback rendering.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/calls.rs Moves typeless decode + OQL walk + render + drop onto on_deep_stack, removes nested guarding from decode_args_with_did, and adds a regression test for deep nesting.
src/management.rs Guards the raw-Candid canister_status fallback decode/render with on_deep_stack for defense in depth.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@aterga
aterga marked this pull request as ready for review July 30, 2026 14:21
@aterga
aterga requested review from a team and sea-snake July 30, 2026 14:21
@aterga
aterga merged commit 70a450b into main Jul 30, 2026
7 checks passed
aterga pushed a commit that referenced this pull request Aug 3, 2026
Post-audit merges (#87-#115) that changed user-visible or documented
behavior, folded into the README:

- II's consent screen requires an explicit access-level choice (Questions
  only vs Actions & questions); it is not a read-only-by-default opt-out.
- Name the connect binding cookie correctly: mcp_connect, not sid, with
  Secure conditional on an https PUBLIC_URL.
- Document the official pages (/privacy-policy, /support, /terms) and the
  OpenAI domain-verification endpoint with its env-gated 404 behavior
  (#109, #112, #115).
- /version now reports the served II pairings via the instances array (#94).
- CWE-674: note the post-parse depth bounds on interface alias chains (#87)
  and the type-less reply decode/render path (#108).
- Companion-control note: percent-encoded slashes/dots in redirect paths are
  rejected (#107).
- Deploy: describe the reference native deployment pipeline (staging tracks
  main, release-* tags ship production with the binary attached) (#81, #93).

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TGhwpdTbUqQbR5AiiN8Ljw
aterga added a commit that referenced this pull request Aug 18, 2026
…ICPBB-438) (#132)

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

4 participants