eBPF-based security platform
chain: Log the register's PCR 9 anchor in systemd's measurement log
KP Singh0735ebbe5ae4
diff --git a/Documentation/concepts/background/tpm.rst b/Documentation/concepts/background/tpm.rstindex a17ac7f8..d69afb1d 100644--- a/Documentation/concepts/background/tpm.rst+++ b/Documentation/concepts/background/tpm.rst@@ -338,7 +338,9 @@ register from the initrd.4. Record the :term:`anchor`. ``init`` extends PCR 9 with the SHA-256 of``nvpcr-init:shirudo:0x1818e00:<written-name>``, systemd's anchorformat, where ``<written-name>`` is the index's written Name in- lowercase ASCII hex.+ lowercase ASCII hex. The same record goes into systemd's userspace+ measurement log, ``/run/log/systemd/tpm2-measure.log``, as an+ ``nvpcr-init`` event, before systemd measures ``nvpcr-separator``.The Name contains the two-byte SHA-256 algorithm identifier ``0x000B``followed by the SHA-256 hash of the index's public fields. Those fieldsdiff --git a/Documentation/internals/measurement-register.rst b/Documentation/internals/measurement-register.rstindex 415636a7..95ed13f3 100644--- a/Documentation/internals/measurement-register.rst+++ b/Documentation/internals/measurement-register.rst@@ -197,7 +197,16 @@ The binding takes these steps:software policy before the extension.#. Extend PCR 9 with SHA-256 of the anchor string``nvpcr-init:shirudo:0x1818e00:<written name>``, systemd's- ``nvpcr-init`` format. From then on PCR 9 no longer holds+ ``nvpcr-init`` format, and append the same record systemd writes for+ its own anchors (``content_type`` ``systemd``, ``eventType``+ ``nvpcr-init``) to systemd's userspace measurement log,+ ``/run/log/systemd/tpm2-measure.log``, under systemd's lock. The+ register is bound in the initrd, so the record precedes the+ ``nvpcr-separator`` that ``systemd-pcrnvdone`` measures in the+ host, and a verifier replays PCR 9 from the firmware log and that one+ log. A failure to log is reported and leaves PCR 9 extended: a+ verifier that cannot replay it fails closed. From then on PCR 9 no+ longer holds``pcr_before``, so the ``init`` branch cannot be satisfied again beforethe next TPM reset. Once systemd measures ``leave-initrd``, PCR 11 nolonger holds ``phase_before`` either.diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex 25cb1879..69206b40 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -522,7 +522,8 @@ Kernel and system interfaces- The ``boot_id`` of eventlog records.* - ``/sys/kernel/security/tpm0/binary_bios_measurements``,``/run/log/systemd/tpm2-measure.log``- - The firmware and systemd event logs ``attest`` includes.+ - The firmware and systemd event logs ``attest`` includes. ``init``+ appends the register's PCR 9 anchor to the systemd log.* - ``/sys/bus/event_source/devices/kprobe/type``,``/sys/bus/event_source/devices/kprobe/format/retprobe``- Attaching kprobe and kretprobe programs.diff --git a/testing/unit/chain.c b/testing/unit/chain.cindex 062c5e64..9a692733 100644--- a/testing/unit/chain.c+++ b/testing/unit/chain.c@@ -1,6 +1,12 @@// SPDX-License-Identifier: BSD-2-Clause/* Copyright (c) 2026 Authors of shirudo */+#include <errno.h>+#include <fcntl.h>+#include <stdlib.h>+#include <sys/stat.h>+#include <unistd.h>+#include "chain.h"#include "crypto.h"#include "unit.h"@@ -86,6 +92,55 @@ static void chain_fold_extends_like_the_register(void)EXPECT_MEMEQ(chain, want, sizeof(want));}+/* The anchor is appended to systemd's log in systemd's own record shape. */+static void chain_log_anchor_appends_a_systemd_record(void)+{+ static const char anchor[] = "nvpcr-init:shirudo:0x1818e00:000b01";+ char path[] = "/tmp/shirudo-unit-measure-XXXXXX", buf[2048] = {}, hex[65];+ const char *rec, *boot;+ struct stat st;+ __u8 digest[32];+ ssize_t n;+ int fd;++ fd = mkstemp(path);+ ASSERT_TRUE(fd >= 0);+ ASSERT_EQ(setenv("SYSTEMD_MEASURE_LOG_USERSPACE", path, 1), 0);+ ASSERT_EQ(sha256_buffer(anchor, strlen(anchor), digest), 0);+ hex_encode(digest, sizeof(digest), hex);+ ASSERT_EQ(chain_log_anchor(anchor, digest), 0);+ ASSERT_EQ(chain_log_anchor(anchor, digest), 0);+ n = read(fd, buf, sizeof(buf) - 1);+ ASSERT_TRUE(n > 0);+ EXPECT_EQ(buf[0], 0x1e);+ rec = strchr(buf + 1, 0x1e);+ ASSERT_TRUE(rec);+ EXPECT_EQ(rec[-1], '\n');+ EXPECT_TRUE(strstr(buf, "{\"pcr\":9,\"digests\":[{\"hashAlg\":"+ "\"sha256\",\"digest\":\"") == buf + 1);+ EXPECT_TRUE(strstr(buf, hex));+ EXPECT_TRUE(strstr(buf, "\"content_type\":\"systemd\",\"content\":"+ "{\"string\":\"nvpcr-init:shirudo:0x1818e00:"+ "000b01\",\"bootId\":\""));+ EXPECT_TRUE(strstr(buf, "\"eventType\":\"nvpcr-init\"}}\n"));+ boot = strstr(buf, "\"bootId\":\"");+ ASSERT_TRUE(boot);+ EXPECT_EQ(strspn(boot + 10, "0123456789abcdef"), 32);+ ASSERT_EQ(fstat(fd, &st), 0);+ EXPECT_EQ(st.st_mode & 07777, 0600);++ /* A record cut short marks the log aborted: nothing more is appended. */+ ASSERT_EQ(fchmod(fd, 0600 | S_ISVTX), 0);+ EXPECT_EQ(chain_log_anchor(anchor, digest), -ESTALE);+ ASSERT_EQ(fstat(fd, &st), 0);+ EXPECT_EQ(st.st_size, n);+ /* Nothing that would need JSON escaping is written. */+ EXPECT_EQ(chain_log_anchor("nvpcr-init:\"x", digest), -EINVAL);+ close(fd);+ unlink(path);+ unsetenv("SYSTEMD_MEASURE_LOG_USERSPACE");+}+static const struct unit_case cases[] = {UNIT_CASE(sha256_2_hashes_the_concatenation),UNIT_CASE(sha256_2_reports_a_failure),@@ -93,6 +148,7 @@ static const struct unit_case cases[] = {UNIT_CASE(upatch_tags_are_distinct),UNIT_CASE(java_unload_event_hashes_its_documented_input),UNIT_CASE(chain_fold_extends_like_the_register),+ UNIT_CASE(chain_log_anchor_appends_a_systemd_record),{}};diff --git a/util/chain.c b/util/chain.cindex 61b9fd67..e71fff5c 100644--- a/util/chain.c+++ b/util/chain.c@@ -11,8 +11,10 @@#include <stdio.h>#include <stdlib.h>#include <string.h>+#include <sys/file.h>#include <sys/stat.h>#include <sys/types.h>+#include <time.h>#include <unistd.h>#include <shirudo/misc.h>@@ -21,6 +23,7 @@#include "chain.h"#include "config.h"#include "crypto.h"+#include "eventlog.h"#include "initrd.h"#include "log.h"#include "malloc.h"@@ -166,6 +169,73 @@ int chain_fold(__u8 chain[32], const __u8 event[32])return sha256_buffer(cat, sizeof(cat), chain);}+/*+ * Append the PCR 9 anchor to systemd's userspace measurement log, in the+ * record shape systemd writes for its own nvpcr-init anchors, so a verifier+ * replays PCR 9 from the firmware log and that one log alone. The register+ * is bound in the initrd, before systemd-pcrnvdone measures nvpcr-separator+ * in the host. Locking and the sticky "write in progress" bit follow+ * systemd: a record a crash cut short marks the whole log as aborted. A+ * failure is reported and leaves the PCR as extended; a verifier that+ * cannot replay PCR 9 fails closed.+ */+#define SYSTEMD_MEASURE_LOG "/run/log/systemd/tpm2-measure.log"++int chain_log_anchor(const char *anchor, const __u8 digest[32])+{+ const char *path = getenv("SYSTEMD_MEASURE_LOG_USERSPACE");+ char boot[64], id[33], hex[65], record[768];+ struct timespec now;+ struct stat st;+ size_t i, j;+ int fd, len, ret = 0;++ if (!path || geteuid() != getuid())+ path = SYSTEMD_MEASURE_LOG;+ for (i = 0; anchor[i]; i++)+ if (anchor[i] == '"' || anchor[i] == '\\' ||+ (__u8)anchor[i] < 0x20)+ return -EINVAL;+ eventlog_boot_id(boot, sizeof(boot));+ for (i = 0, j = 0; boot[i] && j < 32; i++)+ if (boot[i] != '-' && boot[i] != '\n')+ id[j++] = boot[i];+ id[j] = '\0';+ if (j != 32 || clock_gettime(CLOCK_BOOTTIME, &now))+ return -EIO;+ hex_encode(digest, 32, hex);+ len = strsprintf(record, sizeof(record), "\x1e{\"pcr\":%u,\"digests\":[{\"hashAlg\":"+ "\"sha256\",\"digest\":\"%s\"}],\"content_type\":"+ "\"systemd\",\"content\":{\"string\":\"%s\","+ "\"bootId\":\"%s\",\"timestamp\":%llu,\"eventType\":"+ "\"nvpcr-init\"}}\n", CHAIN_PCR, hex, anchor, id,+ (__u64)now.tv_sec * 1000000ULL ++ (__u64)now.tv_nsec / 1000ULL);+ if (len < 0)+ return -E2BIG;+ mkdir("/run/log", 0755);+ mkdir("/run/log/systemd", 0755);+ fd = open(path, O_CREAT | O_WRONLY | O_CLOEXEC | O_NOCTTY |+ O_NOFOLLOW, 0600);+ if (fd < 0)+ return -errno;+ if (flock(fd, LOCK_EX) || fstat(fd, &st)) {+ ret = -errno;+ goto out;+ }+ if (!S_ISREG(st.st_mode) || (st.st_mode & S_ISVTX)) {+ ret = -ESTALE;+ goto out;+ }+ if (fchmod(fd, 0600 | S_ISVTX) || lseek(fd, 0, SEEK_END) < 0 ||+ write(fd, record, len) != len || fsync(fd) ||+ fchmod(fd, 0600) || fsync(fd))+ ret = errno ? -errno : -EIO;+out:+ close(fd);+ return ret;+}+int chain_anchor(const __u8 name[TPM_NAME_LEN],char *out, size_t cap){@@ -401,6 +471,10 @@ int chain_init(const __u8 event[32],CHAIN_PCR);goto out;}+ ret = chain_log_anchor(info->anchor, info->anchor_digest);+ if (ret)+ pr_warn("tpm: PCR %u anchor not in systemd's measurement "+ "log: %s", CHAIN_PCR, strerror(-ret));ret = tpm_nv_read(tpm, NV_INDEX, info->value);out:tpm_close(tpm);diff --git a/util/chain.h b/util/chain.hindex 370c28f7..45d42e3f 100644--- a/util/chain.h+++ b/util/chain.h@@ -73,6 +73,13 @@ int chain_history_step(const __u8 prev[32], const __u8 operation_digest[32],__u8 out[32]) __must_check;/* The PCR record anchoring a register name: nvpcr-init:shirudo:0x..:<name>. */+/*+ * Append the PCR 9 anchor to systemd's userspace measurement log+ * ($SYSTEMD_MEASURE_LOG_USERSPACE for an unprivileged caller) as an+ * nvpcr-init record; digest is the SHA-256 of anchor.+ */+int chain_log_anchor(const char *anchor, const __u8 digest[32]);+int chain_anchor(const __u8 name[TPM_NAME_LEN],char *out, size_t cap) __must_check;