Conversation
cJSON_ReplaceItemInObject() freed replacement->string before duplicating the string argument. When the caller passes the item's own name as the key (e.g. moving an item between objects under the same name), cJSON_strdup() read the freed buffer. Duplicate the key into new_key first, then release the old name — same ordering fix as 22a7d04 applied to add_item_to_object(). Also use new_key for the object lookup, since the original pointer may dangle after the free. Fixes DaveGamble#1081
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.
Fixes #1081 — heap-use-after-free in
cJSON_ReplaceItemInObject()reported with full PoC + ASan trace (CWE-416).Root cause
replace_item_in_object()freedreplacement->stringbefore duplicating thestringargument. When the caller passes the item's own name as the key — e.g. moving an item between objects under the same name —cJSON_strdup()read the just-freed buffer. Without ASan it silently fails the replace (ok=0, object unchanged); under ASan it's a clean heap-UAF read instrlen←cJSON_strdup.Fix
Duplicate the key into
new_keyfirst, then release the old name and assign — the same ordering fix that 22a7d04 applied toadd_item_to_object()for #248.new_keyis also used for the object lookup, since the originalstringpointer may dangle after the free.Side effect: a failed
strdupno longer leavesreplacement->stringclobbered toNULL— the item keeps its old name on OOM.Verification
-fsanitize=addresson master:heap-use-after-free … cJSON_strdup cJSON.c:198 ← replace_item_in_object cJSON.c:2439ok=1 dst={"timeout":30}, zero ASan reportsctest: 19/19 pass under ASan