Repository navigation
Conversation
cJSON_Compare() compared object subtrees twice, once in each direction, recursing fully in both passes. The second pass existed only to reject the case where a is a subset of b, but it re-ran cJSON_Compare() on every shared child. For structurally equal, deeply nested objects this doubles the work at every level, so the running time is exponential in the nesting depth: a small (few-hundred-byte) document nested ~40 levels can consume hours of CPU. An application that calls cJSON_Compare() on attacker-influenced JSON that is structurally equal to a reference document is exposed to a denial of service. The forward pass already deep-compares every key of a against its counterpart in b. The reverse pass therefore only needs to confirm that b has no keys absent from a - a presence check, not a recursive comparison. Replace the second cJSON_Compare() call with a get_object_item() lookup so each shared subtree is compared exactly once. This removes the per-level doubling and makes the runtime linear in the tree size (the pre-existing O(n^2) key lookup is unchanged). Semantics are preserved for objects without duplicate keys: unequal values are still caught by the forward pass, and extra keys in b are still rejected. With duplicate keys, cJSON_Compare already matches only the first occurrence on lookup, so results there were not well defined before this change either. Signed-off-by: Ann Leelu George <annleelu.george@windriver.com>
…26-67216) Add cjson_compare_should_not_be_exponential_on_deep_objects, which builds 1000-level deep objects and compares them. Before the fix cJSON_Compare() recursed into every shared subtree twice per level, so this comparison would not finish in any practical time; with the fix each subtree is visited once and it returns immediately, so the test completing is itself evidence the fix is present. The test also checks that a differing deep leaf and a deep subset are still reported as not equal, confirming the fix preserves correctness. Signed-off-by: Ann Leelu George <annleelu.george@windriver.com>
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.
cJSON_Compare()compares object members twice, once in each direction, andrecurses fully in both passes. For structurally equal nested objects this
doubles the work at every level, so the running time is exponential in the
nesting depth. A few-hundred-byte document nested about 40 levels deep can take
hours of CPU. This is CVE-2026-67216 (CWE-407).
The forward pass already deep-compares every key of
aagainst its match inb. The reverse pass only needs to confirm thatbhas no keys missing froma, so this change replaces the second recursivecJSON_Compare()call with aget_object_item()presence check. Each shared subtree is now compared once.still caught by the forward pass, and extra keys in
bare still rejected.results there were not well defined before this change either.
Tests
Adds
cjson_compare_should_not_be_exponential_on_deep_objectstotests/compare_tests.c. It compares two 1000-level nested objects, and checksthat a differing deep leaf and a deep subset still compare as not equal.
On master (6d9f244) with this branch:
ctest: 22/22 passedcompare_tests: 11 tests, 0 failuresWithout the fix, the new test does not finish (killed after 20 s).
Related: #1073 (remediation request), #1116 (alternative sort-based approach).