Skip to content

fix: No recursion depth limit in decode_internal / resolve_internal / serde deserializer - #642

Merged
martin-g merged 4 commits into
bound-allocated-bytes-for-fixed-schema-sizefrom
recursion-depth-check-decoding-deserializing
Aug 26, 2026
Merged

martin-g merged 4 commits into
bound-allocated-bytes-for-fixed-schema-sizefrom
recursion-depth-check-decoding-deserializing

Conversation

@martin-g

Copy link
Copy Markdown
Member

A recursive schema can lead to stack overflow (i.e. an abort) in OCF decoding, Value resolving and Schema deserialization. Add util::max_decode_recursion_depth(usize) to be able to control the recursipn depth

Reported-by: Security scans

@martin-g
martin-g requested a review from Kriskras99 August 25, 2026 12:48
@martin-g
martin-g force-pushed the recursion-depth-check-decoding-deserializing branch from 658b8a3 to 0fe84cd Compare August 25, 2026 12:50
Comment thread avro/src/util.rs
/// data: a recursive schema lets an attacker drive one recursion level with roughly one wire byte.
///
/// See [`max_decode_recursion_depth`] to change this limit.
pub const DEFAULT_MAX_DECODE_RECURSION_DEPTH: usize = 32;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At 62 the decode::tests::avro_rs_642_resolve_recursion_depth_is_bounded test overflows.
At 558(!) the recursion_depth_deser::avro_rs_642_recursion_depth_is_bounded test overflows.
So I think there might be some optimisation room left for the decode implementation (not for this PR obviously).

Comment thread avro/src/util.rs Resolved
Comment thread avro/src/serde/deser_schema/mod.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should also increment the recursion depth as it does not use new

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!
Thanks for catching this!

Comment thread avro/src/serde/deser_schema/mod.rs Resolved
Comment thread avro/src/serde/deser_schema/mod.rs Outdated
Comment on lines +102 to +106
config.recursion_depth += 1;
let maximum = decode_recursion_limit();
if config.recursion_depth > maximum {
return Err(Details::DecodeRecursionLimit { maximum }.into());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be better to split this off into an implementation on Config, otherwise this would need to be duplicated in with_different_schema

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread avro/src/serde/deser_schema/mod.rs Resolved
Comment thread avro/tests/recursion_depth_deser.rs Resolved
@martin-g
martin-g force-pushed the recursion-depth-check-decoding-deserializing branch from 0fe84cd to a965dd4 Compare August 26, 2026 09:55
martin-g and others added 4 commits August 26, 2026 14:13
… serde deserializer

A recursive schema can lead to stack overflow (i.e. an abort) in OCF
decoding, Value resolving and Schema deserialization.
Add `util::max_decode_recursion_depth(usize)` to be able to control the
recursipn depth

Reported-by: Security scans
Co-authored-by: Kriskras99 <[email protected]>
@martin-g
martin-g force-pushed the recursion-depth-check-decoding-deserializing branch from 7d282f0 to 48d1fbc Compare August 26, 2026 11:13
@martin-g
martin-g merged commit a439ee7 into main Aug 26, 2026
16 checks passed
@martin-g
martin-g deleted the recursion-depth-check-decoding-deserializing branch August 26, 2026 15:40
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.

2 participants