Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cJSON/blob/master/tests/old_utils_tests.c # cJSON [Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `cJSON / tests / old_utils_tests.c` 259 lines ยท 9150 bytes Raw Blame History 1 /* 2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 */ 22 23 #include 24 #include 25 #include 26 27 #include "unity/examples/unity_config.h" 28 #include "unity/src/unity.h" 29 #include "common.h" 30 #include "../cJSON_Utils.h" 31 32 /* JSON Apply Merge tests: */ 33 static const char *merges[15][3] = 34 { 35 {"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"}, 36 {"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"}, 37 {"{\"a\":\"b\"}", "{\"a\":null}", "{}"}, 38 {"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"}, 39 {"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"}, 40 {"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"}, 41 {"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"}, 42 {"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"}, 43 {"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"}, 44 {"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"}, 45 {"{\"a\":\"foo\"}", "null", "null"}, 46 {"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""}, 47 {"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"}, 48 {"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"}, 49 {"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"} 50 }; 51 52 static void json_pointer_tests(void) 53 { 54 cJSON *root = NULL; 55 const char *json= 56 "{" 57 "\"foo\": [\"bar\", \"baz\"]," 58 "\"\": 0," 59 "\"a/b\": 1," 60 "\"c%d\": 2," 61 "\"e^f\": 3," 62 "\"g|h\": 4," 63 "\"i\\\\j\": 5," 64 "\"k\\\"l\": 6," 65 "\" \": 7," 66 "\"m~n\": 8" 67 "}"; 68 69 root = cJSON_Parse(json); 70 71 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, ""), root); 72 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo"), cJSON_GetObjectItem(root, "foo")); 73 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo/0"), cJSON_GetObjectItem(root, "foo")->child); 74 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo/0"), cJSON_GetObjectItem(root, "foo")->child); 75 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/"), cJSON_GetObjectItem(root, "")); 76 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/a~1b"), cJSON_GetObjectItem(root, "a/b")); 77 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c%d"), cJSON_GetObjectItem(root, "c%d")); 78 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c^f"), cJSON_GetObjectItem(root, "c^f")); 79 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c|f"), cJSON_GetObjectItem(root, "c|f")); 80 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/i\\j"), cJSON_GetObjectItem(root, "i\\j")); 81 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/k\"l"), cJSON_GetObjectItem(root, "k\"l")); 82 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/ "), cJSON_GetObjectItem(root, " ")); 83 TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/m~0n"), cJSON_GetObjectItem(root, "m~n")); 84 85 cJSON_Delete(root); 86 } 87 88 static void misc_tests(void) 89 { 90 /* Misc tests */ 91 int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 92 cJSON *object = NULL; 93 cJSON *object1 = NULL; 94 cJSON *object2 = NULL; 95 cJSON *object3 = NULL; 96 cJSON *object4 = NULL; 97 cJSON *nums = NULL; 98 cJSON *num6 = NULL; 99 char *pointer = NULL; 100 101 printf("JSON Pointer construct\n"); 102 object = cJSON_CreateObject(); 103 nums = cJSON_CreateIntArray(numbers, 10); 104 num6 = cJSON_GetArrayItem(nums, 6); 105 cJSON_AddItemToObject(object, "numbers", nums); 106 107 pointer = cJSONUtils_FindPointerFromObjectTo(object, num6); 108 TEST_ASSERT_EQUAL_STRING("/numbers/6", pointer); 109 free(pointer); 110 111 pointer = cJSONUtils_FindPointerFromObjectTo(object, nums); 112 TEST_ASSERT_EQUAL_STRING("/numbers", pointer); 113 free(pointer); 114 115 pointer = cJSONUtils_FindPointerFromObjectTo(object, object); 116 TEST_ASSERT_EQUAL_STRING("", pointer); 117 free(pointer); 118 119 object1 = cJSON_CreateObject(); 120 object2 = cJSON_CreateString("m~n"); 121 cJSON_AddItemToObject(object1, "m~n", object2); 122 pointer = cJSONUtils_FindPointerFromObjectTo(object1, object2); 123 TEST_ASSERT_EQUAL_STRING("/m~0n",pointer); 124 free(pointer); 125 126 object3 = cJSON_CreateObject(); 127 object4 = cJSON_CreateString("m/n"); 128 cJSON_AddItemToObject(object3, "m/n", object4); 129 pointer = cJSONUtils_FindPointerFromObjectTo(object3, object4); 130 TEST_ASSERT_EQUAL_STRING("/m~1n",pointer); 131 free(pointer); 132 133 cJSON_Delete(object); 134 cJSON_Delete(object1); 135 cJSON_Delete(object3); 136 } 137 138 static void sort_tests(void) 139 { 140 /* Misc tests */ 141 const char *random = "QWERTYUIOPASDFGHJKLZXCVBNM"; 142 char buf[2] = {'\0', '\0'}; 143 cJSON *sortme = NULL; 144 size_t i = 0; 145 cJSON *current_element = NULL; 146 147 /* JSON Sort test: */ 148 sortme = cJSON_CreateObject(); 149 for (i = 0; i < 26; i++) 150 { 151 buf[0] = random[i]; 152 cJSON_AddItemToObject(sortme, buf, cJSON_CreateNumber(1)); 153 } 154 155 cJSONUtils_SortObject(sortme); 156 157 /* check sorting */ 158 current_element = sortme->child->next; 159 for (i = 1; (i < 26) && (current_element != NULL) && (current_element->prev != NULL); i++) 160 { 161 TEST_ASSERT_TRUE(current_element->string[0] >= current_element->prev->string[0]); 162 current_element = current_element->next; 163 } 164 165 cJSON_Delete(sortme); 166 } 167 168 static void merge_tests(void) 169 { 170 size_t i = 0; 171 char *patchtext = NULL; 172 char *after = NULL; 173 174 /* Merge tests: */ 175 printf("JSON Merge Patch tests\n"); 176 for (i = 0; i < 15; i++) 177 { 178 cJSON *object_to_be_merged = cJSON_Parse(merges[i][0]); 179 cJSON *patch = cJSON_Parse(merges[i][1]); 180 patchtext = cJSON_PrintUnformatted(patch); 181 object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch); 182 after = cJSON_PrintUnformatted(object_to_be_merged); 183 TEST_ASSERT_EQUAL_STRING(merges[i][2], after); 184 185 free(patchtext); 186 free(after); 187 cJSON_Delete(object_to_be_merged); 188 cJSON_Delete(patch); 189 } 190 } 191 192 static void merge_patch_should_not_read_freed_memory_when_patch_is_subtree(void) 193 { 194 /* When patch is a subtree of target, merge_patch must duplicate the patch 195 * before deleting target. Otherwise cJSON_Delete(target) frees the patch 196 * memory and the subsequent cJSON_Duplicate reads freed memory (UAF). 197 * See CVE candidate: heap-use-after-free in merge_patch (cJSON_Utils.c). */ 198 cJSON *target = cJSON_Parse("{\"a\":[1,2,3]}"); 199 cJSON *patch = cJSON_GetObjectItem(target, "a"); 200 cJSON *result = NULL; 201 cJSON *first = NULL; 202 cJSON *second = NULL; 203 cJSON *third = NULL; 204 205 TEST_ASSERT_NOT_NULL(target); 206 TEST_ASSERT_NOT_NULL(patch); 207 208 /* patch (array [1,2,3]) is a subtree of target. This used to trigger 209 * heap-use-after-free under AddressSanitizer before the fix. */ 210 result = cJSONUtils_MergePatch(target, patch); 211 TEST_ASSERT_NOT_NULL(result); 212 TEST_ASSERT_TRUE(cJSON_IsArray(result)); 213 TEST_ASSERT_EQUAL_INT(3, cJSON_GetArraySize(result)); 214 215 first = cJSON_GetArrayItem(result, 0); 216 second = cJSON_GetArrayItem(result, 1); 217 third = cJSON_GetArrayItem(result, 2); 218 TEST_ASSERT_EQUAL_INT(1, first->valueint); 219 TEST_ASSERT_EQUAL_INT(2, second->valueint); 220 TEST_ASSERT_EQUAL_INT(3, third->valueint); 221 222 cJSON_Delete(result); 223 } 224 225 static void generate_merge_tests(void) 226 { 227 size_t i = 0; 228 char *patchedtext = NULL; 229 230 /* Generate Merge tests: */ 231 for (i = 0; i < 15; i++) 232 { 233 cJSON *from = cJSON_Parse(merges[i][0]); 234 cJSON *to = cJSON_Parse(merges[i][2]); 235 cJSON *patch = cJSONUtils_GenerateMergePatch(from,to); 236 from = cJSONUtils_MergePatch(from,patch); 237 patchedtext = cJSON_PrintUnformatted(from); 238 TEST_ASSERT_EQUAL_STRING(merges[i][2], patchedtext); 239 240 cJSON_Delete(from); 241 cJSON_Delete(to); 242 cJSON_Delete(patch); 243 free(patchedtext); 244 } 245 } 246 247 int main(void) 248 { 249 UNITY_BEGIN(); 250 251 RUN_TEST(json_pointer_tests); 252 RUN_TEST(misc_tests); 253 RUN_TEST(sort_tests); 254 RUN_TEST(merge_tests); 255 RUN_TEST(merge_patch_should_not_read_freed_memory_when_patch_is_subtree); 256 RUN_TEST(generate_merge_tests); 257 258 return UNITY_END(); 259 }