Fix: heap-use-after-free in merge_patch when patch is subtree of target - #1065
Open
lilu5458 wants to merge 1 commit into
Open
Fix: heap-use-after-free in merge_patch when patch is subtree of target#1065lilu5458 wants to merge 1 commit into
lilu5458 wants to merge 1 commit into
Conversation
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 DaveGamble#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 DaveGamble#1060 Signed-off-by: lilu <lilu@kylinos.cn>
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.
Summary
Fixes a heap-use-after-free (UAF) in
cJSONUtils_MergePatch()reported in #1060, wherepatchis a non-object subtree oftarget.Root Cause
In
merge_patch()(cJSON_Utils.c), whenpatchis a non-object (scalar, array, or NULL), the function used to:AddressSanitizer confirms the UAF read at
cJSON_Duplicate_rec(cJSON.c:2808), freed bycJSON_Delete(cJSON.c:273) called frommerge_patch(cJSON_Utils.c:1328).Fix
Reorder the operations: duplicate
patchinto a local variable first, then deletetarget, then return the duplicate. This matches the "Option B — Duplicate before delete" approach proposed in #1060.This is a small bug/security fix, so it targets
masterper the contributing guide.Verification
poc_uaf_merge_patch.c) —target={"a":[1,2,3]},patch=cJSON_GetObjectItem(target,"a"). Under ASan the original code crashes atcJSON_Duplicate_rec(cJSON.c:2808); after the fix the PoC runs cleanly (exit 0) and printsresult: [1, 2, 3].merge_patch_should_not_read_freed_memory_when_patch_is_subtreetotests/old_utils_tests.ccovering the subtree-patch scenario.ctestpasses 22/22 tests (including the new one) with no regressions.Credit
The vulnerability and the "duplicate before delete" fix approach were independently reported in #1060 by @1820893135-pixel. This PR implements that fix, adds a PoC + ASan evidence, and adds a regression unit test.
Fixes #1060