Skip to content

eBPF-based security platform

ci: run clang's static analyzer over the userspace sources

Daniel Borkmann8cb8b724b995
diff --git a/.agents/skills/pre-submit/SKILL.md b/.agents/skills/pre-submit/SKILL.md
index 6b30df8f..3b438710 100644
--- a/.agents/skills/pre-submit/SKILL.md
+++ b/.agents/skills/pre-submit/SKILL.md
@@ -54,6 +54,7 @@ signs off.
.github/scripts/check-sparse.sh
.github/scripts/check-spdx.sh
.github/scripts/check-unused.sh
+scan-build-23 --status-bugs --exclude vendor make ANALYZE=1 # from a clean tree
make -C Documentation check # DOCS_CONTAINER=0 without podman or docker
make -B && make -B shirudo-build && make -B quote-check &&
make -B -f Makefile.shirudo test-helpers
@@ -75,8 +76,10 @@ A warning fails every build. Fix it rather than silence it; `WERROR=0`
is only for a compiler newer than the tree while its new warnings get
fixed. `check-sparse.sh` refuses a little- or big-endian field read or
written without `le32_to_cpu()`, `cpu_to_le32()` or their kin, and
-`check-spdx.sh` the wrong license tag for a file's kind, and
-`check-unused.sh` an extern function whose last caller went.
+`check-spdx.sh` the wrong license tag for a file's kind,
+`check-unused.sh` an extern function whose last caller went, and
+scan-build what clang's static analyzer proves on some path: a NULL or
+uninitialized value used, a leak, a use after free, a store never read.
CI does not run the proofs, so this run is the only one a series gets
before it lands. `CHANGED` takes what `git diff` takes and runs the CBMC
diff --git a/.github/scripts/scan-build.sh b/.github/scripts/scan-build.sh
new file mode 100755
index 00000000..f79b25d0
--- /dev/null
+++ b/.github/scripts/scan-build.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# .github/scripts/scan-build.sh -- build the binary, the shirudo-build
+# helper and the quote checker under clang's static analyzer with its
+# default checkers, on Debian testing with what debian-deps.sh installs,
+# and fail on any finding. The analyzer follows the paths through each
+# function and its callees in the file: a value stored and never read,
+# a NULL or uninitialized one used, memory leaked or used after its
+# free. The vendored libbpf is analyzed as it is built but not reported.
+# The build is make ANALYZE=1, without the libc wrappers that hide from
+# the analyzer which function a call makes. The HTML report goes to
+# build/scan-build.
+#
+# .github/scripts/scan-build.sh <vmlinux-btf>
+#
+# Run as root in the checkout root, inside a throwaway container.
+
+set -eu
+
+if [ "$#" -ne 1 ]; then
+ printf 'usage: %s <vmlinux-btf>\n' "$0" >&2
+ exit 2
+fi
+export BPF_VMLINUX_BTF="$1"
+
+grep -aq 'signature_size' "$BPF_VMLINUX_BTF" || {
+ echo "$BPF_VMLINUX_BTF lacks bpf_attr.signature_size; needs a signed-BPF capable bpf-next kernel" >&2
+ exit 1
+}
+sh .github/scripts/debian-deps.sh
+sh .github/scripts/install-clang.sh clang-tools-23
+
+jobs=$(nproc)
+scan-build-23 --status-bugs --use-cc=clang-23 --exclude vendor \
+ -o build/scan-build \
+ sh -c "make -j$jobs ANALYZE=1 && make ANALYZE=1 shirudo-build &&
+ make ANALYZE=1 quote-check"
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 3206f0e9..7ae59425 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -96,6 +96,42 @@ jobs:
if-no-files-found: error
retention-days: 1
+ # clang's static analyzer follows the paths through each function and
+ # reports what a compiler warning does not: a NULL or uninitialized
+ # value used, memory leaked or used after its free, a store never read.
+ scan-build:
+ name: scan-build
+ needs: btf
+ runs-on: ubuntu-latest
+ container: debian:testing
+ timeout-minutes: 60
+
+ steps:
+ # Without git the checkout is no repository, and the build takes its
+ # version from one.
+ - name: Install git
+ run: apt-get update && apt-get install -y --no-install-recommends ca-certificates git
+
+ - name: Check out repository
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ - name: Download the kernel BTF
+ uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
+ with:
+ name: bpf-next-btf
+ path: build
+
+ - name: Build under clang's static analyzer
+ run: sh .github/scripts/scan-build.sh build/vmlinux.btf
+
+ - name: Upload the analyzer's report
+ if: failure()
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
+ with:
+ name: scan-build-report
+ path: build/scan-build
+ if-no-files-found: ignore
+
arch:
name: ${{ matrix.arch }}
needs: btf
diff --git a/AGENTS.md b/AGENTS.md
index ca880d4f..bc71603f 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -345,8 +345,8 @@ one usually still compiles and passes tests.
`proofs` skill says how.
- CI runs `.github/scripts/build-commits.sh`, `check-style.sh`,
`check-dco.sh`, `check-coccinelle.sh`, `check-sparse.sh`,
- `check-spdx.sh`, `check-unused.sh` and the documentation check on
- every pull request;
+ `check-spdx.sh`, `check-unused.sh`, `scan-build.sh` and the
+ documentation check on every pull request;
run them before handing work over, as the `pre-submit` skill says.
Shell that a workflow runs lives in `.github/scripts/`, not inline in
the workflow file.
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 7c9692d2..c90e55ce 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -480,6 +480,19 @@ the commit at ``HEAD`` alone without them.
- An extern function of the userspace C that nothing calls: the
compiler reports an unused ``static`` function, never an extern
one. A call from a test counts. Takes no range.
+ * - ``scan-build.sh <vmlinux-btf>``
+ - The binary, the shirudo-build helper and the quote checker built
+ under clang's static analyzer with its default checkers, failing
+ on any finding: a NULL or uninitialized value used, memory leaked
+ or used after its free, a store never read and more. It installs
+ Debian testing's packages and clang 23's tools, so it runs in a
+ throwaway container; on a host that has them,
+ ``scan-build-23 --status-bugs --exclude vendor make ANALYZE=1``
+ does the same. ``ANALYZE=1`` builds without ``_FORTIFY_SOURCE``
+ and with ``-fno-inline``, whose absence would let glibc's
+ wrappers hide from the analyzer which libc function a call
+ makes. CI runs it as the ``scan-build`` job of the ``Build``
+ workflow and keeps the HTML report of a failed run.
* - ``check-sparse.sh``
- ``make sparse``: sparse over the userspace sources the binaries
are built from, failing on any warning. It finds a value moved
diff --git a/Makefile.shirudo b/Makefile.shirudo
index bfe391e6..e143c3a6 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -169,8 +169,13 @@ else
# No frame larger than 128 KiB: a table belongs on the heap, where its
# allocation fails cleanly, not on a stack two such frames would take a
# good part of. ASan pads every object, so the sanitized build is left
-# without the limit.
-CFLAGS_BASE += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 \
+# without the limit. ANALYZE=1 builds for clang's static analyzer,
+# without _FORTIFY_SOURCE and with -fno-inline: glibc's fortified and
+# inline wrappers hide from it which libc function a call makes, and
+# with it the errno a failure sets and the memory getline() hands back.
+ANALYZE ?=
+CFLAGS_BASE += -U_FORTIFY_SOURCE \
+ $(if $(ANALYZE),-fno-inline,-D_FORTIFY_SOURCE=3) \
-Wframe-larger-than=131072
endif