]> git.ipfire.org Git - thirdparty/openssl.git/blame - fuzz/README.md
Ignore a bad signature in a CertificateVerify message while fuzzing
[thirdparty/openssl.git] / fuzz / README.md
CommitLineData
257e9d03
RS
1Fuzzing OpenSSL
2===============
3
4OpenSSL can use either LibFuzzer or AFL to do fuzzing.
c38bb727 5
f59d0131 6LibFuzzer
257e9d03 7---------
f59d0131 8
639b53ec
BC
9How to fuzz OpenSSL with [libfuzzer](http://llvm.org/docs/LibFuzzer.html),
10starting from a vanilla+OpenSSH server Ubuntu install.
c38bb727 11
639b53ec
BC
12With `clang` from a package manager
13-----------------------------------
c38bb727 14
639b53ec
BC
15Install `clang`, which [ships with `libfuzzer`](http://llvm.org/docs/LibFuzzer.html#fuzzer-usage)
16since version 6.0:
c38bb727 17
a81151bd 18 sudo apt-get install clang
c38bb727 19
639b53ec
BC
20Configure `openssl` for fuzzing. For now, you'll still need to pass in the path
21to the `libFuzzer` library file while configuring; this is represented as
22`$PATH_TO_LIBFUZZER` below. A typical value would be
a81151bd 23`/usr/lib/llvm-7/lib/clang/7.0.1/lib/linux/libclang_rt.fuzzer-x86_64.a`.
c38bb727 24
a81151bd 25 CC=clang ./config enable-fuzz-libfuzzer \
639b53ec 26 --with-fuzzer-lib=$PATH_TO_LIBFUZZER \
3a9b9b2d 27 -DPEDANTIC enable-asan enable-ubsan no-shared \
0282aeb6 28 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
639b53ec
BC
29 -fsanitize=fuzzer-no-link \
30 enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
e104d01d 31 enable-weak-ssl-ciphers enable-rc5 enable-md2 \
f8d4b3be
KR
32 enable-ssl3 enable-ssl3-method enable-nextprotoneg \
33 --debug
639b53ec
BC
34
35Compile:
36
a81151bd
DDO
37 sudo apt-get install make
38 make clean
39 LDCMD=clang++ make -j4
639b53ec
BC
40
41Finally, perform the actual fuzzing:
42
a81151bd 43 fuzz/helper.py $FUZZER
c38bb727 44
639b53ec 45where $FUZZER is one of the executables in `fuzz/`.
a81151bd 46It will run until you stop it.
c38bb727
BL
47
48If you get a crash, you should find a corresponding input file in
f8d4b3be 49`fuzz/corpora/$FUZZER-crash/`.
f59d0131 50
639b53ec
BC
51With `clang` from source/pre-built binaries
52-------------------------------------------
53
54You may also wish to use a pre-built binary from the [LLVM Download
55site](http://releases.llvm.org/download.html), or to [build `clang` from
56source](https://clang.llvm.org/get_started.html). After adding `clang` to your
57path and locating the `libfuzzer` library file, the procedure for configuring
58fuzzing is the same, except that you also need to specify
59a `--with-fuzzer-include` option, which should be the parent directory of the
60prebuilt fuzzer library. This is represented as `$PATH_TO_LIBFUZZER_DIR` below.
61
a81151bd 62 CC=clang ./config enable-fuzz-libfuzzer \
639b53ec
BC
63 --with-fuzzer-include=$PATH_TO_LIBFUZZER_DIR \
64 --with-fuzzer-lib=$PATH_TO_LIBFUZZER \
65 -DPEDANTIC enable-asan enable-ubsan no-shared \
66 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
67 -fsanitize=fuzzer-no-link \
68 enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
69 enable-weak-ssl-ciphers enable-rc5 enable-md2 \
70 enable-ssl3 enable-ssl3-method enable-nextprotoneg \
71 --debug
72
f59d0131 73AFL
257e9d03 74---
f59d0131 75
a81151bd
DDO
76This is an alternative to using LibFuzzer.
77
f59d0131
KR
78Configure for fuzzing:
79
a81151bd
DDO
80 sudo apt-get install afl-clang
81 CC=afl-clang-fast ./config enable-fuzz-afl no-shared no-module \
deaaac2c
MC
82 -DPEDANTIC enable-tls1_3 enable-weak-ssl-ciphers enable-rc5 \
83 enable-md2 enable-ssl3 enable-ssl3-method enable-nextprotoneg \
f8d4b3be
KR
84 enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
85 --debug
a81151bd
DDO
86 make clean
87 make
f59d0131 88
e104d01d
KR
89The following options can also be enabled: enable-asan, enable-ubsan, enable-msan
90
f59d0131
KR
91Run one of the fuzzers:
92
a81151bd 93 afl-fuzz -i fuzz/corpora/$FUZZER -o fuzz/corpora/$FUZZER/out fuzz/$FUZZER
f59d0131 94
31b15b9b 95Where $FUZZER is one of the executables in `fuzz/`.
f8d4b3be
KR
96
97Reproducing issues
257e9d03 98------------------
f8d4b3be
KR
99
100If a fuzzer generates a reproducible error, you can reproduce the problem using
101the fuzz/*-test binaries and the file generated by the fuzzer. They binaries
cb9bb735 102don't need to be built for fuzzing, there is no need to set CC or the call
f8d4b3be
KR
103config with enable-fuzz-* or -fsanitize-coverage, but some of the other options
104above might be needed. For instance the enable-asan or enable-ubsan option might
105be useful to show you when the problem happens. For the client and server fuzzer
106it might be needed to use -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION to
107reproduce the generated random numbers.
108
109To reproduce the crash you can run:
110
a81151bd 111 fuzz/$FUZZER-test $file
f8d4b3be 112
cb9bb735
DDO
113To do all the tests of a specific fuzzer such as asn1 you can run
114
115 fuzz/asn1-test fuzz/corpora/asn1
116or
a7da4d48 117 make test TESTS=fuzz_test_asn1
cb9bb735
DDO
118
119To run several fuzz tests you can use for instance:
120
a7da4d48 121 make test TESTS='test_fuzz_cmp test_fuzz_cms'
cb9bb735
DDO
122
123To run all fuzz tests you can use:
124
a7da4d48 125 make test TESTS='test_fuzz_*'
cb9bb735 126
f8d4b3be 127Random numbers
257e9d03 128--------------
f8d4b3be
KR
129
130The client and server fuzzer normally generate random numbers as part of the TLS
131connection setup. This results in the coverage of the fuzzing corpus changing
132depending on the random numbers. This also has an effect for coverage of the
133rest of the test suite and you see the coverage change for each commit even when
134no code has been modified.
135
136Since we want to maximize the coverage of the fuzzing corpus, the client and
137server fuzzer will use predictable numbers instead of the random numbers. This
138is controlled by the FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION define.
139
140The coverage depends on the way the numbers are generated. We don't disable any
141check of hashes, but the corpus has the correct hash in it for the random
142numbers that were generated. For instance the client fuzzer will always generate
143the same client hello with the same random number in it, and so the server, as
144emulated by the file, can be generated for that client hello.
145
146Coverage changes
257e9d03 147----------------
f8d4b3be
KR
148
149Since the corpus depends on the default behaviour of the client and the server,
150changes in what they send by default will have an impact on the coverage. The
151corpus will need to be updated in that case.
152
930aa9ee 153Updating the corpus
257e9d03 154-------------------
930aa9ee
KR
155
156The client and server corpus is generated with multiple config options:
257e9d03 157
930aa9ee
KR
158- The options as documented above
159- Without enable-ec_nistp_64_gcc_128 and without --debug
160- With no-asm
161- Using 32 bit
162- A default config, plus options needed to generate the fuzzer.
163
164The libfuzzer merge option is used to add the additional coverage
165from each config to the minimal set.
a81151bd
DDO
166
167Minimizing the corpus
257e9d03 168---------------------
a81151bd
DDO
169
170When you have gathered corpus data from more than one fuzzer run
8c1cbc72 171or for any other reason want to minimize the data
a81151bd
DDO
172in some corpus subdirectory `fuzz/corpora/DIR` this can be done as follows:
173
174 mkdir fuzz/corpora/NEWDIR
175 fuzz/$FUZZER -merge=1 fuzz/corpora/NEWDIR fuzz/corpora/DIR