Merge branch 'maint-0.3.3' into maint-0.3.4
[tor.git] / doc / HACKING / Fuzzing.md
blob2039d6a4c014d74a0c05efe2b3017ba772b84986
1 = Fuzzing Tor
3 == The simple version (no fuzzing, only tests)
5 Check out fuzzing-corpora, and set TOR_FUZZ_CORPORA to point to the place
6 where you checked it out.
8 To run the fuzzing test cases in a deterministic fashion, use:
9       make test-fuzz-corpora
11 This won't actually fuzz Tor!  It will just run all the fuzz binaries
12 on our existing set of testcases for the fuzzer.
15 == Different kinds of fuzzing
17 Right now we support three different kinds of fuzzer.
19 First, there's American Fuzzy Lop (AFL), a fuzzer that works by forking
20 a target binary and passing it lots of different inputs on stdin.  It's the
21 trickiest one to set up, so I'll be describing it more below.
23 Second, there's libFuzzer, a llvm-based fuzzer that you link in as a library,
24 and it runs a target function over and over.  To use this one, you'll need to
25 have a reasonably recent clang and libfuzzer installed.  At that point, you
26 just build with --enable-expensive-hardening and --enable-libfuzzer.  That
27 will produce a set of binaries in src/test/fuzz/lf-fuzz-* .  These programs
28 take as input a series of directories full of fuzzing examples.  For more
29 information on libfuzzer, see http://llvm.org/docs/LibFuzzer.html
31 Third, there's Google's OSS-Fuzz infrastructure, which expects to get all of
32 its.  For more on this, see https://github.com/google/oss-fuzz and the
33 projects/tor subdirectory.  You'll need to mess around with Docker a bit to
34 test this one out; it's meant to run on Google's infrastructure.
36 In all cases, you'll need some starting examples to give the fuzzer when it
37 starts out.  There's a set in the "fuzzing-corpora" git repository.  Try
38 setting TOR_FUZZ_CORPORA to point to a checkout of that repository
40 == Writing Tor fuzzers
42 A tor fuzzing harness should have:
43 * a fuzz_init() function to set up any necessary global state.
44 * a fuzz_main() function to receive input and pass it to a parser.
45 * a fuzz_cleanup() function to clear global state.
47 Most fuzzing frameworks will produce many invalid inputs - a tor fuzzing
48 harness should rejecting invalid inputs without crashing or behaving badly.
50 But the fuzzing harness should crash if tor fails an assertion, triggers a
51 bug, or accesses memory it shouldn't. This helps fuzzing frameworks detect
52 "interesting" cases.
55 == Guided Fuzzing with AFL
57 There is no HTTPS, hash, or signature for American Fuzzy Lop's source code, so
58 its integrity can't be verified. That said, you really shouldn't fuzz on a
59 machine you care about, anyway.
61 To Build:
62   Get AFL from http://lcamtuf.coredump.cx/afl/ and unpack it
63   cd afl
64   make
65   cd ../tor
66   PATH=$PATH:../afl/ CC="../afl/afl-gcc" ./configure --enable-expensive-hardening
67   AFL_HARDEN=1 make clean fuzzers
69 To Find The ASAN Memory Limit: (64-bit only)
71 On 64-bit platforms, afl needs to know how much memory ASAN uses,
72 because ASAN tends to allocate a ridiculous amount of virtual memory,
73 and then not actually use it.
75 Read afl/docs/notes_for_asan.txt for more details.
77   Download recidivm from http://jwilk.net/software/recidivm
78   Download the signature
79   Check the signature
80   tar xvzf recidivm*.tar.gz
81   cd recidivm*
82   make
83   /path/to/recidivm -v src/test/fuzz/fuzz-http
84   Use the final "ok" figure as the input to -m when calling afl-fuzz
85   (Normally, recidivm would output a figure automatically, but in some cases,
86   the fuzzing harness will hang when the memory limit is too small.)
88 You could also just say "none" instead of the memory limit below, if you
89 don't care about memory limits.
92 To Run:
93   mkdir -p src/test/fuzz/fuzz_http_findings
94   ../afl/afl-fuzz -i ${TOR_FUZZ_CORPORA}/http -o src/test/fuzz/fuzz_http_findings -m <asan-memory-limit> -- src/test/fuzz/fuzz-http
97 AFL has a multi-core mode, check the documentation for details.
98 You might find the included fuzz-multi.sh script useful for this.
100 macOS (OS X) requires slightly more preparation, including:
101 * using afl-clang (or afl-clang-fast from the llvm directory)
102 * disabling external crash reporting (AFL will guide you through this step)
104 == Triaging Issues
106 Crashes are usually interesting, particularly if using AFL_HARDEN=1 and --enable-expensive-hardening. Sometimes crashes are due to bugs in the harness code.
108 Hangs might be interesting, but they might also be spurious machine slowdowns.
109 Check if a hang is reproducible before reporting it. Sometimes, processing
110 valid inputs may take a second or so, particularly with the fuzzer and
111 sanitizers enabled.
113 To see what fuzz-http is doing with a test case, call it like this:
114   src/test/fuzz/fuzz-http --debug < /path/to/test.case
116 (Logging is disabled while fuzzing to increase fuzzing speed.)
118 == Reporting Issues
120 Please report any issues discovered using the process in Tor's security issue
121 policy:
123 https://trac.torproject.org/projects/tor/wiki/org/meetings/2016SummerDevMeeting/Notes/SecurityIssuePolicy