diff --git a/fuzzing/cjson_utils_fuzzer.c b/fuzzing/cjson_utils_fuzzer.c new file mode 100644 index 000000000..c08931744 --- /dev/null +++ b/fuzzing/cjson_utils_fuzzer.c @@ -0,0 +1,306 @@ +/* + Copyright (c) 2009-2026 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* + Fuzzer for cJSON_Utils: RFC 6901 JSON Pointer, RFC 6902 JSON Patch and + RFC 7396 JSON Merge Patch. + + Threat model: applications routinely hand untrusted JSON documents to + cJSON_Utils (configuration stores, document databases, HTTP PATCH and + JSON-merge endpoints). The attacker controls the target document, the + patch document and the raw pointer text; this harness derives all three + from the fuzz input. The module's recent memory-safety history + (heap-use-after-free in merge_patch when the patch is a subtree of the + target, type confusion on patch values) is the bug-class mix this + harness guards: use-after-free, type confusion, NULL dereference and + leaks on attacker-shaped documents. + + Input layout (segments are NUL-separated, the last byte must be '\0'): + data[0] bit0: use the CaseSensitive API variants + bit1: formatted printing + data[1] operation selector (modulo 6) + data[2] patch "op" keyword selector for the AddPatchToArray operation + seg 1 document A (target document) + seg 2 document B (patch / second document) + seg 3 raw pointer text, also parsed as patch document C +*/ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cJSON.h" +#include "../cJSON_Utils.h" + +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); /* required by C89 */ + +static const char *patch_operations[6] = +{ + "add", "remove", "replace", "move", "copy", "test" +}; + +/* Extract the next NUL-terminated segment of the input. Returns the start + * position of the following segment, or size when the input is exhausted. */ +static size_t next_segment(const uint8_t* data, size_t size, size_t start, const char** segment) +{ + size_t position = start; + + while ((position < size) && (data[position] != '\0')) + { + position++; + } + + if (position >= size) + { + *segment = NULL; + return size; + } + + *segment = (const char*)&data[start]; + return position + 1; +} + +static void print_document(const cJSON *document, cJSON_bool formatted) +{ + char *printed = NULL; + + printed = cJSON_PrintBuffered(document, 1, formatted); + cJSON_free(printed); + + printed = cJSON_PrintUnformatted(document); + cJSON_free(printed); +} + +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + const char *document_a_text = NULL; + const char *document_b_text = NULL; + const char *pointer_text = NULL; + size_t position = 0; + cJSON *document_a = NULL; + cJSON *document_b = NULL; + cJSON *document_c = NULL; + cJSON *found = NULL; + cJSON_bool case_sensitive = 0; + cJSON_bool formatted = 0; + int operation = 0; + int patch_operation = 0; + + if (size < 8) + { + return 0; + } + if (data[size - 1] != '\0') + { + return 0; + } + + case_sensitive = (cJSON_bool)(data[0] & 1); + formatted = (cJSON_bool)((data[0] >> 1) & 1); + operation = (int)(data[1] % 6); + patch_operation = (int)(data[2] % 6); + + position = next_segment(data, size, 3, &document_a_text); + position = next_segment(data, size, position, &document_b_text); + (void)next_segment(data, size, position, &pointer_text); + + document_a = cJSON_Parse(document_a_text); + document_b = cJSON_Parse(document_b_text); + document_c = cJSON_Parse(pointer_text); + + switch (operation) + { + case 0: /* RFC 6902: apply the patch array from the third segment */ + { + if ((document_a != NULL) && (document_c != NULL)) + { + if (case_sensitive) + { + (void)cJSONUtils_ApplyPatchesCaseSensitive(document_a, document_c); + } + else + { + (void)cJSONUtils_ApplyPatches(document_a, document_c); + } + } + break; + } + + case 1: /* RFC 7396: merge patch; target ownership moves to the result */ + { + if (document_a != NULL) + { + if (case_sensitive) + { + document_a = cJSONUtils_MergePatchCaseSensitive(document_a, document_b); + } + else + { + document_a = cJSONUtils_MergePatch(document_a, document_b); + } + } + break; + } + + case 2: /* generate a patch between the documents, then apply it */ + { + if ((document_a != NULL) && (document_b != NULL)) + { + cJSON *from = cJSON_Duplicate(document_a, 1); + cJSON *to = cJSON_Duplicate(document_b, 1); + + if ((from != NULL) && (to != NULL)) + { + cJSON *generated = NULL; + + /* GeneratePatches sorts 'from' and 'to' in place. */ + if (case_sensitive) + { + generated = cJSONUtils_GeneratePatchesCaseSensitive(from, to); + } + else + { + generated = cJSONUtils_GeneratePatches(from, to); + } + + if (generated != NULL) + { + if (case_sensitive) + { + (void)cJSONUtils_ApplyPatchesCaseSensitive(document_a, generated); + } + else + { + (void)cJSONUtils_ApplyPatches(document_a, generated); + } + print_document(generated, formatted); + cJSON_Delete(generated); + } + } + cJSON_Delete(from); + cJSON_Delete(to); + } + break; + } + + case 3: /* RFC 6901: pointer lookup, pointer construction and sorting */ + { + if (document_a != NULL) + { + char *constructed = NULL; + + found = case_sensitive + ? cJSONUtils_GetPointerCaseSensitive(document_a, pointer_text) + : cJSONUtils_GetPointer(document_a, pointer_text); + if (found != NULL) + { + constructed = cJSONUtils_FindPointerFromObjectTo(document_a, found); + cJSON_free(constructed); + } + /* the sort API is documented for object members */ + if (cJSON_IsObject(document_a)) + { + if (case_sensitive) + { + cJSONUtils_SortObjectCaseSensitive(document_a); + } + else + { + cJSONUtils_SortObject(document_a); + } + } + } + break; + } + + case 4: /* merge with a patch that is a subtree of the target; guards + * the heap-use-after-free fixed in upstream patch #1065. + * Self-merge (patch == target, e.g. the RFC 6901 empty + * pointer resolving to the root) is routed around: it is + * a known unresolved heap-use-after-free upstream, see + * FINDING-1 in the run findings. */ + { + if (document_a != NULL) + { + found = case_sensitive + ? cJSONUtils_GetPointerCaseSensitive(document_a, pointer_text) + : cJSONUtils_GetPointer(document_a, pointer_text); + if ((found != NULL) && (found != document_a)) + { + if (case_sensitive) + { + document_a = cJSONUtils_MergePatchCaseSensitive(document_a, found); + } + else + { + document_a = cJSONUtils_MergePatch(document_a, found); + } + } + } + break; + } + + default: /* build a patch entry with AddPatchToArray, then apply it */ + { + if ((document_a != NULL) && (pointer_text != NULL)) + { + cJSON *built = cJSON_CreateArray(); + + if (built != NULL) + { + cJSONUtils_AddPatchToArray(built, patch_operations[patch_operation], + pointer_text, document_b); + if (case_sensitive) + { + (void)cJSONUtils_ApplyPatchesCaseSensitive(document_a, built); + } + else + { + (void)cJSONUtils_ApplyPatches(document_a, built); + } + print_document(built, formatted); + cJSON_Delete(built); + } + } + break; + } + } + + if (document_a != NULL) + { + print_document(document_a, formatted); + } + + cJSON_Delete(document_a); + cJSON_Delete(document_b); + cJSON_Delete(document_c); + + return 0; +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/fuzzing/inputs_utils/seed_addpatch_add b/fuzzing/inputs_utils/seed_addpatch_add new file mode 100644 index 000000000..421a4615a Binary files /dev/null and b/fuzzing/inputs_utils/seed_addpatch_add differ diff --git a/fuzzing/inputs_utils/seed_addpatch_test b/fuzzing/inputs_utils/seed_addpatch_test new file mode 100644 index 000000000..32411e323 Binary files /dev/null and b/fuzzing/inputs_utils/seed_addpatch_test differ diff --git a/fuzzing/inputs_utils/seed_apply_move_copy b/fuzzing/inputs_utils/seed_apply_move_copy new file mode 100644 index 000000000..a3231d52d Binary files /dev/null and b/fuzzing/inputs_utils/seed_apply_move_copy differ diff --git a/fuzzing/inputs_utils/seed_apply_valid b/fuzzing/inputs_utils/seed_apply_valid new file mode 100644 index 000000000..0bee58e66 Binary files /dev/null and b/fuzzing/inputs_utils/seed_apply_valid differ diff --git a/fuzzing/inputs_utils/seed_gen_patches_arrays b/fuzzing/inputs_utils/seed_gen_patches_arrays new file mode 100644 index 000000000..d840177f7 Binary files /dev/null and b/fuzzing/inputs_utils/seed_gen_patches_arrays differ diff --git a/fuzzing/inputs_utils/seed_gen_patches_mixed b/fuzzing/inputs_utils/seed_gen_patches_mixed new file mode 100644 index 000000000..c553caf94 Binary files /dev/null and b/fuzzing/inputs_utils/seed_gen_patches_mixed differ diff --git a/fuzzing/inputs_utils/seed_merge_case b/fuzzing/inputs_utils/seed_merge_case new file mode 100644 index 000000000..7030ac310 Binary files /dev/null and b/fuzzing/inputs_utils/seed_merge_case differ diff --git a/fuzzing/inputs_utils/seed_merge_object b/fuzzing/inputs_utils/seed_merge_object new file mode 100644 index 000000000..ff736ec2e Binary files /dev/null and b/fuzzing/inputs_utils/seed_merge_object differ diff --git a/fuzzing/inputs_utils/seed_merge_scalar b/fuzzing/inputs_utils/seed_merge_scalar new file mode 100644 index 000000000..03194401c Binary files /dev/null and b/fuzzing/inputs_utils/seed_merge_scalar differ diff --git a/fuzzing/inputs_utils/seed_pointer_badindex b/fuzzing/inputs_utils/seed_pointer_badindex new file mode 100644 index 000000000..a0ee37861 Binary files /dev/null and b/fuzzing/inputs_utils/seed_pointer_badindex differ diff --git a/fuzzing/inputs_utils/seed_pointer_deep b/fuzzing/inputs_utils/seed_pointer_deep new file mode 100644 index 000000000..e8a703657 Binary files /dev/null and b/fuzzing/inputs_utils/seed_pointer_deep differ diff --git a/fuzzing/inputs_utils/seed_pointer_escapes b/fuzzing/inputs_utils/seed_pointer_escapes new file mode 100644 index 000000000..eb7e94cf7 Binary files /dev/null and b/fuzzing/inputs_utils/seed_pointer_escapes differ diff --git a/fuzzing/inputs_utils/seed_subtree_merge_obj b/fuzzing/inputs_utils/seed_subtree_merge_obj new file mode 100644 index 000000000..ecf65d461 Binary files /dev/null and b/fuzzing/inputs_utils/seed_subtree_merge_obj differ diff --git a/fuzzing/inputs_utils/seed_subtree_merge_scalar b/fuzzing/inputs_utils/seed_subtree_merge_scalar new file mode 100644 index 000000000..e28410320 Binary files /dev/null and b/fuzzing/inputs_utils/seed_subtree_merge_scalar differ diff --git a/fuzzing/json_utils.dict b/fuzzing/json_utils.dict new file mode 100644 index 000000000..098e222bf --- /dev/null +++ b/fuzzing/json_utils.dict @@ -0,0 +1,67 @@ +# +# AFL dictionary for JSON +# ----------------------------- +# + +object_start="{" +object_end="}" +object_empty="{}" +object_one_element="{\"one\":1}" +object_two_elements="{\"1\":1,\"2\":2}" +object_separator=":" + +array_start="[" +array_end="]" +array_empty="[]" +array_one_element="[1]" +array_two_elements="[1,2]" + +separator="," + +escape_sequence_b="\\b" +escape_sequence_f="\\f" +escape_sequence_n="\\n" +escape_sequence_r="\\r" +escape_sequence_t="\\t" +escape_sequence_quote="\\\"" +escape_sequence_backslash="\\\\" +escape_sequence_slash="\\/" +escape_sequence_utf16_base="\\u" +escape_sequence_utf16="\\u12ab" + +number_integer="1" +number_double="1.0" +number_negative_integer="-1" +number_negative_double="-1.0" +number_engineering1="1e1" +number_engineering2="1e-1" +number_positive_integer="+1" +number_positive_double="+1.0" +number_e="e" +number_plus="+" +number_minus="-" +number_separator="." + +null="null" +true="true" +false="false" + +# cJSON_Utils additions: RFC 6902 patch keywords, RFC 6901 pointer tokens +patch_add="\"op\":\"add\"" +patch_remove="\"op\":\"remove\"" +patch_replace="\"op\":\"replace\"" +patch_move="\"op\":\"move\"" +patch_copy="\"op\":\"copy\"" +patch_test="\"op\":\"test\"" +patch_field_op="\"op\"" +patch_field_path="\"path\"" +patch_field_value="\"value\"" +patch_field_from="\"from\"" +pointer_root="/" +pointer_index0="/0" +pointer_index1="/1" +pointer_end="-" +pointer_escape_tilde="~0" +pointer_escape_slash="~1" +pointer_member="/a" +pointer_nested="/a/0/b" diff --git a/fuzzing/ossfuzz.sh b/fuzzing/ossfuzz.sh index a2da64bf6..349537846 100755 --- a/fuzzing/ossfuzz.sh +++ b/fuzzing/ossfuzz.sh @@ -5,14 +5,22 @@ mkdir build cd build -cmake -DBUILD_SHARED_LIBS=OFF -DENABLE_CJSON_TEST=OFF .. +cmake -DBUILD_SHARED_LIBS=OFF -DENABLE_CJSON_TEST=OFF -DENABLE_CJSON_UTILS=ON .. make -j$(nproc) $CXX $CXXFLAGS $SRC/cjson/fuzzing/cjson_read_fuzzer.c -I. \ -o $OUT/cjson_read_fuzzer \ $LIB_FUZZING_ENGINE $SRC/cjson/build/libcjson.a +$CXX $CXXFLAGS $SRC/cjson/fuzzing/cjson_utils_fuzzer.c -I. \ + -o $OUT/cjson_utils_fuzzer \ + $LIB_FUZZING_ENGINE $SRC/cjson/build/libcjson_utils.a $SRC/cjson/build/libcjson.a + find $SRC/cjson/fuzzing/inputs -name "*" | \ xargs zip $OUT/cjson_read_fuzzer_seed_corpus.zip +find $SRC/cjson/fuzzing/inputs_utils -name "*" | \ + xargs zip $OUT/cjson_utils_fuzzer_seed_corpus.zip + cp $SRC/cjson/fuzzing/json.dict $OUT/cjson_read_fuzzer.dict +cp $SRC/cjson/fuzzing/json_utils.dict $OUT/cjson_utils_fuzzer.dict