Skip to content

Guard Value::deserialize against unbounded recursion from non-JSON-text Deserializers - #1350

Open
choudhryfrompak wants to merge 2 commits into
serde-rs:masterfrom
choudhryfrompak:fix/value-deserialize-recursion-guard
Open

choudhryfrompak wants to merge 2 commits into
serde-rs:masterfrom
choudhryfrompak:fix/value-deserialize-recursion-guard

Conversation

@choudhryfrompak

Copy link
Copy Markdown

from_str/from_slice/from_reader into Value are depth-limited by check_recursion! in src/de.rs (128 levels). But Value also has a normal Deserialize impl (ValueVisitor::visit_seq/visit_map in src/value/de.rs), and that impl has no depth limit of its own — it just loops next_element()/next_entry(). So if something other than serde_json's own Deserializer drives it — bincode, rmp-serde, ciborium, postcard, a hand-rolled format, anything implementing serde::Deserializer — a deeply nested input recurses without bound and overflows the stack instead of returning an error.

I reproduced this with a small hand-rolled Deserializer (no JSON text involved at all) that generates nested single-element sequences on the fly rather than allocating them upfront, so the crash can only come from recursive calls, not from building a huge value first. 2,000,000 levels into Value::deserialize aborts the process on current master (exit 134, stack overflow). The same depth through from_str:: returns a clean "recursion limit exceeded" error, as it should.

This adds a thread-local recursion-depth guard to Value::deserialize, same 128-deep limit and the same "recursion limit exceeded" message as check_recursion!. It only compiles under the std feature (needs thread_local!), so the alloc-only/no_std configuration is unchanged.

One thing I want to flag directly, since I found it while checking for prior discussion: issue #1327 reported a stack overflow comparing two already-built Values with == at extreme depth, and was closed with "the vulnerability would be in whatever constructs nested data structures to an attacker-controlled depth, not in serde_json's data structure definition or equality operator." I don't think this is the same situation. #1327 is about a Value built by someone else's code, compared by serde_json's Eq impl after the fact. This is about serde_json's own Deserialize impl for Value — the exact same ValueVisitor that from_str already uses — recursing without a limit when it's driven by a different Deserializer. The recursion is happening in serde_json's own code; the thing missing is the same bound from_str already gets by construction. I also checked #1262 (exposing a setter for remaining_depth); it's about configuring from_str's own cap and doesn't touch this gap.

To avoid double-counting recursion when Value::deserialize is driven by serde_json's own Deserializer (which would make from_str hit the limit one level sooner than before, shifting the error's line/column), check_recursion! now also marks a thread-local flag for the duration of its own guarded call, and Value::deserialize's guard defers to that flag when it's set. unbounded_depth + disable_recursion_limit() still disables the cap end to end, since that flag is set unconditionally regardless of whether remaining_depth is actively being checked.

tests/regression/value_deserialize_recursion_limit.rs adds a regression test. Actually triggering the old stack overflow in-process would take the whole test binary down with it, so the test re-execs itself with an env var set and does the deep deserialization in that disposable child process, then only asserts the child exited cleanly.

cargo fmt --check and cargo clippy --all-features --all-targets are clean. cargo test, with default features and with --all-features, passes with the same counts as master (including test_stack_overflow and test_disable_recursion_limit unmodified) plus the new test. cargo build --no-default-features --features alloc still builds.

…kends

serde_json's own JSON-text parser bounds recursion at 128 levels via
check_recursion! in de.rs. That guard only runs on serde_json's own
Deserializer, though: Deserialize for Value (ValueVisitor::visit_seq/
visit_map) has no limit of its own, so any other serde Deserializer
(bincode, rmp-serde, ciborium, postcard, a hand-rolled format, ...)
feeding a deeply nested value into Value::deserialize overflows the
stack and aborts the process instead of returning an error. Confirmed
with a 2,000,000-deep hand-rolled Deserializer: exit 134 before this
change, a clean "recursion limit exceeded" Err after.

Add a thread-local recursion-depth guard to Value::deserialize that
mirrors check_recursion!'s 128-deep limit and error message. It
defers to check_recursion! when that's already tracking the same
recursion (from_str/from_slice/from_reader into Value), so the
existing error line/column for over-deep JSON text is unchanged, and
unbounded_depth + disable_recursion_limit() still disables the cap
end to end. Only compiled under the std feature, since it needs
thread_local!; the alloc-only (no_std) build is untouched.
It spawns a real child process via Command::new(env::current_exe()),
which needs std::env::current_exe() -> readlink, unsupported under
Miri's isolation. Same reasoning as test_deserialize_from_stream in
tests/test.rs, which skips for the same kind of real-OS dependency.

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

Development

Successfully merging this pull request may close these issues.

1 participant