eBPF-based security platform
config: Measure config activations into an NvPCR
KP Singhdc5d47141d73
diff --git a/Documentation/internals/config-images.rst b/Documentation/internals/config-images.rstindex 8533d83a..c8939c51 100644--- a/Documentation/internals/config-images.rst+++ b/Documentation/internals/config-images.rst@@ -77,7 +77,7 @@ file refuses none of the runtime image's keys.- ``config.image``, the initial config prefix;``config.verification_key``, one or more certificate paths under``/run/shirudo/runtime/``; ``config.pcr``, a PCR from 0 to 15- other than 9 and 11+ other than 9 and 11, or ``config.nvpcr``, the config NvPCR- ``tpm.tcti``, ``tpm.owner_auth``, ``tls.classical_hosts``. Any``runtime.*`` key is refused.diff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex 95ed13f3..6d4fd87c 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -579,6 +579,18 @@ register. The runtime image's bootstrap settings choose it with``config.pcr``: any static SHA-256 PCR from 0 to 15 except 9 and 11. Aconfig image cannot change the choice.+``config.nvpcr = shirudo-config`` chooses the config NvPCR instead, NV+index ``0x01818E04``, and leaves every PCR to its usual owner. systemd+defines the NvPCR in the initrd from a ``shirudo-config.nvpcr``+definition, before the guards attach, initializes it to+``SHA256(0^64)`` and anchors its name in PCR 9. Its write policy is+``PolicyOR`` of the UKI's signed PCR 11 policy under the ``initrd``+reference and ``PolicyNvWritten``; shirudo asserts the second branch,+with the first read from ``/run/systemd/nvpcr/shirudo-config.auth``,+after checking that the index is an initialized extend index writable+only under exactly that policy. The events and their digests are the+same; only the register differs.+Each event is ``SHA256("shirudo-config-root-v1\0" || kind ||root_hash)``, with the kind as a big-endian 32-bit number, 1 for the bootevent and 2 for a live update, and the 32-byte binary root hash. The@@ -595,6 +607,13 @@ event followed by updates in sequence for the quote's reset count, areplay from ``initial_pcr`` that reaches the quoted PCR value, and theexpected root as the selection. The restart count the history carries isnot compared.+With the config NvPCR (``config.nvpcr``) the evidence includes, instead of+the config PCR, a ``config_nvpcr`` object: the index's name and an+AK-signed ``TPM2_NV_Certify`` of its contents under the same nonce.+``verify`` requires that certification from the quote's TPM boot, a history+that starts at ``SHA256(0^64)``, and a replay that reaches the certified+contents; ``--expect-config-nvpcr=<name>`` pins the index's name, which the+platform computes from its PCR public key.Limits------@@ -631,11 +650,11 @@ The verifier checks what it is asked to check.initrd binding. ``--allow-runtime-events`` accepts any well-formedevents after boot.-The config PCR has no writer restriction.- Any process that can send ``TPM2_PCR_Extend`` can extend the config- PCR, and then every target command except ``shirudo status`` and- ``shirudo image status`` refuses until a reboot because the PCR no- longer replays.+A config PCR is a shared register.+ The guard reserves the configured PCR, but it is also a PCR other+ software may expect to own, and its value at the first event is+ whatever it held. The config NvPCR is Shirudo's alone and starts at a+ known value.PCR 11 needs a measured UKI boot.systemd measures the initrd phases only on a measured UKI boot.@@ -680,6 +699,9 @@ The policy is frozen during authenticated initialization:names.* Every other PCR stays the platform's. systemd's ``systemd-pcrmachine`` and``systemd-pcrfs`` extend PCR 15 as root, for example.+* The config NvPCR ``0x01818E04`` is Shirudo's: every mutation, extensions+ included, needs the full role. With it, the config PCR is free, so PCR 13+ is systemd-stub's again.* Hierarchy changes, clear, global write locks, shutdown and othermeasurement-affecting administration need the full role, as do unknown andvendor commands. ``TPM2_Startup`` stays available: systemd probes a TPM thatdiff --git a/Documentation/reference/cli/attest-verify.rst b/Documentation/reference/cli/attest-verify.rstindex b60b4ea8..89175f64 100644--- a/Documentation/reference/cli/attest-verify.rst+++ b/Documentation/reference/cli/attest-verify.rst@@ -25,7 +25,8 @@ Usage[--cert=<certificate>]... [--upatch=<file>]...[--expect=[java:]<name>=<digest>]... [--tree-root=<digest>][--expect-chain=<register>] [--allow-runtime-events]- [--expect-config-root=<root> --expect-config-pcr=<pcr>]+ [--expect-config-root=<root>+ (--expect-config-pcr=<pcr> | --expect-config-nvpcr=<name>)][--expect-pcr=<pcr>=<value>]...[--expect-bind-pcr=<pcr>=<value>]...[--strict-anchor] [--require-initrd] [--json]@@ -123,10 +124,14 @@ Options* - ``--expect-config-root``- ``<root>``, 64 hex characters- The approved config image root hash. Needs- ``--expect-config-pcr``.+ ``--expect-config-pcr`` or ``--expect-config-nvpcr``.* - ``--expect-config-pcr``- ``<pcr>``, 0 to 15, not 9 or 11- The PCR the config activation extends.+ * - ``--expect-config-nvpcr``+ - ``<name>``, 68 hex characters+ - The TPM name of the config NvPCR the config activation extends,+ in place of ``--expect-config-pcr``. The evidence must certify it.* - ``--expect-pcr``- ``<pcr>=<value>``, PCR 0 to 23- A PCR value the quote must carry. Repeatable.diff --git a/Documentation/reference/configuration.rst b/Documentation/reference/configuration.rstindex 2b271044..2b52d2a1 100644--- a/Documentation/reference/configuration.rst+++ b/Documentation/reference/configuration.rst@@ -112,8 +112,13 @@ Bootstrap keys* - ``config.pcr``- Decimal integer, 0 to 15, not 9 or 11- Allowed- - Required+ - Required, or ``config.nvpcr``- PCR that config activation extends+ * - ``config.nvpcr``+ - ``shirudo-config``+ - Allowed+ - Required, or ``config.pcr``+ - Extend the config NvPCR, NV index ``0x01818E04``, instead of a PCRThe runtime bootstrap's ``config.*`` values are the ones in effect: theroot file system's run of ``init`` and every later command read only thediff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 1d1a792b..8861abdf 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1647,7 +1647,10 @@ R4 Config PCR writers are not authenticatednothing but refusals. The Lean proofs of the activation that countextensions or explain a refusal assume neither(``Shirudo.ConfigActivation``). Detection: none reliable. Mitigation:- D1.+ D1. ``config.nvpcr``+ narrows it: the config NvPCR is extended only by the full role and+ starts at a known value, which the verifier requires; that an+ activation completed is still not proven.R5 The boot chain and process memory stay trusteddm-verity verifies runtime blocks as they are read. It does notdiff --git a/builtin-attest.c b/builtin-attest.cindex 5c620a02..d00f2dec 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -17,6 +17,7 @@#include <unistd.h>#include <shirudo/misc.h>+#include <shirudo/tpm.h>#include <shirudo/string.h>#include "builtin.h"@@ -77,6 +78,7 @@ enum verify_opt {VERIFY_EXPECT_CHAIN,VERIFY_EXPECT_CONFIG_ROOT,VERIFY_EXPECT_CONFIG_PCR,+ VERIFY_EXPECT_CONFIG_NVPCR,VERIFY_EXPECT_PCR,VERIFY_ALLOW_RUNTIME_EVENTS,VERIFY_JSON,@@ -108,6 +110,8 @@ static const struct opt_spec verify_opts[] = {"approved config image hash" },[VERIFY_EXPECT_CONFIG_PCR] = { "--expect-config-pcr", 0, 0, "<n>","reserved config PCR number" },+ [VERIFY_EXPECT_CONFIG_NVPCR] = { "--expect-config-nvpcr", 0, 0, "<name>",+ "the config NvPCR's TPM name" },[VERIFY_EXPECT_PCR] = { "--expect-pcr", 0, OPT_REPEAT, "<n>=<hex>","a PCR value the quote must carry (repeatable)" },[VERIFY_ALLOW_RUNTIME_EVENTS] = { "--allow-runtime-events", 0, 0, NULL,@@ -686,6 +690,15 @@ static int cmd_attest_verify(int argc, const char **argv)x.have_config_pcr = true;break;}+ case VERIFY_EXPECT_CONFIG_NVPCR:+ if (hex_decode(p.value, x.config_nvpcr_name, 34)) {+ opt_usage(&p, "--expect-config-nvpcr must be 68 hex characters");+ goto out;+ }+ x.config_pcr = TPM_CONFIG_NVPCR_INDEX;+ x.have_config_pcr = true;+ x.have_config_nvpcr_name = true;+ break;case VERIFY_EXPECT_CHAIN:if (parse_hex32("--expect-chain", p.value, x.chain))goto out;@@ -756,8 +769,15 @@ static int cmd_attest_verify(int argc, const char **argv)goto out;}}+ if (opt_given(&p, VERIFY_EXPECT_CONFIG_PCR) &&+ opt_given(&p, VERIFY_EXPECT_CONFIG_NVPCR)) {+ opt_usage(&p, "--expect-config-pcr and --expect-config-nvpcr "+ "exclude each other");+ goto out;+ }if (x.have_config_root && !x.have_config_pcr) {- opt_usage(&p, "--expect-config-root requires --expect-config-pcr");+ opt_usage(&p, "--expect-config-root requires --expect-config-pcr "+ "or --expect-config-nvpcr");goto out;}if (!evidence_path || !have_nonce) {diff --git a/include/shirudo/tpm.h b/include/shirudo/tpm.hindex 181c0abf..7569b723 100644--- a/include/shirudo/tpm.h+++ b/include/shirudo/tpm.h@@ -7,5 +7,11 @@#define TPM_NAME_LEN 34 /* nameAlg and a SHA-256 */#define TPM_CC_NV_EXTEND 0x00000136u#define TPM_NV_RSA_PUBLIC_AREA_LEN (24 + TPM_NV_RSA_BYTES)+/*+ * The config NvPCR: systemd defines it in the initrd from its .nvpcr file,+ * before the guards attach, and only Shirudo extends it afterwards.+ */+#define TPM_CONFIG_NVPCR_INDEX 0x01818e04u+#define TPM_CONFIG_NVPCR_NAME "shirudo-config"#endif /* SHIRUDO_TPM_TYPES_H */diff --git a/include/shirudo/tpm_command.h b/include/shirudo/tpm_command.hindex 8598b44f..26003d07 100644--- a/include/shirudo/tpm_command.h+++ b/include/shirudo/tpm_command.h@@ -50,8 +50,11 @@ static __always_inline bool shirudo_tpm_nvpcr(__u32 index)static __always_inline enum shirudo_tpm_accessshirudo_tpm_nv(__u32 index, bool extend){- /* 0x01818e00 is Shirudo's measurement register (NV_INDEX). */- if (index == 0x01818e00u)+ /*+ * 0x01818e00 is Shirudo's measurement register (NV_INDEX), 0x01818e04+ * the config NvPCR: its extensions are config events.+ */+ if (index == 0x01818e00u || index == 0x01818e04u)return SHIRUDO_TPM_RUNTIME;if (shirudo_tpm_nvpcr(index) && !extend)return SHIRUDO_TPM_RUNTIME;diff --git a/testing/functional/image/t5006-image-bootstrap.sh b/testing/functional/image/t5006-image-bootstrap.shindex 4643cd39..a8155b05 100755--- a/testing/functional/image/t5006-image-bootstrap.sh+++ b/testing/functional/image/t5006-image-bootstrap.sh@@ -108,6 +108,17 @@ test_expect_success 'config PCR rejects boot PCRs and resettable PCRs' 'done'+test_expect_success 'the config NvPCR replaces the config PCR, as the runtime image says' '+ cp "$WORK/runtime.conf" "$WORK/nvpcr.conf" &&+ printf "config.nvpcr = shirudo-config\n" >>"$WORK/nvpcr.conf" &&+ "$H" bootstrap "$WORK/initrd.conf" "$WORK/nvpcr.conf" >"$WORK/result" &&+ grep -qx "pcr=25267716" "$WORK/result" &&+ for value in other shirudo-config- 0x01818e04 ""; do+ printf "config.nvpcr = %s\n" "$value" >"$WORK/bad.conf" &&+ ! "$H" bootstrap "$WORK/initrd.conf" "$WORK/bad.conf" || exit 1+ done+'+test_expect_success 'unknown image settings and malformed lines are rejected' 'for setting in "runtime.unknown = value" "config.unknown = value" "missing equals"; doprintf "%s\n" "$setting" >"$WORK/bad.conf" &&diff --git a/testing/functional/tpm/t4009-config-nvpcr.sh b/testing/functional/tpm/t4009-config-nvpcr.shnew file mode 100755index 00000000..813dce1c--- /dev/null+++ b/testing/functional/tpm/t4009-config-nvpcr.sh@@ -0,0 +1,68 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++cd "$(dirname "$0")/../.."+. ./test-lib.sh++H=${SHIRUDO_TEST_CONFIG_ATTEST_HELPER:-$(pwd)/helper/test-config-attest}+[ -x "$H" ] || test_skip_all "test-config-attest helper not built"+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT HUP INT TERM+NVPCR=25267716+ROOT_A=$(python3 -c 'print("41" * 32)')+ROOT_B=$(python3 -c 'print("42" * 32)')++# A history in the config NvPCR (0x01818e04): it starts at the value systemd's+# initializing write leaves, SHA256 of 64 zero bytes, and the verifier checks+# it against the certified NvPCR value instead of a quoted PCR.+python3 - "$WORK" "$NVPCR" <<'PY'+import hashlib, json, sys+from pathlib import Path+root, index = Path(sys.argv[1]), int(sys.argv[2])+sha = lambda data: hashlib.sha256(data).digest()++def event(kind, root_hash):+ return sha(b"shirudo-config-root-v1\0" + kind.to_bytes(4, "big") + bytes.fromhex(root_hash))++def document(initial, events, pcr=index):+ value = bytes.fromhex(initial)+ for e in events:+ value = sha(value + event(e["kind"], e["root_hash"]))+ return {"config": {"schema": 1, "pcr": pcr, "reset_count": 7, "restart_count": 0,+ "initial_pcr": initial, "events": events}}, value.hex()++events = [{"sequence": 1, "kind": 1, "root_hash": "41" * 32},+ {"sequence": 2, "kind": 2, "root_hash": "42" * 32}]+initial = sha(bytes(64)).hex()+for name, (doc, value) in {"good": document(initial, events),+ "zero-start": document("00" * 32, events)}.items():+ (root / (name + ".json")).write_text(json.dumps(doc))+ (root / (name + ".value")).write_text(value)+PY+GOOD=$(cat "$WORK/good.value")++test_expect_success 'a config NvPCR history verifies against the certified NvPCR' '+ "$H" replay "$WORK/good.json" "$ROOT_B" "$NVPCR" 7 0 "$GOOD" >"$WORK/out" &&+ grep -qx "reason=config root history matches the certified NvPCR" "$WORK/out"+'++test_expect_success 'a certified value the history does not reach is refused' '+ ! "$H" replay "$WORK/good.json" "$ROOT_B" "$NVPCR" 7 0 "$(printf %064d 5)" >"$WORK/out" &&+ grep -qx "reason=config history does not match the certified NvPCR" "$WORK/out"+'++test_expect_success 'a history that chooses its own start is refused' '+ ! "$H" replay "$WORK/zero-start.json" "$ROOT_B" "$NVPCR" 7 0 \+ "$(cat "$WORK/zero-start.value")" >"$WORK/out" &&+ grep -qx "reason=config history does not start at the NvPCR'"'"'s initial value" "$WORK/out"+'++test_expect_success 'an NvPCR history is not a PCR 15 history, nor approved for another root' '+ ! "$H" replay "$WORK/good.json" "$ROOT_B" 15 7 0 "$GOOD" >/dev/null &&+ ! "$H" replay "$WORK/good.json" "$ROOT_A" "$NVPCR" 7 0 "$GOOD" >/dev/null &&+ ! "$H" replay "$WORK/good.json" "$ROOT_B" "$NVPCR" 8 0 "$GOOD" >/dev/null+'++test_donediff --git a/testing/helper/test-config-attest.c b/testing/helper/test-config-attest.cindex cbda3c66..592f2604 100644--- a/testing/helper/test-config-attest.c+++ b/testing/helper/test-config-attest.c@@ -128,12 +128,13 @@ static int read_document(const char *path, char **out, size_t *length)int main(int argc, char **argv){struct config_evidence config = {};- __u8 pcrs[24][32] = {}, root[32];+ __u8 pcrs[24][32] = {}, root[32], certified[32];const char *reason = NULL;const __u8 *expected;__u32 mask, reset;char *document = NULL;size_t length = 0;+ bool nvpcr;int pcr, ret;if (argc == 4 && !strcmp(argv[1], "render")) {@@ -150,19 +151,22 @@ int main(int argc, char **argv)if (ret)goto out;if (argc == 8 && !strcmp(argv[1], "replay")) {+ /* The last argument is the quoted PCR or the certified NvPCR. */pcr = !strcmp(argv[4], "-") ? -1 : atoi(argv[4]);- if (config.pcr >= 24 ||+ nvpcr = config_measure_nvpcr(config.pcr);+ if ((!nvpcr && config.pcr >= 24) ||(!strcmp(argv[3], "-") ?0 :hex_decode(argv[3], root, 32)) ||- hex_decode(argv[7], pcrs[config.pcr], 32)) {+ hex_decode(argv[7], nvpcr ? certified : pcrs[config.pcr], 32)) {ret = -EINVAL;goto out;}- mask = 1u << (pcr < 0 ? config.pcr : (__u32)pcr);+ mask = nvpcr ? 0 : 1u << (pcr < 0 ? config.pcr : (__u32)pcr);reset = strtoul(argv[5], NULL, 10);expected = !strcmp(argv[3], "-") ? NULL : root;ret = config_evidence_verify(&config, mask, reset, pcrs,+ nvpcr ? certified : NULL,expected, pcr, &reason);} else if (strcmp(argv[1], "parse")) {ret = -EINVAL;diff --git a/testing/helper/test-tpm-command.c b/testing/helper/test-tpm-command.cindex 046bf37a..bbc9a1d5 100644--- a/testing/helper/test-tpm-command.c+++ b/testing/helper/test-tpm-command.c@@ -65,6 +65,17 @@ int main(void)access = shirudo_tpm_access(command, 18, 13);failures += check(access, SHIRUDO_TPM_RUNTIME, "register NV mutation");failures += check_runtime_only(access, "register NV mutation");+ /* The config NvPCR: every mutation, extension included. */+ put32(command + 14, 0x01818e04);+ access = shirudo_tpm_access(command, 18, 0x01818e04);+ failures += check(access, SHIRUDO_TPM_RUNTIME, "config NvPCR mutation");+ failures += check_runtime_only(access, "config NvPCR mutation");+ failures += check(shirudo_tpm_access(command, 18, 13),+ SHIRUDO_TPM_RUNTIME,+ "config NvPCR stays reserved with a config PCR");+ put32(command + 14, 0x01818e05);+ failures += check(shirudo_tpm_access(command, 18, 0x01818e04),+ SHIRUDO_TPM_PUBLIC, "NV mutation past the reserved block");put32(command + 14, 0x01500000);failures += check(shirudo_tpm_access(command, 18, 13),SHIRUDO_TPM_PUBLIC, "unrelated NV mutation");@@ -112,6 +123,9 @@ int main(void)put32(command + 10, 13);failures += check(shirudo_tpm_access(command, 14, 16),SHIRUDO_TPM_PUBLIC, "PCR 13 with another config PCR");+ /* With the config NvPCR, PCR 13 is systemd-stub's again. */+ failures += check(shirudo_tpm_access(command, 14, 0x01818e04),+ SHIRUDO_TPM_PUBLIC, "PCR 13 with the config NvPCR");}packet(command, 0x11f, 18);put32(command + 10, 0x01818e00);@@ -134,6 +148,9 @@ int main(void)put32(command + 18, 0x01d10202);failures += check(shirudo_tpm_access(command, 32, 13),SHIRUDO_TPM_RUNTIME, "systemd NvPCR redefinition");+ put32(command + 18, 0x01818e04);+ failures += check(shirudo_tpm_access(command, 32, 0x01818e04),+ SHIRUDO_TPM_RUNTIME, "config NvPCR redefinition");packet(command, 0x12a, 45);command[1] = 2;put32(command + 10, 0x4000000c);diff --git a/util/attest.c b/util/attest.cindex 6021029a..2490ed67 100644--- a/util/attest.c+++ b/util/attest.c@@ -25,6 +25,7 @@#include "chain.h"#include "crypto.h"#include "config_attest.h"+#include "config_measure.h"#include "eventlog.h"#include "hex.h"#include "io.h"@@ -165,9 +166,10 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,struct tpm_key ak = { .handle = 0 }, ek = { .handle = 0 };struct tpm_key ak_copy = {}, ek_copy = {};struct tpm_attestation quote = {}, certify = {};+ struct tpm_attestation config_certify = {};struct config_evidence config = {};- struct tpm_attest quoted;- struct tpm_nv_info nv;+ struct tpm_attest quoted, certified;+ struct tpm_nv_info nv, config_nv = {};struct tpm *tpm = NULL;struct json_emit e;__u8 values[24][32] = {};@@ -178,7 +180,7 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,char ts[32], boot_id[64], nv_index[16], nonce_hex[2 * 64 + 1];struct stat st;__u32 i, attempt;- bool ek_cert_rsa = false, ek_rsa;+ bool ek_cert_rsa = false, ek_rsa, nvpcr;int ret;*out = NULL;@@ -191,7 +193,8 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,ret = config_evidence_collect(update, &config);if (ret)return ret;- if (config.present)+ nvpcr = config.present && config_measure_nvpcr(config.pcr);+ if (config.present && !nvpcr)pcr_mask |= 1u << config.pcr;ret = tpm_open(&tpm);if (ret) {@@ -235,11 +238,29 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,(tpm_attest_parse(quote.attest, quote.attest_len,"ed) ||quoted.reset_count != config.reset_count ||- memcmp(values[config.pcr],- config.value, 32))) {+ (!nvpcr && memcmp(values[config.pcr],+ config.value, 32)))) {ret = -EUCLEAN;goto out;}+ /* The quote does not cover an NV index: certify the config NvPCR. */+ if (nvpcr) {+ ret = tpm_nv_info(tpm, config.pcr, &config_nv);+ if (!ret)+ ret = tpm_nv_certify(tpm, &ak, config.pcr, nonce,+ nonce_len, &config_certify);+ if (ret)+ goto out;+ if (tpm_attest_parse(config_certify.attest,+ config_certify.attest_len, &certified) ||+ certified.reset_count != config.reset_count ||+ certified.restart_count != config.restart_count ||+ certified.offset != 0 || certified.nv_len != 32 ||+ memcmp(certified.nv_data, config.value, 32)) {+ ret = -EUCLEAN;+ goto out;+ }+ }ret = tpm_nv_certify(tpm, &ak, NV_INDEX, nonce,nonce_len, &certify);if (ret)@@ -364,6 +385,15 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,emit_hex(&e, "attest", certify.attest, certify.attest_len);emit_hex(&e, "signature", certify.signature, certify.signature_len);json_obj_close(&e);+ if (nvpcr) {+ json_obj_open_key(&e, "config_nvpcr");+ emit_hex(&e, "name", config_nv.name, config_nv.name_len);+ emit_hex(&e, "attest", config_certify.attest,+ config_certify.attest_len);+ emit_hex(&e, "signature", config_certify.signature,+ config_certify.signature_len);+ json_obj_close(&e);+ }json_arr_open(&e, "eventlog");/** A boot without an init record has no record to carry, which@@ -461,6 +491,9 @@ struct init_record {struct parse {char writer_public_hex[2 * TPM_NV_RSA_BYTES + 1];+ char nvpcr_name_hex[69];+ char nvpcr_attest_hex[2 * TPM_ATTEST_MAX + 1];+ char nvpcr_sig_hex[2 * TPM_SIGNATURE_MAX + 1];char nonce_hex[2 * ATTEST_NONCE_MAX + 1];__s64 pcr_mask;char ak_public_hex[2 * TPM_PUBLIC_MAX + 1];@@ -722,14 +755,17 @@ struct verify {struct config_evidence config;bool have_ek;bool have_cred;+ bool have_nvpcr;+ bool have_nvpcr_value;+ __u8 nvpcr_value[32];__u8 nonce[ATTEST_NONCE_MAX];size_t nonce_len;struct tpm_ecc_public ak_pub;__u8 ak_name[TPM_NAME_LEN];/** check_signed() decodes each attestation here, the register's- * over the quote's: certify's pointers stay valid, the quote's do- * not.+ * over the quote's and the config NvPCR's over the register's: the+ * pointers of only the last one decoded stay valid.*/__u8 attest[TPM_ATTEST_MAX];struct tpm_attest quote;@@ -825,6 +861,14 @@ static int evidence_parse(struct verify *v, const char *evidence, size_t len){ "signature", JSON_T_STRING, p->reg_sig_hex,sizeof(p->reg_sig_hex), 0, NULL, 0 },};+ struct json_field nvpcr_sub[] = {+ { "name", JSON_T_STRING, p->nvpcr_name_hex,+ sizeof(p->nvpcr_name_hex), 0, NULL, 0 },+ { "attest", JSON_T_STRING, p->nvpcr_attest_hex,+ sizeof(p->nvpcr_attest_hex), 0, NULL, 0 },+ { "signature", JSON_T_STRING, p->nvpcr_sig_hex,+ sizeof(p->nvpcr_sig_hex), 0, NULL, 0 },+ };struct json_field upatch_sub[] = {{ "name", JSON_T_STRING, p->upatch_name,sizeof(p->upatch_name), 0, NULL, 0 },@@ -907,6 +951,8 @@ static int evidence_parse(struct verify *v, const char *evidence, size_t len)ARRAY_SIZE(ek_sub) },{ "credential", JSON_T_OBJECT, NULL, 0, 0,cred_sub, ARRAY_SIZE(cred_sub) },+ { "config_nvpcr", JSON_T_OBJECT, NULL, 0, 0,+ nvpcr_sub, ARRAY_SIZE(nvpcr_sub) },};int ret;@@ -920,6 +966,10 @@ static int evidence_parse(struct verify *v, const char *evidence, size_t len)return -EINVAL;v->have_ek = ek_sub[0].seen && ek_sub[1].seen && ek_sub[2].seen;v->have_cred = cred_sub[0].seen;+ v->have_nvpcr = top[ARRAY_SIZE(top) - 1].seen;+ if (v->have_nvpcr && (!nvpcr_sub[0].seen || !nvpcr_sub[1].seen ||+ !nvpcr_sub[2].seen))+ return -EINVAL;return 0;}@@ -1129,6 +1179,53 @@ static int verify_register(struct verify *v)return 0;}+/* The quote does not cover an NV index: the config NvPCR is certified. */+static int verify_config_nvpcr(struct verify *v)+{+ const struct attest_expect *expect = v->expect;+ const struct tpm_attest *quote = &v->quote;+ struct reporter *rep = &v->rep;+ struct tpm_attest nvpcr = {};+ struct parse *p = v->p;+ __u8 name[34];+ bool contents;+ int ret;++ if (!v->have_nvpcr) {+ if (expect->have_config_nvpcr_name)+ say(rep, false, "config NvPCR: certification missing");+ return 0;+ }+ if (hex_decode(p->nvpcr_name_hex, name, sizeof(name)))+ return -EINVAL;+ ret = check_signed(v, "config NvPCR", p->nvpcr_attest_hex,+ p->nvpcr_sig_hex, &nvpcr);+ if (ret == -EINVAL)+ return ret;+ contents = !ret && nvpcr.type == TPM_ST_ATTEST_NV &&+ nvpcr.index_name_len == sizeof(name) &&+ !memneq_explicit(nvpcr.index_name, name, sizeof(name)) &&+ nvpcr.offset == 0 && nvpcr.nv_len == 32;+ say(rep, contents, "config NvPCR: certified name and contents");+ say(rep, !ret && !v->qrc &&+ nvpcr.reset_count == quote->reset_count &&+ nvpcr.restart_count == quote->restart_count &&+ nvpcr.firmware_version == quote->firmware_version,+ "config NvPCR: same TPM boot as quote");+ if (expect->have_config_nvpcr_name)+ say(rep, !memneq_explicit(name, expect->config_nvpcr_name,+ sizeof(name)),+ "config NvPCR: the operator-pinned index");+ else+ note(rep, "config NvPCR has no operator pin; use "+ "--expect-config-nvpcr");+ if (contents) {+ memcpy(v->nvpcr_value, nvpcr.nv_data, 32);+ v->have_nvpcr_value = true;+ }+ return 0;+}+static void verify_config(struct verify *v){const struct attest_expect *expect = v->expect;@@ -1145,8 +1242,9 @@ static void verify_config(struct verify *v)pcr = expect->have_config_pcr ? (int)expect->config_pcr : -1;ret = config_evidence_verify(&v->config, quote->pcr_mask,quote->reset_count,- v->p->pcrs, root, pcr,- &reason);+ v->p->pcrs,+ v->have_nvpcr_value ? v->nvpcr_value : NULL,+ root, pcr, &reason);say(rep, !ret, "config: %s", reason);if (v->config.present && !expect->have_config_root)note(rep, "config root has no operator approval; use "@@ -1418,6 +1516,9 @@ static int verify_document(struct verify *v, const char *evidence,if (ret)return ret;ret = verify_register(v);+ if (ret)+ return ret;+ ret = verify_config_nvpcr(v);if (ret)return ret;verify_config(v);diff --git a/util/attest.h b/util/attest.hindex 38e7cb98..770bba2b 100644--- a/util/attest.h+++ b/util/attest.h@@ -99,6 +99,8 @@ struct attest_expect {__u8 config_root[32];bool have_config_pcr;__u32 config_pcr;+ bool have_config_nvpcr_name;+ __u8 config_nvpcr_name[34];};/*diff --git a/util/config_attest.c b/util/config_attest.cindex 579dd915..13371d0d 100644--- a/util/config_attest.c+++ b/util/config_attest.c@@ -240,7 +240,7 @@ int config_evidence_parse(const char *evidence, size_t length,goto fail;}if (schema != 1 || reset < 0 || reset > UINT32_MAX || restart < 0 ||- restart > UINT32_MAX || pcr < 0 || pcr > 15 ||+ restart > UINT32_MAX || pcr < 0 || pcr > UINT32_MAX ||config_measure_pcr_valid(pcr) || strlen(initial) != 64 ||hex_decode(initial, out->initial_pcr, 32)) {ret = -EINVAL;@@ -262,10 +262,12 @@ fail:int config_evidence_verify(const struct config_evidence *view,__u32 quote_mask, __u32 reset,const __u8 pcrs[24][32],+ const __u8 *nvpcr_value,const __u8 *expected_root,int expected_pcr, const char **reason){- __u8 value[32];+ __u8 value[32], initial[32];+ const __u8 *live;int ret;*reason = "config root history matches the quoted PCR";@@ -274,11 +276,35 @@ int config_evidence_verify(const struct config_evidence *view,return expected_root || expected_pcr >= 0 ? -EKEYREJECTED : 0;}if (config_measure_pcr_valid(view->pcr) ||- !(quote_mask & (1u << view->pcr)) ||(expected_pcr >= 0 && view->pcr != (__u32)expected_pcr)) {*reason = "config PCR is unquoted or unexpected";return -EKEYREJECTED;}+ if (config_measure_nvpcr(view->pcr)) {+ *reason = "config root history matches the certified NvPCR";+ if (!nvpcr_value) {+ *reason = "config NvPCR is not certified";+ return -EKEYREJECTED;+ }+ /*+ * The NvPCR holds a known value after systemd initializes it,+ * so the history cannot choose where it starts.+ */+ ret = config_measure_nvpcr_initial(initial);+ if (ret)+ return ret;+ if (memcmp(view->initial_pcr, initial, 32)) {+ *reason = "config history does not start at the NvPCR's initial value";+ return -EKEYREJECTED;+ }+ live = nvpcr_value;+ } else {+ if (!(quote_mask & (1u << view->pcr))) {+ *reason = "config PCR is unquoted or unexpected";+ return -EKEYREJECTED;+ }+ live = pcrs[view->pcr];+ }if (view->reset_count != reset) {*reason = "config history belongs to another TPM boot";return -EKEYREJECTED;@@ -288,8 +314,10 @@ int config_evidence_verify(const struct config_evidence *view,return -EKEYREJECTED;}ret = replay(view, value);- if (ret || memcmp(value, pcrs[view->pcr], 32)) {- *reason = "config history does not match the quoted PCR";+ if (ret || memcmp(value, live, 32)) {+ *reason = config_measure_nvpcr(view->pcr) ?+ "config history does not match the certified NvPCR" :+ "config history does not match the quoted PCR";return ret ? ret : -EKEYREJECTED;}if (expected_root && memcmp(view->events[view->n_events - 1].root_hash,diff --git a/util/config_attest.h b/util/config_attest.hindex d905c509..01d02398 100644--- a/util/config_attest.h+++ b/util/config_attest.h@@ -44,6 +44,7 @@ int config_evidence_parse(const char *evidence, size_t length,int config_evidence_verify(const struct config_evidence *view,__u32 quote_mask, __u32 reset,const __u8 pcrs[24][32],+ const __u8 *nvpcr_value,const __u8 *expected_root,int expected_pcr, const char **reason);diff --git a/util/config_measure.c b/util/config_measure.cindex b93ed3ab..850a6260 100644--- a/util/config_measure.c+++ b/util/config_measure.c@@ -2,6 +2,7 @@/* Copyright (c) 2026 Authors of shirudo */#include <shirudo/misc.h>+#include <shirudo/tpm.h>#include <errno.h>#include <string.h>@@ -9,9 +10,24 @@#include "config_measure.h"#include "crypto.h"+/* A config register is a static PCR or the config NvPCR's index. */int config_measure_pcr_valid(__u32 pcr){- return pcr < 16 && pcr != 9 && pcr != 11 ? 0 : -EINVAL;+ return (pcr < 16 && pcr != 9 && pcr != 11) ||+ pcr == TPM_CONFIG_NVPCR_INDEX ? 0 : -EINVAL;+}++bool config_measure_nvpcr(__u32 pcr)+{+ return pcr == TPM_CONFIG_NVPCR_INDEX;+}++/* systemd's unlogged initializing write extends a zero digest. */+int config_measure_nvpcr_initial(__u8 out[32])+{+ __u8 zero[64] = {};++ return sha256_buffer(zero, sizeof(zero), out);}int config_measure_event(enum config_event_kind kind,diff --git a/util/config_measure.h b/util/config_measure.hindex 330bc43f..dc0f82d6 100644--- a/util/config_measure.h+++ b/util/config_measure.h@@ -6,11 +6,14 @@#include <shirudo/misc.h>+#include <stdbool.h>#include <stdint.h>#include "config_update.h"int config_measure_pcr_valid(__u32 pcr);+bool config_measure_nvpcr(__u32 pcr);+int config_measure_nvpcr_initial(__u8 out[32]);int config_measure_event(enum config_event_kind kind,const __u8 root_hash[32], __u8 out[32]);diff --git a/util/config_update.c b/util/config_update.cindex a3db3d0f..89e80836 100644--- a/util/config_update.c+++ b/util/config_update.c@@ -105,7 +105,7 @@ static int record_decode(const char *data, size_t length, bool state,return -EBADMSG;if (schema != 1 || sequence <= 0 || reset < 0 ||reset > UINT32_MAX || restart < 0 || restart > UINT32_MAX ||- pcr < 0 || pcr > 15 || config_measure_pcr_valid(pcr) ||+ pcr < 0 || pcr > UINT32_MAX || config_measure_pcr_valid(pcr) ||(kind != CONFIG_EVENT_BOOT &&kind != CONFIG_EVENT_UPDATE) ||hex_decode(root, record->target.root_hash, 32) ||diff --git a/util/image_bootstrap.c b/util/image_bootstrap.cindex 4424e1de..8ae56fa3 100644--- a/util/image_bootstrap.c+++ b/util/image_bootstrap.c@@ -3,6 +3,7 @@#include <shirudo/misc.h>#include <shirudo/string.h>+#include <shirudo/tpm.h>#include <errno.h>#include <fcntl.h>@@ -118,6 +119,13 @@ static int bootstrap_parse(FILE *file, struct image_bootstrap *boot,boot->pcr = pcr;boot->have_pcr = true;}+ } else if (!strcmp(key, "config.nvpcr")) {+ if (strcmp(value, TPM_CONFIG_NVPCR_NAME)) {+ ret = -EINVAL;+ } else {+ boot->pcr = TPM_CONFIG_NVPCR_INDEX;+ boot->have_pcr = true;+ }} else if (str_has_prefix(key, "config.") ||str_has_prefix(key, "runtime.")) {ret = -EINVAL;diff --git a/util/image_runtime.c b/util/image_runtime.cindex 4664f312..7f23fbd3 100644--- a/util/image_runtime.c+++ b/util/image_runtime.c@@ -28,6 +28,7 @@#include "../builtin.h"#include "config.h"#include "config_image.h"+#include "config_measure.h"#include "config_update.h"#include "crypto.h"#include "dm_verity.h"@@ -500,14 +501,48 @@ static int pcr_read(void *ctx, __u32 pcr, __u8 out[32]){struct image_context *current = ctx;+ if (config_measure_nvpcr(pcr))+ return tpm_nv_read(current->tpm, pcr, out);return tpm_pcr_read(current->tpm, pcr, out);}+/*+ * The branch is read from systemd's record and only selects which policy+ * the session asserts: the TPM, and tpm_nvpcr_extend() against the index's+ * own policy, decide whether it is the right one.+ */+static int nvpcr_authorize(__u8 out[32])+{+ char *data = NULL, hex[65] = {};+ size_t length = 0;+ int ret;++ ret = read_file(CONFIG_NVPCR_AUTH_PATH, 65, &data, &length);+ if (ret)+ return ret;+ if ((length == 64 || (length == 65 && data[64] == '\n')))+ memcpy(hex, data, 64);+ xfree(data, length + 1);+ if (strlen(hex) != 64 || hex_decode(hex, out, 32))+ return -EBADMSG;+ return 0;+}+static int pcr_extend(void *ctx, __u32 pcr, const __u8 event[32]){struct image_context *current = ctx;+ __u8 authorize[32];+ int ret;- return tpm_pcr_extend(current->tpm, pcr, event);+ if (!config_measure_nvpcr(pcr))+ return tpm_pcr_extend(current->tpm, pcr, event);+ ret = nvpcr_authorize(authorize);+ if (ret) {+ pr_err("image: config NvPCR policy branch %s: %s",+ CONFIG_NVPCR_AUTH_PATH, strerror(-ret));+ return ret;+ }+ return tpm_nvpcr_extend(current->tpm, pcr, authorize, event);}static int collect_mapping(const char *name, dev_t device, void *ctx)diff --git a/util/paths.h b/util/paths.hindex 86e006b3..b590b1cd 100644--- a/util/paths.h+++ b/util/paths.h@@ -157,6 +157,11 @@*/#ifndef NV_INDEX#define NV_INDEX 0x01818E00u+/*+ * The PCR-key branch of the config NvPCR's write policy, which+ * systemd-tpm2-setup records when it initializes the NvPCR.+ */+#define CONFIG_NVPCR_AUTH_PATH "/run/systemd/nvpcr/shirudo-config.auth"#endif/* Site config. */diff --git a/util/tpm.c b/util/tpm.cindex ace134b8..ef140b6e 100644--- a/util/tpm.c+++ b/util/tpm.c@@ -891,6 +891,77 @@ int tpm_nv_extend_first(struct tpm *t, __u32 index,return err;}+/* PPWRITE, OWNERWRITE, AUTHWRITE; POLICYWRITE; the TPM_NT field. */+#define NVPCR_WRITE_AUTH 0x00000007u+#define NVPCR_POLICY_WRITE 0x00000008u+#define NVPCR_TYPE_MASK 0x000000f0u+#define NVPCR_TYPE_EXTEND 0x00000040u++/*+ * Extend a systemd NvPCR after systemd initialized it. Its write policy is+ * PolicyOR(authorize, PolicyNvWritten(YES)), authorize being the branch+ * signed by the UKI's PCR key that systemd records for runtime extends. The+ * index must be an initialized extend index writable only under exactly+ * that policy.+ */+int tpm_nvpcr_extend(struct tpm *t, __u32 index,+ const __u8 authorize[TPM_DIGEST_LEN],+ const __u8 event[TPM_DIGEST_LEN])+{+ __u8 zero[TPM_DIGEST_LEN] = {}, written[TPM_DIGEST_LEN];+ TPML_DIGEST branches = { .count = 2 };+ ESYS_TR session = ESYS_TR_NONE;+ __u8 policy[TPM_DIGEST_LEN];+ TPM2B_DIGEST *digest = NULL;+ struct tpm_nv_info nv;+ TSS2_RC ret;+ int err;++ err = tpm_nv_info(t, index, &nv);+ if (!err)+ err = tpm_policy_nv_written(zero, true, written);+ if (!err)+ err = tpm_policy_or(authorize, written, policy);+ if (err)+ return err;+ if (!nv.defined || !nv.written || nv.data_size != TPM_DIGEST_LEN ||+ (nv.attrs & NVPCR_WRITE_AUTH) || !(nv.attrs & NVPCR_POLICY_WRITE) ||+ (nv.attrs & NVPCR_TYPE_MASK) != NVPCR_TYPE_EXTEND ||+ memneq_explicit(nv.policy, policy, sizeof(policy))) {+ pr_err("tpm: 0x%08x is not the initialized NvPCR its write "+ "policy names", index);+ return -EACCES;+ }+ err = tpm_policy_session_start(t, &session);+ if (err)+ return err;+ ret = Esys_PolicyNvWritten(t->esys, session, ESYS_TR_NONE, ESYS_TR_NONE,+ ESYS_TR_NONE, TPM2_YES);+ if (ret == TSS2_RC_SUCCESS) {+ branches.digests[0].size = TPM_DIGEST_LEN;+ memcpy(branches.digests[0].buffer, authorize, TPM_DIGEST_LEN);+ branches.digests[1].size = TPM_DIGEST_LEN;+ memcpy(branches.digests[1].buffer, written, TPM_DIGEST_LEN);+ ret = Esys_PolicyOR(t->esys, session, ESYS_TR_NONE,+ ESYS_TR_NONE, ESYS_TR_NONE, &branches);+ }+ if (ret == TSS2_RC_SUCCESS)+ ret = Esys_PolicyGetDigest(t->esys, session, t->session,+ ESYS_TR_NONE, ESYS_TR_NONE, &digest);+ if (ret != TSS2_RC_SUCCESS || !digest ||+ digest->size != TPM_DIGEST_LEN ||+ memcmp(digest->buffer, policy, TPM_DIGEST_LEN)) {+ pr_err("tpm: NvPCR 0x%08x policy assertion: rc=0x%08x", index,+ (__u32)ret);+ err = -EACCES;+ }+ Esys_Free(digest);+ if (!err)+ err = nv_extend_session(t, index, session, event);+ session_flush(t, session);+ return err;+}+int tpm_nv_extend(struct tpm *t, __u32 index,const __u8 branch_written[32],const __u8 branch_init[32],diff --git a/util/tpm.h b/util/tpm.hindex 689a3021..be364949 100644--- a/util/tpm.h+++ b/util/tpm.h@@ -77,6 +77,10 @@ int tpm_nv_define(struct tpm *t, __u32 index,const void *owner_auth, size_t owner_auth_len);int tpm_nv_undefine(struct tpm *t, __u32 index,const void *owner_auth, size_t owner_auth_len);+/* Extend an initialized systemd NvPCR; authorize is its PCR-key branch. */+int tpm_nvpcr_extend(struct tpm *t, __u32 index,+ const __u8 authorize[TPM_DIGEST_LEN],+ const __u8 event[TPM_DIGEST_LEN]);/* -ENOENT: not defined; -ENODATA: defined but not written this boot. */int tpm_nv_read(struct tpm *t, __u32 index,__u8 out[TPM_DIGEST_LEN]);