Skip to content

eBPF-based security platform

testing: start one software TPM at a time on a state directory

Daniel Borkmannef0752b9f7d7
diff --git a/testing/functional/image/t5007-config-update.sh b/testing/functional/image/t5007-config-update.sh
index ce5a14fc..32aeaf0c 100755
--- a/testing/functional/image/t5007-config-update.sh
+++ b/testing/functional/image/t5007-config-update.sh
@@ -240,9 +240,9 @@ test_expect_success 'seal failures prevent trusted journal creation' '
if [ -x "$SIM" ]; then
test_expect_success 'actual TPM values match independently computed root events' '
mkdir -m 700 "$WORK/real-tpm" &&
- SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
+ SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
"$H" "$WORK/real-tpm" boot A >"$WORK/real-a" &&
- SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
+ SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
"$H" "$WORK/real-tpm" update B >"$WORK/real-b" &&
python3 - "$WORK/real-b" <<"PYTEST"
import hashlib, sys
@@ -255,9 +255,9 @@ assert f["pcr_value"] == pcr.hex() and f["extends"] == "2"
PYTEST
'
test_expect_success 'ambiguous real TPM completion refuses retries without another extension' '
- ! SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
+ ! SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
SHIRUDO_TEST_CONFIG_EXTEND=ambiguous "$H" "$WORK/real-tpm" update C >/dev/null &&
- ! SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
+ ! SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm" SHIRUDO_TEST_CONFIG_TPM=1 \
"$H" "$WORK/real-tpm" update C >"$WORK/no-retry" &&
[ "$(field "$WORK/no-retry" extends)" = 3 ]
'
diff --git a/testing/functional/tpm/t4003-register.sh b/testing/functional/tpm/t4003-register.sh
index bff9912f..1090fd3d 100644
--- a/testing/functional/tpm/t4003-register.sh
+++ b/testing/functional/tpm/t4003-register.sh
@@ -23,7 +23,7 @@ cleanup() {
exit "$result"
}
trap cleanup EXIT HUP INT TERM
-export SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm"
+export SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm"
export SHIRUDO_EVENTLOG_PATH="$WORK/eventlog"
export SHIRUDO_KEYS_DIR="$WORK/keys"
export SHIRUDO_INITRD_RELEASE="$WORK/initrd-release"
diff --git a/testing/functional/tpm/t4004-config-attest.sh b/testing/functional/tpm/t4004-config-attest.sh
index 4720fab7..430eb5ab 100755
--- a/testing/functional/tpm/t4004-config-attest.sh
+++ b/testing/functional/tpm/t4004-config-attest.sh
@@ -23,7 +23,7 @@ cleanup() {
exit "$result"
}
trap cleanup EXIT HUP INT TERM
-export SHIRUDO_TPM_TCTI="cmd:$SIM $WORK/tpm"
+export SHIRUDO_TPM_TCTI="cmd:exec $SIM $WORK/tpm"
export SHIRUDO_EVENTLOG_PATH="$WORK/eventlog"
export SHIRUDO_KEYS_DIR="$WORK/keys"
export SHIRUDO_INITRD_RELEASE="$WORK/initrd-release"
diff --git a/testing/functional/tpm/t4005-tpm-session.sh b/testing/functional/tpm/t4005-tpm-session.sh
index 2caaec68..63a01b77 100644
--- a/testing/functional/tpm/t4005-tpm-session.sh
+++ b/testing/functional/tpm/t4005-tpm-session.sh
@@ -19,7 +19,7 @@ trap cleanup EXIT
for mode in unsalted-control nv-extend nv-replay pcr-extend nv-read \
pcr-read clock policy-digest identity; do
- export SHIRUDO_TPM_TCTI="cmd:$(pwd)/helper/tpmsim-cmd $WORK/$mode"
+ export SHIRUDO_TPM_TCTI="cmd:exec $(pwd)/helper/tpmsim-cmd $WORK/$mode"
test_expect_success "TPM session: $mode" '
./helper/test-tpm-session "$mode"
'
diff --git a/testing/helper/tpmsim-cmd.c b/testing/helper/tpmsim-cmd.c
index 2f1897aa..3b0e3dc2 100644
--- a/testing/helper/tpmsim-cmd.c
+++ b/testing/helper/tpmsim-cmd.c
@@ -13,6 +13,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -28,7 +29,9 @@
* stdin, so a plain blocking read() never sees the EOF that would
* otherwise drive save_state() below. Route the signal through a
* self-pipe and treat it exactly like EOF instead of dying with
- * whatever TPM state is still unsaved.
+ * whatever TPM state is still unsaved. The TCTI signals and waits for
+ * the /bin/sh it starts, which is this process only when the command
+ * begins with exec.
*/
static int shutdown_pipe[2] = { -1, -1 };
@@ -68,6 +71,27 @@ static int wait_for_command(void)
return (pfd[1].revents & POLLIN) ? 0 : 1;
}
+/*
+ * A caller that exits, or whose shell stays between it and this process,
+ * goes on before the state is saved, and the next instance would load
+ * what the one before it has not written yet. Hold the state directory
+ * for as long as this instance runs: the descriptor returned owns the lock.
+ */
+static int lock_state(void)
+{
+ int fd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+
+ if (fd < 0)
+ return -1;
+ while (flock(fd, LOCK_EX) != 0) {
+ if (errno != EINTR) {
+ close(fd);
+ return -1;
+ }
+ }
+ return fd;
+}
+
/* TPM2_Startup(TPM_SU_CLEAR): sent when no volatile state was restored. */
static const __u8 startup_clear[] = {
0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x44, 0x00, 0x00,
@@ -197,7 +221,7 @@ int main(int argc, char **argv)
__u8 *resp = NULL, *permanent = NULL, *vol = NULL;
__u32 resp_size = 0, resp_cap = 0, permanent_len, vol_len;
char path[4096];
- int ret = 0;
+ int lock_fd, ret = 0;
if (argc != 2) {
fprintf(stderr, "usage: tpmsim-cmd <state-dir>\n");
@@ -208,6 +232,11 @@ int main(int argc, char **argv)
perror("tpmsim-cmd: state directory");
return 1;
}
+ lock_fd = lock_state();
+ if (lock_fd < 0) {
+ perror("tpmsim-cmd: state lock");
+ return 1;
+ }
if (install_shutdown_handler() != 0) {
perror("tpmsim-cmd: shutdown handler");
return 1;
@@ -276,5 +305,6 @@ int main(int argc, char **argv)
free(resp);
free(permanent);
free(vol);
+ close(lock_fd);
return ret;
}