Skip to content

eBPF-based security platform

ci: run the unit tests and fuzz targets under MemorySanitizer

Daniel Borkmanndcaa8c8aa9da
diff --git a/.agents/skills/pre-submit/SKILL.md b/.agents/skills/pre-submit/SKILL.md
index 0984ee13..b8a9570a 100644
--- a/.agents/skills/pre-submit/SKILL.md
+++ b/.agents/skills/pre-submit/SKILL.md
@@ -61,6 +61,8 @@ make -B CC=clang && make -B CC=clang shirudo-build &&
make -B -f Makefile.shirudo CC=clang test-helpers
make tests VM=0 SANITIZE=address,undefined # CC=gcc and CC=clang,
# one worktree each
+sh .github/scripts/msan-deps.sh && # MSan, a worktree of
+ sh .github/scripts/msan-tests.sh 60 # its own
make -C testing fuzz FUZZ_SECONDS=60
make proofs-cbmc CHANGED=$range # the proofs that read
make proofs-lean4 # what the series changed
diff --git a/.agents/skills/testing/SKILL.md b/.agents/skills/testing/SKILL.md
index 75a1f8c2..74b72852 100644
--- a/.agents/skills/testing/SKILL.md
+++ b/.agents/skills/testing/SKILL.md
@@ -20,7 +20,9 @@ make -C testing unit/<suite> # one unit suite
make -C testing functional/<family> # one family, its VM tests too
make tests VM=0 # without the target tests
make tests VM=0 SANITIZE=address,undefined # CI: once with gcc, once with clang
-make -C testing fuzz [FUZZ_SECONDS=60]
+sh .github/scripts/msan-deps.sh # MSan: instrumented libraries once,
+sh .github/scripts/msan-tests.sh [60] # then unit and fuzz from a clean tree
+make -C testing fuzz [FUZZ_SECONDS=60] [FUZZERS=<target>...]
make -C testing help
```
diff --git a/.github/scripts/msan-deps.sh b/.github/scripts/msan-deps.sh
new file mode 100755
index 00000000..9f38bf02
--- /dev/null
+++ b/.github/scripts/msan-deps.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# Build the libraries the MemorySanitizer suite links, each instrumented:
+# MemorySanitizer takes what code it did not instrument writes as never
+# written, so a library left as the distribution ships it makes every read
+# of its output a report. LuaJIT is left out, its interpreter being
+# assembly, and with it the decoder tests. The libraries go to
+# MSAN_PREFIX, /tmp/shirudo-msan by default, and a copy to MSAN_CACHE,
+# build/msan-deps, which a later run copies back instead of building.
+
+set -eu
+
+ZLIB_VERSION=1.4
+ZLIB_SHA256=02dd159783677a749c04adbdd02b55b8a2bcc7e054f51b76b549199a916ad584
+OPENSSL_VERSION=3.5.9
+OPENSSL_SHA256=603f5602e2eef00d77fbd429d34dcd5822bb301757a1bc9cdb24c670f1eb859a
+ELFUTILS_VERSION=0.197
+ELFUTILS_SHA256=1fdf525f22dcf796d99a99ba3ea00a1f7b1ae5421bc62e8443a708920b99b904
+CURL_VERSION=8.18.0
+CURL_SHA256=40df79166e74aa20149365e11ee4c798a46ad57c34e4f68fd13100e2c9a91946
+TSS_VERSION=4.2.0
+
+CC=${CC:-clang-23}
+prefix=${MSAN_PREFIX:-/tmp/shirudo-msan}
+cache=${MSAN_CACHE:-$(cd "$(dirname "$0")/../.." && pwd)/build/msan-deps}
+work=${MSAN_WORK:-/tmp/msan-src}
+jobs=$(nproc)
+
+if [ -s "$cache/lib/libcrypto.so" ]; then
+ rm -rf "$prefix"
+ cp -a "$cache" "$prefix"
+ exit 0
+fi
+
+msan="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1"
+export CC
+export CFLAGS="$msan"
+export LDFLAGS="-fsanitize=memory -L$prefix/lib -Wl,-rpath,$prefix/lib"
+export CPPFLAGS="-I$prefix/include"
+export PKG_CONFIG_PATH="$prefix/lib/pkgconfig"
+
+fetch() {
+ url=$1 sum=$2
+ file="$work/${url##*/}"
+ [ -f "$file" ] || curl -fsSL --retry 3 -o "$file" "$url"
+ printf '%s %s\n' "$sum" "$file" | sha256sum -c --quiet
+ tar -C "$work" -xf "$file"
+}
+
+rm -rf "$prefix" "$cache"
+mkdir -p "$work" "$prefix"
+
+fetch "https://github.com/madler/zlib/releases/download/v$ZLIB_VERSION/zlib-$ZLIB_VERSION.tar.gz" \
+ "$ZLIB_SHA256"
+(cd "$work/zlib-$ZLIB_VERSION" &&
+ ./configure --prefix="$prefix" &&
+ make -j"$jobs" && make install)
+
+# No assembly, which the sanitizer cannot see into either.
+fetch "https://github.com/openssl/openssl/releases/download/openssl-$OPENSSL_VERSION/openssl-$OPENSSL_VERSION.tar.gz" \
+ "$OPENSSL_SHA256"
+(cd "$work/openssl-$OPENSSL_VERSION" &&
+ CFLAGS='' LDFLAGS='' CPPFLAGS='' ./Configure linux-x86_64-clang \
+ --prefix="$prefix" --openssldir=/etc/ssl --libdir=lib \
+ enable-msan no-asm no-docs no-tests \
+ -fsanitize-memory-track-origins=2 -Wl,-rpath,"$prefix/lib" &&
+ make -j"$jobs" && make install_sw)
+
+# libelf alone, the part libbpf links.
+fetch "https://sourceware.org/elfutils/ftp/$ELFUTILS_VERSION/elfutils-$ELFUTILS_VERSION.tar.bz2" \
+ "$ELFUTILS_SHA256"
+(cd "$work/elfutils-$ELFUTILS_VERSION" &&
+ ./configure --prefix="$prefix" --enable-sanitize-memory \
+ --disable-debuginfod --disable-libdebuginfod --without-bzlib \
+ --without-lzma --without-zstd &&
+ make -j"$jobs" -C lib && make -j"$jobs" -C libelf &&
+ make -C libelf install && make -C config install-pkgconfigDATA)
+
+fetch "https://curl.se/download/curl-$CURL_VERSION.tar.xz" "$CURL_SHA256"
+(cd "$work/curl-$CURL_VERSION" &&
+ ./configure --prefix="$prefix" --with-openssl="$prefix" \
+ --with-zlib="$prefix" \
+ --with-ca-bundle=/etc/ssl/certs/ca-certificates.crt \
+ --with-ca-path=/etc/ssl/certs \
+ --without-libpsl --without-brotli --without-zstd \
+ --without-nghttp2 --without-nghttp3 --without-ngtcp2 \
+ --without-libidn2 --without-librtmp --without-libssh2 \
+ --without-libssh --without-libgsasl \
+ --disable-ldap --disable-ldaps --disable-rtsp --disable-dict \
+ --disable-telnet --disable-tftp --disable-pop3 --disable-imap \
+ --disable-smtp --disable-gopher --disable-mqtt --disable-smb \
+ --disable-ftp --disable-file --disable-manual --disable-docs &&
+ make -j"$jobs" && make install)
+
+[ -d "$work/tpm2-tss-$TSS_VERSION" ] ||
+ git clone --quiet --depth 1 --branch "$TSS_VERSION" \
+ https://github.com/tpm2-software/tpm2-tss.git \
+ "$work/tpm2-tss-$TSS_VERSION"
+(cd "$work/tpm2-tss-$TSS_VERSION" && ./bootstrap &&
+ ./configure --prefix="$prefix" --libdir="$prefix/lib" \
+ --disable-defaultflags --disable-fapi --disable-policy \
+ --disable-doxygen-doc \
+ --disable-tcti-i2c-ftdi --disable-tcti-spi-ftdi \
+ --disable-tcti-spi-ltt2go --disable-tcti-libtpms \
+ --disable-tcti-pcap &&
+ make -j"$jobs" && make install)
+
+mkdir -p "$(dirname "$cache")"
+cp -a "$prefix" "$cache"
diff --git a/.github/scripts/msan-tests.sh b/.github/scripts/msan-tests.sh
new file mode 100755
index 00000000..3cd4b9c4
--- /dev/null
+++ b/.github/scripts/msan-tests.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# The MemorySanitizer suite: the unit tests and a run of every fuzz target
+# for the seconds given, built with SANITIZE=memory against the libraries
+# msan-deps.sh instrumented. What runs Lua is left out: LuaJIT's
+# interpreter is assembly no sanitizer instruments, so what it hands back
+# would read as uninitialized. ASan and UBSan cover those.
+#
+# .github/scripts/msan-tests.sh [<seconds>]
+
+set -eu
+
+seconds=${1:-30}
+prefix=${MSAN_PREFIX:-/tmp/shirudo-msan}
+lua_units='upatch_decode'
+lua_fuzzers='decoder'
+
+# The libraries are found by the binaries' run path, not by
+# LD_LIBRARY_PATH, which would hand them to every tool the build runs.
+# The prefix's openssl is the ML-DSA one the configuration asks for.
+export CC="${CC:-clang-23}"
+export PATH="$prefix/bin:$PATH"
+export PKG_CONFIG_PATH="$prefix/lib/pkgconfig"
+export LDFLAGS="-Wl,-rpath,$prefix/lib"
+
+cd "$(dirname "$0")/../.."
+
+listed() {
+ case " $2 " in *" $1 "*) return 0 ;; esac
+ return 1
+}
+
+units=
+for f in testing/unit/*.c; do
+ s=${f##*/}
+ s=${s%.c}
+ [ "$s" = unit ] || listed "$s" "$lua_units" || units="$units unit/$s"
+done
+fuzzers=
+for f in testing/fuzz/fuzz-*.c; do
+ t=${f##*/fuzz-}
+ t=${t%.c}
+ listed "$t" "$lua_fuzzers" || fuzzers="$fuzzers $t"
+done
+
+# The builds run in parallel, the suites one after the other.
+jobs=$(nproc)
+make -j"$jobs" -C testing SANITIZE=memory build-unit
+# shellcheck disable=SC2086 # one word per suite
+make -C testing SANITIZE=memory $units
+make -j"$jobs" -C testing fuzz SANITIZE=memory FUZZ_CC="$CC" \
+ FUZZ_SECONDS="$seconds" FUZZERS="${fuzzers# }"
diff --git a/.github/workflows/sanitizers.yml b/.github/workflows/sanitizers.yml
index d66a1712..b00069da 100644
--- a/.github/workflows/sanitizers.yml
+++ b/.github/workflows/sanitizers.yml
@@ -103,6 +103,87 @@ jobs:
path: build/sanitizer-openssl
key: ${{ steps.sanitizer-openssl.outputs.cache-primary-key }}
+ # MemorySanitizer finds reads of memory never written, which ASan and
+ # UBSan do not, and needs every library the code reads from rebuilt with
+ # it, which msan-deps.sh does and the cache keeps. The suite leaves out
+ # what runs Lua, as LuaJIT's interpreter cannot be instrumented.
+ msan:
+ name: MSan
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ - name: Restore the MemorySanitizer libraries
+ id: msan-deps
+ uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
+ with:
+ path: build/msan-deps
+ key: msan-deps-${{ hashFiles('.github/scripts/msan-deps.sh') }}
+
+ # The unit tests link the util objects, whose build needs the BTF of
+ # a signed-BPF capable bpf-next kernel, as the other jobs do. A push
+ # or a pull request gets a minute per fuzz target, the nightly run
+ # ten.
+ - name: Build under MSan and run the unit tests and fuzz targets in LVH VM
+ uses: cilium/little-vm-helper@c8b0bfbae3bdda7bc69fb9cd370e4b74ce84986d # v0.0.33
+ with:
+ test-name: shirudo-msan
+ image: kind
+ image-version: bpf-next-20260603.072225
+ kernel-version: bpf-next-main
+ host-mount: ${{ github.workspace }}
+ images-folder-parent: ${{ github.workspace }}/../lvh
+ install-dependencies: true
+ cmd: |
+ grep -aq 'signature_size' /sys/kernel/btf/vmlinux || {
+ echo 'kernel BTF lacks bpf_attr.signature_size; needs a signed-BPF capable bpf-next kernel' >&2
+ exit 1
+ }
+ cd /host
+
+ apt-get update
+ apt-get install -y --no-install-recommends \
+ autoconf \
+ autoconf-archive \
+ automake \
+ bison \
+ build-essential \
+ ca-certificates \
+ curl \
+ flex \
+ gawk \
+ git \
+ libluajit-5.1-dev \
+ libtool \
+ m4 \
+ pkg-config \
+ python3 \
+ xz-utils
+ sh .github/scripts/install-clang.sh libclang-rt-23-dev
+
+ git config --global --add safe.directory /host
+
+ sh .github/scripts/msan-deps.sh
+ sh .github/scripts/msan-tests.sh \
+ ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && 600 || 60 }}
+
+ - name: Save the MemorySanitizer libraries
+ if: always() && steps.msan-deps.outputs.cache-hit != 'true' && hashFiles('build/msan-deps/lib/libcrypto.so') != ''
+ uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
+ with:
+ path: build/msan-deps
+ key: ${{ steps.msan-deps.outputs.cache-primary-key }}
+
+ - name: Upload the crash inputs
+ if: failure()
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
+ with:
+ name: msan-crashes
+ path: testing/fuzz/crash-*
+ if-no-files-found: ignore
+
libfuzzer:
name: libFuzzer
runs-on: ubuntu-latest
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index b459c1be..c397f160 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -203,6 +203,11 @@ variables change it:
* - ``SANITIZE=address,undefined``
- Instruments the host binaries and the test helpers with ASan and
UBSan. See :doc:`/contribute/testing/testing`.
+ * - ``SANITIZE=memory``
+ - Instruments them with MSan, the vendored libbpf included, and
+ wants the instrumented libraries
+ :src:`.github/scripts/msan-deps.sh` builds. See
+ :doc:`/contribute/testing/testing`.
Build the shirudo-build helper
------------------------------
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 9b1c2ff1..ea76f453 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -85,7 +85,8 @@ Test tiers
- ``make -C testing end2end``
* - Fuzz
- :src:`testing/fuzz/`
- - On the build host, under libFuzzer, ASan and UBSan
+ - On the build host, under libFuzzer with ASan and UBSan, or with
+ MSan
- ``make -C testing fuzz``
* - Proofs
- :src:`testing/proofs/`
@@ -272,6 +273,45 @@ Run under the sanitizers
objects are unaffected. Recovery is off, so every report fails the run.
CI runs the same with gcc and with clang.
+Run under MemorySanitizer
+-------------------------
+
+MemorySanitizer reports a read of memory that nothing wrote, which ASan
+and UBSan do not. It takes what code it did not instrument writes as
+never written, so every library the code reads from must be built with
+it too.
+
+1. Build the instrumented zlib, OpenSSL, libelf, libcurl and tpm2-tss
+ into ``/tmp/shirudo-msan``. The script keeps a copy in
+ ``build/msan-deps`` and copies it back on a later run instead of
+ building again:
+
+ .. code-block:: console
+
+ $ sh .github/scripts/msan-deps.sh
+
+2. Remove the objects of an earlier build:
+
+ .. code-block:: console
+
+ $ make clean
+
+3. Build with ``SANITIZE=memory`` and run the unit tests and every fuzz
+ target, here for 60 seconds each:
+
+ .. code-block:: console
+
+ $ sh .github/scripts/msan-tests.sh 60
+
+Both need clang 23 with its MemorySanitizer and libFuzzer runtimes.
+``SANITIZE=memory`` instruments the vendored libbpf as well. The suite
+leaves out the decoder's unit tests and fuzz target: LuaJIT's
+interpreter is assembly, which no sanitizer instruments, so the values
+it hands back would read as uninitialized. ASan and UBSan cover those.
+The ``MSan`` job of the ``Sanitizers`` workflow runs the same, the fuzz
+targets for 60 seconds on each push and pull request and for ten
+minutes in the nightly run.
+
Run the fuzz targets
--------------------
@@ -294,8 +334,10 @@ runner.
``<target>`` is the name of ``testing/fuzz/fuzz-<target>.c``, such as
``json``.
-``FUZZ_JOBS`` sets how many targets run at a time, one per CPU by
-default. A crash fails the run and leaves its input in
+``FUZZERS`` names the targets ``make -C testing fuzz`` runs, all of them
+by default, and ``FUZZ_JOBS`` how many run at a time, one per CPU by
+default. The targets are built under the sanitizers ``SANITIZE`` names,
+ASan and UBSan without it. A crash fails the run and leaves its input in
``testing/fuzz/crash-<target>-*``.
What a target finds is discarded after the run unless
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 1c7c872d..bfe391e6 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -153,10 +153,17 @@ CFLAGS_BASE := -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) $(CC_WARNINGS) \
# the test helpers; BPF objects come from clang -target bpf and are never
# affected. Recovery is off so that every report fails the run. The
# fortified libc calls stay out of such a build, as they would bypass the
-# sanitizer's own checks.
+# sanitizer's own checks. SANITIZE=memory instruments the vendored libbpf
+# too and wants every library linked instrumented as well, which
+# .github/scripts/msan-deps.sh builds: MemorySanitizer takes what it did
+# not see written as uninitialized.
SANITIZE ?=
+comma := ,
+SANITIZE_MEMORY := $(filter memory,$(subst $(comma), ,$(SANITIZE)))
+SANITIZE_CFLAGS := $(if $(SANITIZE),-fsanitize=$(SANITIZE)) \
+ $(if $(SANITIZE_MEMORY),-fsanitize-memory-track-origins=2)
ifneq ($(SANITIZE),)
-CFLAGS_BASE += -O1 -fno-omit-frame-pointer -fsanitize=$(SANITIZE) \
+CFLAGS_BASE += -O1 -fno-omit-frame-pointer $(SANITIZE_CFLAGS) \
-fno-sanitize-recover=all -U_FORTIFY_SOURCE
else
# No frame larger than 128 KiB: a table belongs on the heap, where its
@@ -390,12 +397,13 @@ libbpf: $(LIBBPF_A)
$(LIBBPF_A): $(LIBBPF_SRCS)
@printf ' LIBBPF\n'
$(Q)$(MAKE) -C $(LIBBPF_VENDOR) CC="$(CC)" Q="$(Q)" \
- EXTRA_CFLAGS="$(WERROR_CFLAGS)"
+ EXTRA_CFLAGS="$(WERROR_CFLAGS) $(if $(SANITIZE_MEMORY), \
+ -fno-omit-frame-pointer $(SANITIZE_CFLAGS))"
# Build-time only; links the vendored libbpf and dumps BTF as C.
$(VMLINUX_GEN): tools/gen-vmlinux-h/gen-vmlinux-h.c $(LIBBPF_A)
@printf ' CC $@\n'
- $(Q)$(CC) $(CFLAGS_BASE) $(INCLUDES) -o $@ $< $(LIBBPF_A) \
+ $(Q)$(CC) $(CFLAGS_BASE) $(INCLUDES) $(LDFLAGS) -o $@ $< $(LIBBPF_A) \
$(LIBELF_LIBS) $(ZLIB_LIBS)
# ----- phase 2: generated headers ---------------------------------
@@ -1132,13 +1140,15 @@ sparse:
# ----- fuzzing ----------------------------------------------------
# libFuzzer targets for the parsers of input from outside the host, built
-# with clang from the util sources under ASan and UBSan; make -C testing
-# fuzz builds and runs them.
+# with clang from the util sources under the sanitizers SANITIZE names, ASan
+# and UBSan without it; make -C testing fuzz builds and runs them.
FUZZ_CC ?= clang
+FUZZ_SANITIZE := $(if $(SANITIZE),$(SANITIZE_CFLAGS), \
+ -fsanitize=address$(comma)undefined)
FUZZ_WARNINGS := $(call cc-option,$(FUZZ_CC),$(PROBED_WARNINGS))
FUZZ_CFLAGS := -O1 -g -std=gnu11 -fno-omit-frame-pointer $(LANG_CFLAGS) \
$(call cc-option,$(FUZZ_CC),$(PROBED_LANG)) \
- -fsanitize=fuzzer,address,undefined \
+ -fsanitize=fuzzer $(FUZZ_SANITIZE) \
-fno-sanitize-recover=all $(WARNINGS) $(FUZZ_WARNINGS) \
-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(INCLUDES) \
$(LIBCRYPTO_CFLAGS) $(LIBCURL_CFLAGS) $(ZLIB_CFLAGS)
@@ -1263,7 +1273,7 @@ testing/fuzz/fuzz-config-update: FUZZ_INCLUDED := util/config_update.c
$(FUZZ_TARGETS): testing/fuzz/fuzz.h
@printf ' FUZZ $@\n'
- $(Q)$(FUZZ_CC) $(FUZZ_CFLAGS) -o $@ \
+ $(Q)$(FUZZ_CC) $(FUZZ_CFLAGS) $(LDFLAGS) -o $@ \
$(filter-out $(FUZZ_INCLUDED),$(filter %.c,$^)) $(FUZZ_LIBS)
fuzz-targets: $(FUZZ_TARGETS)
diff --git a/testing/Makefile b/testing/Makefile
index 0e6f8748..87a1a4b0 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -400,13 +400,15 @@ test-helpers:
# The parsers of input from outside the host under libFuzzer, each target
# for FUZZ_SECONDS on its seed corpus and, with FUZZ_CORPUS=<dir>, on what
-# earlier runs kept there; needs clang with its fuzzer runtime.
+# earlier runs kept there; FUZZERS names the targets to run, all of them
+# without it. Needs clang with its fuzzer runtime.
FUZZ_SECONDS ?= 60
+FUZZERS ?=
fuzz:
+$(Q)$(MAKE) -C $(ROOT) config
+$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo fuzz-targets
- $(Q)sh $(ROOT)/testing/fuzz/run.sh $(FUZZ_SECONDS)
+ $(Q)sh $(ROOT)/testing/fuzz/run.sh $(FUZZ_SECONDS) $(FUZZERS)
bundle: test-vm-bundle
@@ -421,7 +423,7 @@ help:
' TEST_FILTER=<regex> selects test filenames (empty runs everything).' \
' 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>]' \
+ ' make -C testing fuzz [FUZZ_SECONDS=60] [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' \