eBPF-based security platform
bpf: leave link creation, raw tracepoint opens and pins to shirudo
Daniel Borkmann9c6dd12707ae
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 4a3eebae..9273e032 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -260,8 +260,9 @@ bpffs and keyring guards. The identity map (the bpffs ``s_dev``,shirudo's executable ``(dev, ino)``, the keyring kind and serial, andthe serials of the keys the keyring holds) is written and``BPF_MAP_FREEZE``'d before any link is created, so userspace cannot-change it for the rest of the boot. Writable map fds and map updates-are restricted by the same executable-identity gate.+change it for the rest of the boot. Writable map fds, map updates,+link creation, raw tracepoint opens and pins are restricted by the+same executable-identity gate.``lsm/inode_unlink``, ``lsm/inode_rename``,``lsm/inode_setxattr`` and ``lsm/inode_removexattr`` on shirudo's bpffsallow the operation only when the calling task's ``mm->exe_file`` isdiff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex 03b84dfc..205f9b94 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -121,8 +121,12 @@ T1 Loading unsigned BPFT2 Detaching or replacing enforcement``lsm/bpf`` denies ``LINK_DETACH``, ``LINK_UPDATE``,- ``PROG_ATTACH``, ``PROG_DETACH`` and ``BTF_LOAD`` from userspace.- The links are kept alive by pins in a private bpffs;+ ``PROG_ATTACH``, ``PROG_DETACH`` and ``BTF_LOAD`` from userspace,+ and leaves ``LINK_CREATE``, ``RAW_TRACEPOINT_OPEN`` and ``OBJ_PIN``+ to shirudo's own executable, so root cannot re-attach a vetted+ kprobe program at a site its signer never meant it for, nor pin+ anything into the private bpffs.+ The links are kept alive by pins in that bpffs;``inode_unlink`` and ``inode_rename`` on it allow only shirudo's ownexecutable (the VFS unlinks the victim of a rename without``inode_unlink``, so a rename over a link's pin would otherwisediff --git a/Makefile.shirudo b/Makefile.shirudoindex 3d514a71..0badf575 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -501,6 +501,7 @@ TEST_HELPERS := testing/helper/test-json-parser \testing/helper/test-kernel-release \testing/helper/test-kernel-build-id \testing/helper/test-bpf-load \+ testing/helper/test-bpf-cmd \testing/helper/test-kernel-abi \testing/helper/test-otel-map \testing/helper/test-upatch-map-types \@@ -579,6 +580,10 @@ testing/helper/test-bpf-load: testing/helper/test-bpf-load.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^+testing/helper/test-bpf-cmd: testing/helper/test-bpf-cmd.c+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -o $@ $^+# Needs the vendored uapi bpf.h (excl_prog_hash / fd_array_cnt / keyring_id# are newer than a stock system header) and libcrypto for SHA-256.testing/helper/test-signed-load: testing/helper/test-signed-load.cdiff --git a/bpf/bpf2.c b/bpf/bpf2.cindex 61974eb0..d44358a1 100644--- a/bpf/bpf2.c+++ b/bpf/bpf2.c@@ -11,7 +11,10 @@* a non-zero signature in this cheap pre-filter.* The authoritative verdict is enforced by* lsm/bpf_prog_load below, after the kernel has- * verified the signature.+ * verified the signature. Map creation, updates+ * and freezes, test runs, link creation, raw+ * tracepoint opens and pins are shirudo's own+ * executable's alone.* lsm/bpf_prog_load -- the admission gate that runs INSIDE the* verifier, after signature verification, so the* kernel-recorded verdict is final here. A@@ -143,14 +146,16 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,/** Vetted mutating cmds the signed-loader data-map + attach/pin- * pattern needs from userspace. Map updates and freezes are restricted- * below to shirudo itself; signed-loader map mutations run in kernel- * context. A prog_fd is only obtainable via the signature-gated+ * pattern needs from userspace, each left to shirudo's own+ * executable. Signed-loader map mutations run in kernel context.+ * A prog_fd is only obtainable via the signature-gated* BPF_PROG_LOAD above or GET_FD_BY_ID of an already-vetted prog.* gen_loader emits no link-create and bpf_sys_bpf() cannot- * BPF_OBJ_PIN, so shirudo's one-shot CLI must create + pin the link- * itself. Attaching an already-vetted program grants no new- * code-execution capability.+ * BPF_OBJ_PIN, so shirudo's one-shot CLI must create + pin the+ * link itself, and nobody else has a reason to: a vetted kprobe+ * program re-attached by root at a site of its choosing would run+ * signed code where its signer never meant it to, and a pin of+ * root's into the private bpffs is an entry shirudo did not make.*/case BPF_MAP_CREATE:id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);@@ -171,9 +176,18 @@ int BPF_PROG(shirudo_lsm_bpf, int cmd, union bpf_attr *attr,return -EACCES;return 0;- case BPF_OBJ_PIN:case BPF_LINK_CREATE:case BPF_RAW_TRACEPOINT_OPEN:+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);+ if (!id || !shirudo_task_is_self(id))+ return -EACCES;+ return 0;++ case BPF_OBJ_PIN:+ id = bpf_map_lookup_elem(&shirudo_bpffs_sb, &zero);+ if (!id || !shirudo_task_is_self(id))+ return -EACCES;+ return 0;/** Read-only / observability cmds, so `bpftool prog show`,diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex 81b2794a..8d1fa2e3 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -311,6 +311,7 @@ testing/helper/test-json-parsertesting/helper/test-chaintesting/helper/test-configtesting/helper/test-bpf-load+testing/helper/test-bpf-cmdtesting/helper/test-kernel-abitesting/helper/test-otel-maptesting/helper/test-upatch-map-typesdiff --git a/testing/helper/test-bpf-cmd.c b/testing/helper/test-bpf-cmd.cnew file mode 100644index 00000000..a1534a6b--- /dev/null+++ b/testing/helper/test-bpf-cmd.c@@ -0,0 +1,75 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Issues one bpf() command by name with an all-zero attr through the raw+ * syscall and prints the outcome ("ok" or "errno=<n>"). shirudo's lsm/bpf+ * hook answers before the kernel looks at the attr, so a command the+ * policy denies reports EACCES whatever the attr says, and one it allows+ * reports whatever the kernel makes of an empty attr. No libbpf.+ */++#include <errno.h>+#include <stdio.h>+#include <string.h>+#include <unistd.h>+#include <sys/syscall.h>++#include <linux/bpf.h>++#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))++static const struct {+ const char *name;+ int cmd;+} cmds[] = {+ { "MAP_CREATE", BPF_MAP_CREATE },+ { "MAP_LOOKUP_ELEM", BPF_MAP_LOOKUP_ELEM },+ { "MAP_UPDATE_ELEM", BPF_MAP_UPDATE_ELEM },+ { "MAP_DELETE_ELEM", BPF_MAP_DELETE_ELEM },+ { "MAP_GET_NEXT_KEY", BPF_MAP_GET_NEXT_KEY },+ { "OBJ_PIN", BPF_OBJ_PIN },+ { "OBJ_GET", BPF_OBJ_GET },+ { "PROG_ATTACH", BPF_PROG_ATTACH },+ { "PROG_DETACH", BPF_PROG_DETACH },+ { "PROG_TEST_RUN", BPF_PROG_TEST_RUN },+ { "MAP_GET_NEXT_ID", BPF_MAP_GET_NEXT_ID },+ { "RAW_TRACEPOINT_OPEN", BPF_RAW_TRACEPOINT_OPEN },+ { "BTF_LOAD", BPF_BTF_LOAD },+ { "MAP_LOOKUP_AND_DELETE_ELEM", BPF_MAP_LOOKUP_AND_DELETE_ELEM },+ { "MAP_FREEZE", BPF_MAP_FREEZE },+ { "MAP_UPDATE_BATCH", BPF_MAP_UPDATE_BATCH },+ { "MAP_DELETE_BATCH", BPF_MAP_DELETE_BATCH },+ { "LINK_CREATE", BPF_LINK_CREATE },+ { "LINK_UPDATE", BPF_LINK_UPDATE },+ { "ITER_CREATE", BPF_ITER_CREATE },+ { "LINK_DETACH", BPF_LINK_DETACH },+ { "PROG_BIND_MAP", BPF_PROG_BIND_MAP },+};++int main(int argc, char **argv)+{+ union bpf_attr attr = {};+ size_t i;+ int cmd;++ if (argc != 2) {+ fprintf(stderr, "usage: %s <BPF_CMD name without prefix>\n",+ argv[0]);+ return 2;+ }+ for (i = 0; i < ARRAY_SIZE(cmds); i++)+ if (strcmp(cmds[i].name, argv[1]) == 0)+ break;+ if (i == ARRAY_SIZE(cmds)) {+ fprintf(stderr, "%s: unknown command %s\n", argv[0], argv[1]);+ return 2;+ }+ cmd = cmds[i].cmd;+ if (syscall(__NR_bpf, cmd, &attr, sizeof(attr)) >= 0) {+ printf("ok\n");+ return 0;+ }+ printf("errno=%d\n", errno);+ return 0;+}diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex ee9a0831..ec91ec7a 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -42,6 +42,7 @@ for t in \tamperproof/t1112-prog-load-keyring-gate.sh \tamperproof/t1113-mount-lockdown.sh \tamperproof/t1135-pin-rename.sh \+ tamperproof/t1137-bpf-cmd-deny.sh \tamperproof/t1130-map-lockdown.sh \tamperproof/t1131-pr-set-mm-identity.sh \tamperproof/t1125-key-permission.sh \diff --git a/testing/tamperproof/t1137-bpf-cmd-deny.sh b/testing/tamperproof/t1137-bpf-cmd-deny.shnew file mode 100755index 00000000..7104bc30--- /dev/null+++ b/testing/tamperproof/t1137-bpf-cmd-deny.sh@@ -0,0 +1,54 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1137 -- the bpf() commands lsm/bpf leaves to shirudo's own executable+# or denies outright. A root process that is not shirudo gets EACCES for+# link creation, raw tracepoint opens and pins (a vetted program must+# not be re-attached where its signer never meant it to run, and the+# private bpffs takes no entries shirudo did not make), for the+# tear-down and mutation commands the policy default-denies, and for an+# unclassified command; a read-only command is answered by the kernel.+# The hook decides before the kernel reads the attr, so every attr here+# is empty. Non-destructive: nothing is created.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++H=helper/test-bpf-cmd+[ -x "$H" ] || test_skip_all "test-bpf-cmd helper not built"++denied() {+ for c in "$@"; do+ [ "$(./$H "$c")" = "errno=13" ] || {+ echo "$c was not denied: $(./$H "$c")"+ return 1+ }+ done+}++test_expect_success 'link creation, raw tracepoint open and pin are shirudo-only' '+ denied LINK_CREATE RAW_TRACEPOINT_OPEN OBJ_PIN+'++test_expect_success 'map creation, update, freeze and test run are shirudo-only' '+ denied MAP_CREATE MAP_UPDATE_ELEM MAP_FREEZE PROG_TEST_RUN+'++test_expect_success 'detach, update and delete commands are denied' '+ denied LINK_DETACH LINK_UPDATE PROG_ATTACH PROG_DETACH \+ MAP_DELETE_ELEM MAP_LOOKUP_AND_DELETE_ELEM \+ MAP_UPDATE_BATCH MAP_DELETE_BATCH BTF_LOAD+'++test_expect_success 'commands the policy never classified are denied' '+ denied ITER_CREATE PROG_BIND_MAP+'++test_expect_success 'a read-only command still reaches the kernel' '+ out=$(./$H MAP_GET_NEXT_ID) &&+ [ "$out" != "errno=13" ]+'++test_donediff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.mdindex ffc5e8c5..36f6e045 100644--- a/testing/tamperproof/test.plan.md+++ b/testing/tamperproof/test.plan.md@@ -34,10 +34,10 @@ checks that Shirudo can still export the pre-existing Python exec record.| TP-12 | Update serializer map or freeze serializer/arena map directly | Covered | `t1130-map-lockdown.sh` | Current test attempts update on serializer array and freeze on both. || TP-13 | Writable mmap of arena | Covered | `t1130-map-lockdown.sh` | Preserve denial check. || TP-14 | Invoke pinned serializer with destructive `BPF_PROG_TEST_RUN` context | Covered | `t1130-map-lockdown.sh` | Root receives `EACCES`; Shirudo export still succeeds and contains the Python exec record created before the attempt. |-| TP-15 | Delete map element; update/delete batch; lookup-and-delete variants | Not covered | None | Add raw-syscall denial cases for each applicable command. Policy default-denies them, but that behavior is not asserted. |+| TP-15 | Delete map element; update/delete batch; lookup-and-delete variants | Covered | `t1137-bpf-cmd-deny.sh` | Raw-syscall denial of each command with an empty attr. || TP-16 | Create a BPF map from a non-Shirudo executable | Not covered | None in tamperproof | Add direct negative test for `BPF_MAP_CREATE`; cover authorized Shirudo loader creation as positive control. |-| TP-17 | Create links or pin existing objects from root | Not covered | None | `BPF_LINK_CREATE` and `BPF_OBJ_PIN` are allowed. Test whether root can create an unwanted attachment or pin and whether the resulting object affects enforcement/state. |-| TP-18 | Detach/update links or attach/detach programs from root | Not covered | None | Add negative raw-syscall cases for `BPF_LINK_DETACH`, `BPF_LINK_UPDATE`, `BPF_PROG_ATTACH`, and `BPF_PROG_DETACH`. |+| TP-17 | Create links, open raw tracepoints or pin objects from root | Covered | `t1137-bpf-cmd-deny.sh` | `BPF_LINK_CREATE`, `BPF_RAW_TRACEPOINT_OPEN` and `BPF_OBJ_PIN` are shirudo-only; root gets `EACCES`. |+| TP-18 | Detach/update links or attach/detach programs from root | Covered | `t1137-bpf-cmd-deny.sh` | Raw-syscall denial of `BPF_LINK_DETACH`, `BPF_LINK_UPDATE`, `BPF_PROG_ATTACH` and `BPF_PROG_DETACH`, plus two never-classified commands. || TP-19 | Root assigns Shirudo's executable identity using `PR_SET_MM` | Covered | `t1131-pr-set-mm-identity.sh` and `t1131-pr-set-mm-identity.py` | Requires `CAP_SYS_RESOURCE`; verify both `PR_SET_MM_EXE_FILE` and `PR_SET_MM_MAP` are denied to non-Shirudo callers. || TP-20 | Obtain or transfer Shirudo's open map FDs via `/proc/<pid>/fd`, `pidfd_getfd`, or `SCM_RIGHTS` | Not covered | None | Test each applicable acquisition/transfer route and verify a received FD cannot be used to mutate protected maps. || TP-21 | Concurrent or repeated serializer invocation changes/loses records | Not covered | None | Add after TP-14 policy fix; exercise repeated and concurrent export attempts and verify record integrity. |