eBPF-based security platform
fuzz: run the targets under AFL++ as well
Daniel Borkmannd0589fa19e5d
diff --git a/.agents/skills/fuzz-target/SKILL.md b/.agents/skills/fuzz-target/SKILL.mdindex 49bbc631..984b417f 100644--- a/.agents/skills/fuzz-target/SKILL.md+++ b/.agents/skills/fuzz-target/SKILL.md@@ -53,10 +53,13 @@ state that exception exactly, or the target reports its own error.make -C testing fuzz FUZZ_SECONDS=60 # every target, as CIsh testing/fuzz/run.sh 300 <name> # one target for longertesting/fuzz/fuzz-<name> testing/fuzz/crash-<name>-<hash>+PATH=$PWD/build/afl/bin:$PATH make -C testing afl FUZZERS=<name> # AFL++```-A failure leaves its input in `testing/fuzz/crash-<name>-*`, which git-ignores. Fix the parser, or the harness when the property was wrong,+A new target needs nothing more for AFL++: `make -C testing afl` builds+every `fuzz-*.c` with `afl-clang-fast` as it is. A failure leaves its+input in `testing/fuzz/crash-<name>-*`, or `crash-afl-<name>-<n>` from+AFL++, which git ignores. Fix the parser, or the harness when the property was wrong,add the input to the corpus when it shows a case the seeds do not, anddelete the crash file. libFuzzer's own `-jobs` writes `fuzz-<n>.log`into the current directory: run that from a scratch directory, not thediff --git a/.agents/skills/testing/SKILL.md b/.agents/skills/testing/SKILL.mdindex 2c3e8d26..b7042667 100644--- a/.agents/skills/testing/SKILL.md+++ b/.agents/skills/testing/SKILL.md@@ -24,6 +24,8 @@ make tests VM=0 SANITIZE=thread CC=clang # CI: TSan, for threads and handlerssh .github/scripts/msan-deps.sh # MSan: instrumented libraries once,sh .github/scripts/msan-tests.sh [60] # then unit and fuzz from a clean treemake -C testing fuzz [FUZZ_SECONDS=60] [FUZZERS=<target>...]+.github/scripts/afl-deps.sh # AFL++ for clang 23, into build/afl+PATH=$PWD/build/afl/bin:$PATH make -C testing afl [AFL_SECONDS=600]make -C testing help```diff --git a/.github/scripts/afl-deps.sh b/.github/scripts/afl-deps.shnew file mode 100755index 00000000..61ede143--- /dev/null+++ b/.github/scripts/afl-deps.sh@@ -0,0 +1,41 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# .github/scripts/afl-deps.sh -- build AFL++ for make -C testing afl into+# build/afl, or keep the build there when it is of the same commit and LLVM.+# The commit is of AFL++'s stable branch: no release takes LLVM 23 yet, and+# its compiler passes must be built for the clang the targets are built+# with. Needs curl, make, clang 23 and llvm-23-dev.+#+# .github/scripts/afl-deps.sh++set -eu++commit=d0070729206a80a9af297b2341edd63f68fdac35+sha256=f6d36b6ff119e79a4b32b879ea87996b0b2f6d9e392ad9eee000fabad9b926f8+llvm_config=${LLVM_CONFIG:-llvm-config-23}+prefix=$PWD/build/afl+stamp="$commit $($llvm_config --version)"++if [ "$(cat "$prefix/.built" 2>/dev/null)" = "$stamp" ]; then+ exit 0+fi++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT+curl -fsSL --retry 3 -o "$work/afl.tar.gz" \+ "https://github.com/AFLplusplus/AFLplusplus/archive/$commit.tar.gz"+echo "$sha256 $work/afl.tar.gz" | sha256sum -c -+tar -xzf "$work/afl.tar.gz" -C "$work"++# Python, Nyx and Unicorn serve modes make -C testing afl does not use.+set -- LLVM_CONFIG="$llvm_config" NO_PYTHON=1 NO_NYX=1 NO_UNICORN_ARM64=1+make -C "$work/AFLplusplus-$commit" -j"$(nproc)" "$@" all+rm -rf "$prefix"+make -C "$work/AFLplusplus-$commit" "$@" PREFIX="$prefix" install+[ -x "$prefix/bin/afl-clang-fast" ] || {+ echo "afl-deps: AFL++ built without its LLVM mode" >&2+ exit 1+}+echo "$stamp" >"$prefix/.built"diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 6a17e744..25967fb2 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -92,8 +92,8 @@ Test tiers* - Fuzz- :src:`testing/fuzz/`- On the build host, under libFuzzer with ASan and UBSan, or with- MSan- - ``make -C testing fuzz``+ MSan, and under AFL+++ - ``make -C testing fuzz``, ``make -C testing afl``* - Proofs- :src:`testing/proofs/`- On the build host, with CBMC and Lean 4, about an hour for all@@ -380,6 +380,28 @@ inputs that crashed are in its ``fuzz-crashes`` artifact; copy one into$ testing/fuzz/fuzz-<target> testing/fuzz/crash-<target>-<hash>+The same targets run under AFL++, whose mutations and comparison+logging find inputs libFuzzer does not, such as a magic value or a+checksum the parser compares the input with. AFL++ builds each target+twice, with ``afl-clang-fast`` and again logging its comparisons, into+``build/fuzz-afl`` and ``build/fuzz-afl-cmplog``, apart from the+libFuzzer builds. Its compiler passes must be built for clang 23:+:src:`.github/scripts/afl-deps.sh` builds a pinned AFL++ into+``build/afl``, and needs ``llvm-23-dev``.++.. code-block:: console++ $ .github/scripts/afl-deps.sh+ $ PATH=$PWD/build/afl/bin:$PATH make -C testing afl AFL_SECONDS=600 \+ FUZZ_CORPUS=$PWD/build/fuzz-corpus++``AFL_SECONDS`` is the time per target, ten minutes by default, and+``FUZZERS``, ``FUZZ_JOBS`` and ``FUZZ_CORPUS`` work as for+``make -C testing fuzz``. What AFL++ queued goes into the same kept+corpus, which the next libFuzzer run merges down. A crash fails the run+and leaves its input in ``testing/fuzz/crash-afl-<target>-<n>``; the+libFuzzer build of the target reproduces it like any other input.+Use the test VM---------------diff --git a/Documentation/reference/cheat-sheet.rst b/Documentation/reference/cheat-sheet.rstindex f589f4f3..99b59316 100644--- a/Documentation/reference/cheat-sheet.rst+++ b/Documentation/reference/cheat-sheet.rst@@ -466,11 +466,14 @@ Boot the interactive test VM, log in and stop it:$ make -C testing vm-ssh$ make -C testing vm-stop-Run the fuzz targets for a minute each:+Run the fuzz targets for a minute each, or under AFL++ for ten minutes+each:.. code-block:: console$ make -C testing fuzz FUZZ_SECONDS=60+ $ .github/scripts/afl-deps.sh+ $ PATH=$PWD/build/afl/bin:$PATH make -C testing aflRender and serve the documentation, and check it against the code(:doc:`/contribute/documentation/writing-docs`):diff --git a/Makefile.shirudo b/Makefile.shirudoindex a3f0763f..b0ff3b60 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -1321,7 +1321,8 @@ clean:$(Q)rm -f $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) \$(BUILD_OBJS) $(SIGN_OBJ) $(QUOTE_CHECK_OBJS) $(VERSION_OBJ) \util/version_id.c- $(Q)rm -rf build/target build/unit build/quote-check+ $(Q)rm -rf build/target build/unit build/quote-check build/fuzz-afl \+ build/fuzz-afl-cmplog$(Q)rm -f $(DEPS) testing/helper/*.d$(Q)rm -f $(BPF_OBJS) $(VMLINUX_H) systemd/shirudo.service \systemd/shirudo-initrd.service \diff --git a/testing/Makefile b/testing/Makefileindex 87a1a4b0..d59bbc37 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -29,7 +29,7 @@ test-vm-arch = $(shell TEST_VM_ARCH='$(TEST_VM_ARCH)'; \.DEFAULT_GOAL := all.PHONY: all help clean unit ci-checks functional end2end build-operator build-unit \test-helpers \- fuzz bundle test-vm-bundle kernel bpf-next vm vm-console vm-ssh vm-stop vm-clean \+ fuzz afl bundle test-vm-bundle kernel bpf-next vm vm-console vm-ssh vm-stop vm-clean \vm-operator-end2endKERNEL_PATCHES ?= $(if $(wildcard $(ROOT)/kernel/*.patch),$(ROOT)/kernel/,)@@ -410,6 +410,22 @@ fuzz:+$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo fuzz-targets$(Q)sh $(ROOT)/testing/fuzz/run.sh $(FUZZ_SECONDS) $(FUZZERS)+# The same targets under AFL++, AFL_SECONDS each, FUZZERS and FUZZ_CORPUS+# as above. Each is built twice with AFL_CC, the second time logging its+# comparisons, which afl-fuzz -c runs beside the first to find the magic+# values and checksums the parsers compare input with. Needs AFL++ built+# for the clang the targets take, as .github/scripts/afl-deps.sh builds it.+AFL_CC ?= afl-clang-fast+AFL_SECONDS ?= 600++afl:+ +$(Q)$(MAKE) -C $(ROOT) config+ +$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo fuzz-targets \+ FUZZ_CC=$(AFL_CC) FUZZ_OUT=build/fuzz-afl+ +$(Q)AFL_LLVM_CMPLOG=1 $(MAKE) -C $(ROOT) -f Makefile.shirudo \+ fuzz-targets FUZZ_CC=$(AFL_CC) FUZZ_OUT=build/fuzz-afl-cmplog+ $(Q)sh $(ROOT)/testing/fuzz/afl.sh $(AFL_SECONDS) $(FUZZERS)+bundle: test-vm-bundlehelp:@@ -424,6 +440,7 @@ help:' 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] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \+ ' make -C testing afl [AFL_SECONDS=600] [FUZZ_CORPUS=<dir>] [FUZZERS=<target>...]' \' 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/afl.sh b/testing/fuzz/afl.shnew file mode 100755index 00000000..29766fe8--- /dev/null+++ b/testing/fuzz/afl.sh@@ -0,0 +1,105 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# testing/fuzz/afl.sh -- run the named fuzz targets under afl-fuzz, or all+# of them, as run.sh does under libFuzzer: the builds of make -C testing afl,+# the comparison-logging one beside each, on the seed corpus and what+# FUZZ_CORPUS kept, FUZZ_JOBS at a time. A crash fails the run and leaves+# the input in testing/fuzz.+#+# testing/fuzz/afl.sh <seconds> [<target>...]++set -eu++case ${FUZZ_CORPUS:-} in+'' | /*) ;;+*) FUZZ_CORPUS=$PWD/$FUZZ_CORPUS ;;+esac+cd "$(dirname "$0")"+root=$(cd ../.. && pwd)+bin=$root/build/fuzz-afl+cmplog=$root/build/fuzz-afl-cmplog+seconds=${1:?usage: afl.sh <seconds> [<target>...]}+shift+if [ $# -eq 0 ]; then+ for c in fuzz-*.c; do+ t=${c#fuzz-}+ set -- "$@" "${t%.c}"+ done+fi++# A runner has no core dump handler to keep and the governor is not+# ours to set; neither changes what afl-fuzz finds.+export AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=${AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES-1}+export AFL_SKIP_CPUFREQ=${AFL_SKIP_CPUFREQ-1}+export AFL_TRY_AFFINITY=1 AFL_NO_UI=1 AFL_IGNORE_SEED_PROBLEMS=1++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT++# What afl-fuzz queued goes into the corpus libFuzzer keeps, named by its+# content as libFuzzer names inputs; the next libFuzzer run merges it.+keep() {+ for f in "$2"/default/queue/id:*; do+ [ -f "$f" ] || continue+ sum=$(sha1sum <"$f")+ cp "$f" "$FUZZ_CORPUS/$1/${sum%% *}"+ done+}++run() {+ status=0+ in=$work/$1.in+ out=$work/$1.out+ mkdir -p "$in"+ cp corpus/"$1"/* "$in"/ 2>/dev/null || :+ if [ -n "${FUZZ_CORPUS:-}" ]; then+ mkdir -p "$FUZZ_CORPUS/$1"+ cp "$FUZZ_CORPUS/$1"/* "$in"/ 2>/dev/null || :+ fi+ afl-fuzz -V "$seconds" -t 10000+ -i "$in" -o "$out" \+ -c "$cmplog/fuzz-$1" -- "$bin/fuzz-$1" \+ >"$work/$1.log" 2>&1 || status=$?+ n=0+ for f in "$out"/default/crashes/id:*; do+ [ -f "$f" ] || continue+ n=$((n + 1))+ cp "$f" "crash-afl-$1-$n"+ done+ [ "$n" -eq 0 ] || status=1+ if [ "$status" -eq 0 ] && [ -n "${FUZZ_CORPUS:-}" ]; then+ keep "$1" "$out"+ fi+ if [ -f "$out/default/fuzzer_stats" ]; then+ sed -n 's/^\(execs_done\|corpus_count\|edges_found\) *: //p' \+ "$out/default/fuzzer_stats" | paste -sd ' ' \+ >"$work/$1.stats"+ fi+ echo "$status" >"$work/$1.rc"+}++jobs=${FUZZ_JOBS:-$(nproc 2>/dev/null || echo 1)}+n=0+for t in "$@"; do+ run "$t" &+ n=$((n + 1))+ if [ $((n % jobs)) -eq 0 ]; then+ wait+ fi+done+wait++rc=0+for t in "$@"; do+ if [ "$(cat "$work/$t.rc")" = 0 ]; then+ set -- $(cat "$work/$t.stats" 2>/dev/null || echo '? ? ?')+ printf 'afl: %s: %s runs, %s inputs, %s edges\n' "$t" "$1" "$2" "$3"+ else+ tail -n 40 "$work/$t.log" >&2+ printf 'afl: %s failed; any input is in testing/fuzz/crash-afl-%s-*\n' \+ "$t" "$t" >&2+ rc=1+ fi+done+exit $rc