eBPF-based security platform
testing: verify a TPM init did not bind is refused
Daniel Borkmann691c28811e37
diff --git a/contrib/build-test-vm-bundle.sh b/contrib/build-test-vm-bundle.shindex 0aa08482..5736adf4 100755--- a/contrib/build-test-vm-bundle.sh+++ b/contrib/build-test-vm-bundle.sh@@ -353,6 +353,15 @@ testing/helper/otel-mock.pytesting/helper/test-oci-registry.pyEOF+# A second TPM for t1165: the host's emulator and the libraries of its own.+if swtpm="$(command -v swtpm)"; then+ install -d "$TMP_DIR/test-helpers/swtpm"+ install -m 0755 "$swtpm" "$TMP_DIR/test-helpers/swtpm/"+ while IFS= read -r lib; do+ install -m 0644 "$lib" "$TMP_DIR/test-helpers/swtpm/"+ done < <(ldd "$swtpm" | awk '/libtpms|libswtpm/ { print $3 }')+fi+build_fixture(){diff --git a/testing/run-target-tests.sh b/testing/run-target-tests.shindex f158d701..6af6cbb5 100755--- a/testing/run-target-tests.sh+++ b/testing/run-target-tests.sh@@ -80,6 +80,7 @@ for t in \tamperproof/t1162-persist-undo.sh \tamperproof/t1163-foreign-state-root.sh \tamperproof/t1164-overlay-copy-up.sh \+ tamperproof/t1165-tpm-identity.sh \tamperproof/t1150-verity-devices.sh \tamperproof/t1151-verity-process.sh \tamperproof/t1153-verity-hidden.sh \diff --git a/testing/tamperproof/t1165-pty-tpm.py b/testing/tamperproof/t1165-pty-tpm.pynew file mode 100755index 00000000..12cee8d5--- /dev/null+++ b/testing/tamperproof/t1165-pty-tpm.py@@ -0,0 +1,49 @@+#!/usr/bin/env python3+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# A TPM 2.0 emulator behind a pseudo-terminal, whose slave is a character+# device a bind mount can put at /dev/tpmrm0; or, as probe, one command+# sent to a device path to show the emulator answers there.++import os+import pty+import select+import struct+import sys+import tty+++def probe(path):+ fd = os.open(path, os.O_RDWR | os.O_NONBLOCK)+ # TPM2_GetCapability(TPM_CAP_TPM_PROPERTIES, TPM_PT_MANUFACTURER, 1)+ cmd = struct.pack(">HII", 0x8001, 22, 0x17A) + struct.pack(">III", 6, 0x105, 1)+ if os.write(fd, cmd) != len(cmd):+ return 1+ if not select.select([fd], [], [], 10)[0]:+ return 1+ rsp = os.read(fd, 4096)+ tag, size, rc = struct.unpack(">HII", rsp[:10])+ return 0 if tag == 0x8001 and size == len(rsp) and rc == 0 else 1+++def serve(state, swtpm):+ master, slave = pty.openpty()+ tty.setraw(master)+ tty.setraw(slave)+ # The emulator keeps the slave open too, or the master hangs up.+ os.set_inheritable(master, True)+ os.set_inheritable(slave, True)+ with open(os.path.join(state, "slave"), "w") as f:+ f.write(os.ttyname(slave))+ os.environ["LD_LIBRARY_PATH"] = swtpm+ os.execv(os.path.join(swtpm, "swtpm"), [+ "swtpm", "chardev", "--tpm2", "--fd", str(master),+ "--tpmstate", "dir=" + state,+ "--flags", "not-need-init,startup-clear",+ "--log", "file=" + os.path.join(state, "log") + ",level=1"])+++if sys.argv[1] == "probe":+ sys.exit(probe(sys.argv[2]))+serve(sys.argv[1], sys.argv[2])diff --git a/testing/tamperproof/t1165-tpm-identity.sh b/testing/tamperproof/t1165-tpm-identity.shnew file mode 100755index 00000000..e955c025--- /dev/null+++ b/testing/tamperproof/t1165-tpm-identity.sh@@ -0,0 +1,86 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1165 -- the TPM behind the device path is root's to replace: in a+# mount namespace of root's own a bind mount puts a character device of+# root's at /dev/tpmrm0, and an emulator behind it answered every check+# the register path made, since the index name, policy and reset count+# it compares are in the readable init record. Every TPM handle a+# command takes must first compare the attestation key's name with the+# one init recorded, which takes the endorsement seed no emulator has.++cd "$(dirname "$0")/.."+. ./integration-lib.sh+target_guard+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+command -v unshare >/dev/null 2>&1 || test_skip_all "no unshare"+FIXTURES=${SHIRUDO_TEST_FIXTURES_DIR:?target fixture directory is not set}+SWTPM="$(dirname "$FIXTURES")/test-helpers/swtpm"+[ -x "$SWTPM/swtpm" ] || test_skip_all "no software TPM in the bundle"+NAME=shirudo-test-lifecycle+LOAD="$FIXTURES/$NAME.upatch"+UNLOAD="$FIXTURES/$NAME.unload.upatch"+PIN="/run/shirudo/bpffs/upatches/$NAME"+[ -s "$LOAD" ] && [ -s "$UNLOAD" ] || test_skip_all "missing lifecycle fixture"+[ ! -e "$PIN" ] || test_skip_all "$NAME is loaded"+W=/tmp/t1165+rm -rf "$W"+mkdir -p "$W"++cleanup() {+ [ -s "$W/pid" ] && kill "$(cat "$W/pid")" 2>/dev/null+ if [ -e "$PIN" ]; then+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck \+ >/dev/null 2>&1 || true+ fi+ rm -rf "$W"+}+trap cleanup EXIT++python3 tamperproof/t1165-pty-tpm.py "$W" "$SWTPM" >"$W/out" 2>&1 &+echo $! >"$W/pid"+for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do+ [ -s "$W/slave" ] && break+ sleep 0.5+done+[ -s "$W/slave" ] || test_skip_all "the software TPM did not start"+SLAVE=$(cat "$W/slave")++chain() {+ "$SHIRUDO_BIN" status --json 2>/dev/null |+ python3 -c "import json,sys; print(json.load(sys.stdin)[\"tpm\"][\"chain\"])"+}+CHAIN=$(chain)++at_slave() {+ unshare -m sh -c "mount --make-rprivate / &&+ mount --bind $SLAVE /dev/tpmrm0 && $*"+}++test_expect_success 'the emulator answers at the device path' '+ at_slave python3 tamperproof/t1165-pty-tpm.py probe /dev/tpmrm0+'++test_expect_success 'a load against a TPM init did not bind is refused' '+ rc=0; at_slave "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck \+ >/dev/null 2>"$W/load.err" || rc=$?;+ [ "$rc" != 0 ] &&+ grep -q "not the TPM init bound" "$W/load.err" &&+ [ ! -e "$PIN" ] &&+ [ "$(chain)" = "$CHAIN" ]+'++test_expect_success 'status against it is refused the same way' '+ ! at_slave "$SHIRUDO_BIN" status >/dev/null 2>"$W/status.err" &&+ grep -q "not the TPM init bound" "$W/status.err"+'++test_expect_success 'the same load succeeds against the TPM init bound' '+ "$SHIRUDO_BIN" upatch load "$LOAD" --no-precheck >/dev/null 2>&1 &&+ [ -e "$PIN/link" ] &&+ "$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&+ [ ! -e "$PIN" ]+'++test_done