1 1.1 christos # I Can Haz Fuzz? 2 1.1 christos 3 1.1 christos LibFuzzer 4 1.1 christos ========= 5 1.1 christos 6 1.1 christos Or, how to fuzz OpenSSL with [libfuzzer](http://llvm.org/docs/LibFuzzer.html). 7 1.1 christos 8 1.1 christos Starting from a vanilla+OpenSSH server Ubuntu install. 9 1.1 christos 10 1.1 christos Use Chrome's handy recent build of clang. Older versions may also work. 11 1.1 christos 12 1.1 christos $ sudo apt-get install git 13 1.1 christos $ mkdir git-work 14 1.1 christos $ git clone https://chromium.googlesource.com/chromium/src/tools/clang 15 1.1 christos $ clang/scripts/update.py 16 1.1 christos 17 1.1 christos You may want to git pull and re-run the update from time to time. 18 1.1 christos 19 1.1 christos Update your path: 20 1.1 christos 21 1.1 christos $ PATH=~/third_party/llvm-build/Release+Asserts/bin/:$PATH 22 1.1 christos 23 1.1 christos Get and build libFuzzer (there is a git mirror at 24 1.1 christos https://github.com/llvm-mirror/llvm/tree/master/lib/Fuzzer if you prefer): 25 1.1 christos 26 1.1 christos $ cd 27 1.1 christos $ sudo apt-get install subversion 28 1.1 christos $ mkdir svn-work 29 1.1 christos $ cd svn-work 30 1.1 christos $ svn co https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer Fuzzer 31 1.1 christos $ cd Fuzzer 32 1.1 christos $ clang++ -c -g -O2 -std=c++11 *.cpp 33 1.1 christos $ ar r libFuzzer.a *.o 34 1.1 christos $ ranlib libFuzzer.a 35 1.1 christos 36 1.1 christos Configure for fuzzing: 37 1.1 christos 38 1.1 christos $ CC=clang ./config enable-fuzz-libfuzzer \ 39 1.1 christos --with-fuzzer-include=../../svn-work/Fuzzer \ 40 1.1 christos --with-fuzzer-lib=../../svn-work/Fuzzer/libFuzzer.a \ 41 1.1 christos -DPEDANTIC enable-asan enable-ubsan no-shared \ 42 1.1 christos -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \ 43 1.1 christos -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp \ 44 1.1 christos enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment enable-tls1_3 \ 45 1.1 christos enable-weak-ssl-ciphers enable-rc5 enable-md2 \ 46 1.1 christos enable-ssl3 enable-ssl3-method enable-nextprotoneg \ 47 1.1 christos --debug 48 1.1 christos $ sudo apt-get install make 49 1.1 christos $ LDCMD=clang++ make -j 50 1.1 christos $ fuzz/helper.py $FUZZER 51 1.1 christos 52 1.1 christos Where $FUZZER is one of the executables in `fuzz/`. 53 1.1 christos 54 1.1 christos If you get a crash, you should find a corresponding input file in 55 1.1 christos `fuzz/corpora/$FUZZER-crash/`. 56 1.1 christos 57 1.1 christos AFL 58 1.1 christos === 59 1.1 christos 60 1.1 christos Configure for fuzzing: 61 1.1 christos 62 1.1 christos $ sudo apt-get install afl-clang 63 1.1 christos $ CC=afl-clang-fast ./config enable-fuzz-afl no-shared -DPEDANTIC \ 64 1.1 christos enable-tls1_3 enable-weak-ssl-ciphers enable-rc5 enable-md2 \ 65 1.1 christos enable-ssl3 enable-ssl3-method enable-nextprotoneg \ 66 1.1 christos enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \ 67 1.1 christos --debug 68 1.1 christos $ make 69 1.1 christos 70 1.1 christos The following options can also be enabled: enable-asan, enable-ubsan, enable-msan 71 1.1 christos 72 1.1 christos Run one of the fuzzers: 73 1.1 christos 74 1.1 christos $ afl-fuzz -i fuzz/corpora/$FUZZER -o fuzz/corpora/$FUZZER/out fuzz/$FUZZER 75 1.1 christos 76 1.1 christos Where $FUZZER is one of the executables in `fuzz/`. 77 1.1 christos 78 1.1 christos Reproducing issues 79 1.1 christos ================== 80 1.1 christos 81 1.1 christos If a fuzzer generates a reproducible error, you can reproduce the problem using 82 1.1 christos the fuzz/*-test binaries and the file generated by the fuzzer. They binaries 83 1.1 christos don't need to be build for fuzzing, there is no need to set CC or the call 84 1.1 christos config with enable-fuzz-* or -fsanitize-coverage, but some of the other options 85 1.1 christos above might be needed. For instance the enable-asan or enable-ubsan option might 86 1.1 christos be useful to show you when the problem happens. For the client and server fuzzer 87 1.1 christos it might be needed to use -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION to 88 1.1 christos reproduce the generated random numbers. 89 1.1 christos 90 1.1 christos To reproduce the crash you can run: 91 1.1 christos 92 1.1 christos $ fuzz/$FUZZER-test $file 93 1.1 christos 94 1.1 christos Random numbers 95 1.1 christos ============== 96 1.1 christos 97 1.1 christos The client and server fuzzer normally generate random numbers as part of the TLS 98 1.1 christos connection setup. This results in the coverage of the fuzzing corpus changing 99 1.1 christos depending on the random numbers. This also has an effect for coverage of the 100 1.1 christos rest of the test suite and you see the coverage change for each commit even when 101 1.1 christos no code has been modified. 102 1.1 christos 103 1.1 christos Since we want to maximize the coverage of the fuzzing corpus, the client and 104 1.1 christos server fuzzer will use predictable numbers instead of the random numbers. This 105 1.1 christos is controlled by the FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION define. 106 1.1 christos 107 1.1 christos The coverage depends on the way the numbers are generated. We don't disable any 108 1.1 christos check of hashes, but the corpus has the correct hash in it for the random 109 1.1 christos numbers that were generated. For instance the client fuzzer will always generate 110 1.1 christos the same client hello with the same random number in it, and so the server, as 111 1.1 christos emulated by the file, can be generated for that client hello. 112 1.1 christos 113 1.1 christos Coverage changes 114 1.1 christos ================ 115 1.1 christos 116 1.1 christos Since the corpus depends on the default behaviour of the client and the server, 117 1.1 christos changes in what they send by default will have an impact on the coverage. The 118 1.1 christos corpus will need to be updated in that case. 119 1.1 christos 120 1.1 christos Updating the corpus 121 1.1 christos =================== 122 1.1 christos 123 1.1 christos The client and server corpus is generated with multiple config options: 124 1.1 christos - The options as documented above 125 1.1 christos - Without enable-ec_nistp_64_gcc_128 and without --debug 126 1.1 christos - With no-asm 127 1.1 christos - Using 32 bit 128 1.1 christos - A default config, plus options needed to generate the fuzzer. 129 1.1 christos 130 1.1 christos The libfuzzer merge option is used to add the additional coverage 131 1.1 christos from each config to the minimal set. 132