Conversation
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 #1082 — heap-use-after-free in
cJSONUtils_ApplyPatches()/cJSONUtils_ApplyPatchesCaseSensitive()reported with full PoC + ASan trace (CWE-416).Root cause
Both functions walk the patch array via
current_patch->next. When the patch array is itself part of the document being patched and aremoveoperation deletes it,apply_patch()→cJSON_Delete()frees the node the loop is iterating over; the nextcurrent_patch->nextdereference reads freed memory.ASan:
heap-use-after-freeREAD of size 8 atcJSON_Utils.c:1061, freed bycJSON_Delete←apply_patch(cJSON_Utils.c:896).This is the same class that #1065 fixed in
cJSONUtils_MergePatch()— there the patch could be a subtree of the merge target, here the patch list can be a subtree of the patched object.Fix
Iterate over a deep copy (
cJSON_Duplicate(patches, true)) in both functions.apply_patch()already duplicates thevalueit inserts intoobject, so nothing inobjectpoints into the copy; deleting the copy after the loop is leak-free on both the success and error paths. The copy costs one duplicate per call — the alternative (documenting thatpatchesmust not be part ofobject) leaves the crash reachable.Verification
-fsanitize=addresson master (6d9f244):heap-use-after-freeREAD atcJSON_Utils.c:1061←apply_patch←cJSON_Deleterc=0for bothcJSONUtils_ApplyPatchesandcJSONUtils_ApplyPatchesCaseSensitive, zero ASan reports; normal patching (patchesoutsideobject) unaffectedtests/old_utils_tests.c(mirroring the Fix: heap-use-after-free in merge_patch when patch is subtree of target #1065 test pattern): they trigger the ASan abort on unfixed code (verified red), pass fixedctest: 22/22 pass under ASan