eBPF-based security platform
testing: let the fuzz runner keep what the targets find between runs
Daniel Borkmann51278f45ee5c
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex f8dac51e..53e10762 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -253,6 +253,21 @@ runner.default. A crash fails the run and leaves its input in``testing/fuzz/crash-<target>-*``.+What a target finds is discarded after the run unless+``FUZZ_CORPUS=<dir>`` names a directory to keep it in. Each target then+starts from ``<dir>/<target>`` as well as its seed corpus and adds what+it finds there, and after a run without a crash the directory is merged+down to the inputs that add coverage, so that it does not grow with+every run. A relative ``<dir>`` is taken from the directory the command+runs in, which ``make -C testing`` changes to ``testing/``:++.. code-block:: console++ $ make -C testing fuzz FUZZ_SECONDS=600 FUZZ_CORPUS=$PWD/build/fuzz-corpus++An input in the kept corpus that crashes a later build fails that run+like any new crash.+Use the test VM---------------diff --git a/testing/Makefile b/testing/Makefileindex fef80232..294d349e 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -332,7 +332,8 @@ test-helpers:+$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo test-helpers# The parsers of input from outside the host under libFuzzer, each target-# for FUZZ_SECONDS on its seed corpus; needs clang with its fuzzer runtime.+# for FUZZ_SECONDS on its seed corpus and, with FUZZ_CORPUS=<dir>, on what+# earlier runs kept there; needs clang with its fuzzer runtime.FUZZ_SECONDS ?= 60fuzz:@@ -352,7 +353,7 @@ help:' make -C testing end2end' \' VM=0 leaves out the tests that boot a VM, which need root;' \' make -j boots several at once.' \- ' make -C testing fuzz [FUZZ_SECONDS=60]' \+ ' make -C testing fuzz [FUZZ_SECONDS=60] [FUZZ_CORPUS=<dir>]' \' make -C testing vm, vm-console, vm-ssh, vm-stop, or vm-clean' \' make -C testing vm KERNEL=7.4' \' make -C testing kernel KERNEL_TAG=v7.4' \diff --git a/testing/fuzz/run.sh b/testing/fuzz/run.shindex 34d444a5..b16d3b6d 100755--- a/testing/fuzz/run.sh+++ b/testing/fuzz/run.sh@@ -4,13 +4,20 @@## testing/fuzz/run.sh -- run the named libFuzzer targets, or one for each# fuzz-*.c here, for the given seconds on their seed corpora and FUZZ_JOBS-# of them at a time, keeping what they find in a scratch directory. A-# crash fails the run and leaves the input that caused it in testing/fuzz.+# of them at a time, keeping what they find in a scratch directory, or+# with FUZZ_CORPUS=<dir> in <dir>/<target> for the next run, merged down+# to the inputs that add coverage. A crash fails the run and leaves the+# input that caused it in testing/fuzz.## testing/fuzz/run.sh <seconds> [<target>...]set -eu+# A relative FUZZ_CORPUS names a directory from where the caller stands.+case ${FUZZ_CORPUS:-} in+'' | /*) ;;+*) FUZZ_CORPUS=$PWD/$FUZZ_CORPUS ;;+esaccd "$(dirname "$0")"seconds=${1:?usage: run.sh <seconds> [<target>...]}shift@@ -24,12 +31,37 @@ fiwork=$(mktemp -d)trap 'rm -rf "$work"' EXIT+# Each run adds what it found, so the kept corpus would grow without end:+# merge it into a fresh directory, which takes only the inputs that add+# coverage, and keep it unchanged when the merge fails.+keep() {+ min=$FUZZ_CORPUS/.$1.min+ rm -rf "$min"+ mkdir -p "$min"+ if ./fuzz-"$1" -merge=1 -timeout=10 -rss_limit_mb=2048 "$min" "$2" \+ >"$work/$1.merge.log" 2>&1; then+ rm -rf "$2"+ mv "$min" "$2"+ else+ rm -rf "$min"+ printf 'fuzz: %s: merge failed; the corpus stays as it was\n' \+ "$1" >&2+ fi+}+run() {status=0- mkdir -p "$work/$1"+ out=$work/$1+ if [ -n "${FUZZ_CORPUS:-}" ]; then+ out=$FUZZ_CORPUS/$1+ fi+ mkdir -p "$out"./fuzz-"$1" -max_total_time="$seconds" -timeout=10 \-rss_limit_mb=2048 -artifact_prefix="$PWD/crash-$1-" \- "$work/$1" "corpus/$1" >"$work/$1.log" 2>&1 || status=$?+ "$out" "corpus/$1" >"$work/$1.log" 2>&1 || status=$?+ if [ "$status" -eq 0 ] && [ -n "${FUZZ_CORPUS:-}" ]; then+ keep "$1" "$out"+ fiecho $status >"$work/$1.rc"}