Skip to content

eBPF-based security platform

upatch: test target artifact and link integrity

John Fastabendffc2e84d111a
diff --git a/Makefile.shirudo b/Makefile.shirudo
index 18c79340..184539fd 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -544,6 +544,7 @@ TEST_HELPERS := testing/helper/test-json-parser \
testing/helper/test-upatch-decode \
testing/helper/test-upatch-serializer-pages \
testing/helper/test-signed-load \
+ testing/helper/test-upatch-link-lockdown \
testing/helper/test-exec-churn \
testing/upatch/kernel-obs/exec/exec-paths
@@ -620,6 +621,10 @@ testing/helper/test-bpf-cmd: testing/helper/test-bpf-cmd.c
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -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 $@ $^
+
# 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.c
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.sh
index c5f33fda..dcbfedb2 100755
--- a/contrib/build-test-vm-bundle.sh
+++ b/contrib/build-test-vm-bundle.sh
@@ -319,6 +319,7 @@ testing/helper/test-upatch-prog-metadata
testing/helper/test-upatch-decode
testing/helper/test-upatch-serializer-pages
testing/helper/test-signed-load
+testing/helper/test-upatch-link-lockdown
testing/helper/test-exec-churn
testing/upatch/kernel-obs/exec/exec-paths
testing/helper/otel-mock.py
diff --git a/testing/helper/test-upatch-link-lockdown.c b/testing/helper/test-upatch-link-lockdown.c
new file mode 100644
index 00000000..278cfd8a
--- /dev/null
+++ b/testing/helper/test-upatch-link-lockdown.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * Check that an untrusted caller cannot detach or update a pinned upatch link.
+ */
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+
+#include <linux/bpf.h>
+
+static long sys_bpf(int cmd, union bpf_attr *attr)
+{
+ return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
+}
+
+int main(int argc, char **argv)
+{
+ union bpf_attr attr = {};
+ int link_fd;
+ int detach_errno, update_errno;
+
+ if (argc != 2) {
+ fprintf(stderr, "usage: %s <pinned-link>\\n", argv[0]);
+ return 2;
+ }
+ attr.pathname = (__u64)(uintptr_t)argv[1];
+ link_fd = (int)sys_bpf(BPF_OBJ_GET, &attr);
+ if (link_fd < 0) {
+ fprintf(stderr, "BPF_OBJ_GET: %s\\n", strerror(errno));
+ return 1;
+ }
+ memset(&attr, 0, sizeof(attr));
+ attr.link_detach.link_fd = link_fd;
+ if (sys_bpf(BPF_LINK_DETACH, &attr) == 0)
+ return 1;
+ detach_errno = errno;
+ memset(&attr, 0, sizeof(attr));
+ attr.link_update.link_fd = link_fd;
+ attr.link_update.new_prog_fd = 0x7fffffff;
+ if (sys_bpf(BPF_LINK_UPDATE, &attr) == 0)
+ return 1;
+ update_errno = errno;
+ close(link_fd);
+ printf("detach_errno=%d update_errno=%d\\n", detach_errno, update_errno);
+ return detach_errno == EACCES && update_errno == EACCES ? 0 : 1;
+}
diff --git a/testing/tamperproof/t1132-upatch-integrity.sh b/testing/tamperproof/t1132-upatch-integrity.sh
new file mode 100644
index 00000000..2aaea938
--- /dev/null
+++ b/testing/tamperproof/t1132-upatch-integrity.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t1132 -- target-side artifact signature, pin-xattr and link protection.
+
+cd "$(dirname "$0")/.."
+. ./integration-lib.sh
+target_guard
+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"
+FIXTURES=$SHIRUDO_TEST_FIXTURES_DIR
+NAME=shirudo-test-basic
+LOAD="$FIXTURES/$NAME.upatch"
+PIN="/run/shirudo/bpffs/upatches/$NAME/link"
+PINDIR="/run/shirudo/bpffs/upatches/$NAME"
+ARCHIVE="/var/lib/shirudo/upatches/$NAME.upatch"
+HELPER=helper/test-upatch-link-lockdown
+WORK=$(mktemp -d)
+trap 'rm -rf "$WORK"' EXIT
+[ -s "$LOAD" ] || test_skip_all "missing trusted basic upatch"
+[ -x "$HELPER" ] || test_skip_all "upatch link helper is not built"
+[ -e "$PIN" ] || test_skip_all "basic fixture is not loaded"
+[ -s "$ARCHIVE" ] || test_skip_all "basic fixture archive is missing"
+python3 - "$LOAD" "$WORK/mutated.upatch" <<'PY'
+import hashlib, pathlib, struct, sys
+src, dst = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])
+data = bytearray(src.read_bytes())
+loader_off, loader_len = struct.unpack_from("<II", data, 304)
+data_off, data_len = struct.unpack_from("<II", data, 328)
+name = b"shirudo-tamper-invalid-signature"
+data[16:272] = name + bytes(256 - len(name))
+data[loader_off] ^= 1
+payload = data[loader_off:loader_off + loader_len]
+if data_len:
+ payload += data[data_off:data_off + data_len]
+data[272:304] = hashlib.sha256(payload).digest()
+dst.write_bytes(data)
+PY
+test_expect_success 'trusted certificate cannot authorize a mutated loader' '
+ "$SHIRUDO_BIN" upatch load "$WORK/mutated.upatch" --no-precheck \
+ --report="$WORK/report.json" >/dev/null 2>&1 || true
+ cat=$(report_field category <"$WORK/report.json") &&
+ [ "$cat" = signature_rejected ] &&
+ [ ! -e /run/shirudo/bpffs/upatches/shirudo-tamper-invalid-signature ] &&
+ [ ! -e /var/lib/shirudo/upatches/shirudo-tamper-invalid-signature.upatch ]
+'
+test_expect_success 'root cannot change the live payload binding xattr' '
+ before=$(python3 -c "import os,sys; print(os.getxattr(sys.argv[1], \"security.bpf.shirudo.payload\").hex())" "$PINDIR") &&
+ ! python3 -c "import os,sys; os.setxattr(sys.argv[1], \"security.bpf.shirudo.payload\", bytes(32))" "$PINDIR" 2>/dev/null &&
+ ! python3 -c "import os,sys; os.removexattr(sys.argv[1], \"security.bpf.shirudo.payload\")" "$PINDIR" 2>/dev/null &&
+ after=$(python3 -c "import os,sys; print(os.getxattr(sys.argv[1], \"security.bpf.shirudo.payload\").hex())" "$PINDIR") &&
+ [ "$before" = "$after" ]
+'
+test_expect_success 'root cannot detach or update a live upatch link' '
+ "$HELPER" "$PIN" &&
+ [ -e "$PIN" ]
+'
+test_done
diff --git a/testing/tamperproof/test.plan.md b/testing/tamperproof/test.plan.md
index 160e2eb4..fc2d905e 100644
--- a/testing/tamperproof/test.plan.md
+++ b/testing/tamperproof/test.plan.md
@@ -26,7 +26,7 @@ checks that Shirudo can still export the pre-existing Python exec record.
| TP-04 | Replace the named Shirudo keyring with a same-named keyring | Covered | `t1106-keyring-swap.sh` | Keep status mismatch and load refusal checks; this destructive test runs last. |
| TP-05 | Add a key to the sealed BPF keyring | Covered | `t1121-bpf-keyring.sh` | Keep sealing denial and enrollment checks. |
| TP-06 | Unlink, clear, revoke, invalidate, re-permission, timeout, or link a key in the anchor | Covered | `t1125-key-permission.sh` | Keep per-operation denial assertions and status check. |
-| TP-07 | Root modifies a labelled file under `/var/lib/shirudo` or changes its xattr | Covered | `t1101-seal.sh` | Keep write and xattr denial assertions. |
+| TP-07 | Root modifies a labelled file under `/var/lib/shirudo` or changes its xattr | Covered | `t1101-seal.sh` | Keep certificate and archived-upatch write, xattr, unlink, rename-away, and rename-over denial assertions. |
| TP-08 | Direct or lazy unmount, remount, or mount over private bpffs, or lazy unmount of a mount above it | Covered | `t1113-mount-lockdown.sh` | Keep mountpoint and enforcement-survival checks; the ancestor case runs in a private mount namespace. |
| TP-09 | Obtain writable serializer-array or arena FD by pinned path | Covered | `t1130-map-lockdown.sh` | Current test covers `shirudo_ser` and `obs_i_arena`. |
| TP-10 | Obtain writable map FD by map ID | Covered | `t1130-map-lockdown.sh` | Current test covers both maps. |
@@ -50,6 +50,8 @@ checks that Shirudo can still export the pre-existing Python exec record.
| TP-28 | Root renames a pin in the private bpffs, or renames one over the lsm/bpf link's pin | Covered | `t1135-pin-rename.sh` | Both are refused with `EPERM`; the pins stay and enforcement survives. Before `lsm/inode_rename` the rename over a pin dropped the link with no guard firing. |
| TP-29 | A refused attempt leaves no trace | Covered | `t1136-tamper-counters.sh` | Every guard counts its refusals by point in a frozen pinned map; `status` lists the non-zero counters, `status --json` and the keepalive carry them all, and root cannot reset them. |
+| TP-22 | Mutate a trusted upatch loader and recompute its userspace digest | Covered | `t1132-upatch-integrity.sh` | The kernel signature gate rejects the altered loader as `signature_rejected`; no pin or archive is created. |
+
## Review notes
- The default-deny branch in `bpf/bpf2.c` appears to reject several