Blame fuzz/README.md

Packit c4476c
# I Can Haz Fuzz?
Packit c4476c
Packit c4476c
LibFuzzer
Packit c4476c
=========
Packit c4476c
Packit c4476c
Or, how to fuzz OpenSSL with [libfuzzer](http://llvm.org/docs/LibFuzzer.html).
Packit c4476c
Packit c4476c
Starting from a vanilla+OpenSSH server Ubuntu install.
Packit c4476c
Packit c4476c
Use Chrome's handy recent build of clang. Older versions may also work.
Packit c4476c
Packit c4476c
    $ sudo apt-get install git
Packit c4476c
    $ mkdir git-work
Packit c4476c
    $ git clone https://chromium.googlesource.com/chromium/src/tools/clang
Packit c4476c
    $ clang/scripts/update.py
Packit c4476c
Packit c4476c
You may want to git pull and re-run the update from time to time.
Packit c4476c
Packit c4476c
Update your path:
Packit c4476c
Packit c4476c
    $ PATH=~/third_party/llvm-build/Release+Asserts/bin/:$PATH
Packit c4476c
Packit c4476c
Get and build libFuzzer (there is a git mirror at
Packit c4476c
https://github.com/llvm-mirror/llvm/tree/master/lib/Fuzzer if you prefer):
Packit c4476c
Packit c4476c
    $ cd
Packit c4476c
    $ sudo apt-get install subversion
Packit c4476c
    $ mkdir svn-work
Packit c4476c
    $ cd svn-work
Packit c4476c
    $ svn co https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer Fuzzer
Packit c4476c
    $ cd Fuzzer
Packit c4476c
    $ clang++ -c -g -O2 -std=c++11 *.cpp
Packit c4476c
    $ ar r libFuzzer.a *.o
Packit c4476c
    $ ranlib libFuzzer.a
Packit c4476c
Packit c4476c
Configure for fuzzing:
Packit c4476c
Packit c4476c
    $ CC=clang ./config enable-fuzz-libfuzzer \
Packit c4476c
            --with-fuzzer-include=../../svn-work/Fuzzer \
Packit c4476c
            --with-fuzzer-lib=../../svn-work/Fuzzer/libFuzzer.a \
Packit c4476c
            -DPEDANTIC enable-asan enable-ubsan no-shared \
Packit c4476c
            -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
Packit c4476c
            -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp \
Packit c4476c
            enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment enable-tls1_3 \
Packit c4476c
            enable-weak-ssl-ciphers enable-rc5 enable-md2 \
Packit c4476c
            enable-ssl3 enable-ssl3-method enable-nextprotoneg \
Packit c4476c
            --debug
Packit c4476c
    $ sudo apt-get install make
Packit c4476c
    $ LDCMD=clang++ make -j
Packit c4476c
    $ fuzz/helper.py $FUZZER
Packit c4476c
Packit c4476c
Where $FUZZER is one of the executables in `fuzz/`.
Packit c4476c
Packit c4476c
If you get a crash, you should find a corresponding input file in
Packit c4476c
`fuzz/corpora/$FUZZER-crash/`.
Packit c4476c
Packit c4476c
AFL
Packit c4476c
===
Packit c4476c
Packit c4476c
Configure for fuzzing:
Packit c4476c
Packit c4476c
    $ sudo apt-get install afl-clang
Packit c4476c
    $ CC=afl-clang-fast ./config enable-fuzz-afl no-shared -DPEDANTIC \
Packit c4476c
        enable-tls1_3 enable-weak-ssl-ciphers enable-rc5 enable-md2 \
Packit c4476c
        enable-ssl3 enable-ssl3-method enable-nextprotoneg \
Packit c4476c
        enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
Packit c4476c
        --debug
Packit c4476c
    $ make
Packit c4476c
Packit c4476c
The following options can also be enabled: enable-asan, enable-ubsan, enable-msan
Packit c4476c
Packit c4476c
Run one of the fuzzers:
Packit c4476c
Packit c4476c
    $ afl-fuzz -i fuzz/corpora/$FUZZER -o fuzz/corpora/$FUZZER/out fuzz/$FUZZER
Packit c4476c
Packit c4476c
Where $FUZZER is one of the executables in `fuzz/`.
Packit c4476c
Packit c4476c
Reproducing issues
Packit c4476c
==================
Packit c4476c
Packit c4476c
If a fuzzer generates a reproducible error, you can reproduce the problem using
Packit c4476c
the fuzz/*-test binaries and the file generated by the fuzzer. They binaries
Packit c4476c
don't need to be build for fuzzing, there is no need to set CC or the call
Packit c4476c
config with enable-fuzz-* or -fsanitize-coverage, but some of the other options
Packit c4476c
above might be needed. For instance the enable-asan or enable-ubsan option might
Packit c4476c
be useful to show you when the problem happens. For the client and server fuzzer
Packit c4476c
it might be needed to use -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION to
Packit c4476c
reproduce the generated random numbers.
Packit c4476c
Packit c4476c
To reproduce the crash you can run:
Packit c4476c
Packit c4476c
    $ fuzz/$FUZZER-test $file
Packit c4476c
Packit c4476c
Random numbers
Packit c4476c
==============
Packit c4476c
Packit c4476c
The client and server fuzzer normally generate random numbers as part of the TLS
Packit c4476c
connection setup. This results in the coverage of the fuzzing corpus changing
Packit c4476c
depending on the random numbers. This also has an effect for coverage of the
Packit c4476c
rest of the test suite and you see the coverage change for each commit even when
Packit c4476c
no code has been modified.
Packit c4476c
Packit c4476c
Since we want to maximize the coverage of the fuzzing corpus, the client and
Packit c4476c
server fuzzer will use predictable numbers instead of the random numbers. This
Packit c4476c
is controlled by the FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION define.
Packit c4476c
Packit c4476c
The coverage depends on the way the numbers are generated. We don't disable any
Packit c4476c
check of hashes, but the corpus has the correct hash in it for the random
Packit c4476c
numbers that were generated. For instance the client fuzzer will always generate
Packit c4476c
the same client hello with the same random number in it, and so the server, as
Packit c4476c
emulated by the file, can be generated for that client hello.
Packit c4476c
Packit c4476c
Coverage changes
Packit c4476c
================
Packit c4476c
Packit c4476c
Since the corpus depends on the default behaviour of the client and the server,
Packit c4476c
changes in what they send by default will have an impact on the coverage. The
Packit c4476c
corpus will need to be updated in that case.
Packit c4476c
Packit c4476c
Updating the corpus
Packit c4476c
===================
Packit c4476c
Packit c4476c
The client and server corpus is generated with multiple config options:
Packit c4476c
- The options as documented above
Packit c4476c
- Without enable-ec_nistp_64_gcc_128 and without --debug
Packit c4476c
- With no-asm
Packit c4476c
- Using 32 bit
Packit c4476c
- A default config, plus options needed to generate the fuzzer.
Packit c4476c
Packit c4476c
The libfuzzer merge option is used to add the additional coverage
Packit c4476c
from each config to the minimal set.