Skip to content

Limit default CBOR recursion depth to 16 - #133

Open
manjarisrikant wants to merge 1 commit into
google:mainfrom
manjarisrikant:limit-cbor-recursion-depth
Open

manjarisrikant wants to merge 1 commit into
google:mainfrom
manjarisrikant:limit-cbor-recursion-depth

Conversation

@manjarisrikant

Copy link
Copy Markdown

Decode CBOR slices with DEFAULT_CBOR_RECURSION_LIMIT (16) instead of ciborium's default recursion limit of 256, since valid COSE objects only require shallow CBOR nesting.

Also add CborSerializable::from_slice_with_recursion_limit and TaggedCborSerializable::from_tagged_slice_with_recursion_limit so callers with deeper or tighter nesting requirements can specify a custom recursion limit.

Decode CBOR slices with `DEFAULT_CBOR_RECURSION_LIMIT` (16) instead of
`ciborium`'s default recursion limit of 256, since valid COSE objects
only require shallow CBOR nesting.

Also add `CborSerializable::from_slice_with_recursion_limit` and
`TaggedCborSerializable::from_tagged_slice_with_recursion_limit` so
callers with deeper or tighter nesting requirements can specify a custom
recursion limit.
@aliciawyy

Copy link
Copy Markdown

Hi @manjarisrikant , could you share more context about the motivation of this change here since many reviewers on Github has access to buganizer. Thanks, Alice

@daviddrysdale

Copy link
Copy Markdown
Contributor

This probably helps in low-stack environments, cf. https://android-review.googlesource.com/c/platform/system/keymint/+/2609980 ?

@manjarisrikant

manjarisrikant commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

Thanks @aliciawyy and @daviddrysdale!

Yes, @daviddrysdale — it is for the same reason: protecting both stack and fixed-size heap budgets in constrained #![no_std] environments.

With ciborium::de::from_reader's default limit of 256, a small untrusted CBOR payload of nested container/tag headers can force up to 256 levels of recursive deserialize stack frames and nested Vec<Value> / Box<Value> heap allocations inside read_to_value before from_cbor_value validates the COSE structure, exhausting tight stack or fixed-heap limits.

Setting DEFAULT_CBOR_RECURSION_LIMIT = 16 comfortably covers standard COSE / CWT structures (typically 2–6 levels), while from_slice_with_recursion_limit and from_tagged_slice_with_recursion_limit allow callers with custom nesting requirements to override it.

(Internal Buganizer context: http://b/511320984)

This branch has not been deployed

No deployments
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