eBPF-based security platform
status: say whether a TPM answered
Daniel Borkmannf179928b6a57
diff --git a/Documentation/operations.rst b/Documentation/operations.rstindex fcaa918c..8f589447 100644--- a/Documentation/operations.rst+++ b/Documentation/operations.rst@@ -76,6 +76,10 @@ machine-readable one. The fields that matter day to day:up), ``staging`` (enforcement up, certificate in the user keyringuntil the next reboot) or ``inactive`` (``init`` has not run).+``tpm.reachable``+ Whether a TPM answered on the TCTI, which the human report names on+ its ``TPM:`` line.+``tpm.bound_in````initrd`` or ``root``: where the register was bound. Productionmeans ``initrd``.@@ -103,7 +107,8 @@ Conditionsshirudo did not record.* - ``TpmMeasured``- ``WARN`` when no register is bound this boot- (``init-pending``) or the TPM cannot be read.+ (``init-pending``) or no TPM answers; the detail says+ which.``keyring_serial`` and ``keyring_serial_pinned``The live keyring and the one frozen at ``init``; they match on adiff --git a/builtin-status.c b/builtin-status.cindex 50fd678e..ccce56a0 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -51,6 +51,7 @@#include "util/format.h"#include "util/sysinfo.h"#include "util/timefmt.h"+#include "util/tpm.h"#include "util/upatch_maps.h"#include "util/upatch_maps_otel.h"#include "util/tamper.h"@@ -435,7 +436,7 @@ static void local_cond_input(struct shirudo_conditions_input *in,__s64 keyring_serial,__s64 keyring_serial_pinned,int ks_seen, int ksp_seen, int ksm_seen,- int ks_matches,+ int ks_matches, int tpm_reachable,int chain_seen, int chain_matches, int cm_seen,const char *nv_index){@@ -449,6 +450,7 @@ static void local_cond_input(struct shirudo_conditions_input *in,.keyring_serial = keyring_serial,.keyring_serial_pinned = keyring_serial_pinned,.keyring_serial_matches = ks_matches,+ .tpm_reachable = tpm_reachable,.ks_seen = ks_seen,.ksp_seen = ksp_seen,.ksm_seen = ksm_seen,@@ -542,6 +544,7 @@ int cmd_status(int argc, const char **argv)char chain_hex[2 * SHIRUDO_CHAIN_DIGEST_LEN + 1] = "";char name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1] = "";struct chain_status cst = {};+ char tcti[SHIRUDO_TPM_TCTI_MAX];char nv_index[16];char *json_buf = NULL;size_t json_cap = 0;@@ -684,6 +687,8 @@ int cmd_status(int argc, const char **argv){snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);+ if (tpm_tcti(tcti, sizeof(tcti)))+ strscpy(tcti, "?", sizeof(tcti));chain_rc = chain_status(&cst);if (chain_rc == 0 && !(cst.defined && cst.written))chain_rc = -ENODEV;@@ -765,7 +770,7 @@ int cmd_status(int argc, const char **argv)keyring_present && keyring_pinned >= 1,keyring_present && keyring_pinned >= 1 &&keyring_serial == keyring_pinned,- chain_rc == 0,+ cst.tpm, chain_rc == 0,match == 1, match >= 0,nv_index);otel_status = status_verdict(&cin);@@ -875,6 +880,7 @@ int cmd_status(int argc, const char **argv)sysinfo_emit_json(&e, &sysi);{json_obj_open_key(&e, "tpm");+ json_kv_bool(&e, "reachable", cst.tpm);json_kv_string(&e, "nv_index", nv_index);if (chain_rc == 0)json_kv_string(&e, "chain", chain_hex);@@ -1054,7 +1060,7 @@ int cmd_status(int argc, const char **argv)keyring_present && keyring_pinned >= 1,keyring_present && keyring_pinned >= 1 &&keyring_serial == keyring_pinned,- chain_rc == 0,+ cst.tpm, chain_rc == 0,match == 1, match >= 0,nv_index);verdict = status_verdict(&cin);@@ -1156,6 +1162,8 @@ int cmd_status(int argc, const char **argv){printf("\nTPM Measurement:\n");+ printf(" %-18s %s (%s)\n", "TPM:",+ cst.tpm ? "reachable" : "NOT reachable", tcti);printf(" %-18s %s (anchored in PCR %u)\n", "NV index:",nv_index, SHIRUDO_CHAIN_PCR);if (chain_rc == 0) {@@ -1165,10 +1173,16 @@ int cmd_status(int argc, const char **argv)printf(" %-18s %s\n", "Anchored:",cst.anchored ? "yes (name on record)" :"NO (index is not the one init bound)");- } else {+ } else if (!cst.tpm) {+ printf(" %-18s %s\n", "Chain:",+ "unavailable (no TPM answered)");+ } else if (chain_rc == -ENODEV) {printf(" %-18s %s\n", "Chain:",- "unavailable (no TPM / register not "- "bound this boot)");+ "unavailable (register not bound this "+ "boot)");+ } else {+ printf(" %-18s unavailable (TPM error: %s)\n",+ "Chain:", strerror(-chain_rc));}if (cst.logged) {printf(" %-18s %s\n", "PCR before init:",diff --git a/testing/helper/test-conditions.c b/testing/helper/test-conditions.cindex adfbea4c..1d34a6aa 100644--- a/testing/helper/test-conditions.c+++ b/testing/helper/test-conditions.c@@ -6,10 +6,11 @@* derives from what it reads off the host.** test-conditions <lsm 0|1> <bpf|user|none> <serial> <pinned> \- * <none|match|mismatch>+ * <notpm|none|match|mismatch>** serial is the live keyring's, -1 when it is absent; pinned the one init- * froze, 0 when it froze none; the last word the register's replay.+ * froze, 0 when it froze none; the last word the register's replay, with+ * notpm for no TPM answering and none for no register bound.*/#include <stdint.h>@@ -38,7 +39,7 @@ int main(int argc, char **argv)if (argc != 6 || arg_s64(argv[1], &lsm) || arg_s64(argv[3], &serial) ||arg_s64(argv[4], &pinned)) {fprintf(stderr, "usage: test-conditions <lsm> <mode> <serial> "- "<pinned> <none|match|mismatch>\n");+ "<pinned> <notpm|none|match|mismatch>\n");return 2;}mode = argv[2];@@ -55,7 +56,8 @@ int main(int argc, char **argv)in.ksp_seen = pinned >= 1;in.ksm_seen = in.keyring_present && pinned >= 1;in.keyring_serial_matches = in.ksm_seen && serial == pinned;- in.chain_seen = strcmp(argv[5], "none") != 0;+ in.tpm_reachable = strcmp(argv[5], "notpm") != 0;+ in.chain_seen = in.tpm_reachable && strcmp(argv[5], "none") != 0;in.cm_seen = in.chain_seen;in.chain_matches = !strcmp(argv[5], "match");in.nv_index = "0x01818E00";diff --git a/testing/t0050-conditions.sh b/testing/t0050-conditions.shindex 8fc461db..e15899e0 100755--- a/testing/t0050-conditions.sh+++ b/testing/t0050-conditions.sh@@ -3,8 +3,9 @@# Copyright (c) 2026 Authors of shirudo## t0050 -- util/conditions.c: the KeyringSealed condition names the kind-# of keyring the anchor is in, and a keyring other than the one init-# froze, or none where it froze one, makes the status tampered.+# of keyring the anchor is in, a keyring other than the one init froze,+# or none where it froze one, makes the status tampered, and the register+# conditions tell a TPM that does not answer from one with no register.cd "$(dirname "$0")". ./test-lib.sh@@ -63,4 +64,25 @@ test_expect_success 'a register mismatch is the reason given first' 'grep -qx "reason=live register != eventlog replay"'+test_expect_success 'no TPM answering is told apart from an unbound register' '+ out=$(./$H 1 bpf 100 100 notpm) &&+ echo "$out" | grep -qx "status=degraded" &&+ echo "$out" | grep -qx "reason=no TPM answered" &&+ echo "$out" | grep -qx "ChainIntact WARN no TPM answered, no register to replay" &&+ echo "$out" | grep -qx "TpmMeasured WARN no TPM answered"+'++test_expect_success 'a TPM without a register this boot says so' '+ out=$(./$H 1 bpf 100 100 none) &&+ echo "$out" | grep -qx "status=degraded" &&+ echo "$out" | grep -qx "reason=no register bound this boot" &&+ echo "$out" | grep -qx "ChainIntact WARN no register bound this boot (init incomplete)" &&+ echo "$out" | grep -qx "TpmMeasured WARN NV index 0x01818E00 not bound this boot"+'++test_expect_success 'a bound register is measured' '+ ./$H 1 bpf 100 100 match |+ grep -qx "TpmMeasured OK NV index 0x01818E00 readable"+'+test_donediff --git a/util/conditions.c b/util/conditions.cindex bba2f360..aa52c4f7 100644--- a/util/conditions.c+++ b/util/conditions.c@@ -71,7 +71,9 @@ size_t conditions(const struct shirudo_conditions_input *in,if (!in->chain_seen) {c[n].status = "WARN";snprintf(c[n].detail, sizeof(c[n].detail),- "no TPM register (init incomplete or TPM lost)");+ in->tpm_reachable ?+ "no register bound this boot (init incomplete)" :+ "no TPM answered, no register to replay");} else if (in->cm_seen && in->chain_matches) {c[n].status = "OK";snprintf(c[n].detail, sizeof(c[n].detail),@@ -94,9 +96,13 @@ size_t conditions(const struct shirudo_conditions_input *in,snprintf(c[n].detail, sizeof(c[n].detail),"NV index %s readable",in->nv_index ? in->nv_index : "?");+ else if (in->tpm_reachable)+ snprintf(c[n].detail, sizeof(c[n].detail),+ "NV index %s not bound this boot",+ in->nv_index ? in->nv_index : "?");elsesnprintf(c[n].detail, sizeof(c[n].detail),- "not measured");+ "no TPM answered");n++;}return n;@@ -155,6 +161,7 @@ void status_reason(const struct shirudo_conditions_input *in,} else if (strcmp(v, "init-pending") == 0) {snprintf(buf, cap, "target hasn't been `shirudo init`'d");} else if (strcmp(v, "degraded") == 0) {- snprintf(buf, cap, "no TPM register (unmeasured)");+ snprintf(buf, cap, in->tpm_reachable ?+ "no register bound this boot" : "no TPM answered");}}diff --git a/util/conditions.h b/util/conditions.hindex 62a2b923..a182c8de 100644--- a/util/conditions.h+++ b/util/conditions.h@@ -31,6 +31,7 @@ struct shirudo_conditions_input {__s64 keyring_serial_pinned;int keyring_serial_matches; /* 0/1 if ksm_seen */int ks_seen, ksp_seen, ksm_seen;+ int tpm_reachable;int chain_seen;int chain_matches; /* 0/1 if cm_seen */int cm_seen;diff --git a/util/tpm.c b/util/tpm.cindex 34680edf..1eb85ed3 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -14,6 +14,7 @@#include <tss2/tss2_tctildr.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "config.h"#include "log.h"@@ -30,16 +31,34 @@ struct shirudo_tpm {ESYS_CONTEXT *esys;};-int tpm_open(struct shirudo_tpm **out)+/*+ * The env, then the file, then /dev/tpmrm0. An override is for VMs,+ * CI and a software TPM; it cannot defeat the operator-side guarantee,+ * since evidence is signed by the key the operator pinned.+ */+int tpm_tcti(char *buf, size_t size){struct shirudo_config cfg = {};- struct shirudo_tpm *t;const char *tcti;- TSS2_RC rc;+ ssize_t n;- t = xcalloc(1, sizeof(*t));- if (!t)- return -ENOMEM;+ tcti = getenv("SHIRUDO_TPM_TCTI");+ if ((!tcti || !*tcti) && config_load(&cfg) == 0 &&+ cfg.tpm_tcti[0])+ tcti = cfg.tpm_tcti;+ if (!tcti || !*tcti)+ tcti = "device:/dev/tpmrm0";+ n = strscpy(buf, tcti, size);+ explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */+ return n < 0 ? (int)n : 0;+}++int tpm_open(struct shirudo_tpm **out)+{+ char tcti[SHIRUDO_TPM_TCTI_MAX];+ struct shirudo_tpm *t;+ TSS2_RC rc;+ int err;/** Quiet libtss2's own stderr logging (it screams ERROR on the@@ -49,24 +68,20 @@ int tpm_open(struct shirudo_tpm **out)*/setenv("TSS2_LOG", "all+none", 0);- /*- * The env, then the file, then /dev/tpmrm0. An override is for VMs,- * CI and a software TPM; it cannot defeat the operator-side guarantee,- * since evidence is signed by the key the operator pinned.- */- tcti = getenv("SHIRUDO_TPM_TCTI");- if ((!tcti || !*tcti) && config_load(&cfg) == 0 &&- cfg.tpm_tcti[0])- tcti = cfg.tpm_tcti;- if (!tcti || !*tcti)- tcti = "device:/dev/tpmrm0";+ err = tpm_tcti(tcti, sizeof(tcti));+ if (err) {+ pr_err("tpm: the TCTI is longer than %d bytes",+ SHIRUDO_TPM_TCTI_MAX - 1);+ return err;+ }+ t = xcalloc(1, sizeof(*t));+ if (!t)+ return -ENOMEM;rc = Tss2_TctiLdr_Initialize(tcti, &t->tcti);- if (rc != TSS2_RC_SUCCESS)+ if (rc != TSS2_RC_SUCCESS) {pr_err("tpm: TctiLdr_Initialize(%s): rc=0x%08x", tcti,(__u32)rc);- explicit_bzero(&cfg, sizeof(cfg)); /* scrub tpm_owner_auth */- if (rc != TSS2_RC_SUCCESS) {xfree(t, sizeof(*t));return -EIO;}diff --git a/util/tpm.h b/util/tpm.hindex 08c08fd4..a5349835 100644--- a/util/tpm.h+++ b/util/tpm.h@@ -22,6 +22,7 @@struct shirudo_tpm;+int tpm_tcti(char *buf, size_t size);int tpm_open(struct shirudo_tpm **out);void tpm_close(struct shirudo_tpm *t);