Skip to content

Commit

Permalink
tests: integrate fuzz-targets into the test suite
Browse files Browse the repository at this point in the history
[v5]

1) `fuzz-libdwfl` and `fuzz-libelf` were moved from OSS-Fuzz.

2)  The regression testsuite was extended.

3) The OSS-Fuzz build script was removed. It should probably be
kept on OSS-Fuzz at this point.

4) The honggfuzz kludges were removed because
google/honggfuzz#431 was fixed.

[v4]

1) `--enable-afl` was added to make it possible to build and run
the fuzz target with afl-gcc/afl-g++/afl-fuzz. It's compatible
with both AFL and AFL++ and can be tested on Ubuntu by running
the following commands:
```
apt-get install afl++
autoreconf -i -f
./configure --enable-maintainer-mode --enable-afl
make -j$(nproc) V=1
make check V=1 VERBOSE=1 TESTS=run-fuzz-dwfl-core.sh FUZZ_TIME=600
```

It's compatible with ASan/UBsan as well so something like
`--enable-sanitize-address` and `--enable-sanitize-undefined` can
additionally be passed to run the fuzzer under ASan/UBsan.

It was tested on Fedora with AFL (https://github.com/google/AFL) and
on Ubuntu with AFL++ (https://github.com/AFLplusplus/AFLplusplus/).

2) Both `use_afl` and `use_honggfuzz` are now shown among the additional
test features printed by `./configure` to make it easier to figure out
how exactly elfutils is tested.

[v3]

The test handles infinite loops much better now.
In https://sourceware.org/bugzilla/show_bug.cgi?id=28715#c4 it took
it about 5 hours on Packit to discover an infinite loop on 32 bit
platforms because it didn't enforce any timeouts. It was fixed
by passing --tmout_sigvtalrm to honggfuzz (which treats timeouts
as normal crashes) and by additionally running the fuzz target
with timeout -s9.

[v2]

1) At https://sourceware.org/pipermail/elfutils-devel/2021q4/004541.html
it was pointed out that build-fuzzers.sh is too tied to OSS-Fuzz
and while it was kind of decoupled from it as much as possible
in the sense that it was enough to install clang and run the script to build
the fuzz target with libFuzzer it's true that it can't be integrated smoothly
into buildbot for example where gcc is used and various configure options
control what exactly is testsed. To address that, `--enable-honggfuzz` is
introduced. It looks for hfuzz-gcc, hfuzz-g++ and honggfuzz and if
they exist elfutils is built with those wrappers and the fuzz target
is additionally run for a few minutes under honggfuzz to make
regression testing more effective. It was tested on Fedora 35 and
in #53 on Ubuntu Focal with both gcc and
clang with and without sanitizers/Valgrind
and with two versions of honggfuzz (including the latest stable version).
To make it work on Ubuntu the following commands should be run
```
apt-get install libbfd-dev libunwind8-dev
git clone https://github.com/google/honggfuzz
cd honggfuzz
git checkout 2.4
make
make PREFIX=/usr install

cd PATH/TO/ELFUTILS
autoreconf -i -f
./configure --enable-maintainer-mode --enable-honggfuzz
make check V=1 VERBOSE=1 # FUZZ_TIME can be optionally passed
```

If hongfuzz is installed elsewhere it's possible to point
configure to it with CC, CXX and HONGGFUZZ
```
./configure CC=path-to-hfuzz-gcc CXX=path-to-hfuzz-g++ HONGGFUZZ=path-to-honggfuzz
```

I decided to use honggfuzz instead of AFL because AFL doesn't seem
to be maintained actively anymore. Other than that I can't seem to
make it work with various combinations of compilers, sanitizers and
so on. But thanks to the way the fuzz target is written it should
be possible to add it eventually by analogy with honggfuzz.

2) fuzz-dwfl-core-corpus was renamed to fuzz-dwfl-core-crashes to
make it more clear that it isn't exaclty a seed corpus.

3) run-strip-g.sh and run-strip-nothing.sh started to compile test programs
using temporary files instead of gcc -xc -. It should address
google/honggfuzz#431 but more generally
autoconf uses temporary files to make sure compiler works so
it seems in general it's safer to rely on compiler features that
are known to work.

4) A comment was added where I tried to expand on why the fuzz target
is written that way.

[v1]
The fuzz target was integrated into OSS-Fuzz in
google/oss-fuzz#6944 and since then it
has been running there continously (uncovering various issues
along the way). It's all well and good but since OSS-Fuzz
is far from the elfutils repository it's unnecessarily hard
to build the fuzz target locally, verify patches and more generally
test new code to make sure that it doesn't introduce new issues (
or reintroduce regressions). This patch aims to address all those
issues by moving the fuzz target into the elfutils repository,
integrating it into the testsuite and also providing a script
that can be used to build full-fledged fuzzers utilizing libFuzzer.
With this patch applied `make check` can be used to make sure
that files kept in tests/fuzz-dwfl-core-corpus don't crash the
code on various architecures.
`--enable-sanitize-{address,undefined}` and/or `--enable-valgrind`
can additionally be used to uncover issues like
https://sourceware.org/bugzilla/show_bug.cgi?id=28685
that don't always manifest themselves in simple segfaults. On top
of all that now the fuzz target can be built and linked against
libFuzzer locally by just running `./tests/build-fuzzers.sh`.

The patch was tested in #53 :

* the testsuite was run on aarch64, armhfp, i386, ppc64le, s390x
  and x86_64

* Fedora packages were built on those architectures;

* elfutils was built with both clang and gcc with and without sanitizers
  to make sure the tests pass there;

* `make distcheck` passed;

* coverage reports were built to make sure "static" builds are intact;

* the fuzz target was built and run with ClusterFuzzLite to make sure
  it's still compatible with OSS-Fuzz;

* the code was analyzed by various static analyzers to make sure new alerts
  aren't introduced.

Signed-off-by: Evgeny Vereshchagin <[email protected]>
  • Loading branch information
evverx committed Mar 30, 2022
1 parent 00d3d3f commit 6360879
Show file tree
Hide file tree
Showing 22 changed files with 460 additions and 2 deletions.
39 changes: 39 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ AS_IF([test "$use_locks" = yes],

AH_TEMPLATE([USE_LOCKS], [Defined if libraries should be thread-safe.])

AC_ARG_ENABLE([afl],
AS_HELP_STRING([--enable-afl],[run fuzzers with afl]), [use_afl=$enableval], [use_afl=no])
if test "$use_afl" = yes; then
AC_CHECK_PROG(CC, afl-gcc, afl-gcc)
if test -z "$CC"; then
AC_MSG_ERROR([failed to find afl-gcc])
fi
AC_CHECK_PROG(CXX, afl-g++, afl-g++)
if test -z "$CXX"; then
AC_MSG_ERROR([failed to find afl-g++])
fi
AC_CHECK_PROG(AFL_FUZZ, afl-fuzz, afl-fuzz)
if test -z "$AFL_FUZZ"; then
AC_MSG_ERROR([failed to find afl-fuzz])
fi
fi

AC_ARG_ENABLE([honggfuzz],
AS_HELP_STRING([--enable-honggfuzz],[run fuzzers with honggfuzz]), [use_honggfuzz=$enableval], [use_honggfuzz=no])
if test "$use_honggfuzz" = yes; then
if test "$use_afl" = yes; then
AC_MSG_ERROR([cannot enable afl and honggfuzz together])
fi
AC_CHECK_PROG(CC, hfuzz-gcc, hfuzz-gcc)
if test -z "$CC"; then
AC_MSG_ERROR([failed to find hfuzz-gcc])
fi
AC_CHECK_PROG(CXX, hfuzz-g++, hfuzz-g++)
if test -z "$CXX"; then
AC_MSG_ERROR([failed to find hfuzz-g++])
fi
AC_CHECK_PROG(HONGGFUZZ, honggfuzz, honggfuzz)
if test -z "$HONGGFUZZ"; then
AC_MSG_ERROR([failed to find honggfuzz])
fi
fi

AC_PROG_CC_C99
AC_PROG_CXX
AC_PROG_RANLIB
Expand Down Expand Up @@ -846,6 +883,8 @@ AC_MSG_NOTICE([
debug branch prediction : ${use_debugpred}
gprof support : ${use_gprof}
gcov support : ${use_gcov}
run fuzzers with honggfuzz : ${use_honggfuzz}
run fuzzers with AFL : ${use_afl}
run all tests under valgrind : ${use_valgrind}
gcc undefined behaviour sanitizer : ${use_undefined}
gcc address sanitizer : ${use_address}
Expand Down
3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
/find-prologues
/funcretval
/funcscopes
/fuzz-dwfl-core
/fuzz-libdwfl
/fuzz-libelf
/get-aranges
/get-files
/get-lines
Expand Down
55 changes: 53 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
getphdrnum leb128 read_unaligned \
msg_tst system-elf-libelf-test \
nvidia_extended_linemap_libdw \
fuzz-dwfl-core \
fuzz-libdwfl \
fuzz-libelf \
$(asm_TESTS)

asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
Expand Down Expand Up @@ -197,7 +200,10 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
msg_tst system-elf-libelf-test \
$(asm_TESTS) run-disasm-bpf.sh run-low_high_pc-dw-form-indirect.sh \
run-nvidia-extended-linemap-libdw.sh run-nvidia-extended-linemap-readelf.sh \
run-readelf-dw-form-indirect.sh run-strip-largealign.sh
run-readelf-dw-form-indirect.sh run-strip-largealign.sh \
run-fuzz-dwfl-core.sh \
run-fuzz-libdwfl.sh \
run-fuzz-libelf.sh

if !BIARCH
export ELFUTILS_DISABLE_BIARCH = 1
Expand Down Expand Up @@ -580,7 +586,21 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
run-readelf-dw-form-indirect.sh testfile-dw-form-indirect.bz2 \
run-nvidia-extended-linemap-libdw.sh run-nvidia-extended-linemap-readelf.sh \
testfile_nvidia_linemap.bz2 \
testfile-largealign.o.bz2 run-strip-largealign.sh
testfile-largealign.o.bz2 run-strip-largealign.sh \
run-fuzzer.sh \
run-fuzz-dwfl-core.sh \
run-fuzz-libdwfl.sh \
run-fuzz-libelf.sh \
fuzz-dwfl-core-crashes/empty \
fuzz-dwfl-core-crashes/oss-fuzz-41566 \
fuzz-dwfl-core-crashes/oss-fuzz-41570 \
fuzz-dwfl-core-crashes/oss-fuzz-41572 \
fuzz-libdwfl-crashes/oss-fuzz-45629 \
fuzz-libdwfl-crashes/oss-fuzz-45634 \
fuzz-libdwfl-crashes/oss-fuzz-45635 \
fuzz-libdwfl-crashes/oss-fuzz-45646 \
fuzz-libelf-crashes/oss-fuzz-45637 \
fuzz-libelf-crashes/oss-fuzz-45682


if USE_VALGRIND
Expand All @@ -595,9 +615,13 @@ installed_TESTS_ENVIRONMENT = libdir='$(DESTDIR)$(libdir)'; \
abs_srcdir='$(abs_srcdir)'; \
abs_builddir='$(abs_builddir)'; \
abs_top_builddir='$(abs_top_builddir)'; \
afl_fuzz='$(AFL_FUZZ)'; \
honggfuzz='$(HONGGFUZZ)'; \
export abs_srcdir; export abs_builddir; \
export abs_top_builddir; \
export libdir; export bindir; \
export afl_fuzz; \
export honggfuzz; \
export LC_ALL; export LANG; export VALGRIND_CMD; \
unset DEBUGINFOD_URLS; \
NM='$(NM)'; export NM; \
Expand All @@ -609,8 +633,12 @@ TESTS_ENVIRONMENT = LC_ALL=C; LANG=C; VALGRIND_CMD='$(valgrind_cmd)'; \
abs_srcdir='$(abs_srcdir)'; \
abs_builddir='$(abs_builddir)'; \
abs_top_builddir='$(abs_top_builddir)'; \
afl_fuzz='$(AFL_FUZZ)'; \
honggfuzz='$(HONGGFUZZ)'; \
export abs_srcdir; export abs_builddir; \
export abs_top_builddir; \
export afl_fuzz; \
export honggfuzz; \
export LC_ALL; export LANG; export VALGRIND_CMD; \
unset DEBUGINFOD_URLS; \
NM='$(NM)'; export NM; \
Expand Down Expand Up @@ -755,6 +783,29 @@ leb128_LDADD = $(libelf) $(libdw)
read_unaligned_LDADD = $(libelf) $(libdw)
nvidia_extended_linemap_libdw_LDADD = $(libelf) $(libdw)

# Fuzz targets are split into two files so that they can be
# compatible with the test suite, OSS-Fuzz and various fuzzing
# engines.
#
# OSS-Fuzz takes files containing LLVMFuzzerTestOneInput
# and links them against libFuzzer, AFL++ and honggfuzz. The oss-fuzz
# build script can be found at https://github.com/google/oss-fuzz/tree/master/projects/elfutils
#
# The testsuite links them against fuzz-main.c (which is a local driver reading
# files into buffers and passing those buffers to LLVMFuzzerTestOneInput).
#
# And various fuzzing engines working with binaries reading files
# can be used as well because fuzz-main.c provides what they expect.
noinst_HEADERS=fuzz.h
fuzz_dwfl_core_SOURCES = fuzz-main.c fuzz-dwfl-core.c
fuzz_dwfl_core_LDADD = $(libelf) $(libdw)

fuzz_libdwfl_SOURCES = fuzz-main.c fuzz-libdwfl.c
fuzz_libdwfl_LDADD = $(libelf) $(libdw)

fuzz_libelf_SOURCES = fuzz-main.c fuzz-libelf.c
fuzz_libelf_LDADD = $(libelf) $(libdw)

# We want to test the libelf header against the system elf.h header.
# Don't include any -I CPPFLAGS. Except when we install our own elf.h.
if !INSTALL_ELFH
Expand Down
Empty file.
Binary file added tests/fuzz-dwfl-core-crashes/oss-fuzz-41566
Binary file not shown.
Binary file added tests/fuzz-dwfl-core-crashes/oss-fuzz-41570
Binary file not shown.
Binary file added tests/fuzz-dwfl-core-crashes/oss-fuzz-41572
Binary file not shown.
58 changes: 58 additions & 0 deletions tests/fuzz-dwfl-core.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <assert.h>
#include <config.h>
#include <stdlib.h>
#include ELFUTILS_HEADER(dwfl)
#include "fuzz.h"
#include "system.h"

/* This fuzz target was initially used to fuzz systemd and
there elfutils is hidden behind functions receiving file
names and file descriptors. To cover that code the fuzz
target converts bytes it receives into temporary files
and passes their file descriptors to elf_begin instead of calling
something like elf_memory (which can process bytes directly).
New fuzzers covering elfutils should avoid this pattern. */

static const Dwfl_Callbacks core_callbacks =
{
.find_elf = dwfl_build_id_find_elf,
.find_debuginfo = dwfl_standard_find_debuginfo,
};

int
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
char fname[] = "/tmp/fuzz-dwfl-core.XXXXXX";
int fd = -1;
off_t offset;
ssize_t n;
Elf *core = NULL;
Dwfl *dwfl = NULL;

fd = mkstemp (fname);
assert (fd >= 0);

n = write_retry (fd, data, size);
assert (n >= 0);

offset = lseek (fd, 0, SEEK_SET);
assert (offset == 0);

elf_version (EV_CURRENT);
core = elf_begin (fd, ELF_C_READ_MMAP, NULL);
if (core == NULL)
goto cleanup;
dwfl = dwfl_begin (&core_callbacks);
assert(dwfl != NULL);
if (dwfl_core_file_report (dwfl, core, NULL) < 0)
goto cleanup;
if (dwfl_report_end (dwfl, NULL, NULL) != 0)
goto cleanup;

cleanup:
dwfl_end (dwfl);
elf_end (core);
close (fd);
unlink (fname);
return 0;
}
2 changes: 2 additions & 0 deletions tests/fuzz-libdwfl-crashes/oss-fuzz-45629
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!<arch>
� `
Binary file added tests/fuzz-libdwfl-crashes/oss-fuzz-45634
Binary file not shown.
Binary file added tests/fuzz-libdwfl-crashes/oss-fuzz-45635
Binary file not shown.
1 change: 1 addition & 0 deletions tests/fuzz-libdwfl-crashes/oss-fuzz-45646
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�� U�
50 changes: 50 additions & 0 deletions tests/fuzz-libdwfl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <assert.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "libdwfl.h"
#include "system.h"

static const char *debuginfo_path = "";
static const Dwfl_Callbacks cb =
{
NULL,
dwfl_standard_find_debuginfo,
NULL,
(char **)&debuginfo_path,
};


int
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
char fname[] = "/tmp/fuzz-libdwfl.XXXXXX";
int fd;
ssize_t n;

fd = mkstemp (fname);
assert (fd >= 0);

n = write_retry (fd, data, size);
assert (n == (ssize_t) size);

close (fd);

Dwarf_Addr bias = 0;
Dwfl *dwfl = dwfl_begin (&cb);
dwfl_report_begin (dwfl);

Dwfl_Module *mod = dwfl_report_offline (dwfl, fname, fname, -1);
dwfl_module_getdwarf (mod, &bias);

dwfl_end (dwfl);
unlink (fname);
return 0;
}
Binary file added tests/fuzz-libelf-crashes/oss-fuzz-45637
Binary file not shown.
Binary file added tests/fuzz-libelf-crashes/oss-fuzz-45682
Binary file not shown.
89 changes: 89 additions & 0 deletions tests/fuzz-libelf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <assert.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "system.h"

void
fuzz_logic_one (char *fname, int compression_type)
{
(void) elf_version (EV_CURRENT);
int fd = open (fname, O_RDONLY);
Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
if (elf != NULL) {
size_t strndx;
elf_getshdrstrndx (elf, &strndx);

Elf_Scn *scn = NULL;
// Iterate through sections
while ((scn = elf_nextscn (elf, scn)) != NULL) {
GElf_Shdr mem;
GElf_Shdr *shdr = gelf_getshdr (scn, &mem);
const char *name = elf_strptr (elf, strndx, shdr->sh_name);

// Two options for reading sections. We keep the code structure
// so it resembles the test code.
// Compress and get data of the section
if ((shdr->sh_flags & SHF_COMPRESSED) != 0) {
if (elf_compress (scn, compression_type, 0) >= 0) {
elf_getdata (scn, NULL);
}
} else if (name != NULL) {
if (name[0] == '.' && name[1] == 'z') {
if (elf_compress_gnu (scn, 0, 0) >= 0) {
elf_getdata (scn, NULL);
}
}
}
}
elf_end (elf);
}
close (fd);
}

void
fuzz_logic_twice (char *fname, int open_flags, Elf_Cmd cmd)
{
(void) elf_version (EV_CURRENT);
int fd = open (fname, open_flags);
Elf *elf = elf_begin (fd, cmd, NULL);
if (elf != NULL) {
size_t elf_size = 0;
elf_rawfile (elf, &elf_size);
elf_end (elf);
}
close (fd);
}

int
LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
char fname[] = "/tmp/fuzz-libelf.XXXXXX";
int fd;
ssize_t n;

fd = mkstemp (fname);
assert (fd >= 0);

n = write_retry (fd, data, size);
assert (n == (ssize_t) size);

close (fd);

fuzz_logic_one (fname, 0);
fuzz_logic_one (fname, 1);
fuzz_logic_twice (fname, O_RDONLY, ELF_C_READ);
fuzz_logic_twice (fname, O_RDONLY | O_WRONLY, ELF_C_RDWR);
fuzz_logic_twice (fname, O_RDONLY, ELF_C_READ_MMAP);
fuzz_logic_twice (fname, O_RDONLY | O_WRONLY, ELF_C_RDWR_MMAP);

unlink (fname);
return 0;
}
Loading

0 comments on commit 6360879

Please sign in to comment.