eBPF-based security platform
testing: enroll an end-to-end node from its vTPM builder's records
John Fastabendb0badc83be55
diff --git a/.agents/skills/testing/SKILL.md b/.agents/skills/testing/SKILL.mdindex 54c0a45f..49ab6000 100644--- a/.agents/skills/testing/SKILL.md+++ b/.agents/skills/testing/SKILL.md@@ -34,6 +34,11 @@ the keepalive backend of `tools/backend`, which `host.sh` starts forevery run. The tier boots two VMs at once, each with its ownswtpm: node A runs the tests, node B (`make -C testing vm-node-end2end`,node mode of `vm/run.sh`) takes `role=core` from `config-core` and waits.+Node mode first runs `vm/tpm-builder.sh`, which creates the node's AK+and EK with tpm2-tools and writes their names to `tpm-names` in the VM's+state directory; host.sh verifies node B against them and t9109 enrolls+it in the backend, so node mode needs tpm2-tools and swtpm-tools on the+host.With a backend, a fresh edge node (`END2END_NODE=edge`, `role=edge` from`config-edge`) boots once node A has stopped, and `host.sh` runs t9109,then t9110, which continues t9109's tree in segments, on the host againstdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 97eb0d3b..1e4d0479 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -105,7 +105,8 @@ signed config image.* - ``t1007-test-vm-cli``- Test VM scripts: kernel selection, host dependencies, arm64boot settings, the bundle stamp, the build container, serve mode,- node mode only for a core or edge node of the end-to-end tier, the+ node mode only for a core or edge node of the end-to-end tier,+ whose vTPM keys the builder helper creates before boot, theoperator VM's role and its signing key* - ``t1008-kernel-ami-config``- The slim kernel configuration for NitroTPM CRB, EC2, EFI and SSM@@ -757,8 +758,10 @@ signed config image.by ``host.sh`` on the host: ``bpm build`` and ``bpm tree append``of an upatch with ``fixes``, tree sync on both nodes, quotedkeepalives through the backend's collector and checker, its tree- ingest and sbom-scan jobs, ``node_status`` with the masks ``VA==``- and ``qA==``, each node's records against the backend's rows,+ ingest and sbom-scan jobs, node B enrolled from its vTPM+ builder's names and the edge node left untrusted,+ ``node_status`` with the masks ``VA==`` and ``qA==``, each node's+ records against the backend's rows,every SBOM state, ``node_vulns`` exposure and a body altered intransit* - ``t9110-keepalive-history``@@ -930,7 +933,7 @@ Coverage by area``t6125``, ``t6128``, ``t6143``, ``t6144``. Unit: ``chain``* - Attestation- ``t2003``, ``t2005``, ``t4003``, ``t4004``, ``t4102``,- ``t4104``, ``t9105``. Fuzz: ``attest``, ``tpm-wire``+ ``t4104``, ``t9105``, ``t9109``. Fuzz: ``attest``, ``tpm-wire``* - Eventlog- ``t4002``, ``t4102``, ``t5007``, ``t6114``, ``t6128``. Fuzz:``eventlog``. Unit: ``eventlog``, ``io``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex f99965eb..1a9e4c6d 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -841,7 +841,14 @@ Once both have stopped, a second fresh edge node and node B,``edge``: the runner'snode mode prepares a node as it prepares node A, activates the configimage of its role, runs no tests, starts no timers and leaves it running-until ``host.sh`` stops it at the end. Each takes its role from a signed+until ``host.sh`` stops it at the end. Before the guest boots, node mode+acts as the builder of :doc:`/operate/run/attest`:+:src:`testing/vm/tpm-builder.sh` creates the attestation key and the+endorsement key in the node's fresh vTPM state, with the tpm2-tools+commands that page gives, and writes their names to ``tpm-names`` in the+VM's state directory. ``host.sh`` copies the file beside the node's SSH+key and verifies node B's evidence against those names, never against+the names the evidence carries, and checks that node A's key fails. Each takes its role from a signedconfig image, as ``t9105`` configures node A:.. list-table::@@ -965,12 +972,16 @@ and the nodes' directory in its environment; its output is#. writes the SBOM store of the nodes' image, and one image for eachother state of an SBOM, and runs the backend's sbom-scan job with thestub ``trivy`` of the fixtures, which prints one ``openssl`` finding;+#. checks that node B's records carry the attestation key its builder+ recorded, and enrolls that key, inserting ``enrolled`` for it into+ ``shirudo.nodes`` as an operator does;#. seeds the kernel advisory of the fixtures, whose range holds every testkernel, and the nodes' build, and refreshes ``node_status`` and``node_vulns``.-It then checks the tree, the stored records and their quotes,-``node_status`` with the masks ``VA==`` and ``qA==``, each node's+It then checks the tree, the stored records and their quotes, node B+``enrolled`` and the edge node ``untrusted`` in ``nodes`` and+``node_status``, ``node_status`` with the masks ``VA==`` and ``qA==``, each node's``state.json``, ``outcomes``, ``tree-eventlog`` and loaded set againstthe backend's rows, the state of every SBOM, and the exposure of eachnode: the kernel advisory ``shielded`` by ``xfs-fix`` on the edge node anddiff --git a/testing/end2end/host.sh b/testing/end2end/host.shindex bd0386ed..5bf6d4c3 100755--- a/testing/end2end/host.sh+++ b/testing/end2end/host.sh@@ -80,7 +80,7 @@ node_run_dir() {# Boot node $1, b with the core role or edge with the edge role, prepared# like node A but without tests, and record in $NODES_DIR/node-$1 how to-# reach it.+# reach it and the key names its vTPM's builder recorded before it booted.node_boot() {run=$(node_run_dir "$1")mkdir -p "$NODES_DIR/node-$1"@@ -89,6 +89,7 @@ node_boot() {TEST_VM_CPUS=2 TEST_VM_MEMORY="${TEST_VM_NODE_MEMORY:-2G}" \>"$W/node-$1$NODE_ROUND.log" 2>&1 &&cp "$run/ssh.port" "$NODES_DIR/node-$1/port" &&+ cp "$run/tpm-names" "$NODES_DIR/node-$1/tpm-names" &&install -m 0600 "$run/ssh/id_ed25519" "$NODES_DIR/node-$1/key"; then: >"$NODES_DIR/node-$1/ready"else@@ -329,18 +330,25 @@ ak=$(python3 -c 'import json, sys; print(json.load(open(sys.argv[1]))["ak"]["namnode_sh b "/usr/local/bin/shirudo-runtime attest --nonce=$(cat "$W/nonce")" \>"$W/node-b-evidence.json" || die "node B gave no evidence"# Node B loads nothing of its own: a test gives it only what a tree holds.-ak_b=$(python3 -c 'import json, sys+python3 -c 'import json, sysevidence = json.load(open(sys.argv[1]))-assert not [r for r in evidence["eventlog"] if r["type"] == "upatch_load"]-print(evidence["ak"]["name"])' "$W/node-b-evidence.json") ||+assert not [r for r in evidence["eventlog"] if r["type"] == "upatch_load"]' \+ "$W/node-b-evidence.json" ||die "node B loaded an upatch while it was prepared"+# The names come from the builder, never from the node.+ak_b=$(sed -n 's/^ak-name //p' "$NODES_DIR/node-b/tpm-names")+ek_b=$(sed -n 's/^ek-name //p' "$NODES_DIR/node-b/tpm-names")"$S" attest verify --evidence="$W/node-b-evidence.json" \- --nonce="$(cat "$W/nonce")" --ak-name="$ak_b" --allow-runtime-events \- --require-initrd >"$W/node-b-verify.out" 2>&1 &&+ --nonce="$(cat "$W/nonce")" --ak-name="$ak_b" --ek-name="$ek_b" \+ --allow-runtime-events --require-initrd >"$W/node-b-verify.out" 2>&1 &&grep -qx 'verdict: ok' "$W/node-b-verify.out" || {cat "$W/node-b-verify.out" >&2- die "the evidence of node B does not verify"+ die "the evidence of node B does not verify against its builder's names"}+! "$S" attest verify --evidence="$W/node-b-evidence.json" \+ --nonce="$(cat "$W/nonce")" --ak-name="$ak" --ek-name="$ek_b" \+ --allow-runtime-events --require-initrd >"$W/node-b-wrong.out" 2>&1 ||+ die "the evidence of node B verifies against node A's attestation key"[ "$ak_b" != "$ak" ] || die "node A and node B share an attestation key"[ "$(node_sh b cat /run/shirudo/config/etc/shirudo/node.labels)" = role=core ] ||die "node B is not labelled role=core"diff --git a/testing/end2end/t9109-pipeline.sh b/testing/end2end/t9109-pipeline.shindex 8993db00..2695059e 100755--- a/testing/end2end/t9109-pipeline.sh+++ b/testing/end2end/t9109-pipeline.sh@@ -9,6 +9,9 @@# collector; the backend's tree ingest and sbom-scan jobs load the tree# and the SBOM of the nodes' image; and node_status and node_vulns give# each node's health and exposure. A body altered in transit is refused.+# The operator enrolls node B from the names its vTPM's builder recorded+# before it booted, so the backend trusts its key; the edge node, never+# enrolled, stays untrusted.## end2end/host.sh runs it on the host after node A's suite, as root, with# the keepalive backend it started, the operator key and the two nodes.@@ -55,6 +58,11 @@ payload() {python3 -c 'import json, sys; print(json.load(sys.stdin)["payload_sha256"])'}+# The attestation key name node B's builder recorded before it booted.+builder_ak() {+ sed -n "s/^ak-name //p" "$NODES_DIR/node-b/tpm-names"+}+# Print the rows of node_status, the age of the record aside.status_rows() {ch "SELECT ak_name, status, trust, failed_check,@@ -157,8 +165,19 @@ test_expect_success 'each node sends two quoted keepalives, a few seconds apart'test "$(ak edge)" != "$(ak b)"'-test_expect_success 'the backend keeps both nodes, untrusted, and every record' '- printf "%s\tuntrusted\n" "$(ak b)" "$(ak edge)" | sort >"$W/nodes" &&+test_expect_success 'node B keeps the attestation key its builder recorded' '+ test -n "$(builder_ak)" &&+ test "$(ak b)" = "$(builder_ak)"+'++test_expect_success 'the operator enrolls node B from its builder'\''s records' '+ ch "INSERT INTO shirudo.nodes (ak_name, trust)+ VALUES ('\''$(builder_ak)'\'', '\''enrolled'\'')"+'++test_expect_success 'the backend keeps node B enrolled, the edge node untrusted, and every record' '+ { printf "%s\tenrolled\n" "$(ak b)" &&+ printf "%s\tuntrusted\n" "$(ak edge)"; } | sort >"$W/nodes" &&ch_cmp "SELECT ak_name, max(trust) FROM shirudo.nodesGROUP BY ak_name ORDER BY ak_name" "$W/nodes" &&test "$(ch "SELECT count() FROM shirudo.rejected")" = 0 &&@@ -172,13 +191,14 @@ test_expect_success 'the backend keeps both nodes, untrusted, and every record'ON k.ak_name = l.ak_name AND k.newest = l.clock AND k.newest_body = l.body")" = 2'-test_expect_success 'node_status shows both nodes healthy with the masks VA== and qA==' '+test_expect_success 'node_status shows both nodes healthy, node B enrolled, with the masks VA== and qA==' 'version=$(on edge "$RUNTIME version --version-id") &&test "$version" = "$(on b "$RUNTIME version --version-id")" &&ch "INSERT INTO shirudo.expected_builds (version_id, note)VALUES ('\''$version'\'', '\''t9109'\'')" &&refresh node_status_mv node_vulns_mv &&- printf "%s\thealthy\tuntrusted\t0\n" "$(ak b)" "$(ak edge)" | sort >"$W/status" &&+ { printf "%s\thealthy\tenrolled\t0\n" "$(ak b)" &&+ printf "%s\thealthy\tuntrusted\t0\n" "$(ak edge)"; } | sort >"$W/status" &&ch_cmp "SELECT ak_name, status, trust, failed_check FROM shirudo.node_statusORDER BY ak_name" "$W/status" &&status_log edge >"$W/log-edge" &&diff --git a/testing/functional/build/t1007-test-vm-cli.sh b/testing/functional/build/t1007-test-vm-cli.shindex bc05974a..90522e01 100755--- a/testing/functional/build/t1007-test-vm-cli.sh+++ b/testing/functional/build/t1007-test-vm-cli.sh@@ -51,6 +51,9 @@ test_expect_success 'node mode boots only a core or edge node of the end2end suigrep -Fq "TEST_VM_NODE_ROLE=\$(if \$(filter edge,\$(END2END_NODE)),edge,core)" \../testing/Makefile &&grep -Fq "export TEST_VM_NODE_ROLE=" vm/run.sh &&+ grep -Fq "\"\$REPO_ROOT/testing/vm/tpm-builder.sh\" \"\$RUN_DIR/tpm\" \"\$RUN_DIR/tpm-names\"" \+ vm/run.sh &&+ grep -Fq "tpm2_createprimary:tpm2-tools:tpm2-tools" vm/run.sh &&grep -Fq "if [[ \"\$VM_MODE\" == node ]]" vm/guest.sh &&grep -Fq "image update \"/var/lib/shirudo/images/config-\${TEST_VM_NODE_ROLE}\"" \vm/guest.shdiff --git a/testing/vm/run.sh b/testing/vm/run.shindex b59ac176..1b4a758c 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -518,6 +518,13 @@ start_swtpm()local _chmod 0700 "$RUN_DIR/tpm"+ # An end2end node's TPM comes from a builder the operator trusts, which+ # records the names of its keys before the guest boots.+ if [[ "$VM_MODE" == node ]]; then+ "$REPO_ROOT/testing/vm/tpm-builder.sh" "$RUN_DIR/tpm" "$RUN_DIR/tpm-names" \+ >>"$RUN_DIR/swtpm.log" 2>&1 ||+ die "could not create the keys of the vTPM; see $RUN_DIR/swtpm.log"+ fiif user_systemd_available; thenSWTPM_UNIT_FILE="${RUN_DIR}/swtpm.unit"SWTPM_UNIT="shirudo-vm-${VM_NAME}-swtpm.service"@@ -1556,6 +1563,10 @@ validate_bpm_ociif [[ "$VM_INITRD" == 1 ]]; thenHOST_DEPENDENCIES+=("dracut:dracut-core:dracut" "lsinitrd:dracut-core:dracut" "file:file:file")fi+if [[ "$VM_MODE" == node ]]; then+ HOST_DEPENDENCIES+=("tpm2_createprimary:tpm2-tools:tpm2-tools"+ "swtpm_ioctl:swtpm-tools:swtpm-tools")+ficheck_host_dependenciesvalidate_host_bundle[[ "$CPUS" =~ ^[1-9][0-9]*$ ]] || die "TEST_VM_CPUS must be a positive integer"diff --git a/testing/vm/tpm-builder.sh b/testing/vm/tpm-builder.shnew file mode 100755index 00000000..2064ce41--- /dev/null+++ b/testing/vm/tpm-builder.sh@@ -0,0 +1,42 @@+#!/usr/bin/env bash+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+# testing/vm/tpm-builder.sh <state-dir> <names> -- act as the builder of+# "Enroll from a builder's records": create the attestation key and the+# endorsement key in the vTPM state before the guest boots, with the+# commands that page gives, and write their names to <names>.++set -euo pipefail++state=$1+names=$2+work=$(mktemp -d)++stop() {+ swtpm_ioctl --unix "$work/tpm.ctrl" -s >/dev/null 2>&1 || :+ rm -rf -- "$work"+}+trap stop EXIT++name() {+ printf '000b%s\n' "$(tail -c +3 "$1" | sha256sum | cut -c1-64)"+}++swtpm socket --tpm2 --tpmstate "dir=$state,mode=0700" \+ --server "type=unixio,path=$work/tpm" \+ --ctrl "type=unixio,path=$work/tpm.ctrl" \+ --flags not-need-init,startup-clear --daemon+for _ in {1..50}; do+ [[ -S "$work/tpm" ]] && break+ sleep 0.1+done+export TPM2TOOLS_TCTI="swtpm:path=$work/tpm"+cd "$work"+tpm2_createprimary -C e -g sha256 -G ecc256:ecdsa-sha256:null \+ -a 'fixedtpm|fixedparent|sensitivedataorigin|userwithauth|restricted|sign' \+ -c ak.ctx >/dev/null+tpm2_readpublic -c ak.ctx -o ak.pub >/dev/null+tpm2_flushcontext -t+tpm2_createek -c - -G ecc -u ek.pub >/dev/null+printf 'ak-name %s\nek-name %s\n' "$(name ak.pub)" "$(name ek.pub)" >"$names"+swtpm_ioctl --unix "$work/tpm.ctrl" -s