eBPF-based security platform
upatch: check the link the kernel made against the attach metadata
Daniel Borkmanna033a27afddc
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex 8fae5888..4c61fe51 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -173,7 +173,11 @@ Load. ``shirudo upatch load fix.upatch`` on the target:loads the payload program, which the kernel checks against theloader's ``excl_prog_hash``;5. attaches the payload (``BPF_LINK_CREATE`` or- ``BPF_RAW_TRACEPOINT_OPEN``), pins the link at+ ``BPF_RAW_TRACEPOINT_OPEN``), checks that the link the kernel+ reports is at the site the metadata names (the root cgroup of the+ initial cgroup namespace, the initial network namespace, the kprobe+ symbol and offset, the uprobe offsets) and refuses+ ``attach_scope_mismatch`` otherwise, pins the link at``/run/shirudo/bpffs/upatches/<name>/link`` and every map under``maps/`` beside it, and records the payload digest as the``security.bpf.shirudo.payload`` xattr on the pin directory;@@ -662,6 +666,9 @@ kernel release, and the NV index and chain value after the operation.The categories are ``ok``, ``tampered``, ``keyring_missing``,``lsm_denied``, ``signature_rejected``, ``signature_unsupported``,``verifier_rejected``, ``parser_rejected``, ``btf_mismatch``,+``attach_target_missing``, ``binary_mismatch``,+``attach_scope_mismatch`` (the link the kernel made is not at the site+the metadata names: another cgroup, network namespace or PMU),``payload_mismatch``, ``already_unloaded``, ``detach_failed``,``tpm_error``, ``eventlog_full``, ``kernel_abi_unsupported``,``init_prereqs_missing``, ``version_mismatch`` and ``unknown``.diff --git a/Documentation/threat-model.rst b/Documentation/threat-model.rstindex ac37e52e..c1d35ab7 100644--- a/Documentation/threat-model.rst+++ b/Documentation/threat-model.rst@@ -124,6 +124,14 @@ T2 Detaching or replacing enforcementand leaves ``LINK_CREATE``, ``RAW_TRACEPOINT_OPEN`` and ``OBJ_PIN``to a task authorized by successful runtime exec. Root cannot re-attach a vetted programat a site its signer never meant it for, nor pin objects of its own.+ The site a load attaches at is partly the caller's to arrange, and+ root runs ``shirudo upatch load``: a cgroup_skb program under a+ cgroup namespace root or a bind mount of a subtree, a netfilter+ program in a network namespace of the caller's own and a kprobe+ opened through a sysfs that names another PMU would each be+ measured as loaded while guarding nothing or the wrong thing, so+ the load compares the link the kernel reports with the attach+ metadata before pinning it and refuses ``attach_scope_mismatch``.The links are kept alive by pins in the private bpffs;``inode_unlink`` and ``inode_rename`` there require that authority.The VFS unlinks a rename's victim without ``inode_unlink``, so bothdiff --git a/builtin-upatch.c b/builtin-upatch.cindex a87e7d64..e79da864 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -17,6 +17,7 @@#include <stdlib.h>#include <string.h>#include <sys/file.h>+#include <sys/socket.h>#include <sys/stat.h>#include <sys/syscall.h>#include <sys/xattr.h>@@ -1131,6 +1132,101 @@ static int prog_attach_for_spec(int prog_fd,}}+/* The initial network namespace's cookie, NET_NS_INIT_ID in linux/nsfs.h. */+#define SHIRUDO_NET_NS_INIT_COOKIE 7ULL+/* The root of the unified cgroup hierarchy. */+#define SHIRUDO_ROOT_CGROUP_ID 1ULL+#ifndef SO_NETNS_COOKIE+#define SO_NETNS_COOKIE 71+#endif++/* A netfilter link hooks the caller's network namespace. */+static int netns_initial(void)+{+ socklen_t len = sizeof(__u64);+ __u64 cookie = 0;+ int fd, rc;++ fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);+ if (fd < 0)+ return -errno;+ rc = getsockopt(fd, SOL_SOCKET, SO_NETNS_COOKIE, &cookie, &len) ?+ -errno : 0;+ close(fd);+ if (rc)+ return rc;+ return cookie == SHIRUDO_NET_NS_INIT_COOKIE ? 0 : -EXDEV;+}++/*+ * The site a kprobe, uprobe, cgroup or netfilter program attaches at is+ * named by the caller, and the caller's cgroup namespace and mount of+ * the cgroup tree, network namespace and sysfs are root's to arrange:+ * a cgroup_skb program attached under a namespace root or a bind mount+ * of a subtree guards a corner, a netfilter program in a namespace of+ * its own guards nothing, a kprobe opened through a sysfs that names+ * another PMU lands elsewhere, and each is measured as loaded. What the+ * kernel reports about the link is compared with the spec before the+ * link is pinned; -EXDEV when they differ.+ */+static int link_matches_spec(int link_fd,+ const struct shirudo_prog_spec *spec)+{+ char func[SHIRUDO_UPATCH_ATTACH_TARGET_MAX] = {};+ struct bpf_link_info info = {};+ __u32 len = sizeof(info);+ bool ret;++ if (spec->kind == SHIRUDO_UPATCH_ATTACH_KPROBE ||+ spec->kind == SHIRUDO_UPATCH_ATTACH_KRETPROBE) {+ info.perf_event.kprobe.func_name = ptr_to_u64(func);+ info.perf_event.kprobe.name_len = sizeof(func);+ }+ if (bpf_link_get_info_by_fd(link_fd, &info, &len))+ return -errno;+ if (info.type == BPF_LINK_TYPE_CGROUP)+ return info.cgroup.cgroup_id == SHIRUDO_ROOT_CGROUP_ID ?+ 0 : -EXDEV;+ switch (spec->kind) {+ case SHIRUDO_UPATCH_ATTACH_CGROUP_SKB_INGRESS:+ case SHIRUDO_UPATCH_ATTACH_CGROUP_SKB_EGRESS:+ return -EXDEV;+ case SHIRUDO_UPATCH_ATTACH_NETFILTER_IPV6_PRE_ROUTING:+ if (info.type != BPF_LINK_TYPE_NETFILTER ||+ info.netfilter.pf != NFPROTO_IPV6 ||+ info.netfilter.hooknum != NF_INET_PRE_ROUTING ||+ info.netfilter.priority != NF_IP6_PRI_RAW_BEFORE_DEFRAG)+ return -EXDEV;+ return netns_initial();+ case SHIRUDO_UPATCH_ATTACH_KPROBE:+ case SHIRUDO_UPATCH_ATTACH_KRETPROBE:+ ret = spec->kind == SHIRUDO_UPATCH_ATTACH_KRETPROBE;+ if (info.type != BPF_LINK_TYPE_PERF_EVENT ||+ info.perf_event.type != (ret ? BPF_PERF_EVENT_KRETPROBE :+ BPF_PERF_EVENT_KPROBE) ||+ info.perf_event.kprobe.name_len > sizeof(func) ||+ info.perf_event.kprobe.offset != spec->offset ||+ strcmp(func, spec->target))+ return -EXDEV;+ return 0;+ case SHIRUDO_UPATCH_ATTACH_UPROBE_MULTI:+ if (info.type != BPF_LINK_TYPE_UPROBE_MULTI ||+ info.uprobe_multi.count != spec->nr_uprobe_offsets)+ return -EXDEV;+ return 0;+ case SHIRUDO_UPATCH_ATTACH_FMOD_RET:+ if (info.type != BPF_LINK_TYPE_TRACING ||+ info.tracing.attach_type != BPF_MODIFY_RETURN)+ return -EXDEV;+ return 0;+ default:+ if (info.type != BPF_LINK_TYPE_RAW_TRACEPOINT &&+ info.type != BPF_LINK_TYPE_TRACING)+ return -EXDEV;+ return 0;+ }+}+static int obj_pin(int fd, const char *path){union bpf_attr attr = {};@@ -1305,6 +1401,16 @@ static int attach_and_pin_progs(const struct shirudo_loader_ctx *cx,rc = link_fd;goto out;}+ /* Unpinned, a closed link detaches. */+ rc = link_matches_spec(link_fd, spec);+ if (rc) {+ pr_err("attach prog %u (%s %s): the link is not at "+ "the site the metadata names: %s", i,+ attach_kind_name(spec->kind), spec->target,+ strerror(-rc));+ close(link_fd);+ goto out;+ }rc = obj_pin(link_fd, pin);close(link_fd);if (rc) {@@ -1818,6 +1924,8 @@ static int upatch_apply(const char *path, bool verify_only,cat = "attach_target_missing";else if (e == ESTALE)cat = "binary_mismatch";+ else if (e == EXDEV)+ cat = "attach_scope_mismatch";else if (e == EOPNOTSUPP)cat = "kernel_abi_unsupported";else if (e == EACCES || e == EPERM)diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex 6c4a238b..b77b8309 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -596,6 +596,9 @@ build_fixture otel-backend.bpf.c shirudo-test-otel-backend "$FIXTURE_WORK/trustebuild_fixture serialized-metric.bpf.c shirudo-test-serialized-metric "$FIXTURE_WORK/trusted"build_fixture arena-tree-serializer.bpf.c shirudo-test-arena-tree "$FIXTURE_WORK/trusted"build_fixture uprobe-multi.bpf.c shirudo-test-uprobe-multi "$FIXTURE_WORK/trusted"+build_fixture scope-netfilter.bpf.c shirudo-test-scope-netfilter "$FIXTURE_WORK/trusted"+build_fixture scope-cgroup.bpf.c shirudo-test-scope-cgroup "$FIXTURE_WORK/trusted"+build_fixture scope-kprobe.bpf.c shirudo-test-scope-kprobe "$FIXTURE_WORK/trusted"build_fixture basic.bpf.c shirudo-test-foreign "$FIXTURE_WORK/foreign"build_fixture basic.bpf.c shirudo-test-own-keyring "$FIXTURE_WORK/own"build_obs_fixture "$FIXTURE_WORK/trusted"@@ -636,6 +639,16 @@ observability_payload="$($REPO_ROOT/shirudo upatch info \observability_anomaly_payload="$($REPO_ROOT/shirudo upatch info \"$TMP_DIR/fixtures/kernel-obs-cred-anomaly.upatch" --json |python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+for scope in netfilter cgroup kprobe; do+ scope_payload="$($REPO_ROOT/shirudo upatch info \+ "$TMP_DIR/fixtures/shirudo-test-scope-$scope.upatch" --json |+ python3 -c 'import json,sys; print(json.load(sys.stdin)["payload_sha256"])')"+ "$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \+ "shirudo-test-scope-$scope" --payload-sha256 "$scope_payload" \+ -o "$TMP_DIR/fixtures/shirudo-test-scope-$scope.unload.upatch" \+ --key "$FIXTURE_WORK/trusted/signing_key.pem" \+ --cert "$FIXTURE_WORK/trusted/cert.der"+done"$REPO_ROOT/tools/shirudo-build/shirudo-build" --unload \shirudo-test-basic --payload-sha256 "$basic_payload" \-o "$TMP_DIR/fixtures/shirudo-test-basic.unload.upatch" \diff --git a/testing/fixtures/scope-cgroup.bpf.c b/testing/fixtures/scope-cgroup.bpf.cnew file mode 100644index 00000000..cf2a1280--- /dev/null+++ b/testing/fixtures/scope-cgroup.bpf.c@@ -0,0 +1,11 @@+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause+#include "vmlinux.h"+#include <bpf/bpf_helpers.h>++char _license[] SEC("license") = "Dual BSD/GPL";++SEC("cgroup_skb/ingress")+int shirudo_test_scope_cgroup(struct __sk_buff *skb)+{+ return 1;+}diff --git a/testing/fixtures/scope-kprobe.bpf.c b/testing/fixtures/scope-kprobe.bpf.cnew file mode 100644index 00000000..2506d6ac--- /dev/null+++ b/testing/fixtures/scope-kprobe.bpf.c@@ -0,0 +1,12 @@+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause+#include "vmlinux.h"+#include <bpf/bpf_helpers.h>+#include <bpf/bpf_tracing.h>++char _license[] SEC("license") = "Dual BSD/GPL";++SEC("kprobe/do_nanosleep")+int BPF_KPROBE(shirudo_test_scope_kprobe)+{+ return 0;+}diff --git a/testing/fixtures/scope-netfilter.bpf.c b/testing/fixtures/scope-netfilter.bpf.cnew file mode 100644index 00000000..b871d940--- /dev/null+++ b/testing/fixtures/scope-netfilter.bpf.c@@ -0,0 +1,13 @@+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause+#include "vmlinux.h"+#include <bpf/bpf_helpers.h>++#define NF_ACCEPT 1++char _license[] SEC("license") = "Dual BSD/GPL";++SEC("netfilter")+int shirudo_test_scope_netfilter(struct bpf_nf_ctx *ctx)+{+ return NF_ACCEPT;+}diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex b0e405bc..97b103ca 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -68,6 +68,7 @@ for t in \tamperproof/t1132-seal-scratch.sh \tamperproof/t1125-key-permission.sh \tamperproof/t1140-verity-authority.sh \+ tamperproof/t1143-attach-scope.sh \tamperproof/t1150-verity-devices.sh \tamperproof/t1151-verity-process.sh \tamperproof/t1153-verity-hidden.sh \diff --git a/testing/tamperproof/t1143-attach-scope.sh b/testing/tamperproof/t1143-attach-scope.shnew file mode 100755index 00000000..6dbd7b68--- /dev/null+++ b/testing/tamperproof/t1143-attach-scope.sh@@ -0,0 +1,99 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1143 -- the site a load attaches at is the caller's to arrange: a+# netfilter program hooks the caller's network namespace, a cgroup_skb+# program the root of the caller's cgroup namespace, and a kprobe is+# opened through the PMU number sysfs names. Root running the load in a+# namespace of its own, or with a uprobe PMU bound over the kprobe one,+# got a shield that guards nothing or the wrong thing, measured as loaded.+# The load must compare the link the kernel made with the metadata and+# refuse with attach_scope_mismatch; the same loads succeed in place.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+NF="$FIXTURES/shirudo-test-scope-netfilter.upatch"+CG="$FIXTURES/shirudo-test-scope-cgroup.upatch"+KP="$FIXTURES/shirudo-test-scope-kprobe.upatch"+B=/run/shirudo/bpffs/upatches+KPROBE_TYPE=/sys/bus/event_source/devices/kprobe/type+WORK=/tmp/t1143+for f in "$NF" "$CG" "$KP"; do+ [ -s "$f" ] && [ -s "${f%.upatch}.unload.upatch" ] ||+ test_skip_all "missing scope fixture $f"+done+command -v unshare >/dev/null 2>&1 || test_skip_all "no unshare"+[ -r /sys/bus/event_source/devices/uprobe/type ] ||+ test_skip_all "no uprobe PMU"+# The kprobe cases need the kprobe PMU; the test kernel has none while+# unloading beside it oopses this bpf-next (shared LSM trampolines).+HAVE_KPROBE=0+[ -r "$KPROBE_TYPE" ] && HAVE_KPROBE=1++# Under the library's errexit a failing step would end the cleanup.+restore() {+ ! mountpoint -q "$KPROBE_TYPE" || umount "$KPROBE_TYPE" || true+ for f in "$NF" "$CG" "$KP"; do+ "$SHIRUDO_BIN" upatch unload "${f%.upatch}.unload.upatch" \+ --no-precheck >/dev/null 2>&1 || true+ done+ rmdir /sys/fs/cgroup/t1143 2>/dev/null || true+ rm -rf "$WORK"+}+trap restore EXIT++category() {+ "$@" --no-precheck --report=- 2>/dev/null | report_field category+}++test_expect_success 'a netfilter load in another network namespace is refused' '+ [ "$(unshare -n "$SHIRUDO_BIN" upatch load "$NF" --no-precheck \+ --report=- 2>/dev/null | report_field category)" = attach_scope_mismatch ] &&+ [ ! -e "$B/shirudo-test-scope-netfilter" ]+'++# The child joins a cgroup of its own, so that the namespace root it then+# unshares is not the hierarchy's root; in a mount namespace of its own it+# replaces the cgroup tree with one mounted afresh, rooted at the+# namespace root, for the load to find.+test_expect_success 'a cgroup_skb load under a cgroup namespace root is refused' '+ mkdir -p "$WORK" /sys/fs/cgroup/t1143 &&+ cat >"$WORK/inner.sh" <<-EOF &&+ umount /sys/fs/cgroup && mount -t cgroup2 cgroup2 /sys/fs/cgroup || exit 1+ exec "$SHIRUDO_BIN" upatch load "$CG" --no-precheck --report=-+ EOF+ cat >"$WORK/cgns.sh" <<-EOF &&+ echo \$\$ >/sys/fs/cgroup/t1143/cgroup.procs+ exec unshare -Cm sh "$WORK/inner.sh"+ EOF+ [ "$(sh "$WORK/cgns.sh" 2>/dev/null | report_field category)" = attach_scope_mismatch ] &&+ [ ! -e "$B/shirudo-test-scope-cgroup" ]+'++if [ "$HAVE_KPROBE" = 1 ]; then+test_expect_success 'a kprobe load through a sysfs that names the uprobe PMU is refused' '+ mkdir -p "$WORK" && cp /bin/true "$WORK/do_nanosleep" &&+ cat /sys/bus/event_source/devices/uprobe/type >"$WORK/type" &&+ mount --bind "$WORK/type" "$KPROBE_TYPE" &&+ cat=$(cd "$WORK" && category "$SHIRUDO_BIN" upatch load "$KP") &&+ umount "$KPROBE_TYPE" &&+ [ "$cat" = attach_scope_mismatch ] &&+ [ ! -e "$B/shirudo-test-scope-kprobe" ]+'+fi++test_expect_success 'the same loads succeed in the initial namespaces' '+ [ "$(category "$SHIRUDO_BIN" upatch load "$NF")" = ok ] &&+ [ "$(category "$SHIRUDO_BIN" upatch load "$CG")" = ok ] &&+ [ -e "$B/shirudo-test-scope-netfilter/link" ] &&+ [ -e "$B/shirudo-test-scope-cgroup/link" ] &&+ { [ "$HAVE_KPROBE" = 0 ] ||+ { [ "$(category "$SHIRUDO_BIN" upatch load "$KP")" = ok ] &&+ [ -e "$B/shirudo-test-scope-kprobe/link" ]; }; }+'++test_done