eBPF-based security platform
testing: boot a VM twice and test a revocation the next boot applies
John Fastabend40c8b2ad6d73
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex f2644007..85a3bba6 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -484,6 +484,12 @@ signed config image.tracing and XDP upatches and their archives* - ``t5105-verity-interruption``- An interruption after measurement blocks changes and attestation+ * - ``t5106-verity-revocation-boot``+ - In ``functional/image-reboot/``, a VM of its own that boots+ twice. A staged config that revokes a signer is activated by the+ next boot from the initrd: the signer is not enrolled, the archive+ of the upatch it signed is purged and not restored, and a load it+ signed is refused``tamperproof`` (6)~~~~~~~~~~~~~~~~~~~@@ -1209,7 +1215,7 @@ Coverage by area``t6134``, ``t6135``, ``t6136``, ``t6137``* - Keyrings- ``t2101``, ``t2102``, ``t2103``, ``t5101``, ``t5104``,- ``t6004``, ``t6104``, ``t6112``, ``t6129``, ``t6140``+ ``t5106``, ``t6004``, ``t6104``, ``t6112``, ``t6129``, ``t6140``* - Signing keys and the trusted set- ``t2001``, ``t2002``, ``t2003``, ``t2004``, ``t2005``,``t5003``. Unit: ``io``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 71e3220e..8904465e 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -270,9 +270,10 @@ A shell test is named ``t<F><K><NN>-<what>.sh``:- TPM structures, the measurement register, the eventlog,attestation, the initrd binding and the register guard* - 5- - ``functional/image/``+ - ``functional/image/``, ``functional/image-reboot/``- Runtime and config images, settings, keys, activation,- revocation and interruption+ revocation and interruption; ``image-reboot`` boots a VM of its+ own twice, for what only the next boot does with a config* - 6- ``functional/tamperproof/``, ``functional/labels/``- Attacks on the admission and BPF command policy, bpffs, the@@ -550,7 +551,12 @@ Ubuntu cloud image and a software TPM, and runs ``init`` from a dracutinitrd with the dm-verity runtime image and a signed config image. Thefamily runs, the end-to-end run and the interactive VM all boot it.-A family's VM runs its target tests itself and powers off. The+A family's VM runs its target tests itself and powers off. The VM of+``image-reboot`` runs its tests, reboots and runs them again before it+powers off: the reboot keeps the disk and resets the TPM, and a test+reads which boot it is in from ``SHIRUDO_TEST_BOOT``, ``1`` or ``2``, and+keeps what it carries across in ``SHIRUDO_TEST_RESULTS``. The second+boot's output follows the first in ``result/guest.log``. Theend-to-end VM prepares itself the same way and waits while the host runsits target tests over SSH, then powers off. The operator VM of theend-to-end test has no TPM and no shirudo kernel arguments; it buildsdiff --git a/testing/functional/build/t1007-test-vm-cli.sh b/testing/functional/build/t1007-test-vm-cli.shindex c82b7611..42282ce5 100755--- a/testing/functional/build/t1007-test-vm-cli.sh+++ b/testing/functional/build/t1007-test-vm-cli.sh@@ -952,7 +952,7 @@ test_expect_success 'the runners split the tests by number' '! grep -q "/t[0-9]1[0-9][0-9]-" "$WORK/operator-tests" &&families=$(make -s -C .. -f testing/Makefile help |sed -n "s/.*families: //p") &&- [ "$families" = "build signing upatch tpm image labels tamperproof bpm bpm-pending bpm-segments otel" ] &&+ [ "$families" = "build signing upatch tpm image image-reboot labels tamperproof bpm bpm-pending bpm-segments otel" ] &&for d in functional/*/; dof=${d#functional/}case " $families " in *" ${f%/} "*) ;; *) return 1 ;; esacdiff --git a/testing/functional/image-reboot/t5106-verity-revocation-boot.py b/testing/functional/image-reboot/t5106-verity-revocation-boot.pynew file mode 100644index 00000000..81fae21a--- /dev/null+++ b/testing/functional/image-reboot/t5106-verity-revocation-boot.py@@ -0,0 +1,118 @@+#!/usr/bin/env python3+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++import hashlib+import importlib.util+import json+from pathlib import Path+import subprocess+import sys++spec = importlib.util.spec_from_file_location(+ "revocation_test",+ Path(__file__).parent.parent / "image" / "t5104-verity-revocation.py")+revocation = importlib.util.module_from_spec(spec)+spec.loader.exec_module(revocation)+signer = revocation.signer+config = signer.config+require = config.require+NAME = revocation.NAME+PIN = revocation.PIN+ARCHIVE = revocation.ARCHIVE+STAGED = "config-revoke"+BOOT_ID = Path("/proc/sys/kernel/random/boot_id")+EVENTLOG = Path("/var/lib/shirudo/eventlog")+++def cert_sha256(name):+ return hashlib.sha256((signer.FIXTURES / name).read_bytes()).hexdigest()+++def boot_init_event():+ events = [json.loads(line) for line in EVENTLOG.read_text().splitlines()]+ inits = [event for event in events if event["type"] == "init"]+ require(inits, "the eventlog holds no init event")+ return inits[-1]+++def initrd_journal():+ return subprocess.run(+ ["journalctl", "-b", "-u", "shirudo-initrd.service", "--no-pager",+ "-o", "cat"], check=True, capture_output=True, text=True).stdout+++def main():+ mode = sys.argv[1]+ if mode == "load":+ signer.check_members(signer.keyring())+ require(not PIN.exists() and not ARCHIVE.exists(),+ "stale upatch of the boot signer")+ config.run("load", "upatch", "load", revocation.UPATCH, "--no-precheck")+ require((PIN / "link").exists() and ARCHIVE.is_file(),+ "upatch not installed")+ revocation.snapshot("loaded", 2)+ elif mode == "stage":+ before = config.load("loaded")+ config.run("stage", "image", "stage", config.IMAGES / STAGED)+ staged = revocation.snapshot("staged", 2)+ require(staged["status"]["next_boot_root_hash"] == config.root(STAGED),+ "status does not name the staged config")+ require(staged["status"]["root_hash"] == before["status"]["root_hash"] and+ staged["chain"] == before["chain"] and+ staged["keys"] == before["keys"],+ "staging changed the active config, the register or the keys")+ require((PIN / "link").exists() and ARCHIVE.is_file(),+ "staging a revocation unloaded the signer's upatch")+ staged["boot_id"] = BOOT_ID.read_text()+ config.save("staged", staged)+ elif mode == "booted":+ staged = config.load("staged")+ require(BOOT_ID.read_text() != staged["boot_id"],+ "the VM did not reboot")+ status = json.loads(config.run("status", "status", "--json"))+ require(status["lsm_attached"] is True, "LSM not attached")+ require(status["tpm"]["bound_in"] == "initrd",+ f"register not bound from the initrd: {status['tpm']}")+ require(status["tpm"]["chain_matches_eventlog"] is True,+ "register does not match the eventlog")+ booted = revocation.snapshot("booted", 1)+ require(booted["status"]["root_hash"] == config.root(STAGED),+ "the staged config is not active")+ require(booted["status"]["next_boot_root_hash"] is None,+ "the stage outlived the boot that took it")+ require(booted["status"]["reset_count"] != staged["status"]["reset_count"],+ "the TPM was not reset")+ events = booted["config"]["events"]+ require(len(events) == 1 and events[0]["kind"] == 1 and+ events[0]["root_hash"] == config.root(STAGED),+ f"the boot did not record one BOOT event of the staged config: {events}")+ elif mode == "purged":+ booted = config.load("booted")+ primary = signer.certificate_description(signer.FIXTURES / "primary.der")+ require({value[4] for value in booted["keys"].values()} == {primary},+ "the boot enrolled another key than the primary")+ require(not PIN.exists(), "the revoked signer's upatch was restored")+ require(not ARCHIVE.exists(), "the revoked signer's archive remains")+ event = boot_init_event()+ require(cert_sha256("primary.der") in event["certs"] and+ cert_sha256("boot.der") not in event["certs"],+ f"wrong certificates in the init event: {event['certs']}")+ require(NAME not in [entry["name"] for entry in event["set"]],+ "the init event counts the revoked signer's upatch")+ require(f"keys revoke: {NAME} purged from the archive" in initrd_journal(),+ "the initrd did not report the purge")+ elif mode == "refused":+ before = revocation.snapshot("before-refused", 1)+ config.run("revoked-load", "upatch", "load", revocation.UPATCH,+ "--no-precheck", success=False)+ require(not PIN.exists() and not ARCHIVE.exists(),+ "a refused load left a pin or an archive")+ require(before == revocation.snapshot("refused", 1),+ "refused load changed state")+ else:+ raise ValueError(mode)+++if __name__ == "__main__":+ main()diff --git a/testing/functional/image-reboot/t5106-verity-revocation-boot.sh b/testing/functional/image-reboot/t5106-verity-revocation-boot.shnew file mode 100644index 00000000..c43183b7--- /dev/null+++ b/testing/functional/image-reboot/t5106-verity-revocation-boot.sh@@ -0,0 +1,45 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t5106 -- a config that revokes a BPF signer, staged and then activated+# by the next boot. The boot enrolls no revoked key, purges the archive of+# what that key signed before the restore and binds the register from the+# initrd as any boot does.++cd "$(dirname "$0")/../.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+: "${SHIRUDO_TEST_KEY_FIXTURES:?signed key fixtures are required}"+: "${SHIRUDO_TEST_RESULTS:?results that outlive the reboot are required}"+work=$SHIRUDO_TEST_RESULTS/revocation-boot+mkdir -p "$work"+export SHIRUDO_BIN SHIRUDO_TEST_KEY_FIXTURES+py=functional/image-reboot/t5106-verity-revocation-boot.py++case ${SHIRUDO_TEST_BOOT:-1} in+1)+ test_expect_success 'the boot signer loads a upatch that is pinned and archived' '+ python3 "$py" load "$work"+ '+ test_expect_success 'staging the revocation leaves this boot as it is' '+ python3 "$py" stage "$work"+ '+ ;;+2)+ test_expect_success 'the next boot activates the staged config from the initrd' '+ python3 "$py" booted "$work"+ '+ test_expect_success 'the boot enrolled no revoked signer and restored nothing it signed' '+ python3 "$py" purged "$work"+ '+ test_expect_success 'a load the revoked signer signed is refused and changes nothing' '+ python3 "$py" refused "$work"+ '+ ;;+*)+ test_expect_success 'the test knows the boot it runs in' 'false'+ ;;+esac+test_donediff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex 1475eae7..952075b5 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -5,6 +5,8 @@# testing/run-target-tests.sh [-t <regex>] <family> -- the target tests (t?1??) of one# family in number order, in the test VM that family booted for itself.# A test that skips as a whole fails the family: every one is required.+# The image-reboot family runs twice, before and after a reboot of its VM,+# and its tests read which from SHIRUDO_TEST_BOOT, 1 or 2.set -eucd "$(dirname "$0")"@@ -47,6 +49,7 @@ export SHIRUDO_TEST_SIGNER_RESULTS="$SHIRUDO_TEST_RESULTS/signers"export SHIRUDO_TEST_INTERRUPTION_RESULTS="$SHIRUDO_TEST_RESULTS/interruption"export SHIRUDO_TEST_CLI_RESULTS="$SHIRUDO_TEST_RESULTS/cli"export SHIRUDO_TEST_REVOKE_RESULTS="$SHIRUDO_TEST_RESULTS/revocation"+export SHIRUDO_TEST_BOOT="${SHIRUDO_TEST_BOOT:-1}"fail=0found=0# Keep post-test log handling in this shell; an end-to-end policy test may@@ -88,6 +91,7 @@ for t in "$dir"/t[0-9]1[0-9][0-9]-*.sh; dofi ;;esaclog="$SHIRUDO_TEST_RESULTS/$(basename "$t").log"+ [ "$SHIRUDO_TEST_BOOT" = 1 ] || log="${log%.log}.boot$SHIRUDO_TEST_BOOT.log"rc=0sh "$t" >"$log" 2>&1 || rc=$?print_test_log "$log"diff --git a/testing/vm/guest.sh b/testing/vm/guest.shindex 6e5a1cde..356b0ea1 100755--- a/testing/vm/guest.sh+++ b/testing/vm/guest.sh@@ -380,6 +380,52 @@ prepare_operator()shirudo version}+# The second boot of a family that reboots. The first boot prepared the+# guest and the initrd has run init again; only what a boot loses is redone.+second_boot()+{+ log "second boot of the $TEST_VM_SUITE suite"+ mount -t overlay overlay \+ -o "lowerdir=${LOWER},upperdir=${UPPER},workdir=${OVERLAY_WORK}" \+ "$WORKSPACE" || test -d "$WORKSPACE/testing"+ cd "$WORKSPACE"+ log "initrd shirudo journal"+ journalctl -b -u shirudo-initrd.service --no-pager | tail -40 || true+ /usr/local/bin/shirudo-runtime init+ log "running target tests: $TEST_VM_SUITE, second boot"+ (+ cd testing+ . /etc/shirudo-test-vm.env+ SHIRUDO_TEST_BOOT=2 ./run-target-tests.sh "$TEST_VM_SUITE"+ )+ log "integration suite passed"+}++# cloud-init runs the bootstrap script once per instance; this unit runs+# it on the boot that follows, where the mark tells it which boot it is.+request_second_boot()+{+ cat >/etc/systemd/system/shirudo-test-vm-second-boot.service <<'EOF'+[Unit]+Description=shirudo test VM: the target tests of the second boot++[Service]+Type=oneshot+ExecStart=/usr/local/sbin/shirudo-test-vm-bootstrap+TimeoutStartSec=infinity++[Install]+WantedBy=multi-user.target+EOF+ systemctl enable shirudo-test-vm-second-boot.service+ : >/etc/shirudo-test-vm-second-boot+}++if [[ "${TEST_VM_BOOT:-1}" == 2 ]]; then+ second_boot+ exit 0+fi+configure_oci_caconfigure_bpm_oci_repositoryconfigure_map_export@@ -634,4 +680,9 @@ log "running target tests: $TEST_VM_SUITE". /etc/shirudo-test-vm.env./run-target-tests.sh "$TEST_VM_SUITE")+if [[ "${TEST_VM_BOOTS:-1}" == 2 ]]; then+ request_second_boot+ log "first boot passed; rebooting for the second"+ exit 75+filog "integration suite passed"diff --git a/testing/vm/run.sh b/testing/vm/run.shindex c6bf9f84..6ec3e6db 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -1802,6 +1802,11 @@ ethernets:optional: falseEOF+# The image-reboot family runs its target tests over two boots of one VM:+# the guest reboots between them, which keeps the disk and resets the TPM.+VM_BOOTS=1+[[ "$VM_MODE" != test || "$VM_SUITE" != image-reboot ]] || VM_BOOTS=2+cat >"$USER_DATA" <<'EOF'#cloud-configgrowpart:@@ -1815,6 +1820,12 @@ write_files:content: |#!/usr/bin/env bashset -uo pipefail+ # A two-boot family runs this again on its second boot, from the+ # unit guest.sh installs, and cloud-init may too, as the reboot cut+ # its first run short: one of them runs, and the mark says which boot.+ exec 9>/run/shirudo-test-vm-bootstrap.lock+ flock -n 9 || exit 0+ [ ! -e /etc/shirudo-test-vm-second-boot ] || export TEST_VM_BOOT=2mkdir -p /mnt/shirudo-src /mnt/shirudo-bundle /mnt/shirudo-out /mnt/shirudo-kernel /mnt/shirudo-package-cert /mnt/shirudo-bpm-treeif ! mount -t 9p -o trans=virtio,version=9p2000.L,msize=104857600 shirudo-out /mnt/shirudo-out; thenecho "test-vm: could not mount result share" >/dev/console@@ -1855,6 +1866,7 @@ printf ' export TEST_VM_LOCAL_STACK=%q\n' "${TEST_VM_LOCAL_STACK:-0}" >>"$Uprintf ' export VM_MODE=%q\n' "$VM_MODE" >>"$USER_DATA"printf ' export TEST_VM_MODE=%q\n' "$TEST_MODE" >>"$USER_DATA"printf ' export TEST_VM_SUITE=%q\n' "$VM_SUITE" >>"$USER_DATA"+printf ' export TEST_VM_BOOTS=%q\n' "$VM_BOOTS" >>"$USER_DATA"printf ' export TEST_FILTER=%q\n' "${TEST_FILTER:-}" >>"$USER_DATA"printf ' export TEST_VM_NODE_ROLE=%q\n' "$NODE_ROLE" >>"$USER_DATA"printf ' export TEST_VM_SKIP_SEAL=%q\n' "$SKIP_SEAL" >>"$USER_DATA"@@ -1920,6 +1932,14 @@ cat >>"$USER_DATA" <<'EOF'if [ "$rc" -eq 0 ]; thenbash /mnt/shirudo-src/testing/vm/guest.sh || rc=$?fi+ if [ "$rc" -eq 75 ]; then+ # guest.sh asks for the family's second boot, which publishes the+ # status; a reboot that does not come fails the run.+ sync+ systemctl reboot+ sleep 300+ rc=1+ fiprintf '%s\n' "$rc" >/mnt/shirudo-out/statussyncif { [ "${VM_MODE:-test}" = interactive ] ||@@ -2001,9 +2021,10 @@ QEMU_ARGS=(-chardev "socket,id=console,path=console.sock,server=on,wait=off,logfile=${RUN_DIR}/console.log,logappend=on" \-serial chardev:console \-display none \- -monitor none \- -no-reboot+ -monitor none)+# Any reboot but the one a two-boot family asks for ends the run.+[[ "$VM_BOOTS" == 2 ]] || QEMU_ARGS+=(-no-reboot)if [[ -n "$KERNEL_ARTIFACT_DIR" ]]; thenQEMU_ARGS+=(