Package: release.debian.org
Severity: normal
Tags: trixie
X-Debbugs-Cc: [email protected]
Control: affects -1 + src:cjson
User: [email protected]
Usertags: pu

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

[ Reason ]
CVE-2026-16554 (#1143047)
CVE-2026-29036 (#1146718)
CVE-2026-67215 (#1143048)
CVE-2026-67216 (#1143048)
CVE-2026-67217 (#1143048)
CVE-2026-87933 (#1149958)

[ Impact ]
Please refer to CVE descriptions on the security tracker or BTS.

[ Tests ]
Each patch comes with tests confirming the fix. Testsuite and
autopkgtest continue to pass.

[ Risks ]
Low as they are minimal fixes simply backported with no other changes.
Note though that due to upstream's inactivity they have not been applied
upstream; these patches have been taken from pull requests.

[ Checklist ]
  [x] *all* changes are documented in the d/changelog
  [x] I reviewed all changes and I approve them
  [x] attach debdiff against the package in (old)stable
  [x] the issue is verified as fixed in unstable

[ Changes ]
Backported fixes for the following important security bugs:
  * Add patch for CVE-2026-16554 (Closes: #1143047)
    Guard print_string_ptr counters against 32-bit overflow.
  * Add patch for CVE-2026-29036 (Closes: #1146718)
    Fix decode_pointer_inplace dropping bytes after escapes.
  * Add patch for CVE-2026-67215 (Closes: #1143048)
    Cap JSON Patch graft depth at the nesting limit.
  * Add patch for CVE-2026-67216 (Closes: #1143048)
    Bound cJSON_Compare cost: sort-based objects, depth cap.
  * Add patch for CVE-2026-67217 (Closes: #1143048)
    Apply JSON Patch arrays atomically.
  * Add patch for CVE-2026-87933 (Closes: #1149958)
    Fix heap-use-after-free in merge_patch when patch is subtree of target.

[ Other info ]
Security team has already indicated these fixes can go through -pu.

- --
Maytham

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEESl/RzRFQh8wD3DXB1ZeJcgbF8H8FAmsKwocACgkQ1ZeJcgbF
8H/dAg/6A3zuMmiZ8f78aRwWwsvTXk4lKt47ccK/d/xR8ojFBrjhc0KPFG+nMDiz
twfknw3b1dz1dzcjjFNaP2NZzYFAcNFdZezNjVvoLrvz/NuRaQcWhFdw37a66rp9
T8MtD7ZsnGtwhEQfzjx9Sa1bGq5F49YCclTlBAs937joUzZFqrs8Bx9fLFJHjHl8
tVCe7X9M8s+RUSxk//Jny38Ow3fkMzy3fy0Wiyd/kMMydcjiK8bSAL66Me80DH+5
pj8Evkh7dMuz1c3IUdJHeeuJEVMfweaWIpHiH2CVlcYR7C9WhvPCZnFQHTZUB1RI
VE62QKgV0i7/vu+acRGVw1UK2YiygdkIxmoYErVE1arW44UJpU7dZnZv0qw3kGvO
+yQSlVW7hcMGQQscvqiFUVtCC1SDiW6iAY9v/ClAb+Kg83uvLkU5XDfCW+a5xjYi
F9LePNZ7WOCyJe+IyJ12YoQXJQePsHXg3LuLDBTcwdtXPidM4Hx7gg4fA3YjeiQ9
j6YBpOn/O1SacaJPCQye1y5DAwXydTXZoo4S+7fHHRaaNTfI/mtLFYBe9WuVQFyb
UYBhMeElOQJKxSN5A30zeaPeZ7HU2qYZc+WWlF2h/hXY4wBZ+zm7Aon3qZmQtQel
aWk7yMz2nNVBjPpS7LATiJQomdXlCVvxkz8NM0/jQ3iJriK9+qg=
=3Emx
-----END PGP SIGNATURE-----
diff -Nru cjson-1.7.18/debian/changelog cjson-1.7.18/debian/changelog
--- cjson-1.7.18/debian/changelog       2025-09-12 06:24:25.000000000 +0800
+++ cjson-1.7.18/debian/changelog       2026-11-28 19:46:27.000000000 +0800
@@ -1,3 +1,20 @@
+cjson (1.7.18-3.1+deb13u2) trixie; urgency=medium
+
+  * Add patch for CVE-2026-16554 (Closes: #1143047)
+    Guard print_string_ptr counters against 32-bit overflow.
+  * Add patch for CVE-2026-29036 (Closes: #1146718)
+    Fix decode_pointer_inplace dropping bytes after escapes.
+  * Add patch for CVE-2026-67215 (Closes: #1143048)
+    Cap JSON Patch graft depth at the nesting limit.
+  * Add patch for CVE-2026-67216 (Closes: #1143048)
+    Bound cJSON_Compare cost: sort-based objects, depth cap.
+  * Add patch for CVE-2026-67217 (Closes: #1143048)
+    Apply JSON Patch arrays atomically.
+  * Add patch for CVE-2026-87933 (Closes: #1149958)
+    Fix heap-use-after-free in merge_patch when patch is subtree of target.
+
+ -- Maytham Alsudany <[email protected]>  Sat, 28 Nov 2026 19:46:27 +0800
+
 cjson (1.7.18-3.1+deb13u1) trixie-security; urgency=medium
 
   * CVE-2025-57052 (Closes: #1114757)
diff -Nru cjson-1.7.18/debian/patches/0002-CVE-2026-16554.patch 
cjson-1.7.18/debian/patches/0002-CVE-2026-16554.patch
--- cjson-1.7.18/debian/patches/0002-CVE-2026-16554.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0002-CVE-2026-16554.patch       2026-11-26 
16:33:55.000000000 +0800
@@ -0,0 +1,114 @@
+From 527bbde4c66993ed0153c30ad5045a4e4e24df9a Mon Sep 17 00:00:00 2001
+From: w4ffl35 <[email protected]>
+Date: Mon, 26 Oct 2026 16:28:23 -0600
+Subject: [PATCH] Guard print_string_ptr counters against 32-bit overflow
+ (CVE-2026-16554)
+
+On 32-bit platforms size_t is 32 bits: escape_characters += 5 wraps
+past ~858M control characters, and the length sums can wrap too, so
+ensure() hands out a small buffer while the write loop emits the true
+(giant) escaped length -- heap overflow, reachable via
+cJSON_PrintBuffered with a pre-allocated buffer. Fail instead of
+wrapping at each accumulation step and before sizing the allocation.
+
+Add an escape-heavy exactness regression test.
+---
+ cJSON.c              | 24 +++++++++++++++++++++++-
+ tests/print_string.c | 31 +++++++++++++++++++++++++++++++
+ 2 files changed, 54 insertions(+), 1 deletion(-)
+
+diff --git a/cJSON.c b/cJSON.c
+index 88c2d95b3..bd2141990 100644
+--- a/cJSON.c
++++ b/cJSON.c
+@@ -991,18 +991,40 @@ static cJSON_bool print_string_ptr(const unsigned char * 
const input, printbuffe
+             case '\r':
+             case '\t':
+                 /* one character escape sequence */
++                if (escape_characters == (size_t)-1)
++                {
++                    /* counter would wrap and under-allocate below */
++                    return false;
++                }
+                 escape_characters++;
+                 break;
+             default:
+                 if (*input_pointer < 32)
+                 {
+                     /* UTF-16 escape sequence uXXXX */
++                    if (escape_characters > ((size_t)-1 - 5))
++                    {
++                        /* counter would wrap and under-allocate below */
++                        return false;
++                    }
+                     escape_characters += 5;
+                 }
+                 break;
+         }
+     }
+-    output_length = (size_t)(input_pointer - input) + escape_characters;
++    {
++        size_t input_length = (size_t)(input_pointer - input);
++        /* The allocation below adds the quotes; if any of these sums
++         * wrapped (32-bit size_t past ~4G, CVE-2026-16554), ensure()
++         * would hand out a small buffer while the write loop emits
++         * the true length. Fail instead. */
++        if ((escape_characters > ((size_t)-1 - sizeof("\"\""))) ||
++            (input_length > ((size_t)-1 - sizeof("\"\"") - 
escape_characters)))
++        {
++            return false;
++        }
++        output_length = input_length + escape_characters;
++    }
+ 
+     output = ensure(output_buffer, output_length + sizeof("\"\""));
+     if (output == NULL)
+diff --git a/tests/print_string.c b/tests/print_string.c
+index e6f5b92dc..6951d3705 100644
+--- a/tests/print_string.c
++++ b/tests/print_string.c
+@@ -65,6 +65,36 @@ static void print_string_should_print_utf8(void)
+     assert_print_string("\"ü猫慕\"", "ü猫慕");
+ }
+ 
++static void print_string_should_print_escape_heavy_strings_exactly(void)
++{
++    /* CVE-2026-16554 regression guard: escape counting must stay exact
++     * at scale so the allocation always matches the emitted length. */
++    char input[101];
++    char expected[701];
++    unsigned char printed[1024];
++    printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
++    size_t i = 0;
++
++    memset(input, 1, 100);
++    input[100] = '\0';
++    expected[0] = '"';
++    for (i = 0; i < 100; i++)
++    {
++        memcpy(expected + 1 + 6 * i, "\\u0001", 6);
++    }
++    expected[601] = '"';
++    expected[602] = '\0';
++
++    buffer.buffer = printed;
++    buffer.length = sizeof(printed);
++    buffer.offset = 0;
++    buffer.noalloc = true;
++    buffer.hooks = global_hooks;
++
++    TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, 
&buffer), "Failed to print string.");
++    TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "Escape-heavy output 
isn't exact.");
++}
++
+ int CJSON_CDECL main(void)
+ {
+     /* initialize cJSON item */
+@@ -73,6 +103,7 @@ int CJSON_CDECL main(void)
+     RUN_TEST(print_string_should_print_empty_strings);
+     RUN_TEST(print_string_should_print_ascii);
+     RUN_TEST(print_string_should_print_utf8);
++    RUN_TEST(print_string_should_print_escape_heavy_strings_exactly);
+ 
+     return UNITY_END();
+ }
diff -Nru cjson-1.7.18/debian/patches/0003-CVE-2026-29036.patch 
cjson-1.7.18/debian/patches/0003-CVE-2026-29036.patch
--- cjson-1.7.18/debian/patches/0003-CVE-2026-29036.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0003-CVE-2026-29036.patch       2026-11-26 
16:34:23.000000000 +0800
@@ -0,0 +1,107 @@
+From d77a5167167122f6fcc0ac2f677bccb138294973 Mon Sep 17 00:00:00 2001
+From: w4ffl35 <[email protected]>
+Date: Tue, 27 Oct 2026 09:06:37 -0600
+Subject: [PATCH] utils: fix decode_pointer_inplace dropping bytes after
+ escapes (CVE-2026-29036)
+
+decode_pointer_inplace advanced the write pointer on every loop iteration
+but never copied passthrough bytes down, so any escape not at end of token
+truncated the decoded name ('/a~1b' became 'a~'). Worse, the old code wrote
+'/' to decoded[1] instead of decoded[0] for ~1. JSON Patch add/remove/replace
+then targeted wrong keys or failed, enabling silent data corruption and
+wrong-key deletion via crafted ~0/~1 sequences.
+
+Rewrite compacts properly: escapes collapse two bytes into one, all other
+bytes copied down; decoded never overtakes the read cursor. Invalid escapes
+still abort as before. Regression cases 16/17 in cjson-utils-tests.json.
+
+Red/green: new cases fail before (apply error 13), all 3 patch suites pass
+after; full ctest 21/22 (old_utils_tests fails only on the unrelated
+not-yet-merged #1109 variant, identical before/after this change).
+---
+ cJSON_Utils.c                                 | 19 +++++++++++----
+ tests/json-patch-tests/cjson-utils-tests.json | 24 ++++++++++++++-----
+ 2 files changed, 33 insertions(+), 10 deletions(-)
+
+diff --git a/cJSON_Utils.c b/cJSON_Utils.c
+index 4f3a9ed8d..42adc9217 100644
+--- a/cJSON_Utils.c
++++ b/cJSON_Utils.c
+@@ -364,17 +364,21 @@ static void decode_pointer_inplace(unsigned char *string)
+         return;
+     }
+ 
+-    for (; *string; (void)decoded_string++, string++)
++    /* Compact in place: every byte is copied down (escapes collapse two
++     * bytes into one), so decoded_string never overtakes string and the
++     * final NUL always lands within the original buffer. Invalid escapes
++     * abort like before, leaving the already-decoded prefix in place. */
++    while (*string != '\0')
+     {
+         if (string[0] == '~')
+         {
+             if (string[1] == '0')
+             {
+-                decoded_string[0] = '~';
++                *decoded_string = '~';
+             }
+             else if (string[1] == '1')
+             {
+-                decoded_string[1] = '/';
++                *decoded_string = '/';
+             }
+             else
+             {
+@@ -382,11 +386,18 @@ static void decode_pointer_inplace(unsigned char *string)
+                 return;
+             }
+ 
++            decoded_string++;
++            string += 2;
++        }
++        else
++        {
++            *decoded_string = *string;
++            decoded_string++;
+             string++;
+         }
+     }
+ 
+-    decoded_string[0] = '\0';
++    *decoded_string = '\0';
+ }
+ 
+ /* non-broken cJSON_DetachItemFromArray */
+diff --git a/tests/json-patch-tests/cjson-utils-tests.json 
b/tests/json-patch-tests/cjson-utils-tests.json
+index 28a5e3073..0e237eb6b 100644
+--- a/tests/json-patch-tests/cjson-utils-tests.json
++++ b/tests/json-patch-tests/cjson-utils-tests.json
+@@ -82,10 +82,22 @@
+       "expected": {"foo": ["bar", ["abc", "def"]] }
+     },
+ 
+-    { 
+-        "comment": "15",
+-        "doc": {"foo": {"bar": 1}},
+-        "patch": [{"op": "add", "path": "/foo/bar/baz", "value": "5"}],
+-        "error": "attempting to add to subfield of non-object"
+-    } 
++    {
++      "comment": "15",
++      "doc": {"foo": {"bar": 1}},
++      "patch": [{"op": "add", "path": "/foo/bar/baz", "value": "5"}],
++      "error": "attempting to add to subfield of non-object"
++    },
++    {
++      "comment": "16 (CVE-2026-29036): ~1 must decode to /, targeting key 
a/b",
++      "doc": {"a/b": 1, "a~b": 2},
++      "patch": [{"op": "replace", "path": "/a~1b", "value": 3}],
++      "expected": {"a/b": 3, "a~b": 2}
++    },
++    {
++      "comment": "17 (CVE-2026-29036): remove via ~1 must delete key a/b, not 
a~b",
++      "doc": {"a/b": 1, "a~b": 2},
++      "patch": [{"op": "remove", "path": "/a~1b"}],
++      "expected": {"a~b": 2}
++    }
+ ]
diff -Nru cjson-1.7.18/debian/patches/0004-CVE-2026-67215.patch 
cjson-1.7.18/debian/patches/0004-CVE-2026-67215.patch
--- cjson-1.7.18/debian/patches/0004-CVE-2026-67215.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0004-CVE-2026-67215.patch       2026-11-26 
16:34:23.000000000 +0800
@@ -0,0 +1,212 @@
+From 204c26ab8f4ce1e67ce4210b4895ac8039d03b95 Mon Sep 17 00:00:00 2001
+From: w4ffl35 <[email protected]>
+Date: Mon, 26 Oct 2026 16:24:17 -0600
+Subject: [PATCH] Cap JSON Patch graft depth at the nesting limit
+ (CVE-2026-67215)
+
+An untrusted RFC 6902 patch can graft duplicated subtrees via
+add/copy (or replace the root) to amplify the document past
+CJSON_NESTING_LIMIT, setting up stack exhaustion in recursive
+passes. Refuse grafts whose parent depth plus value depth would reach
+the limit (status 12), including root replacement with an over-limit
+value. Depth helpers are self-bounding: value depth never recurses
+past the limit, and parent depth is counted from pointer separators.
+
+Add regression tests for rejection and the shallow success path.
+---
+ cJSON_Utils.c            | 75 ++++++++++++++++++++++++++++++++++++++-
+ tests/misc_utils_tests.c | 76 ++++++++++++++++++++++++++++++++++++++++
+ 2 files changed, 150 insertions(+), 1 deletion(-)
+
+diff --git a/cJSON_Utils.c b/cJSON_Utils.c
+index 4f3a9ed8d..5a8b244d0 100644
+--- a/cJSON_Utils.c
++++ b/cJSON_Utils.c
+@@ -804,6 +804,59 @@ static void overwrite_item(cJSON * const root, const 
cJSON replacement)
+     memcpy(root, &replacement, sizeof(cJSON));
+ }
+ 
++/* Longest root-to-leaf chain of a value subtree, counting the node
++ * itself as 1. Recursion is self-bounding: it never descends past
++ * CJSON_NESTING_LIMIT, so attacker-shaped values cannot exhaust the
++ * stack here. Returns a value > CJSON_NESTING_LIMIT for over-limit
++ * subtrees (fail closed). */
++static size_t patch_value_depth(const cJSON * const node, size_t depth)
++{
++    const cJSON *child = NULL;
++    size_t max = depth;
++
++    if (node == NULL)
++    {
++        return depth;
++    }
++    if (depth > CJSON_NESTING_LIMIT)
++    {
++        return depth;
++    }
++    for (child = node->child; child != NULL; child = child->next)
++    {
++        size_t child_depth = patch_value_depth(child, depth + 1);
++        if (child_depth > max)
++        {
++            max = child_depth;
++        }
++        if (max > CJSON_NESTING_LIMIT)
++        {
++            return max;
++        }
++    }
++    return max;
++}
++
++/* Depth of the node a JSON Pointer addresses, counting path segments.
++ * A raw '/' is always a separator ('/' in a key is escaped as ~1),
++ * so the segment count is the node's depth with the root at 0. */
++static size_t pointer_depth(const unsigned char *pointer)
++{
++    size_t depth = 0;
++    if (pointer == NULL)
++    {
++        return 0;
++    }
++    for (; *pointer; pointer++)
++    {
++        if (*pointer == '/')
++        {
++            depth++;
++        }
++    }
++    return depth;
++}
++
+ static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool 
case_sensitive)
+ {
+     cJSON *path = NULL;
+@@ -866,6 +919,14 @@ static int apply_patch(cJSON *object, const cJSON *patch, 
const cJSON_bool case_
+                 goto cleanup;
+             }
+ 
++            if (patch_value_depth(value, 1) > CJSON_NESTING_LIMIT)
++            {
++                /* replacing the root with an over-limit value would
++                 * build a document no recursive pass can handle. */
++                status = 12;
++                goto cleanup;
++            }
++
+             overwrite_item(object, *value);
+ 
+             /* delete the duplicated value */
+@@ -978,7 +1039,19 @@ static int apply_patch(cJSON *object, const cJSON 
*patch, const cJSON_bool case_
+         status = 9;
+         goto cleanup;
+     }
+-    else if (cJSON_IsArray(parent))
++
++    /* Refuse grafts that would push the document past the nesting
++     * limit: add/copy amplification must not build documents whose
++     * depth exhausts the stack in recursive passes (CVE-2026-67215).
++     * The grafted node lands one level below its parent, so the
++     * deepest new node is at parent_depth + value_depth. */
++    if (pointer_depth(parent_pointer) + patch_value_depth(value, 1) >= 
CJSON_NESTING_LIMIT)
++    {
++        status = 12;
++        goto cleanup;
++    }
++
++    if (cJSON_IsArray(parent))
+     {
+         if (strcmp((char*)child_pointer, "-") == 0)
+         {
+diff --git a/tests/misc_utils_tests.c b/tests/misc_utils_tests.c
+index 7d300bc8e..ed651218b 100644
+--- a/tests/misc_utils_tests.c
++++ b/tests/misc_utils_tests.c
+@@ -70,11 +70,87 @@ static void 
cjson_utils_functions_shouldnt_crash_with_null_pointers(void)
+     cJSON_Delete(item);
+ }
+ 
++/* Build {"lvl": {"lvl": ... {"end": 1} ... }} nested levels deep. */
++static cJSON *build_nested_object(int levels)
++{
++    cJSON *root = cJSON_CreateObject();
++    cJSON *current = root;
++    int i = 0;
++
++    if (root == NULL)
++    {
++        return NULL;
++    }
++    for (i = 0; i < levels; i++)
++    {
++        cJSON *next = cJSON_CreateObject();
++        if (next == NULL)
++        {
++            cJSON_Delete(root);
++            return NULL;
++        }
++        cJSON_AddItemToObject(current, "lvl", next);
++        current = next;
++    }
++    cJSON_AddNumberToObject(current, "end", 1);
++    return root;
++}
++
++static void apply_patches_should_reject_grafts_past_the_nesting_limit(void)
++{
++    /* CVE-2026-67215: add/copy must not amplify the document past the
++     * nesting limit (single copy grafting a 600-deep subtree under a
++     * depth-600 parent would reach depth 1202). */
++    cJSON *inner = build_nested_object(600);
++    cJSON *doc = cJSON_CreateObject();
++    cJSON *patch = cJSON_CreateArray();
++    cJSON *op = cJSON_CreateObject();
++    char path[8192] = "/d";
++    int i = 0;
++
++    TEST_ASSERT_NOT_NULL(inner);
++    TEST_ASSERT_NOT_NULL(doc);
++    TEST_ASSERT_NOT_NULL(patch);
++    TEST_ASSERT_NOT_NULL(op);
++    cJSON_AddItemToObject(doc, "d", inner);
++    for (i = 0; i < 599; i++)
++    {
++        strcat(path, "/lvl");
++    }
++    strcat(path, "/copy");
++    cJSON_AddStringToObject(op, "op", "copy");
++    cJSON_AddStringToObject(op, "from", "/d");
++    cJSON_AddStringToObject(op, "path", path);
++    cJSON_AddItemToArray(patch, op);
++
++    TEST_ASSERT_NOT_EQUAL(0, cJSONUtils_ApplyPatches(doc, patch));
++
++    cJSON_Delete(doc);
++    cJSON_Delete(patch);
++}
++
++static void apply_patches_should_allow_grafts_within_the_nesting_limit(void)
++{
++    cJSON *doc = cJSON_Parse("{\"a\": {\"b\": 1}}");
++    cJSON *patch = cJSON_Parse("[{\"op\": \"copy\", \"from\": \"/a\", "
++                               "\"path\": \"/c\"}]");
++
++    TEST_ASSERT_NOT_NULL(doc);
++    TEST_ASSERT_NOT_NULL(patch);
++    TEST_ASSERT_EQUAL_INT(0, cJSONUtils_ApplyPatches(doc, patch));
++    TEST_ASSERT_NOT_NULL(cJSON_GetObjectItem(doc, "c"));
++
++    cJSON_Delete(doc);
++    cJSON_Delete(patch);
++}
++
+ int main(void)
+ {
+     UNITY_BEGIN();
+ 
+     RUN_TEST(cjson_utils_functions_shouldnt_crash_with_null_pointers);
++    RUN_TEST(apply_patches_should_reject_grafts_past_the_nesting_limit);
++    RUN_TEST(apply_patches_should_allow_grafts_within_the_nesting_limit);
+ 
+     return UNITY_END();
+ }
diff -Nru cjson-1.7.18/debian/patches/0005-CVE-2026-67216.patch 
cjson-1.7.18/debian/patches/0005-CVE-2026-67216.patch
--- cjson-1.7.18/debian/patches/0005-CVE-2026-67216.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0005-CVE-2026-67216.patch       2026-11-26 
16:34:24.000000000 +0800
@@ -0,0 +1,505 @@
+From 749d3f861f61d8049b9eb5694be952b3fa44bbef Mon Sep 17 00:00:00 2001
+From: w4ffl35 <[email protected]>
+Date: Mon, 26 Oct 2026 16:27:18 -0600
+Subject: [PATCH] Bound cJSON_Compare cost: sort-based objects, depth cap
+ (CVE-2026-67216)
+
+Object comparison scanned the other member list per member, twice,
+so cost grew exponentially with nesting depth; recursion was also
+unbounded. Compare objects by sorting member-pointer copies once and
+comparing pairwise (O(n log n) per level, caller's trees untouched),
+and fail safe past CJSON_NESTING_LIMIT in cJSON_Compare and the
+TEST-operation compare_json (which additionally no longer reorders
+the compared trees). Add regression tests for nested/order-insensitive
+equality and over-limit fail-safe.
+---
+ cJSON.c               | 159 +++++++++++++++++++++++++++++++++++-------
+ cJSON_Utils.c         | 142 +++++++++++++++++++++++++++++++------
+ tests/compare_tests.c |  58 +++++++++++++++
+ 3 files changed, 313 insertions(+), 46 deletions(-)
+
+diff --git a/cJSON.c b/cJSON.c
+index 88c2d95b3..53e296ff0 100644
+--- a/cJSON.c
++++ b/cJSON.c
+@@ -1980,6 +1980,46 @@ static cJSON *get_object_item(const cJSON * const 
object, const char * const nam
+     return current_element;
+ }
+ 
++/* qsort comparators for member-pointer arrays (no context pointer,
++ * so one function per case rule). NULL names sort first. */
++static int compare_member_names_sensitive(const void *a, const void *b)
++{
++    const char *sa = (*(cJSON * const *)a)->string;
++    const char *sb = (*(cJSON * const *)b)->string;
++    if (sa == sb)
++    {
++        return 0;
++    }
++    if (sa == NULL)
++    {
++        return -1;
++    }
++    if (sb == NULL)
++    {
++        return 1;
++    }
++    return strcmp(sa, sb);
++}
++
++static int compare_member_names_insensitive(const void *a, const void *b)
++{
++    const unsigned char *sa = (const unsigned char*)(*(cJSON * const 
*)a)->string;
++    const unsigned char *sb = (const unsigned char*)(*(cJSON * const 
*)b)->string;
++    if (sa == sb)
++    {
++        return 0;
++    }
++    if (sa == NULL)
++    {
++        return -1;
++    }
++    if (sb == NULL)
++    {
++        return 1;
++    }
++    return case_insensitive_strcmp(sa, sb);
++}
++
+ CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const 
char * const string)
+ {
+     return get_object_item(object, string, false);
+@@ -3069,8 +3109,14 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * 
const item)
+     return (item->type & 0xFF) == cJSON_Raw;
+ }
+ 
+-CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * 
const b, const cJSON_bool case_sensitive)
++static cJSON_bool cJSON_Compare_rec(const cJSON * const a, const cJSON * 
const b, const cJSON_bool case_sensitive, size_t depth)
+ {
++    if (depth >= CJSON_NESTING_LIMIT)
++    {
++        /* nested past the limit: fail safe (CVE-2026-67216). */
++        return false;
++    }
++
+     if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
+     {
+         return false;
+@@ -3134,7 +3180,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * 
const a, const cJSON * cons
+ 
+             for (; (a_element != NULL) && (b_element != NULL);)
+             {
+-                if (!cJSON_Compare(a_element, b_element, case_sensitive))
++                if (!cJSON_Compare_rec(a_element, b_element, case_sensitive, 
depth + 1))
+                 {
+                     return false;
+                 }
+@@ -3153,40 +3199,96 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * 
const a, const cJSON * cons
+ 
+         case cJSON_Object:
+         {
+-            cJSON *a_element = NULL;
+-            cJSON *b_element = NULL;
+-            cJSON_ArrayForEach(a_element, a)
++            /* Sort both member lists once and compare pairwise: O(n log n)
++             * per level instead of the old O(n^2) double scan, so nested
++             * objects cannot blow up exponentially (CVE-2026-67216).
++             * The caller's trees are left untouched. */
++            cJSON **a_members = NULL;
++            cJSON **b_members = NULL;
++            size_t a_count = 0;
++            size_t b_count = 0;
++            size_t i = 0;
++            cJSON *element = NULL;
++            cJSON_bool equal = false;
++
++            for (element = a->child; element != NULL; element = element->next)
+             {
+-                /* TODO This has O(n^2) runtime, which is horrible! */
+-                b_element = get_object_item(b, a_element->string, 
case_sensitive);
+-                if (b_element == NULL)
+-                {
+-                    return false;
+-                }
+-
+-                if (!cJSON_Compare(a_element, b_element, case_sensitive))
+-                {
+-                    return false;
+-                }
++                a_count++;
++            }
++            for (element = b->child; element != NULL; element = element->next)
++            {
++                b_count++;
++            }
++            if (a_count != b_count)
++            {
++                return false;
++            }
++            if (a_count == 0)
++            {
++                return true;
++            }
++            if ((a_count > (size_t)-1 / sizeof(cJSON*)) || (b_count > 
(size_t)-1 / sizeof(cJSON*)))
++            {
++                return false;
++            }
++            a_members = (cJSON**)cJSON_malloc(a_count * sizeof(cJSON*));
++            b_members = (cJSON**)cJSON_malloc(b_count * sizeof(cJSON*));
++            if ((a_members == NULL) || (b_members == NULL))
++            {
++                cJSON_free(a_members);
++                cJSON_free(b_members);
++                return false;
++            }
++            for (element = a->child, i = 0; element != NULL; element = 
element->next, i++)
++            {
++                a_members[i] = element;
++            }
++            for (element = b->child, i = 0; element != NULL; element = 
element->next, i++)
++            {
++                b_members[i] = element;
++            }
++            if (case_sensitive)
++            {
++                qsort(a_members, a_count, sizeof(cJSON*), 
compare_member_names_sensitive);
++                qsort(b_members, b_count, sizeof(cJSON*), 
compare_member_names_sensitive);
++            }
++            else
++            {
++                qsort(a_members, a_count, sizeof(cJSON*), 
compare_member_names_insensitive);
++                qsort(b_members, b_count, sizeof(cJSON*), 
compare_member_names_insensitive);
+             }
+ 
+-            /* doing this twice, once on a and b to prevent true comparison 
if a subset of b
+-             * TODO: Do this the proper way, this is just a fix for now */
+-            cJSON_ArrayForEach(b_element, b)
++            equal = true;
++            for (i = 0; i < a_count; i++)
+             {
+-                a_element = get_object_item(a, b_element->string, 
case_sensitive);
+-                if (a_element == NULL)
++                const char *a_name = a_members[i]->string;
++                const char *b_name = b_members[i]->string;
++                cJSON_bool names_equal = false;
++                if (a_name == b_name)
+                 {
+-                    return false;
++                    names_equal = true;
+                 }
+-
+-                if (!cJSON_Compare(b_element, a_element, case_sensitive))
++                else if ((a_name != NULL) && (b_name != NULL))
+                 {
+-                    return false;
++                    if (case_sensitive)
++                    {
++                        names_equal = (strcmp(a_name, b_name) == 0);
++                    }
++                    else
++                    {
++                        names_equal = (case_insensitive_strcmp((const 
unsigned char*)a_name, (const unsigned char*)b_name) == 0);
++                    }
++                }
++                if (!names_equal || !cJSON_Compare_rec(a_members[i], 
b_members[i], case_sensitive, depth + 1))
++                {
++                    equal = false;
++                    break;
+                 }
+             }
++            cJSON_free(a_members);
++            cJSON_free(b_members);
+ 
+-            return true;
++            return equal;
+         }
+ 
+         default:
+@@ -3194,6 +3296,11 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * 
const a, const cJSON * cons
+     }
+ }
+ 
++CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * 
const b, const cJSON_bool case_sensitive)
++{
++    return cJSON_Compare_rec(a, b, case_sensitive, 0);
++}
++
+ CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
+ {
+     return global_hooks.allocate(size);
+diff --git a/cJSON_Utils.c b/cJSON_Utils.c
+index 4f3a9ed8d..d28a731f8 100644
+--- a/cJSON_Utils.c
++++ b/cJSON_Utils.c
+@@ -108,6 +108,46 @@ static int compare_strings(const unsigned char *string1, 
const unsigned char *st
+     return tolower(*string1) - tolower(*string2);
+ }
+ 
++/* qsort comparators for member-pointer arrays (no context pointer, so
++ * one function per case rule). NULL names sort first. */
++static int compare_json_names_sensitive(const void *a, const void *b)
++{
++    const unsigned char *sa = (const unsigned char*)(*(cJSON * const 
*)a)->string;
++    const unsigned char *sb = (const unsigned char*)(*(cJSON * const 
*)b)->string;
++    if (sa == sb)
++    {
++        return 0;
++    }
++    if (sa == NULL)
++    {
++        return -1;
++    }
++    if (sb == NULL)
++    {
++        return 1;
++    }
++    return strcmp((const char*)sa, (const char*)sb);
++}
++
++static int compare_json_names_insensitive(const void *a, const void *b)
++{
++    const unsigned char *sa = (const unsigned char*)(*(cJSON * const 
*)a)->string;
++    const unsigned char *sb = (const unsigned char*)(*(cJSON * const 
*)b)->string;
++    if (sa == sb)
++    {
++        return 0;
++    }
++    if (sa == NULL)
++    {
++        return -1;
++    }
++    if (sb == NULL)
++    {
++        return 1;
++    }
++    return compare_strings(sa, sb, false);
++}
++
+ /* securely comparison of floating-point variables */
+ static cJSON_bool compare_double(double a, double b)
+ {
+@@ -601,8 +641,14 @@ static void sort_object(cJSON * const object, const 
cJSON_bool case_sensitive)
+     object->child = sort_list(object->child, case_sensitive);
+ }
+ 
+-static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool 
case_sensitive)
++static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool 
case_sensitive, size_t depth)
+ {
++    if (depth >= CJSON_NESTING_LIMIT)
++    {
++        /* nested past the limit: fail safe (CVE-2026-67216). */
++        return false;
++    }
++
+     if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
+     {
+         /* mismatched type. */
+@@ -635,7 +681,7 @@ static cJSON_bool compare_json(cJSON *a, cJSON *b, const 
cJSON_bool case_sensiti
+         case cJSON_Array:
+             for ((void)(a = a->child), b = b->child; (a != NULL) && (b != 
NULL); (void)(a = a->next), b = b->next)
+             {
+-                cJSON_bool identical = compare_json(a, b, case_sensitive);
++                cJSON_bool identical = compare_json(a, b, case_sensitive, 
depth + 1);
+                 if (!identical)
+                 {
+                     return false;
+@@ -653,33 +699,89 @@ static cJSON_bool compare_json(cJSON *a, cJSON *b, const 
cJSON_bool case_sensiti
+             }
+ 
+         case cJSON_Object:
+-            sort_object(a, case_sensitive);
+-            sort_object(b, case_sensitive);
+-            for ((void)(a = a->child), b = b->child; (a != NULL) && (b != 
NULL); (void)(a = a->next), b = b->next)
++        {
++            /* Sort copies of the member lists once and compare pairwise:
++             * O(n log n) per level instead of sort-then-scan recursion
++             * with quadratic behavior on nested objects (CVE-2026-67216).
++             * Unlike the old code, the compared trees are not reordered. */
++            cJSON **a_members = NULL;
++            cJSON **b_members = NULL;
++            size_t a_count = 0;
++            size_t b_count = 0;
++            size_t i = 0;
++            cJSON *element = NULL;
++
++            for (element = a->child; element != NULL; element = element->next)
++            {
++                a_count++;
++            }
++            for (element = b->child; element != NULL; element = element->next)
++            {
++                b_count++;
++            }
++            /* object length mismatch? */
++            if (a_count != b_count)
++            {
++                return false;
++            }
++            if (a_count == 0)
++            {
++                return true;
++            }
++            if ((a_count > (size_t)-1 / sizeof(cJSON*)) || (b_count > 
(size_t)-1 / sizeof(cJSON*)))
++            {
++                return false;
++            }
++            a_members = (cJSON**)cJSON_malloc(a_count * sizeof(cJSON*));
++            b_members = (cJSON**)cJSON_malloc(b_count * sizeof(cJSON*));
++            if ((a_members == NULL) || (b_members == NULL))
++            {
++                cJSON_free(a_members);
++                cJSON_free(b_members);
++                return false;
++            }
++            for (element = a->child, i = 0; element != NULL; element = 
element->next, i++)
++            {
++                a_members[i] = element;
++            }
++            for (element = b->child, i = 0; element != NULL; element = 
element->next, i++)
++            {
++                b_members[i] = element;
++            }
++            if (case_sensitive)
++            {
++                qsort(a_members, a_count, sizeof(cJSON*), 
compare_json_names_sensitive);
++                qsort(b_members, b_count, sizeof(cJSON*), 
compare_json_names_sensitive);
++            }
++            else
++            {
++                qsort(a_members, a_count, sizeof(cJSON*), 
compare_json_names_insensitive);
++                qsort(b_members, b_count, sizeof(cJSON*), 
compare_json_names_insensitive);
++            }
++            for (i = 0; i < a_count; i++)
+             {
+                 cJSON_bool identical = false;
+                 /* compare object keys */
+-                if (compare_strings((unsigned char*)a->string, (unsigned 
char*)b->string, case_sensitive))
++                if (compare_strings((unsigned char*)a_members[i]->string, 
(unsigned char*)b_members[i]->string, case_sensitive))
+                 {
+                     /* missing member */
+-                    return false;
++                    identical = false;
++                }
++                else
++                {
++                    identical = compare_json(a_members[i], b_members[i], 
case_sensitive, depth + 1);
+                 }
+-                identical = compare_json(a, b, case_sensitive);
+                 if (!identical)
+                 {
++                    cJSON_free(a_members);
++                    cJSON_free(b_members);
+                     return false;
+                 }
+             }
+-
+-            /* object length mismatch (one of both children is not null) */
+-            if ((a != NULL) || (b != NULL))
+-            {
+-                return false;
+-            }
+-            else
+-            {
+-                return true;
+-            }
++            cJSON_free(a_members);
++            cJSON_free(b_members);
++            return true;
++        }
+ 
+         default:
+             break;
+@@ -831,7 +933,7 @@ static int apply_patch(cJSON *object, const cJSON *patch, 
const cJSON_bool case_
+     else if (opcode == TEST)
+     {
+         /* compare value: {...} with the given path */
+-        status = !compare_json(get_item_from_pointer(object, 
path->valuestring, case_sensitive), get_object_item(patch, "value", 
case_sensitive), case_sensitive);
++        status = !compare_json(get_item_from_pointer(object, 
path->valuestring, case_sensitive), get_object_item(patch, "value", 
case_sensitive), case_sensitive, 0);
+         goto cleanup;
+     }
+ 
+@@ -1453,7 +1555,7 @@ static cJSON *generate_merge_patch(cJSON * const from, 
cJSON * const to, const c
+         else
+         {
+             /* object key exists in both objects */
+-            if (!compare_json(from_child, to_child, case_sensitive))
++            if (!compare_json(from_child, to_child, case_sensitive, 0))
+             {
+                 /* not identical --> generate a patch */
+                 cJSON_AddItemToObject(patch, to_child->string, 
cJSONUtils_GenerateMergePatch(from_child, to_child));
+diff --git a/tests/compare_tests.c b/tests/compare_tests.c
+index 797c7740c..c6fd3f2d0 100644
+--- a/tests/compare_tests.c
++++ b/tests/compare_tests.c
+@@ -189,6 +189,62 @@ static void cjson_compare_should_compare_objects(void)
+                 false))
+ }
+ 
++static void cjson_compare_should_compare_nested_objects(void)
++{
++    /* nested identical documents compare true regardless of member
++     * order (exercises the sorted single-pass comparison). */
++    TEST_ASSERT_TRUE(compare_from_string(
++                "{\"b\": {\"d\": 4, \"c\": 3}, \"a\": [1, 2]}",
++                "{\"a\": [1, 2], \"b\": {\"c\": 3, \"d\": 4}}",
++                true));
++    TEST_ASSERT_TRUE(compare_from_string(
++                "{\"B\": 1, \"a\": 2}",
++                "{\"b\": 1, \"A\": 2}",
++                false));
++    /* nested differences are found */
++    TEST_ASSERT_FALSE(compare_from_string(
++                "{\"b\": {\"d\": 4, \"c\": 3}, \"a\": [1, 2]}",
++                "{\"a\": [1, 2], \"b\": {\"c\": 3, \"d\": 5}}",
++                true));
++    /* different member counts are not equal */
++    TEST_ASSERT_FALSE(compare_from_string(
++                "{\"a\": 1}",
++                "{\"a\": 1, \"b\": 2}",
++                true));
++    /* empty objects are equal */
++    TEST_ASSERT_TRUE(compare_from_string("{}", "{}", true));
++}
++
++static void cjson_compare_should_fail_safe_past_nesting_limit(void)
++{
++    /* CVE-2026-67216: over-limit nesting fails safe instead of
++     * recursing without bound. */
++    cJSON *a = cJSON_CreateArray();
++    cJSON *b = cJSON_CreateArray();
++    cJSON *current_a = a;
++    cJSON *current_b = b;
++    int i = 0;
++
++    TEST_ASSERT_NOT_NULL(a);
++    TEST_ASSERT_NOT_NULL(b);
++    for (i = 0; i < 1500; i++)
++    {
++        cJSON *next_a = cJSON_CreateArray();
++        cJSON *next_b = cJSON_CreateArray();
++        TEST_ASSERT_NOT_NULL(next_a);
++        TEST_ASSERT_NOT_NULL(next_b);
++        cJSON_AddItemToArray(current_a, next_a);
++        cJSON_AddItemToArray(current_b, next_b);
++        current_a = next_a;
++        current_b = next_b;
++    }
++
++    TEST_ASSERT_FALSE(cJSON_Compare(a, b, true));
++
++    cJSON_Delete(a);
++    cJSON_Delete(b);
++}
++
+ int CJSON_CDECL main(void)
+ {
+     UNITY_BEGIN();
+@@ -203,6 +259,8 @@ int CJSON_CDECL main(void)
+     RUN_TEST(cjson_compare_should_compare_raw);
+     RUN_TEST(cjson_compare_should_compare_arrays);
+     RUN_TEST(cjson_compare_should_compare_objects);
++    RUN_TEST(cjson_compare_should_compare_nested_objects);
++    RUN_TEST(cjson_compare_should_fail_safe_past_nesting_limit);
+ 
+     return UNITY_END();
+ }
diff -Nru cjson-1.7.18/debian/patches/0006-CVE-2026-67217.patch 
cjson-1.7.18/debian/patches/0006-CVE-2026-67217.patch
--- cjson-1.7.18/debian/patches/0006-CVE-2026-67217.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0006-CVE-2026-67217.patch       2026-11-26 
16:34:24.000000000 +0800
@@ -0,0 +1,279 @@
+From be79a2e8861e0384c814de739ec5ed0e8271c412 Mon Sep 17 00:00:00 2001
+From: w4ffl35 <[email protected]>
+Date: Mon, 26 Oct 2026 16:23:15 -0600
+Subject: [PATCH 1/3] Apply JSON Patch arrays atomically (CVE-2026-67217)
+
+cJSONUtils_ApplyPatches{,CaseSensitive} applied operations directly to
+the caller's document and returned on the first failure, leaving
+earlier operations applied: an attacker-supplied patch could destroy
+data and then fail validation. Run all operations against a duplicate
+and commit to the original only when every operation succeeds, keeping
+the caller's pointer stable. Working from duplicates also closes the
+use-after-free when the patch array itself lives inside the patched
+document. Add regression tests for rollback and success paths.
+---
+ cJSON_Utils.c            | 63 ++++++++++++++++++++++------------------
+ tests/misc_utils_tests.c | 55 +++++++++++++++++++++++++++++++++++
+ 2 files changed, 89 insertions(+), 29 deletions(-)
+
+--- a/cJSON_Utils.c
++++ b/cJSON_Utils.c
+@@ -1221,9 +1221,17 @@
+     return status;
+ }
+ 
+-CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * 
const patches)
++/* Apply a patch array atomically: run every operation against a copy
++ * and only commit to the caller's document when all of them succeed.
++ * A failing patch must not leave earlier operations' destruction
++ * behind (CVE-2026-67217). Working from copies also keeps the loop
++ * safe when the patch array itself lives inside the patched document
++ * (applying a patch may delete it). */
++static int apply_patches(cJSON * const object, const cJSON * const patches, 
const cJSON_bool case_sensitive)
+ {
+     const cJSON *current_patch = NULL;
++    cJSON *patches_copy = NULL;
++    cJSON *work = NULL;
+     int status = 0;
+ 
+     if (!cJSON_IsArray(patches))
+@@ -1232,51 +1240,50 @@
+         return 1;
+     }
+ 
+-    if (patches != NULL)
+-    {
+-        current_patch = patches->child;
++    /* Duplicate first: either input may alias the other, and applying a
++     * patch can delete nodes out from under the iteration. */
++    patches_copy = cJSON_Duplicate(patches, 1);
++    work = cJSON_Duplicate(object, 1);
++    if ((patches_copy == NULL) || (work == NULL))
++    {
++        /* out of memory (or NULL document); nothing was modified. */
++        cJSON_Delete(patches_copy);
++        cJSON_Delete(work);
++        return 1;
+     }
+ 
++    current_patch = patches_copy->child;
+     while (current_patch != NULL)
+     {
+-        status = apply_patch(object, current_patch, false);
++        status = apply_patch(work, current_patch, case_sensitive);
+         if (status != 0)
+         {
++            cJSON_Delete(patches_copy);
++            cJSON_Delete(work);
+             return status;
+         }
+         current_patch = current_patch->next;
+     }
+ 
++    /* All operations succeeded: commit the copy over the original,
++     * keeping the caller's pointer stable. */
++    work->next = object->next;
++    work->prev = object->prev;
++    overwrite_item(object, *work);
++    cJSON_free(work);
++    cJSON_Delete(patches_copy);
++
+     return 0;
+ }
+ 
+-CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, 
const cJSON * const patches)
++CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * 
const patches)
+ {
+-    const cJSON *current_patch = NULL;
+-    int status = 0;
+-
+-    if (!cJSON_IsArray(patches))
+-    {
+-        /* malformed patches. */
+-        return 1;
+-    }
+-
+-    if (patches != NULL)
+-    {
+-        current_patch = patches->child;
+-    }
+-
+-    while (current_patch != NULL)
+-    {
+-        status = apply_patch(object, current_patch, true);
+-        if (status != 0)
+-        {
+-            return status;
+-        }
+-        current_patch = current_patch->next;
+-    }
++    return apply_patches(object, patches, false);
++}
+ 
+-    return 0;
++CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, 
const cJSON * const patches)
++{
++    return apply_patches(object, patches, true);
+ }
+ 
+ static void compose_patch(cJSON * const patches, const unsigned char * const 
operation, const unsigned char * const path, const unsigned char *suffix, const 
cJSON * const value)
+--- a/tests/misc_utils_tests.c
++++ b/tests/misc_utils_tests.c
+@@ -144,6 +144,113 @@
+     cJSON_Delete(patch);
+ }
+ 
++static void apply_patches_should_preserve_non_root_links(const cJSON_bool 
array, const cJSON_bool case_sensitive)
++{
++    int index = 0;
++    for (index = 0; index < 3; index++)
++    {
++        cJSON *doc = cJSON_Parse(array ? "[{\"v\":1},{\"v\":1},{\"v\":1}]" :
++                                
"{\"first\":{\"v\":1},\"middle\":{\"v\":1},\"last\":{\"v\":1}}");
++        cJSON *patch = 
cJSON_Parse("[{\"op\":\"replace\",\"path\":\"/v\",\"value\":2}]");
++        cJSON *target = NULL;
++        cJSON *previous = NULL;
++        cJSON *next = NULL;
++
++        TEST_ASSERT_NOT_NULL(doc);
++        TEST_ASSERT_NOT_NULL(patch);
++        target = cJSON_GetArrayItem(doc, index);
++        TEST_ASSERT_NOT_NULL(target);
++        previous = target->prev;
++        next = target->next;
++
++        TEST_ASSERT_EQUAL_INT(0, case_sensitive ? 
cJSONUtils_ApplyPatchesCaseSensitive(target, patch) :
++                                                
cJSONUtils_ApplyPatches(target, patch));
++        TEST_ASSERT_EQUAL_PTR(previous, target->prev);
++        TEST_ASSERT_EQUAL_PTR(next, target->next);
++        TEST_ASSERT_EQUAL_PTR(target, cJSON_GetArrayItem(doc, index));
++        TEST_ASSERT_EQUAL_PTR(index == 0 ? target : previous->next, target);
++        if (next != NULL)
++        {
++            TEST_ASSERT_EQUAL_PTR(target, next->prev);
++        }
++        TEST_ASSERT_EQUAL_PTR(cJSON_GetArrayItem(doc, 2), doc->child->prev);
++        TEST_ASSERT_EQUAL_INT(3, cJSON_GetArraySize(doc));
++        TEST_ASSERT_EQUAL_INT(2, cJSON_GetObjectItem(target, "v")->valueint);
++        if (!array)
++        {
++            TEST_ASSERT_EQUAL_STRING(index == 0 ? "first" : (index == 1 ? 
"middle" : "last"), target->string);
++            TEST_ASSERT_EQUAL_PTR(target, cJSON_GetObjectItem(doc, 
target->string));
++        }
++        cJSON_Delete(patch);
++        cJSON_Delete(doc);
++    }
++}
++
++static void apply_patches_should_preserve_object_siblings(void)
++{
++    apply_patches_should_preserve_non_root_links(false, false);
++    apply_patches_should_preserve_non_root_links(false, true);
++}
++
++static void apply_patches_should_preserve_array_siblings(void)
++{
++    apply_patches_should_preserve_non_root_links(true, false);
++    apply_patches_should_preserve_non_root_links(true, true);
++}
++
++static void 
apply_patches_should_leave_document_untouched_when_a_patch_fails(void)
++{
++    /* CVE-2026-67217: patch arrays must apply atomically. A failing
++     * operation must not leave earlier operations applied. */
++    cJSON *doc = cJSON_Parse("{\"a\": 1, \"b\": 2}");
++    cJSON *patch = cJSON_Parse("[{\"op\": \"remove\", \"path\": \"/a\"}, "
++                               "{\"op\": \"remove\", "
++                               "\"path\": \"/nonexistent\"}]");
++    cJSON *a = NULL;
++    cJSON *b = NULL;
++
++    TEST_ASSERT_NOT_NULL(doc);
++    TEST_ASSERT_NOT_NULL(patch);
++
++    TEST_ASSERT_NOT_EQUAL(0, cJSONUtils_ApplyPatches(doc, patch));
++
++    a = cJSON_GetObjectItem(doc, "a");
++    b = cJSON_GetObjectItem(doc, "b");
++    TEST_ASSERT_NOT_NULL(a);
++    TEST_ASSERT_NOT_NULL(b);
++    TEST_ASSERT_EQUAL_INT(1, a->valueint);
++    TEST_ASSERT_EQUAL_INT(2, b->valueint);
++
++    cJSON_Delete(doc);
++    cJSON_Delete(patch);
++}
++
++static void apply_patches_should_apply_valid_patches(void)
++{
++    cJSON *doc = cJSON_Parse("{\"a\": 1}");
++    cJSON *patch = cJSON_Parse("[{\"op\": \"replace\", \"path\": \"/a\", "
++                               "\"value\": 2}, "
++                               "{\"op\": \"add\", \"path\": \"/c\", "
++                               "\"value\": 3}]");
++    cJSON *a = NULL;
++    cJSON *c = NULL;
++
++    TEST_ASSERT_NOT_NULL(doc);
++    TEST_ASSERT_NOT_NULL(patch);
++
++    TEST_ASSERT_EQUAL_INT(0, cJSONUtils_ApplyPatches(doc, patch));
++
++    a = cJSON_GetObjectItem(doc, "a");
++    c = cJSON_GetObjectItem(doc, "c");
++    TEST_ASSERT_NOT_NULL(a);
++    TEST_ASSERT_NOT_NULL(c);
++    TEST_ASSERT_EQUAL_INT(2, a->valueint);
++    TEST_ASSERT_EQUAL_INT(3, c->valueint);
++
++    cJSON_Delete(doc);
++    cJSON_Delete(patch);
++}
++
+ int main(void)
+ {
+     UNITY_BEGIN();
+@@ -151,6 +258,11 @@
+     RUN_TEST(cjson_utils_functions_shouldnt_crash_with_null_pointers);
+     RUN_TEST(apply_patches_should_reject_grafts_past_the_nesting_limit);
+     RUN_TEST(apply_patches_should_allow_grafts_within_the_nesting_limit);
++    
RUN_TEST(apply_patches_should_leave_document_untouched_when_a_patch_fails);
++    RUN_TEST(apply_patches_should_apply_valid_patches);
++    RUN_TEST(apply_patches_should_preserve_object_siblings);
++    RUN_TEST(apply_patches_should_preserve_array_siblings);
++
+ 
+     return UNITY_END();
+ }
+--- a/cJSON_Utils.h
++++ b/cJSON_Utils.h
+@@ -44,26 +44,9 @@
+ CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * 
const patches);
+ CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, 
const cJSON * const patches);
+ 
+-/*
+-// Note that ApplyPatches is NOT atomic on failure. To implement an atomic 
ApplyPatches, use:
+-//int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches)
+-//{
+-//    cJSON *modme = cJSON_Duplicate(*object, 1);
+-//    int error = cJSONUtils_ApplyPatches(modme, patches);
+-//    if (!error)
+-//    {
+-//        cJSON_Delete(*object);
+-//        *object = modme;
+-//    }
+-//    else
+-//    {
+-//        cJSON_Delete(modme);
+-//    }
+-//
+-//    return error;
+-//}
+-// Code not added to library since this strategy is a LOT slower.
+-*/
++/* cJSONUtils_ApplyPatches and cJSONUtils_ApplyPatchesCaseSensitive are
++ * atomic: either all of the patches are applied, or the object is left
++ * unchanged. */
+ 
+ /* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch 
spec. */
+ /* target will be modified by patch. return value is new ptr for target. */
diff -Nru cjson-1.7.18/debian/patches/0007-CVE-2026-87933.patch 
cjson-1.7.18/debian/patches/0007-CVE-2026-87933.patch
--- cjson-1.7.18/debian/patches/0007-CVE-2026-87933.patch       1970-01-01 
08:00:00.000000000 +0800
+++ cjson-1.7.18/debian/patches/0007-CVE-2026-87933.patch       2026-11-26 
16:34:25.000000000 +0800
@@ -0,0 +1,104 @@
+From 6d9f2443ab071f86e5d9b43025a40929ec41c46c Mon Sep 17 00:00:00 2001
+From: lilu5458 <[email protected]>
+Date: Wed, 16 Sep 2026 09:55:35 +0800
+Subject: [PATCH] Fix: heap-use-after-free in merge_patch when patch is subtree
+ of target (#1065)
+
+When cJSONUtils_MergePatch(target, patch) is called with a non-object
+patch (scalar, array, or NULL) that happens to be a subtree of target,
+merge_patch() called cJSON_Delete(target) first, which freed the patch
+memory, and then cJSON_Duplicate(patch, 1) read the already-freed memory,
+triggering a heap-use-after-free (detected by AddressSanitizer at
+cJSON_Duplicate_rec, cJSON.c:2808).
+
+Fix: duplicate the patch first into a local variable, then delete the
+target, then return the duplicate. This matches the Option B approach
+proposed in issue #1060.
+
+Verified locally:
+- Reproduced the UAF with a minimal PoC under ASan before the fix.
+- After the fix the PoC runs cleanly (exit 0, correct result [1,2,3]).
+- Added a regression unit test
+  (merge_patch_should_not_read_freed_memory_when_patch_is_subtree).
+- Full ctest suite passes (22/22 tests).
+
+Fixes #1060
+
+Signed-off-by: lilu <[email protected]>
+---
+ cJSON_Utils.c           |  8 ++++++--
+ tests/old_utils_tests.c | 34 ++++++++++++++++++++++++++++++++++
+ 2 files changed, 40 insertions(+), 2 deletions(-)
+
+diff --git a/cJSON_Utils.c b/cJSON_Utils.c
+index 8b38eb253..4f3a9ed8d 100644
+--- a/cJSON_Utils.c
++++ b/cJSON_Utils.c
+@@ -1324,9 +1324,13 @@ static cJSON *merge_patch(cJSON *target, const cJSON * 
const patch, const cJSON_
+ 
+     if (!cJSON_IsObject(patch))
+     {
+-        /* scalar value, array or NULL, just duplicate */
++        /* scalar value, array or NULL, just duplicate.
++         * Duplicate the patch first in case it is a subtree of target,
++         * otherwise cJSON_Delete(target) would free the patch memory
++         * and the subsequent cJSON_Duplicate would read freed memory. */
++        cJSON *duplicate = cJSON_Duplicate(patch, 1);
+         cJSON_Delete(target);
+-        return cJSON_Duplicate(patch, 1);
++        return duplicate;
+     }
+ 
+     if (!cJSON_IsObject(target))
+diff --git a/tests/old_utils_tests.c b/tests/old_utils_tests.c
+index 690dbb583..bdc739344 100644
+--- a/tests/old_utils_tests.c
++++ b/tests/old_utils_tests.c
+@@ -189,6 +189,39 @@ static void merge_tests(void)
+     }
+ }
+ 
++static void 
merge_patch_should_not_read_freed_memory_when_patch_is_subtree(void)
++{
++    /* When patch is a subtree of target, merge_patch must duplicate the patch
++     * before deleting target. Otherwise cJSON_Delete(target) frees the patch
++     * memory and the subsequent cJSON_Duplicate reads freed memory (UAF).
++     * See CVE candidate: heap-use-after-free in merge_patch (cJSON_Utils.c). 
*/
++    cJSON *target = cJSON_Parse("{\"a\":[1,2,3]}");
++    cJSON *patch = cJSON_GetObjectItem(target, "a");
++    cJSON *result = NULL;
++    cJSON *first = NULL;
++    cJSON *second = NULL;
++    cJSON *third = NULL;
++
++    TEST_ASSERT_NOT_NULL(target);
++    TEST_ASSERT_NOT_NULL(patch);
++
++    /* patch (array [1,2,3]) is a subtree of target. This used to trigger
++     * heap-use-after-free under AddressSanitizer before the fix. */
++    result = cJSONUtils_MergePatch(target, patch);
++    TEST_ASSERT_NOT_NULL(result);
++    TEST_ASSERT_TRUE(cJSON_IsArray(result));
++    TEST_ASSERT_EQUAL_INT(3, cJSON_GetArraySize(result));
++
++    first = cJSON_GetArrayItem(result, 0);
++    second = cJSON_GetArrayItem(result, 1);
++    third = cJSON_GetArrayItem(result, 2);
++    TEST_ASSERT_EQUAL_INT(1, first->valueint);
++    TEST_ASSERT_EQUAL_INT(2, second->valueint);
++    TEST_ASSERT_EQUAL_INT(3, third->valueint);
++
++    cJSON_Delete(result);
++}
++
+ static void generate_merge_tests(void)
+ {
+     size_t i = 0;
+@@ -219,6 +252,7 @@ int main(void)
+     RUN_TEST(misc_tests);
+     RUN_TEST(sort_tests);
+     RUN_TEST(merge_tests);
++    RUN_TEST(merge_patch_should_not_read_freed_memory_when_patch_is_subtree);
+     RUN_TEST(generate_merge_tests);
+ 
+     return UNITY_END();
diff -Nru cjson-1.7.18/debian/patches/series cjson-1.7.18/debian/patches/series
--- cjson-1.7.18/debian/patches/series  2025-09-12 06:24:25.000000000 +0800
+++ cjson-1.7.18/debian/patches/series  2026-11-26 16:34:25.000000000 +0800
@@ -1,2 +1,8 @@
 0001-allocate-memory-for-the-temporary-buffer.patch
 CVE-2025-57052.patch-
+0002-CVE-2026-16554.patch
+0003-CVE-2026-29036.patch
+0004-CVE-2026-67215.patch
+0005-CVE-2026-67216.patch
+0006-CVE-2026-67217.patch
+0007-CVE-2026-87933.patch

Reply via email to