eBPF-based security platform
testing: verify the perf event attach of a loaded program is refused
Daniel Borkmannd194b7029a08
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 3f24e703..dc879c67 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -616,6 +616,7 @@ TEST_HELPERS := $(VERITY_TEST_HELPERS) \testing/helper/test-kernel-build-id \testing/helper/test-bpf-load \testing/helper/test-bpf-cmd \+ testing/helper/test-perf-attach \testing/helper/test-kernel-abi \testing/helper/test-otel-map \testing/helper/test-upatch-map-types \@@ -759,6 +760,10 @@ testing/helper/test-bpf-cmd: testing/helper/test-bpf-cmd.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^+testing/helper/test-perf-attach: testing/helper/test-perf-attach.c+ @printf ' CC $@\n'+ $(Q)$(CC) $(CFLAGS) -I$(BPF_UAPI_INCLUDE) -o $@ $^+testing/helper/test-upatch-link-lockdown: testing/helper/test-upatch-link-lockdown.c@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -I$(BPF_UAPI_INCLUDE) -o $@ $^diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex 2a6b0b3e..0aa08482 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -338,6 +338,7 @@ testing/helper/test-tpm-preopenedtesting/helper/test-configtesting/helper/test-bpf-loadtesting/helper/test-bpf-cmd+testing/helper/test-perf-attachtesting/helper/test-kernel-abitesting/helper/test-otel-maptesting/helper/test-upatch-map-typesdiff --git a/testing/helper/test-perf-attach.c b/testing/helper/test-perf-attach.cnew file mode 100644index 00000000..07d582dc--- /dev/null+++ b/testing/helper/test-perf-attach.c@@ -0,0 +1,151 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * Attach a loaded kprobe-type program to a perf event of the caller's+ * own through PERF_EVENT_IOC_SET_BPF, the legacy attach path that+ * never passes through bpf(). The event is a uprobe on this executable,+ * which needs no kprobe support; whether it fires is beside the point.+ * Prints "attached" or "errno=<n>". No libbpf.+ */++#include <elf.h>+#include <errno.h>+#include <fcntl.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/ioctl.h>+#include <sys/syscall.h>+#include <unistd.h>++#include <linux/bpf.h>+#include <linux/perf_event.h>++static long bpf(int cmd, union bpf_attr *attr)+{+ return syscall(__NR_bpf, cmd, attr, sizeof(*attr));+}++static int uprobe_type(void)+{+ FILE *f = fopen("/sys/bus/event_source/devices/uprobe/type", "r");+ int type = -1;++ if (!f)+ return -1;+ if (fscanf(f, "%d", &type) != 1)+ type = -1;+ fclose(f);+ return type;+}++/* The file offset of the first executable segment, where a probe may sit. */+static long exec_offset(const char *path)+{+ Elf64_Ehdr eh;+ Elf64_Phdr ph;+ long off = -1;+ int fd, i;++ fd = open(path, O_RDONLY | O_CLOEXEC);+ if (fd < 0)+ return -1;+ if (read(fd, &eh, sizeof(eh)) != sizeof(eh))+ goto out;+ for (i = 0; i < eh.e_phnum; i++) {+ if (pread(fd, &ph, sizeof(ph),+ eh.e_phoff + (off_t)i * eh.e_phentsize) != sizeof(ph))+ break;+ if (ph.p_type == PT_LOAD && (ph.p_flags & PF_X)) {+ off = (long)ph.p_offset + 64;+ break;+ }+ }+out:+ close(fd);+ return off;+}++static int find_prog(const char *want, __u32 *id_out)+{+ union bpf_attr attr;+ __u32 id = 0;++ for (;;) {+ struct bpf_prog_info info;+ int fd;++ memset(&attr, 0, sizeof(attr));+ attr.start_id = id;+ if (bpf(BPF_PROG_GET_NEXT_ID, &attr))+ return -1;+ id = attr.next_id;+ memset(&attr, 0, sizeof(attr));+ attr.prog_id = id;+ fd = (int)bpf(BPF_PROG_GET_FD_BY_ID, &attr);+ if (fd < 0)+ continue;+ memset(&info, 0, sizeof(info));+ memset(&attr, 0, sizeof(attr));+ attr.info.bpf_fd = (__u32)fd;+ attr.info.info_len = sizeof(info);+ attr.info.info = (__u64)(size_t)&info;+ if (!bpf(BPF_OBJ_GET_INFO_BY_FD, &attr) &&+ info.type == BPF_PROG_TYPE_KPROBE &&+ (!want || !strncmp(info.name, want, sizeof(info.name)))) {+ *id_out = id;+ return fd;+ }+ close(fd);+ }+}++int main(int argc, char **argv)+{+ struct perf_event_attr pe;+ char self[4096];+ __u32 id = 0;+ ssize_t n;+ long off;+ int type, prog_fd, perf_fd;++ type = uprobe_type();+ if (type < 0) {+ fprintf(stderr, "no uprobe PMU\n");+ return 2;+ }+ n = readlink("/proc/self/exe", self, sizeof(self) - 1);+ if (n < 0) {+ fprintf(stderr, "readlink: %s\n", strerror(errno));+ return 2;+ }+ self[n] = '\0';+ off = exec_offset(self);+ if (off < 0) {+ fprintf(stderr, "no executable segment in %s\n", self);+ return 2;+ }+ prog_fd = find_prog(argc > 1 ? argv[1] : NULL, &id);+ if (prog_fd < 0) {+ fprintf(stderr, "no loaded kprobe-type program\n");+ return 2;+ }+ memset(&pe, 0, sizeof(pe));+ pe.size = sizeof(pe);+ pe.type = (__u32)type;+ pe.config1 = (__u64)(size_t)self;+ pe.config2 = (__u64)off;+ perf_fd = (int)syscall(__NR_perf_event_open, &pe, -1, 0, -1,+ PERF_FLAG_FD_CLOEXEC);+ if (perf_fd < 0) {+ fprintf(stderr, "perf_event_open: %s\n", strerror(errno));+ return 2;+ }+ if (ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd) == 0) {+ printf("attached prog_id=%u\n", id);+ return 0;+ }+ printf("errno=%d\n", errno);+ return 0;+}diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex e6539d93..4d0e56ab 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -74,6 +74,7 @@ for t in \tamperproof/t1143-attach-scope.sh \tamperproof/t1144-signed-metadata.sh \tamperproof/t1145-foreign-entries.sh \+ tamperproof/t1159-perf-attach.sh \tamperproof/t1150-verity-devices.sh \tamperproof/t1151-verity-process.sh \tamperproof/t1153-verity-hidden.sh \diff --git a/testing/tamperproof/t1159-perf-attach.sh b/testing/tamperproof/t1159-perf-attach.shnew file mode 100755index 00000000..f70c0e13--- /dev/null+++ b/testing/tamperproof/t1159-perf-attach.sh@@ -0,0 +1,52 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1159 -- PERF_EVENT_IOC_SET_BPF is the one attach that never passes+# through bpf(): a kprobe-type program a signed loader created is+# root's to reach by id, and the ioctl would bind it to a perf event of+# root's own, a probe where its signer never meant it to run. The+# file_ioctl hook keeps the ioctl to shirudo, counted as PerfAttach; the+# upatch's own link is untouched. Needs the uprobe PMU, not kprobes.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"++H=helper/test-perf-attach+[ -x "$H" ] || test_skip_all "test-perf-attach helper not built"+[ -r /sys/bus/event_source/devices/uprobe/type ] ||+ test_skip_all "no uprobe PMU"+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+NAME=shirudo-test-uprobe-multi+LOAD="$FIXTURES/$NAME.upatch"+UNLOAD="$FIXTURES/$NAME.unload.upatch"+TARGET="$FIXTURES/shirudo-test-uprobe-target"+RUNTIME=/tmp/shirudo-test-uprobe-target+PIN="/run/shirudo/bpffs/upatches/$NAME/link"+for f in "$LOAD" "$UNLOAD" "$TARGET"; do+ [ -s "$f" ] || test_skip_all "missing uprobe fixture $f"+done+trap 'if [ -e "$PIN" ]; then "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true; fi' EXIT++# The counter $1 from status --json.+counter() {+ "$SHIRUDO_BIN" status --json | python3 -c '+import json, sys+print(json.load(sys.stdin)["tamper_attempts"]["counters"][sys.argv[1]])+' "$1"+}++cp "$TARGET" "$RUNTIME"+"$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 ||+ test_skip_all "kernel does not support this uprobe.multi load"++test_expect_success 'root cannot bind the loaded program to a perf event of its own' '+ before=$(counter PerfAttach) &&+ [ "$(./$H probe)" = "errno=13" ] &&+ [ "$(counter PerfAttach)" -gt "$before" ] &&+ [ -e "$PIN" ]+'++test_done