Skip to content

Commit df6b519

Browse files
openharmony_cigitee-org
authored andcommitted
!84 fix 【漏洞修复】 CVE-2023-26819
Merge pull request !84 from 蒋码云/master
2 parents 3833805 + 6689549 commit df6b519

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

cJSON.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,14 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
30143014
}
30153015

30163016
/* Duplication */
3017+
cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse);
3018+
30173019
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
3020+
{
3021+
return cJSON_Duplicate_rec(item, 0, recurse );
3022+
}
3023+
3024+
cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse)
30183025
{
30193026
cJSON *newitem = NULL;
30203027
cJSON *child = NULL;
@@ -3061,7 +3068,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
30613068
child = item->child;
30623069
while (child != NULL)
30633070
{
3064-
newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */
3071+
if(depth >= CJSON_CIRCULAR_LIMIT) {
3072+
goto fail;
3073+
}
3074+
newchild = cJSON_Duplicate_rec(child, ++depth, true); /* Duplicate (with recurse) each item in the ->next chain */
30653075
if (!newchild)
30663076
{
30673077
goto fail;

cJSON.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ typedef int cJSON_bool;
150150
#define CJSON_NESTING_LIMIT 1000
151151
#endif
152152

153+
/* Limits the length of circular references can be before cJSON rejects to parse them.
154+
* This is to prevent stack overflows. */
155+
#ifndef CJSON_CIRCULAR_LIMIT
156+
#define CJSON_CIRCULAR_LIMIT 10000
157+
#endif
158+
153159
/* returns the version of cJSON as a string */
154160
CJSON_PUBLIC(const char*) cJSON_Version(void);
155161

tests/misc_tests.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ static void cjson_should_not_parse_to_deeply_nested_jsons(void)
219219
TEST_ASSERT_NULL_MESSAGE(cJSON_Parse(deep_json), "To deep JSONs should not be parsed.");
220220
}
221221

222+
static void cjson_should_not_follow_too_deep_circular_references(void)
223+
{
224+
cJSON *o = cJSON_CreateArray();
225+
cJSON *a = cJSON_CreateArray();
226+
cJSON *b = cJSON_CreateArray();
227+
cJSON *x;
228+
229+
cJSON_AddItemToArray(o, a);
230+
cJSON_AddItemToArray(a, b);
231+
cJSON_AddItemToArray(b, o);
232+
233+
x = cJSON_Duplicate(o, 1);
234+
TEST_ASSERT_NULL(x);
235+
cJSON_DetachItemFromArray(b, 0);
236+
cJSON_Delete(o);
237+
}
238+
222239
static void cjson_set_number_value_should_set_numbers(void)
223240
{
224241
cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number, NULL, 0, 0, NULL}};
@@ -753,6 +770,7 @@ int CJSON_CDECL main(void)
753770
RUN_TEST(cjson_get_object_item_case_sensitive_should_not_crash_with_array);
754771
RUN_TEST(typecheck_functions_should_check_type);
755772
RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons);
773+
RUN_TEST(cjson_should_not_follow_too_deep_circular_references);
756774
RUN_TEST(cjson_set_number_value_should_set_numbers);
757775
RUN_TEST(cjson_detach_item_via_pointer_should_detach_items);
758776
RUN_TEST(cjson_replace_item_via_pointer_should_replace_items);

0 commit comments

Comments
 (0)