eBPF-based security platform
attest: fail when this boot's eventlog cannot be read
Daniel Borkmann093567f348ec
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex e9da0698..a52d31c4 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -314,7 +314,10 @@ signed config image.refusal of TPM logging overrides during bootstrap and enforcement.An unsalted negative control accepts a forged acknowledgmentwith an empty or SHA-256 response HMAC; malformed lengths are refused.- Salted sessions reject it and leave the real measured value unchanged+ Salted sessions reject it and leave the real measured value unchanged.+ ``attest`` carries this boot's records, none without a log, and+ fails on a log with a line it cannot read rather than carry the+ records before it* - ``t4006-keepalive-quotes``- The quotes of :src:`testing/fixtures/keepalive/quotes.tsv`: eachrow's ``body_sha256`` is SHA-256 of its body file, and fails ondiff --git a/Documentation/reference/cli/attest.rst b/Documentation/reference/cli/attest.rstindex dccfb428..b44becef 100644--- a/Documentation/reference/cli/attest.rst+++ b/Documentation/reference/cli/attest.rst@@ -147,8 +147,12 @@ Exit status- No TPM answered (``attest: no TPM answered (tpm_error)``), theregister is not bound this boot, the TPM refused the challenge(``attest: the TPM refused the challenge; the attestation key or- the endorsement key is not this TPM's``), or producing- or writing the evidence failed.+ the endorsement key is not this TPM's``), this boot's eventlog+ could not be read to its end (``attest: eventlog <path>+ unreadable: <error>``), or producing or writing the evidence+ failed. A boot without an ``init`` record is no error: its+ evidence carries an empty ``eventlog``, which ``attest verify``+ fails.* - 2- A usage error: an unknown option, an option given twice, a valuemissing or empty, or an argument. An invalid ``--nonce`` ordiff --git a/Documentation/reference/eventlog.rst b/Documentation/reference/eventlog.rstindex 797fafab..b6004746 100644--- a/Documentation/reference/eventlog.rst+++ b/Documentation/reference/eventlog.rst@@ -348,7 +348,8 @@ Size limitseventlog and the pending record together. Above that the replayfails. ``status`` then reports ``ChainIntact`` ``ALERT`` and theverdict ``tampered``, ``upatch load`` and ``upatch unload`` fail- with ``tpm_error``, and ``init`` refuses to bind the register.+ with ``tpm_error``, ``init`` refuses to bind the register, and+ ``attest`` fails rather than write evidence without the records.The eventlog grows across boots and shirudo never shortens it. Keep``eventlog.max_bytes`` at or below 8388608 so that a refusal withdiff --git a/Makefile.shirudo b/Makefile.shirudoindex 25d8fba2..e8fa5181 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -917,7 +917,7 @@ testing/helper/test-nv-cphash: testing/helper/test-nv-cphash.c util/keyring.o \-Wl,--wrap=enforcement_activetesting/helper/test-tpm-session: testing/helper/test-tpm-session.c \- $(TPM_CLIENT_SRCS) $(REGISTER_OBJS)+ util/attest.o util/upatch.o $(TPM_CLIENT_SRCS) $(REGISTER_OBJS)@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $(filter-out %.h,$^) $(REGISTER_LIBS) \-Wl,--wrap=enforcement_active,--wrap=identity_readdiff --git a/testing/functional/tpm/t4005-tpm-session.sh b/testing/functional/tpm/t4005-tpm-session.shindex 63a01b77..a6011580 100644--- a/testing/functional/tpm/t4005-tpm-session.sh+++ b/testing/functional/tpm/t4005-tpm-session.sh@@ -9,10 +9,11 @@ cd "$(dirname "$0")/../.."WORK=$(mktemp -d) || exit 1cleanup() {for mode in unsalted-control nv-extend nv-replay pcr-extend nv-read \- pcr-read clock policy-digest identity; do+ pcr-read clock policy-digest identity evidence; dorm -f "$WORK/$mode/permanent" "$WORK/$mode/volatile" "$WORK/$mode/NVChip"rmdir "$WORK/$mode"done+ rm -f "$WORK/eventlog"rmdir "$WORK"}trap cleanup EXIT@@ -25,4 +26,11 @@ for mode in unsalted-control nv-extend nv-replay pcr-extend nv-read \'done+test_expect_success 'attest fails on an eventlog it cannot read to the end' '+ SHIRUDO_TPM_TCTI="cmd:exec $(pwd)/helper/tpmsim-cmd $WORK/evidence" \+ SHIRUDO_EVENTLOG_PATH="$WORK/eventlog" \+ SHIRUDO_EVENTLOG_PENDING_PATH="$WORK/eventlog.pending" \+ ./helper/test-tpm-session evidence+'+test_donediff --git a/testing/helper/test-tpm-session.c b/testing/helper/test-tpm-session.cindex 435942bb..bc0c5f00 100644--- a/testing/helper/test-tpm-session.c+++ b/testing/helper/test-tpm-session.c@@ -11,8 +11,13 @@#include <shirudo/byteorder.h>#include <shirudo/string.h>+#include "attest.h"#include "chain.h"#include "crypto.h"+#include "eventlog.h"+#include "json.h"+#include "malloc.h"+#include "paths.h"#include "tpm-session.h"#define TEST_INDEX 0x01818E02u@@ -219,6 +224,52 @@ out:return ret;}+/*+ * attest_evidence_config() on the eventlog as it stands: its return, or+ * -EBADMSG when the evidence's eventlog does not begin with array.+ */+static int evidence_begins(const char *array)+{+ __u8 nonce[8] = { 3 };+ size_t len, cap;+ char *doc;+ int ret;++ ret = attest_evidence_config(nonce, sizeof(nonce), 1u << CHAIN_PCR,+ NULL, NULL, &doc, &len, &cap);+ if (ret)+ return ret;+ ret = strstr(doc, array) ? 0 : -EBADMSG;+ xfree(doc, cap);+ return ret;+}++/*+ * A log read only up to a line the reader refuses would leave evidence+ * with the records before that line alone, which says nothing about+ * what the register holds; attest has to fail instead. A boot without+ * an init record still gets evidence, with an empty eventlog.+ */+static int evidence_cases(const __u8 event[32])+{+ char init[1024];+ struct json_emit e = { .buf = init, .cap = sizeof(init) };+ int ret = 1;++ eventlog_record_open(&e, "init", event);+ json_obj_close(&e);+ CHECK(!e.err);+ CHECK(evidence_begins("\"eventlog\":[]") == 0);+ CHECK(!eventlog_append_to(eventlog_path(), init));+ CHECK(evidence_begins("\"eventlog\":[{\"schema\":1,") == 0);+ CHECK(!eventlog_append_to(eventlog_path(), "not a record"));+ CHECK(evidence_begins("\"eventlog\":[") == -EINVAL);+ puts("evidence carries this boot's records, and an unreadable log fails it");+ ret = 0;+out:+ return ret;+}+int main(int argc, char **argv){__u8 branch_written[32], branch_init[32], policy[32], values[64];@@ -226,6 +277,7 @@ int main(int argc, char **argv)__u8 before[32], after[32], event[32] = { 2 };struct tpm *t = NULL, *other = NULL;struct tpm_attestation attestation;+ bool register_defined = false;struct tpm_nv_info info;const char *log_setting;struct tpm_key ak = {};@@ -337,6 +389,14 @@ int main(int argc, char **argv)CHECK(!tpm_open(&t));CHECK(!tpm_nv_read(t, TEST_INDEX, after) && !memcmp(before, after, 32));puts("forged readback rejected; live register unchanged");+ } else if (!strcmp(argv[1], "evidence")) {+ CHECK(!tpm_nv_define(t, NV_INDEX, policy, NULL, 0));+ register_defined = true;+ CHECK(!tpm_nv_extend_first(t, NV_INDEX, CHAIN_PCR_MASK,+ branch_written, branch_init, event));+ tpm_close(t);+ t = NULL;+ CHECK(!evidence_cases(event));} else {CHECK(!strcmp(argv[1], "identity"));tpm_close(t);@@ -366,7 +426,9 @@ out:identity.ek_name[1] = TPM2_ALG_SHA256;if (defined) {t = NULL;- if (tpm_open(&t) || tpm_nv_undefine(t, TEST_INDEX, NULL, 0))+ if (tpm_open(&t) || tpm_nv_undefine(t, TEST_INDEX, NULL, 0) ||+ (register_defined &&+ tpm_nv_undefine(t, NV_INDEX, NULL, 0)))ret = 1;tpm_close(t);}diff --git a/util/attest.c b/util/attest.cindex c1421ad6..24126d30 100644--- a/util/attest.c+++ b/util/attest.c@@ -365,7 +365,17 @@ int attest_evidence_config(const __u8 *nonce, size_t nonce_len,emit_hex(&e, "signature", certify.signature, certify.signature_len);json_obj_close(&e);json_arr_open(&e, "eventlog");- eventlog_boot_lines(emit_line, &e);+ /*+ * A boot without an init record has no record to carry, which+ * verify reports. A log read only part of the way would carry a+ * history that stops short of what the register holds.+ */+ ret = eventlog_boot_lines(emit_line, &e);+ if (ret && ret != -ENOENT) {+ pr_err("attest: eventlog %s unreadable: %s", eventlog_path(),+ strerror(-ret));+ goto out;+ }json_arr_close(&e);ret = config_evidence_emit(&e, &config);if (ret)diff --git a/util/attest.h b/util/attest.hindex ffedb3eb..38e7cb98 100644--- a/util/attest.h+++ b/util/attest.h@@ -48,7 +48,8 @@ struct config_update;/** Target side. The buffer is xmalloc'd to *cap and NUL-terminated;* -ENXIO when no TPM answers, -ENODEV when no register is bound this- * boot, -EKEYREJECTED when the TPM refuses the challenge.+ * boot, -EKEYREJECTED when the TPM refuses the challenge, and what the+ * eventlog's reader returns when this boot's records cannot be read.*/int attest_evidence_config(const __u8 *nonce, size_t nonce_len,__u32 pcr_mask,