Repository navigation
Guard Value::deserialize against unbounded recursion from non-JSON-text Deserializers - #1350
Open
choudhryfrompak wants to merge 2 commits into
Open
choudhryfrompak wants to merge 2 commits into
choudhryfrompak wants to merge 2 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.