eBPF-based security platform
init: refuse to restore an archived upatch the register does not cover
Daniel Borkmann5982cb2b8432
diff --git a/Documentation/architecture.rst b/Documentation/architecture.rstindex aafd8b3f..38a28799 100644--- a/Documentation/architecture.rst+++ b/Documentation/architecture.rst@@ -461,7 +461,10 @@ an operator predicts its value from the certificate and the installedset alone (``shirudo attest predict``); no history is needed. If anyre-apply fails the register is not written and ``status`` reports``degraded``. A re-run of ``init`` finds the register bound and on-record and leaves it alone; one that finds the recorded register gone+record and leaves it alone, and restores nothing: an archived artifact+that is not loaded by then, which root can arrange by renaming a+signed artifact into the archive, is refused ``tampered`` rather than+attached without an event; one that finds the recorded register gonerefuses ``tampered`` until the next TPM reset.Measurement register. The measurement lives in a TPM 2.0 NV indexdiff --git a/builtin-init.c b/builtin-init.cindex cd8c2a1d..37618777 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -847,9 +847,15 @@ out:* All or nothing: one failure leaves init incomplete and the register* unextended. On a re-run an upatch already pinned this boot with the* archived payload is counted and left alone; loading it again would- * only attach a second link to fail at the pin.+ * only attach a second link to fail at the pin. Once this boot's init+ * event is on record the restore has happened and the register covers+ * what it loaded, so an archived artifact that is not loaded cannot be+ * applied any more: nothing would measure it. It was renamed into the+ * archive since, or left behind by an unload cut short, and either is+ * for the operator to resolve.*/-static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)+static int boot_restore(bool recorded,+ struct shirudo_measured_upatch **out_set, size_t *out_n){struct shirudo_measured_upatch *set = NULL;struct dirent **names = NULL;@@ -925,6 +931,14 @@ static int boot_restore(struct shirudo_measured_upatch **out_set, size_t *out_n)cnt++;goto next;}+ if (recorded) {+ pr_err("boot-restore: %s is archived but not loaded "+ "after the register was bound this boot "+ "(tampered); unload it or reboot",+ names[i]->d_name);+ rc = -EPERM;+ goto next;+ }if (shirudo_upatch_apply(path) != 0) {pr_err("boot-restore: failed to apply %s",names[i]->d_name);@@ -1247,6 +1261,8 @@ static int init_phase_error(int rc, const char *phase)static int run_init(void){struct shirudo_measured_upatch *set = NULL;+ struct shirudo_eventlog_init rec;+ bool recorded;size_t n = 0;int rc;@@ -1287,7 +1303,14 @@ static int run_init(void)if (rc)return init_phase_error(rc, "loading trusted keys");- rc = boot_restore(&set, &n);+ rc = eventlog_boot_init(&rec);+ if (rc && rc != -ENOENT) {+ pr_err("init: eventlog %s unreadable: %s", eventlog_path(),+ strerror(-rc));+ return rc;+ }+ recorded = rc == 0;+ rc = boot_restore(recorded, &set, &n);if (rc) {pr_err("init: boot-restore failed; further changes ""and attestation remain blocked");diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex 0964ff2e..fd1e6ae7 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/t1142-archive-restore.sh \tamperproof/t1143-attach-scope.sh \tamperproof/t1144-signed-metadata.sh \tamperproof/t1150-verity-devices.sh \diff --git a/testing/tamperproof/t1142-archive-restore.sh b/testing/tamperproof/t1142-archive-restore.shnew file mode 100755index 00000000..3af9932b--- /dev/null+++ b/testing/tamperproof/t1142-archive-restore.sh@@ -0,0 +1,61 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1142 -- an artifact renamed into the archive after the register was+# bound. Rename brings an inode into a sealed directory without creating+# one, so root can place any operator-signed .upatch there; a re-run of+# init used to restore it, attached and pinned, without a register event,+# since this boot's init event was already on record. init must now+# refuse the restore and leave the artifact unloaded. Cleaned up through+# the artifact's own unload directive, which removes the archive copy.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard++FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+NAME=shirudo-test-scope-kprobe+LOAD="$FIXTURES/$NAME.upatch"+UNLOAD="$FIXTURES/$NAME.unload.upatch"+ARCHIVE=/var/lib/shirudo/upatches+STAGE=/var/lib/t1142-stage+PIN="/run/shirudo/bpffs/upatches/$NAME/link"+[ -s "$LOAD" ] && [ -s "$UNLOAD" ] || test_skip_all "missing scope fixtures"+[ ! -e "$PIN" ] || test_skip_all "$NAME is loaded"++restore() {+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 || true+ rm -rf "$STAGE"+ "$SHIRUDO_BIN" init >/dev/null 2>&1 || true+}+trap restore EXIT++CHAIN=$("$SHIRUDO_BIN" status --json 2>/dev/null |+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")++test_expect_success 'root can rename a signed artifact into the archive' '+ mkdir -p "$STAGE" && cp "$LOAD" "$STAGE/$NAME.upatch" &&+ mv "$STAGE/$NAME.upatch" "$ARCHIVE/$NAME.upatch" &&+ [ -e "$ARCHIVE/$NAME.upatch" ]+'++test_expect_success 'a re-run of init refuses to restore it unmeasured' '+ ! "$SHIRUDO_BIN" init >/dev/null 2>"$STAGE/init.err" &&+ grep -q "archived but not loaded after the register was bound" \+ "$STAGE/init.err" &&+ [ ! -e "$PIN" ] &&+ [ "$("$SHIRUDO_BIN" status --json 2>/dev/null |+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")" = "$CHAIN" ]+'++test_expect_success 'the unload directive removes the planted copy and init completes again' '+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck --report=- 2>/dev/null |+ report_field category | grep -qx already_unloaded &&+ [ ! -e "$ARCHIVE/$NAME.upatch" ] &&+ "$SHIRUDO_BIN" init >/dev/null 2>&1 &&+ [ "$("$SHIRUDO_BIN" status --json 2>/dev/null |+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])")" = "$CHAIN" ]+'++test_done