From 878227e7217f3363f9c095b7fb8c1dbdde1ec34f Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 11 Jun 2019 21:48:17 -0700 Subject: [PATCH] bpo-29505: Fuzz json module, enforce size limit on int(x) fuzz (GH-13991) * bpo-29505: Enable fuzz testing of the json module, enforce size limit on int(x) fuzz and json input size to avoid timeouts. Contributed by by Ammar Askar for Google. (cherry picked from commit a6e190e94b47324f14e22a09200c68b722d55699) Co-authored-by: Ammar Askar --- Modules/_xxtestfuzz/README.rst | 10 ++++ .../dictionaries/fuzz_json_loads.dict | 40 +++++++++++++ .../fuzz_json_loads_corpus/empty_array.json | 1 + .../fuzz_json_loads_corpus/empty_object.json | 1 + .../fuzz_json_loads_corpus/pass1.json | 58 +++++++++++++++++++ .../fuzz_json_loads_corpus/pass2.json | 1 + .../fuzz_json_loads_corpus/pass3.json | 6 ++ .../fuzz_json_loads_corpus/simple_array.json | 1 + Modules/_xxtestfuzz/fuzz_tests.txt | 1 + Modules/_xxtestfuzz/fuzzer.c | 53 ++++++++++++++++- 10 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 Modules/_xxtestfuzz/dictionaries/fuzz_json_loads.dict create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_array.json create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_object.json create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass1.json create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass2.json create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass3.json create mode 100644 Modules/_xxtestfuzz/fuzz_json_loads_corpus/simple_array.json diff --git a/Modules/_xxtestfuzz/README.rst b/Modules/_xxtestfuzz/README.rst index b48f3c89a42..42bd02a03cb 100644 --- a/Modules/_xxtestfuzz/README.rst +++ b/Modules/_xxtestfuzz/README.rst @@ -35,6 +35,16 @@ And invoke it from ``LLVMFuzzerTestOneInput``:: ``LLVMFuzzerTestOneInput`` will run in oss-fuzz, with each test in ``fuzz_tests.txt`` run separately. +Seed data (corpus) for the test can be provided in a subfolder called +``_corpus`` such as ``fuzz_json_loads_corpus``. A wide variety +of good input samples allows the fuzzer to more easily explore a diverse +set of paths and provides a better base to find buggy input from. + +Dictionaries of tokens (see oss-fuzz documentation for more details) can +be placed in the ``dictionaries`` folder with the name of the test. +For example, ``dictionaries/fuzz_json_loads.dict`` contains JSON tokens +to guide the fuzzer. + What makes a good fuzz test --------------------------- diff --git a/Modules/_xxtestfuzz/dictionaries/fuzz_json_loads.dict b/Modules/_xxtestfuzz/dictionaries/fuzz_json_loads.dict new file mode 100644 index 00000000000..ad64917ccc2 --- /dev/null +++ b/Modules/_xxtestfuzz/dictionaries/fuzz_json_loads.dict @@ -0,0 +1,40 @@ +"0" +",0" +":0" +"0:" +"-1.2e+3" + +"true" +"false" +"null" + +"\"\"" +",\"\"" +":\"\"" +"\"\":" + +"{}" +",{}" +":{}" +"{\"\":0}" +"{{}}" + +"[]" +",[]" +":[]" +"[0]" +"[[]]" + +"''" +"\\" +"\\b" +"\\f" +"\\n" +"\\r" +"\\t" +"\\u0000" +"\\x00" +"\\0" +"\\uD800\\uDC00" +"\\uDBFF\\uDFFF" + diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_array.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_array.json new file mode 100644 index 00000000000..fe51488c706 --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_array.json @@ -0,0 +1 @@ +[] diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_object.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_object.json new file mode 100644 index 00000000000..0967ef424bc --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_object.json @@ -0,0 +1 @@ +{} diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass1.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass1.json new file mode 100644 index 00000000000..70e26854369 --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass1.json @@ -0,0 +1,58 @@ +[ + "JSON Test Pattern pass1", + {"object with 1 member":["array with 1 element"]}, + {}, + [], + -42, + true, + false, + null, + { + "integer": 1234567890, + "real": -9876.543210, + "e": 0.123456789e-12, + "E": 1.234567890E+34, + "": 23456789012E66, + "zero": 0, + "one": 1, + "space": " ", + "quote": "\"", + "backslash": "\\", + "controls": "\b\f\n\r\t", + "slash": "/ & \/", + "alpha": "abcdefghijklmnopqrstuvwyz", + "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", + "digit": "0123456789", + "0123456789": "digit", + "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", + "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", + "true": true, + "false": false, + "null": null, + "array":[ ], + "object":{ }, + "address": "50 St. James Street", + "url": "http://www.JSON.org/", + "comment": "// /* */": " ", + " s p a c e d " :[1,2 , 3 + +, + +4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], + "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", + "quotes": "" \u0022 %22 0x22 034 "", + "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" +: "A key can be any string" + }, + 0.5 ,98.6 +, +99.44 +, + +1066, +1e1, +0.1e1, +1e-1, +1e00,2e+00,2e-00 +,"rosebud"] \ No newline at end of file diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass2.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass2.json new file mode 100644 index 00000000000..d3c63c7ad84 --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass2.json @@ -0,0 +1 @@ +[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] \ No newline at end of file diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass3.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass3.json new file mode 100644 index 00000000000..4528d51f1ac --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass3.json @@ -0,0 +1,6 @@ +{ + "JSON Test Pattern pass3": { + "The outermost value": "must be an object or array.", + "In this test": "It is an object." + } +} diff --git a/Modules/_xxtestfuzz/fuzz_json_loads_corpus/simple_array.json b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/simple_array.json new file mode 100644 index 00000000000..ce1e6ecaec7 --- /dev/null +++ b/Modules/_xxtestfuzz/fuzz_json_loads_corpus/simple_array.json @@ -0,0 +1 @@ +[1, 2, 3, "abcd", "xyz"] diff --git a/Modules/_xxtestfuzz/fuzz_tests.txt b/Modules/_xxtestfuzz/fuzz_tests.txt index 2e53bfdc716..f0121291eaa 100644 --- a/Modules/_xxtestfuzz/fuzz_tests.txt +++ b/Modules/_xxtestfuzz/fuzz_tests.txt @@ -1,3 +1,4 @@ fuzz_builtin_float fuzz_builtin_int fuzz_builtin_unicode +fuzz_json_loads diff --git a/Modules/_xxtestfuzz/fuzzer.c b/Modules/_xxtestfuzz/fuzzer.c index 54f816ebc93..e862a99cfb3 100644 --- a/Modules/_xxtestfuzz/fuzzer.c +++ b/Modules/_xxtestfuzz/fuzzer.c @@ -28,8 +28,15 @@ static int fuzz_builtin_float(const char* data, size_t size) { return 0; } +#define MAX_INT_TEST_SIZE 0x10000 + /* Fuzz PyLong_FromUnicodeObject as a proxy for int(str). */ static int fuzz_builtin_int(const char* data, size_t size) { + /* Ignore test cases with very long ints to avoid timeouts + int("9" * 1000000) is not a very interesting test caase */ + if (size > MAX_INT_TEST_SIZE) { + return 0; + } /* Pick a random valid base. (When the fuzzed function takes extra parameters, it's somewhat normal to hash the input to generate those parameters. We want to exercise all code paths, so we do so here.) */ @@ -72,6 +79,42 @@ static int fuzz_builtin_unicode(const char* data, size_t size) { return 0; } +#define MAX_JSON_TEST_SIZE 0x10000 + +/* Initialized in LLVMFuzzerTestOneInput */ +PyObject* json_loads_method = NULL; +/* Fuzz json.loads(x) */ +static int fuzz_json_loads(const char* data, size_t size) { + /* Since python supports arbitrarily large ints in JSON, + long inputs can lead to timeouts on boring inputs like + `json.loads("9" * 100000)` */ + if (size > MAX_JSON_TEST_SIZE) { + return 0; + } + PyObject* input_bytes = PyBytes_FromStringAndSize(data, size); + if (input_bytes == NULL) { + return 0; + } + PyObject* parsed = PyObject_CallFunctionObjArgs(json_loads_method, input_bytes, NULL); + /* Ignore ValueError as the fuzzer will more than likely + generate some invalid json and values */ + if (parsed == NULL && PyErr_ExceptionMatches(PyExc_ValueError)) { + PyErr_Clear(); + } + /* Ignore RecursionError as the fuzzer generates long sequences of + arrays such as `[[[...` */ + if (parsed == NULL && PyErr_ExceptionMatches(PyExc_RecursionError)) { + PyErr_Clear(); + } + /* Ignore unicode errors, invalid byte sequences are common */ + if (parsed == NULL && PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { + PyErr_Clear(); + } + Py_DECREF(input_bytes); + Py_XDECREF(parsed); + return 0; +} + /* Run fuzzer and abort on failure. */ static int _run_fuzz(const uint8_t *data, size_t size, int(*fuzzer)(const char* , size_t)) { int rv = fuzzer((const char*) data, size); @@ -88,7 +131,6 @@ static int _run_fuzz(const uint8_t *data, size_t size, int(*fuzzer)(const char* /* CPython generates a lot of leak warnings for whatever reason. */ int __lsan_is_turned_off(void) { return 1; } -wchar_t wide_program_name[NAME_MAX]; int LLVMFuzzerInitialize(int *argc, char ***argv) { wchar_t* wide_program_name = Py_DecodeLocale(*argv[0], NULL); @@ -110,6 +152,12 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { initialize CPython ourselves on the first run. */ Py_InitializeEx(0); } +#if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_json_loads) + if (json_loads_method == NULL) { + PyObject* json_module = PyImport_ImportModule("json"); + json_loads_method = PyObject_GetAttrString(json_module, "loads"); + } +#endif int rv = 0; @@ -121,6 +169,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { #endif #if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_builtin_unicode) rv |= _run_fuzz(data, size, fuzz_builtin_unicode); +#endif +#if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_json_loads) + rv |= _run_fuzz(data, size, fuzz_json_loads); #endif return rv; }