Skip to content

eBPF-based security platform

build: check the userspace sources with sparse

Daniel Borkmann078172343ab3
diff --git a/.github/scripts/check-sparse.sh b/.github/scripts/check-sparse.sh
new file mode 100755
index 00000000..627f2d9c
--- /dev/null
+++ b/.github/scripts/check-sparse.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# .github/scripts/check-sparse.sh -- run sparse over the userspace sources
+# the binaries are built from, through make sparse, and fail on any
+# warning, above all on a value moved between host order and the __le
+# and __be types of <shirudo/types.h> without cpu_to_le32() and its kin
+# from <shirudo/byteorder.h>.
+#
+# .github/scripts/check-sparse.sh
+#
+# Needs sparse and the build dependencies make needs, OpenSSL 3.5 among
+# them; in CI, .github/scripts/vm-toolchain.sh sparse provides both.
+
+set -eu
+
+cd "$(dirname "$0")/../.."
+command -v cgcc >/dev/null 2>&1 || {
+ printf 'check-sparse: cgcc not found; install sparse\n' >&2
+ exit 2
+}
+exec make sparse
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index edff55c7..e35c63e9 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -35,6 +35,35 @@ jobs:
- name: Check the SPDX tag of every tracked source
run: .github/scripts/check-spdx.sh
+ sparse:
+ name: Sparse
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ # sparse reads the sources against OpenSSL 3.5 and the other build
+ # dependencies, which the toolchain of the LVH guest the other
+ # builds use provides.
+ - name: Run sparse over the userspace sources in LVH VM
+ uses: cilium/little-vm-helper@f86d344aca812dda5aef167867a9a0a90ca6cca5 # v0.0.31
+ with:
+ test-name: shirudo-sparse
+ 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
+ cpu: 4
+ mem: 8G
+ cmd: |
+ cd /host
+ .github/scripts/vm-toolchain.sh sparse
+ . build/vm-toolchain.env
+ .github/scripts/check-sparse.sh
+
checkpatch:
name: Checkpatch
runs-on: ubuntu-latest
diff --git a/AGENTS.md b/AGENTS.md
index 23d4d1c3..af2f83f4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -390,10 +390,13 @@ one usually still compiles and passes tests.
`.github/scripts/check-style.sh <range>` runs the kernel's
checkpatch.pl in strict mode over the C each commit touches and over
its message, with the userspace exceptions listed in the script, and
- rejects the netdev block-comment opener. CI runs all three on every
- pull request, and `.github/scripts/build-commits.sh <range>` builds
- every commit of a pull request on its own; run them before handing
- work over.
+ rejects the netdev block-comment opener.
+ `.github/scripts/check-sparse.sh` runs `make sparse`, sparse over the
+ userspace sources, which fails on a little-endian or big-endian field
+ read or written without `le32_to_cpu()`, `cpu_to_le32()` or their kin
+ from `<shirudo/byteorder.h>`. CI runs all four on every pull request,
+ and `.github/scripts/build-commits.sh <range>` builds every commit of
+ a pull request on its own; run them before handing work over.
- Shell that a workflow runs lives in `.github/scripts/`, not inline in
the workflow file.
- `make -C packaging bindeb-pkg`, `make -C packaging binrpm-pkg` and `make -C packaging bintar-pkg` build the
diff --git a/Documentation/contribute/code/contributing.rst b/Documentation/contribute/code/contributing.rst
index 9e6b4760..6b8a4ec5 100644
--- a/Documentation/contribute/code/contributing.rst
+++ b/Documentation/contribute/code/contributing.rst
@@ -248,6 +248,12 @@ Before sending
characters. ``--diff`` checks the uncommitted changes, ``--files``
whole files and ``--types`` lists what stays enabled. CI runs it as
the ``Checkpatch`` job of the ``Lint`` workflow.
+- Run ``make sparse`` when the change reads or writes a binary format.
+ It runs sparse over the userspace sources and fails on any warning,
+ such as a little-endian field read or written without
+ ``le32_to_cpu()``, ``cpu_to_le32()`` or their kin from
+ ``<shirudo/byteorder.h>``. CI runs it as the ``Sparse`` job of the
+ ``Lint`` workflow.
- Keep each commit to one logical change that builds and passes on its
own, so the history bisects. CI builds every commit of a pull request
on its own in the ``Build every commit`` workflow.
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 90414eb1..66dd2b0f 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -424,6 +424,12 @@ the commit at ``HEAD`` alone without them.
files.
* - ``check-spdx.sh``
- The SPDX tag of every tracked source. Takes no range.
+ * - ``check-sparse.sh``
+ - ``make sparse``: sparse over the userspace sources the binaries
+ are built from, failing on any warning. It finds a value moved
+ between host order and ``__le32`` or ``__be32`` without
+ ``cpu_to_le32()``, ``le32_to_cpu()`` or their kin from
+ ``<shirudo/byteorder.h>``. Takes no range.
* - ``build-commits.sh <range>``
- Builds every commit on its own: ``make``, ``make
shirudo-build`` and ``make -C testing test-helpers``. It
@@ -454,6 +460,7 @@ Before you send a change:
$ .github/scripts/check-dco.sh origin/main..HEAD
$ .github/scripts/check-style.sh origin/main..HEAD
$ .github/scripts/check-spdx.sh
+ $ .github/scripts/check-sparse.sh
$ .github/scripts/build-commits.sh origin/main..HEAD
2. Run the tests the change touches, and ``make tests`` before you
diff --git a/Makefile b/Makefile
index 5091dda9..4e717951 100644
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,7 @@ export prefix exec_prefix bindir libexecdir sharedir libdir mandir completionsdi
systemdunitdir dracutmodulesdir SHIRUDO_TARGET_PKG_CONFIG_PATH \
SHIRUDO_TARGET_STATIC_LIBDIR
-.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build docs bpf-deps install install-target install-operator clean distclean config tests
+.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build docs bpf-deps install install-target install-operator clean distclean config tests sparse
all: config
+$(Q)$(MAKE) -f Makefile.shirudo PROFILE=operator CONFIG_MK=config.mk all
@@ -87,6 +87,7 @@ help:
@printf '%s\n' \
'shirudo make targets:' \
' Build: make, make target, make target-deps, make bpf-deps, make install' \
+ ' Checks: make sparse' \
' Install roles: make install-target, make install-operator' \
' Package: make -C packaging help' \
' Tests: make tests (all of them), make -C testing help' \
@@ -96,6 +97,9 @@ help:
shirudo-build:
+$(Q)$(MAKE) -C tools/shirudo-build
+sparse: config
+ +$(Q)$(MAKE) -f Makefile.shirudo PROFILE=operator CONFIG_MK=config.mk sparse
+
docs:
+$(Q)$(MAKE) -C Documentation serve
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 739a9784..270aaa2a 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -879,6 +879,29 @@ build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \
unit-tests: $(UNIT_TESTS)
+# ----- sparse -----------------------------------------------------
+
+# make sparse runs sparse over the sources the binaries are built from,
+# the vendored ones aside, and fails on any warning it gives. The
+# vendored linux/types.h defines __bitwise as nothing, so sparse reads
+# the system's copy, which makes __le32 and its kin restricted types
+# under __CHECKER__, in its place. _FORTIFY_SOURCE=3 and libcurl's
+# typecheck-gcc.h are left out: sparse does not know the builtin the
+# first needs and reports the sizeof the second applies to a function,
+# and the compiler runs both checks.
+SPARSE_TYPES_H ?= /usr/include/linux/types.h
+SPARSE_SRCS := shirudo.c $(BUILTIN_SRCS) $(UTIL_SRCS) $(BUILD_SRCS)
+
+.PHONY: sparse
+sparse:
+ $(Q)for f in $(SPARSE_SRCS); do \
+ printf ' SPARSE %s\n' "$$f"; \
+ cgcc -no-compile -Wsparse-error $(CFLAGS) -U_FORTIFY_SOURCE \
+ -DCURL_DISABLE_TYPECHECK \
+ -include $(SPARSE_TYPES_H) -D_UAPI_LINUX_TYPES_H \
+ "$$f" || exit 1; \
+ done
+
# ----- fuzzing ----------------------------------------------------
# libFuzzer targets for the parsers of input from outside the host, built