summaryrefslogtreecommitdiffstats
path: root/Modules/_xxtestfuzz
diff options
context:
space:
mode:
authorAmmar Askar <aaskar@google.com>2019-06-12 04:30:35 (GMT)
committerGregory P. Smith <greg@krypto.org>2019-06-12 04:30:34 (GMT)
commita6e190e94b47324f14e22a09200c68b722d55699 (patch)
tree8043c131c0623eb9c54aa76bee7f40df2b1eae56 /Modules/_xxtestfuzz
parent405f648db7c44b07348582b5101d4716e0ce5ac3 (diff)
downloadcpython-a6e190e94b47324f14e22a09200c68b722d55699.zip
cpython-a6e190e94b47324f14e22a09200c68b722d55699.tar.gz
cpython-a6e190e94b47324f14e22a09200c68b722d55699.tar.bz2
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.
Diffstat (limited to 'Modules/_xxtestfuzz')
-rw-r--r--Modules/_xxtestfuzz/README.rst10
-rw-r--r--Modules/_xxtestfuzz/dictionaries/fuzz_json_loads.dict40
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_array.json1
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/empty_object.json1
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass1.json58
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass2.json1
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/pass3.json6
-rw-r--r--Modules/_xxtestfuzz/fuzz_json_loads_corpus/simple_array.json1
-rw-r--r--Modules/_xxtestfuzz/fuzz_tests.txt1
-rw-r--r--Modules/_xxtestfuzz/fuzzer.c53
10 files changed, 171 insertions, 1 deletions
diff --git a/Modules/_xxtestfuzz/README.rst b/Modules/_xxtestfuzz/README.rst
index b48f3c8..42bd02a 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
+``<test_name>_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 0000000..ad64917
--- /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 0000000..fe51488
--- /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 0000000..0967ef4
--- /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 0000000..70e2685
--- /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": "&#34; \u0022 %22 0x22 034 &#x22;",
+ "\/\\\"\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 0000000..d3c63c7
--- /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 0000000..4528d51
--- /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 0000000..ce1e6ec
--- /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 2e53bfd..f012129 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 54f816e..e862a99 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;
@@ -122,5 +170,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
#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;
}