Skip to content

Compare: avoid exponential recursion in cJSON_Compare (CVE-2026-67216) - #1146

Open
annleelu wants to merge 2 commits into
DaveGamble:masterfrom
annleelu:compare-exponential-recursion
Open

annleelu wants to merge 2 commits into
DaveGamble:masterfrom
annleelu:compare-exponential-recursion

Conversation

@annleelu

@annleelu annleelu commented Dec 1, 2026

Copy link
Copy Markdown

cJSON_Compare() compares object members twice, once in each direction, and
recurses 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 a against its match in
b. The reverse pass only needs to confirm that b has no keys missing from
a, so this change replaces the second recursive cJSON_Compare() call with a
get_object_item() presence check. Each shared subtree is now compared once.

  • No API, ABI or header change.
  • Results are unchanged 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, lookup already matches only the first occurrence, so
    results there were not well defined before this change either.

Tests

Adds cjson_compare_should_not_be_exponential_on_deep_objects to
tests/compare_tests.c. It compares two 1000-level nested objects, and checks
that a differing deep leaf and a deep subset still compare as not equal.

On master (6d9f244) with this branch:

  • build: 0 warnings
  • ctest: 22/22 passed
  • compare_tests: 11 tests, 0 failures

Without the fix, the new test does not finish (killed after 20 s).

Related: #1073 (remediation request), #1116 (alternative sort-based approach).

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>
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.

1 participant