openssl/fuzz
Neil Horman 4f619ca622 Ensure cmd from fuzz buffer is always valid
The quic-srtm fuzzer uses a loop in which an integer command is
extracted from the fuzzer buffer input to determine the action to take,
switching on the values between 0 and 3, and ignoring all other
commands.  Howver in the failing fuzzer test case here:
https://oss-fuzz.com/testcase-detail/5618331942977536

The buffer provided shows a large number of 0 values (indicating an SRTM
add command), and almost no 1, 2, or 3 values.  As such, the fuzzer only
truly exercises the srtm add path, which has the side effect of growing
the SRTM hash table unboundedly, leading to a timeout when 10 entries
need to be iterated over when the hashtable doall command is executed.

Fix this by ensuring that the command is always valid, and reasonably
distributed among all the operations with some modulo math.

Introducing this change bounds the hash table size in the reproducer
test case to less than half of the initially observed size, and avoids
the timeout.

Fixes openssl/project#679

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/24827)
2024-07-23 08:20:15 -04:00
..
corpora@9f76670613 fuzz: Add attribute certificate fuzz test 2024-04-24 14:08:03 +01:00
acert.c fuzz: Add attribute certificate fuzz test 2024-04-24 14:08:03 +01:00
asn1.c Add support for targetingInformation X.509v3 extension 2024-06-17 14:03:25 +02:00
asn1parse.c Update copyright year 2021-06-17 13:24:59 +01:00
bignum.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
bndiv.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
build.info Add provider fuzzer 2024-05-24 11:56:41 +02:00
client.c Update copyright year 2022-05-03 13:34:51 +01:00
cmp.c crypto/cmp/,apps/lib/cmp_mock_srv.c: various improvements on delayed delivery 2023-12-21 23:06:42 +01:00
cms.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
conf.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
crl.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
ct.c Deprecate ERR_get_state() 2019-09-12 18:34:06 +02:00
decoder.c fuzz/decoder.c: Lower the limits on key checks 2024-07-05 07:45:53 -04:00
driver.c Following the license change, modify the boilerplates in fuzz/ 2018-12-06 15:37:38 +01:00
dtlsclient.c Copyright year updates 2024-04-09 13:43:26 +02:00
dtlsserver.c Copyright year updates 2024-04-09 13:43:26 +02:00
fuzz_introspector_exclusion.config Exclude more in the fuzz introspector report 2023-11-14 17:00:07 +01:00
fuzz_rand.c Copyright year updates 2023-09-07 09:59:15 +01:00
fuzzer.h Use <openssl/e_os2.h> rather than <stdint.h> 2022-11-17 11:09:31 +01:00
hashtable.c Fix typos found by codespell 2024-06-24 15:09:11 +02:00
helper.py Fuzzing: Fix helper shebang on NixOS 2023-11-23 14:46:01 +00:00
mkfuzzoids.pl Add a local perl module to get year last changed 2021-03-31 13:59:53 +02:00
oids.txt Add Content Type OID for id-ct-rpkiSignedPrefixList 2024-03-15 09:10:22 +01:00
pem.c Copyright year updates 2023-09-07 09:59:15 +01:00
provider.c Change strnlen() to OPENSSL_strnlen() in fuzz/provider. 2024-07-18 17:26:55 +02:00
punycode.c fuzz: add punycode decoder fuzz test 2022-11-11 08:14:48 +11:00
quic-client.c Add support for streams to the quic-client fuzzer 2023-11-03 09:58:35 +00:00
quic-lcidm.c Limit the number of commands that can be used in the quic-lcidm fuzzer 2024-07-11 14:17:11 -04:00
quic-rcidm.c Copyright year updates 2024-04-09 13:43:26 +02:00
quic-srtm.c Ensure cmd from fuzz buffer is always valid 2024-07-23 08:20:15 -04:00
README.md Update the fuzz README to provide info about the stdc++ requirements 2023-10-23 10:08:12 +01:00
server.c that open brace { should be on the previous line 2024-07-22 06:55:35 -04:00
smime.c Added a fuzzer for SMIME 2023-02-27 09:29:20 +00:00
test-corpus.c Update copyright year 2020-10-15 14:10:06 +01:00
v3name.c Add fuzz test for v3name 2023-03-01 20:25:15 +11:00
x509.c Copyright year updates 2023-09-07 09:59:15 +01:00

Fuzzing OpenSSL

OpenSSL can use either LibFuzzer or AFL to do fuzzing.

LibFuzzer

How to fuzz OpenSSL with libfuzzer, starting from a vanilla+OpenSSH server Ubuntu install.

With clang from a package manager

Install clang, which ships with libfuzzer since version 6.0:

sudo apt-get install clang

Configure openssl for fuzzing. For now, you'll still need to pass in the path to the libFuzzer library file while configuring; this is represented as $PATH_TO_LIBFUZZER below. A typical value would be /usr/lib/llvm-7/lib/clang/7.0.1/lib/linux/libclang_rt.fuzzer-x86_64.a.

CC=clang ./config enable-fuzz-libfuzzer \
        --with-fuzzer-lib=$PATH_TO_LIBFUZZER \
        -DPEDANTIC enable-asan enable-ubsan no-shared \
        -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
        -fsanitize=fuzzer-no-link \
        enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
        enable-weak-ssl-ciphers enable-rc5 enable-md2 \
        enable-ssl3 enable-ssl3-method enable-nextprotoneg \
        --debug

Clang uses the gcc libstdc++ library so this must also be installed. You can check which version of gcc clang is using like this:

$ clang --verbose
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/12
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

So, in the above example clang is using gcc version 12. Ensure that the selected gcc version has the relevant libstdc++ files installed:

$ ls /usr/lib/gcc/x86_64-linux-gnu/12 | grep stdc++
libstdc++.a
libstdc++fs.a
libstdc++.so

On Ubuntu for gcc-12 this requires the libstdc++-12-dev package installed.

$ sudo apt-get install libstdc++-12-dev

Compile:

sudo apt-get install make
make clean
LDCMD=clang++ make -j4

Finally, perform the actual fuzzing:

fuzz/helper.py $FUZZER

where $FUZZER is one of the executables in fuzz/. It will run until you stop it.

If you get a crash, you should find a corresponding input file in fuzz/corpora/$FUZZER-crash/.

With clang from source/pre-built binaries

You may also wish to use a pre-built binary from the LLVM Download site, or to build clang from source. After adding clang to your path and locating the libfuzzer library file, the procedure for configuring fuzzing is the same, except that you also need to specify a --with-fuzzer-include option, which should be the parent directory of the prebuilt fuzzer library. This is represented as $PATH_TO_LIBFUZZER_DIR below.

CC=clang ./config enable-fuzz-libfuzzer \
        --with-fuzzer-include=$PATH_TO_LIBFUZZER_DIR \
        --with-fuzzer-lib=$PATH_TO_LIBFUZZER \
        -DPEDANTIC enable-asan enable-ubsan no-shared \
        -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION \
        -fsanitize=fuzzer-no-link \
        enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
        enable-weak-ssl-ciphers enable-rc5 enable-md2 \
        enable-ssl3 enable-ssl3-method enable-nextprotoneg \
        --debug

AFL

This is an alternative to using LibFuzzer.

Configure for fuzzing:

sudo apt-get install afl-clang
CC=afl-clang-fast ./config enable-fuzz-afl no-shared no-module \
    -DPEDANTIC enable-tls1_3 enable-weak-ssl-ciphers enable-rc5 \
    enable-md2 enable-ssl3 enable-ssl3-method enable-nextprotoneg \
    enable-ec_nistp_64_gcc_128 -fno-sanitize=alignment \
    --debug
make clean
make

The following options can also be enabled: enable-asan, enable-ubsan, enable-msan

Run one of the fuzzers:

afl-fuzz -i fuzz/corpora/$FUZZER -o fuzz/corpora/$FUZZER/out fuzz/$FUZZER

Where $FUZZER is one of the executables in fuzz/.

Reproducing issues

If a fuzzer generates a reproducible error, you can reproduce the problem using the fuzz/-test binaries and the file generated by the fuzzer. They binaries don't need to be built for fuzzing, there is no need to set CC or the call config with enable-fuzz- or -fsanitize-coverage, but some of the other options above might be needed. For instance the enable-asan or enable-ubsan option might be useful to show you when the problem happens. For the client and server fuzzer it might be needed to use -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION to reproduce the generated random numbers.

To reproduce the crash you can run:

fuzz/$FUZZER-test $file

To do all the tests of a specific fuzzer such as asn1 you can run

fuzz/asn1-test fuzz/corpora/asn1

or make test TESTS=fuzz_test_asn1

To run several fuzz tests you can use for instance:

make test TESTS='test_fuzz_cmp test_fuzz_cms'

To run all fuzz tests you can use:

make test TESTS='test_fuzz_*'

Random numbers

The client and server fuzzer normally generate random numbers as part of the TLS connection setup. This results in the coverage of the fuzzing corpus changing depending on the random numbers. This also has an effect for coverage of the rest of the test suite and you see the coverage change for each commit even when no code has been modified.

Since we want to maximize the coverage of the fuzzing corpus, the client and server fuzzer will use predictable numbers instead of the random numbers. This is controlled by the FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION define.

The coverage depends on the way the numbers are generated. We don't disable any check of hashes, but the corpus has the correct hash in it for the random numbers that were generated. For instance the client fuzzer will always generate the same client hello with the same random number in it, and so the server, as emulated by the file, can be generated for that client hello.

Coverage changes

Since the corpus depends on the default behaviour of the client and the server, changes in what they send by default will have an impact on the coverage. The corpus will need to be updated in that case.

Updating the corpus

The client and server corpus is generated with multiple config options:

  • The options as documented above
  • Without enable-ec_nistp_64_gcc_128 and without --debug
  • With no-asm
  • Using 32 bit
  • A default config, plus options needed to generate the fuzzer.

The libfuzzer merge option is used to add the additional coverage from each config to the minimal set.

Minimizing the corpus

When you have gathered corpus data from more than one fuzzer run or for any other reason want to minimize the data in some corpus subdirectory fuzz/corpora/DIR this can be done as follows:

mkdir fuzz/corpora/NEWDIR
fuzz/$FUZZER -merge=1 fuzz/corpora/NEWDIR fuzz/corpora/DIR