Executive brief
cJSON is a lightweight JSON parsing library used in many C/C++ applications. A use-after-free vulnerability exists in the MergePatch function, which is used to merge JSON objects. An attacker who can control JSON input to an application using this library could cause a crash or potentially execute arbitrary code by triggering memory corruption.
Technical details
A heap use-after-free (UAF) exists in cJSON's RFC 7386 MergePatch implementation within the merge_patch() function in cJSON_Utils.c. When cJSONUtils_MergePatch() is called with a non-object patch (e.g., an array or scalar), the function frees the target object at line 1328, then immediately calls cJSON_Duplicate(patch, 1). The cJSON_Duplicate_rec() function dereferences the freed memory when reading item->type from the already-freed object, causing a use-after-free condition. The vulnerability requires the attacker to supply malformed JSON input to an application that uses the MergePatch API. A fix is available via pull request: duplicate the patch before freeing the target, or null-check before freeing.
Affected products
- DaveGamble cJSON up to 1.7.19
Timeline
- 2026-09-10: disclosed
- other: Exploit made public; fix awaits pull request acceptance