Skip to content

eBPF-based security platform

treewide: compile every BPF object with -fno-jump-tables

Daniel Borkmannc13473ff9d33
diff --git a/AGENTS.md b/AGENTS.md
index 5f81b8bb..31a5c9c4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -325,7 +325,10 @@ one usually still compiles and passes tests.
- **BPF targets instruction set v4.** Every `clang -target bpf` in the
tree passes `-mcpu=v4`: the upatch build, the embedded objects, the
configure probe and the tests. Not `-mcpu=probe`, which describes the
- build host's kernel, not the targets'. upatches are therefore compiled
+ build host's kernel, not the targets'. Each compile also passes
+ `-fno-jump-tables`: clang 23 otherwise turns a dense `switch` into a
+ table in `.rodata` read at an offset the verifier cannot bound, and
+ the program is refused. upatches are therefore compiled
with the full BPF instruction set: load-acquire and store-release,
sign-extending loads and moves, and signed division and modulo are all
there, so write the code that needs them directly and do not work
diff --git a/Documentation/concepts/background/co-re-btf.rst b/Documentation/concepts/background/co-re-btf.rst
index e99b9538..5de055a2 100644
--- a/Documentation/concepts/background/co-re-btf.rst
+++ b/Documentation/concepts/background/co-re-btf.rst
@@ -109,7 +109,8 @@ How shirudo uses CO-RE
----------------------
``shirudo build`` compiles a ``.bpf.c`` file with
-``clang -target bpf -mcpu=v4 -O2 -g`` against a ``vmlinux.h`` and hands the object
+``clang -target bpf -mcpu=v4 -fno-jump-tables -O2 -g`` against a ``vmlinux.h`` and
+hands the object
to gen_loader. Unless
the operator names a header, the build uses ``./vmlinux.h``, then
``./vendor/linux/vmlinux.h``, then the copy installed with shirudo
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index c9fc140a..11ee2736 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -94,7 +94,8 @@ signed config image.
- The slim kernel configuration for NitroTPM CRB, EC2, EFI and SSM
* - ``t1009-upatch-isa``
- The upatch build compiles for BPF instruction set v4, and a
- ``-mcpu`` in ``--extra-cflags`` replaces it
+ ``-mcpu`` in ``--extra-cflags`` replaces it; a dense ``switch``
+ compiles to branches, not a table in ``.rodata``
``signing`` (2)
~~~~~~~~~~~~~~~
diff --git a/Documentation/reference/cli/build.rst b/Documentation/reference/cli/build.rst
index 1e8a18bc..968d4154 100644
--- a/Documentation/reference/cli/build.rst
+++ b/Documentation/reference/cli/build.rst
@@ -60,12 +60,14 @@ Building a upatch
``./vmlinux.h``, else ``./vendor/linux/vmlinux.h``, else the installed
``vmlinux.h`` (``/usr/share/shirudo/vmlinux.h`` in the operator
package, ``/usr/local/share/shirudo/vmlinux.h`` after ``make
- install``). It then runs ``/usr/bin/clang -target bpf -mcpu=v4 -O2
- -g -Wall -Wextra -Wshadow -Wtype-limits`` with the ``vmlinux.h``
- directory, the libbpf headers and the shirudo BPF headers on the
- include path, plus ``--extra-cflags``. ``-mcpu=v4`` selects BPF
- instruction set v4, the newest clang knows and one every target kernel
- runs. The clang path is fixed when the helper is built,
+ install``). It then runs ``/usr/bin/clang -target bpf -mcpu=v4
+ -fno-jump-tables -O2 -g -Wall -Wextra -Wshadow -Wtype-limits`` with
+ the ``vmlinux.h`` directory, the libbpf headers and the shirudo BPF
+ headers on the include path, plus ``--extra-cflags``. ``-mcpu=v4``
+ selects BPF instruction set v4, the newest clang knows and one every
+ target kernel runs. ``-fno-jump-tables`` keeps a ``switch`` in
+ branches, where clang would otherwise read its results from a table in
+ ``.rodata`` at an offset the verifier cannot bound. The clang path is fixed when the helper is built,
``/usr/lib/llvm-23/bin/clang`` in the Debian package, and ``PATH`` is
not searched. The object goes to a temporary file in
``/tmp``, or with ``--emit-artifacts`` beside the output.
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 3c626f91..89773f09 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -205,7 +205,11 @@ endif
# -Wmissing-declarations; the BPF_PROG() / BPF_KPROBE() macros
# generate auto-named params that often go unused. Neither is
# fixable upstream of us, so silence both for BPF compilation.
-CLANG_BPF := $(CLANG) -target bpf -mcpu=v4 -O2 -g -Wall -Wextra -Wvla -Wshadow \
+# -fno-jump-tables keeps a switch in branches: clang 23 otherwise reads
+# its results from a table in .rodata at an offset the verifier cannot
+# bound, and refuses the program.
+CLANG_BPF := $(CLANG) -target bpf -mcpu=v4 -fno-jump-tables -O2 -g -Wall -Wextra \
+ -Wvla -Wshadow \
-Wtype-limits -Wimplicit-fallthrough \
-Wno-missing-declarations -Wno-unused-parameter \
-Wdeclaration-after-statement $(WERROR_CFLAGS)
diff --git a/testing/functional/build/t1009-upatch-isa.sh b/testing/functional/build/t1009-upatch-isa.sh
index 79e02056..e8360e28 100644
--- a/testing/functional/build/t1009-upatch-isa.sh
+++ b/testing/functional/build/t1009-upatch-isa.sh
@@ -5,7 +5,9 @@
# t1009 -- the upatch build compiles for BPF instruction set v4, which
# load-acquire and store-release need, unless --extra-cflags names another
# -mcpu. A sign-extending load is one instruction, BPF_MEMSX, only in v4,
-# and every clang that builds shirudo emits it there.
+# and every clang that builds shirudo emits it there. A dense switch
+# stays in branches, where clang 23 would read its results from a table
+# in .rodata at an offset the verifier cannot bound.
cd "$(dirname "$0")/../.."
. ./test-lib.sh
@@ -39,11 +41,38 @@ int BPF_PROG(shirudo_test_isa)
}
EOF
-# Builds $W/<name>.upatch and keeps the object as $W/<name>.o.
+cat >"$W/switch.bpf.c" <<'EOF'
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+SEC("lsm/file_ioctl")
+int BPF_PROG(shirudo_test_switch, struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ switch (cmd & 0xff) {
+ case 0: return 0;
+ case 1: return -1;
+ case 2: return -13;
+ case 3: return 0;
+ case 4: return -1;
+ case 5: return -22;
+ case 6: return 0;
+ case 7: return -95;
+ default: return 0;
+ }
+}
+EOF
+
+# Builds $W/<name>.upatch from $W/<source> and keeps the object as
+# $W/<name>.o.
build() {
name=$1
- shift
- (cd .. && tools/shirudo-build/shirudo-build "$W/isa.bpf.c" \
+ source=$2
+ shift 2
+ (cd .. && tools/shirudo-build/shirudo-build "$W/$source" \
-o "$W/$name.upatch" --name "t1009-$name" \
--vmlinux vendor/linux/vmlinux.h --keys-dir "$W/keys" \
--emit-artifacts "$@") >"$W/$name.log" 2>&1
@@ -71,16 +100,41 @@ sys.exit(1)
EOF
}
+# Whether the BPF object has a .rodata section.
+rodata() {
+ python3 - "$1" <<'EOF'
+import struct
+import sys
+
+data = open(sys.argv[1], "rb").read()
+shoff, = struct.unpack_from("<Q", data, 0x28)
+shentsize, shnum, shstrndx = struct.unpack_from("<HHH", data, 0x3a)
+names, = struct.unpack_from("<Q", data, shoff + shstrndx * shentsize + 0x18)
+for i in range(shnum):
+ name, = struct.unpack_from("<I", data, shoff + i * shentsize)
+ end = data.index(b"\0", names + name)
+ if data[names + name:end].startswith(b".rodata"):
+ sys.exit(0)
+sys.exit(1)
+EOF
+}
+
test_expect_success 'the upatch build compiles for BPF instruction set v4' '
- build default &&
+ build default isa.bpf.c &&
[ -s "$W/default.upatch" ] &&
memsx "$W/default.o"
'
test_expect_success '--extra-cflags overrides the instruction set' '
- build v3 --extra-cflags -mcpu=v3 &&
+ build v3 isa.bpf.c --extra-cflags -mcpu=v3 &&
[ -s "$W/v3.upatch" ] &&
! memsx "$W/v3.o"
'
+test_expect_success 'a switch compiles to branches, not a table in .rodata' '
+ build switch switch.bpf.c &&
+ [ -s "$W/switch.upatch" ] &&
+ ! rodata "$W/switch.o"
+'
+
test_done
diff --git a/testing/functional/upatch/t3004-upatch-serializer-metadata.sh b/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
index fdc7611d..64e14339 100644
--- a/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
+++ b/testing/functional/upatch/t3004-upatch-serializer-metadata.sh
@@ -116,7 +116,8 @@ test_expect_success 'builder rejects serializer targeting reserved output map' '
test_expect_success 'builder rejects duplicate serializer targets' '
if ! command -v llvm-objcopy >/dev/null 2>&1; then exit 0; fi
(cd .. &&
- clang -target bpf -mcpu=v4 -O2 -g -I vendor/linux -I vendor/libbpf \
+ clang -target bpf -mcpu=v4 -fno-jump-tables -O2 -g \
+ -I vendor/linux -I vendor/libbpf \
-I bpf/lib -c testing/fixtures/arena-tree-serializer.bpf.c \
-o "$WORK/arena-tree-serializer.o" &&
llvm-objcopy --redefine-sym \
diff --git a/testing/helper/test-seal-seccomp.py b/testing/helper/test-seal-seccomp.py
index bf94c587..b86e8ee1 100644
--- a/testing/helper/test-seal-seccomp.py
+++ b/testing/helper/test-seal-seccomp.py
@@ -20,8 +20,8 @@ with tempfile.TemporaryDirectory(prefix="shirudo-seal-seccomp-") as directory:
work = Path(directory)
(work / "vmlinux.h").write_text(header)
command = shlex.split(os.environ.get("CLANG", "clang"))
- command += ["-target", "bpf", "-mcpu=v4", "-O2", "-g", "-Wall", "-Wextra",
- "-Werror",
+ command += ["-target", "bpf", "-mcpu=v4", "-fno-jump-tables", "-O2", "-g",
+ "-Wall", "-Wextra", "-Werror",
"-Wno-missing-declarations", "-Wno-unused-parameter",
"-Wdeclaration-after-statement", "-I" + str(work),
"-I" + str(root / "include"),
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index edbccbf2..af78e27c 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -111,6 +111,11 @@ int build_compile(const char *source, const char *output,
* targets, not for the kernel it runs on.
*/
push(argv, &n, "-mcpu=v4");
+ /*
+ * A switch stays in branches: clang 23 otherwise reads its results
+ * from a table in .rodata at an offset the verifier cannot bound.
+ */
+ push(argv, &n, "-fno-jump-tables");
push(argv, &n, "-O2");
push(argv, &n, "-g");
push(argv, &n, "-Wall");