eBPF-based security platform
chain: carry and validate the boot writer public key
Leonard Cohnen506acc981243
diff --git a/Documentation/tpm.rst b/Documentation/tpm.rstindex 9b2d9dfc..c7c23fe2 100644--- a/Documentation/tpm.rst+++ b/Documentation/tpm.rst@@ -123,8 +123,9 @@ this order:5. Append the ``init`` record to the eventlog (section 3) with bothPCR values, whether the initrd was running, the written name, the- anchor and its digest, the register value and the TPM's reset- count.+ anchor and its digest, the register value, the TPM's reset+ count and ``nv_writer_public``: the 384-byte big-endian RSA modulus+ in 768 hex digits, without a TPM header. The exponent is fixed at 65537.The phase measurements are ``systemd-pcrphase-initrd.service``'s,which systemd runs only for a measured UKI boot; without them PCR 11@@ -143,6 +144,8 @@ means this register period already has its binding. A re-run ofit and adds no event; finds it gone, unwritten or renamed, it refuseswith ``tampered`` and binds nothing before the next TPM reset, becausea second binding would restart the history the record vouches for.+An absent or unreadable writer identity refuses as well. Reuse and+extension check the live policy, reset count and frozen writer identity.Without a record for the current count, whether the boot is new orthe TPM was reset without a reboot, the index is bound; one foundwritten by someone else first is undefined and bound again, with a@@ -236,7 +239,8 @@ extension, written with ``fsync`` and sealed like everything under``payload_sha256`` pairs), ``pcr`` (9), ``pcr_before``,``phase_pcr`` (11), ``phase_before``, ``bound_in`` (``initrd`` or``root``), ``nv_name`` (the written name), ``anchor``,- ``anchor_digest`` and ``chain`` (the register after the event).+ ``anchor_digest``, ``chain`` (the register after the event) and+ ``nv_writer_public`` (the boot writer's RSA modulus).``upatch_load`` and ``upatch_unload````upatch`` with ``name`` and ``payload_sha256``; an unload a@@ -478,9 +482,12 @@ not shirudo evidence exits 2.--event=tree:<seq>=<operation_digest>...]`` prints ``set_digest``,``init_event``, ``register_at_boot`` and, with events,``register_after_events``, from the same functions the target uses.-With ``--bind-pcr=9=<hex> --bind-pcr=11=<hex>`` it also prints+With ``--bind-pcr=9=<hex> --bind-pcr=11=<hex> --writer-public=<hex>``+it also prints``register_name``, the anchor string and ``pcr9_after_anchor``, so the-name and PCR 9 can be pinned from values the operator knows.+name and PCR 9 can be pinned from values the operator knows. The writer+input is the 768-digit ``nv_writer_public`` from the boot's init record;+it is required together with both binding PCRs.``shirudo attest challenge --evidence=<path> --out=<path>[--ek-ca=<bundle>]`` is described in section 4; it prints its checks,diff --git a/Makefile.shirudo b/Makefile.shirudoindex 985f374b..ac81b4ba 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -885,13 +885,14 @@ testing/fuzz/fuzz-tpm-wire: testing/fuzz/fuzz-tpm-wire.c $(FUZZ_BASE) \util/tpm_wire.c util/crypto_openssl.c util/io.c \$(FUZZ_SEAL)testing/fuzz/fuzz-attest: testing/fuzz/fuzz-attest.c $(FUZZ_BASE) \- util/attest.c util/chain.c util/tpm.c \+ util/attest.c util/chain.c util/nv_writer.c util/tpm.c \util/tpm_wire.c util/eventlog.c util/timefmt.c \util/crypto_openssl.c util/io.c $(FUZZ_SEAL) \util/config_attest.c util/config_update.c \- util/config_measure.c util/initrd.c util/version_id.c-testing/fuzz/fuzz-attest: FUZZ_CFLAGS += $(TSS2_CFLAGS)-testing/fuzz/fuzz-attest: FUZZ_LIBS += $(TSS2_LIBS)+ util/config_measure.c util/initrd.c util/identity.c \+ util/keyring.c util/version_id.c $(LIBBPF_A)+testing/fuzz/fuzz-attest: FUZZ_CFLAGS += $(TSS2_CFLAGS) $(LIBELF_CFLAGS)+testing/fuzz/fuzz-attest: FUZZ_LIBS += $(TSS2_LIBS) $(LIBBPF_A) $(LIBELF_LIBS) $(ZLIB_LIBS)testing/fuzz/fuzz-eventlog: testing/fuzz/fuzz-eventlog.c $(FUZZ_BASE) \util/eventlog.c util/timefmt.c $(FUZZ_SEAL) \util/io.c util/crypto_openssl.c testing/fuzz/chain.cdiff --git a/builtin-attest.c b/builtin-attest.cindex c50c5bff..0f0cb048 100644--- a/builtin-attest.c+++ b/builtin-attest.c@@ -90,6 +90,7 @@ static const struct shirudo_help_item predict_options[] = {{ "--event=<kind>:<name>=<hex>","a load, unload, tree:<seq> or revoke:<fingerprint> event after boot" },{ "--bind-pcr=<n>=<hex>", "PCR 9 and 11 values at binding, for the register name" },+ { "--writer-public=<hex>", "init record's 384-byte RSA modulus (with --bind-pcr)" },};static const struct shirudo_help_section attest_help_sections[] = {@@ -291,6 +292,7 @@ static int apply_event(const char *spec, __u8 chain[32])static int cmd_attest_predict(int argc, const char **argv){+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];__u8 cert_sha[32], tree_root[32] = {}, set_digest[32], init[32];__u8 cert_shas[SHIRUDO_KEYS_MAX][32];size_t n_certs = 0;@@ -298,7 +300,7 @@ static int cmd_attest_predict(int argc, const char **argv)const char *events[EVENTS_MAX];struct members m = {};char hex[2 * SHIRUDO_TPM_NAME_LEN + 1];- bool have_cert = false, have_bind[2] = {};+ bool have_cert = false, have_writer = false, have_bind[2] = {};size_t n_events = 0, i;int ret = 2;@@ -333,6 +335,12 @@ static int cmd_attest_predict(int argc, const char **argv)if (n_events >= EVENTS_MAX)goto out;events[n_events++] = a + 8;+ } else if (!strncmp(a, "--writer-public=", 16)) {+ if (hex_decode(a + 16, writer_public, sizeof(writer_public))) {+ pr_err("attest predict: invalid RSA-3072 --writer-public");+ goto out;+ }+ have_writer = true;} else if (!strncmp(a, "--bind-pcr=", 11)) {const char *value = NULL;int slot = -1;@@ -387,9 +395,16 @@ static int cmd_attest_predict(int argc, const char **argv)__u8 name[SHIRUDO_TPM_NAME_LEN], anchor_digest[32];char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];- tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, bind[0], 2,- branch_written, branch_init,- policy);+ if (!have_writer) {+ pr_err("attest predict: --bind-pcr requires --writer-public");+ goto out;+ }+ if (tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, bind[0], 2,+ writer_public, branch_written, branch_init, policy)) {+ pr_err("attest predict: cannot compute the signed write policy");+ ret = 1;+ goto out;+ }tpm_nv_name(SHIRUDO_NV_INDEX,SHIRUDO_TPM_REGISTER_ATTRS |SHIRUDO_TPM_NV_WRITTEN, policy, 32, name);@@ -404,7 +419,7 @@ static int cmd_attest_predict(int argc, const char **argv)chain_fold(bind[0], anchor_digest);hex_encode(bind[0], 32, hex);printf("pcr%u_after_anchor: %s\n", SHIRUDO_CHAIN_PCR, hex);- } else if (have_bind[0] || have_bind[1]) {+ } else if (have_bind[0] || have_bind[1] || have_writer) {pr_err("attest predict: --bind-pcr needs both %u and %u",SHIRUDO_CHAIN_PCR, SHIRUDO_CHAIN_PHASE_PCR);goto out;diff --git a/builtin-init.c b/builtin-init.cindex 3f041a0c..8ab6804a 100644--- a/builtin-init.c+++ b/builtin-init.c@@ -997,6 +997,7 @@ static int append_init_event(const struct shirudo_measured_upatch *set,const __u8 init_digest[32],const struct shirudo_chain_init_info *info){+ char writer_hex[HEXLEN(SHIRUDO_TPM_NV_RSA_BYTES)];char buf[64 * 1024];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };char cert_hex[HEXLEN(32)], set_hex[HEXLEN(32)], digest_hex[HEXLEN(32)];@@ -1020,6 +1021,7 @@ static int append_init_event(const struct shirudo_measured_upatch *set,hex_encode(info->name, SHIRUDO_TPM_NAME_LEN, name_hex);hex_encode(info->anchor_digest, 32, anchor_hex);hex_encode(info->value, 32, chain_hex);+ hex_encode(info->writer_public, sizeof(info->writer_public), writer_hex);json_obj_open(&e);json_kv_int(&e, "schema", 2);@@ -1057,6 +1059,7 @@ static int append_init_event(const struct shirudo_measured_upatch *set,json_kv_string(&e, "bound_in",info->initrd ? "initrd" : "root");json_kv_string(&e, "nv_name", name_hex);+ json_kv_string(&e, "nv_writer_public", writer_hex);json_kv_string(&e, "anchor", info->anchor);json_kv_string(&e, "anchor_digest", anchor_hex);json_kv_string(&e, "chain", chain_hex);diff --git a/testing/helper/test-eventlog.c b/testing/helper/test-eventlog.cindex 1db46c56..deaa732c 100644--- a/testing/helper/test-eventlog.c+++ b/testing/helper/test-eventlog.c@@ -26,6 +26,8 @@static int append_init(const char *path, const char *bound_in,__u32 reset_count){+ char writer_hex[2 * SHIRUDO_TPM_NV_RSA_BYTES + 1];+ __u8 modulus[SHIRUDO_TPM_NV_RSA_BYTES] = { [0] = 0x80, [383] = 1 };char buf[4096], ts[32], boot_id[64], nv_index[16], hex[65];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };char name_hex[2 * SHIRUDO_TPM_NAME_LEN + 1];@@ -36,6 +38,7 @@ static int append_init(const char *path, const char *bound_in,snprintf(nv_index, sizeof(nv_index), "0x%08X", SHIRUDO_NV_INDEX);hex_encode(zero, 32, hex);hex_encode(zero, SHIRUDO_TPM_NAME_LEN, name_hex);+ hex_encode(modulus, sizeof(modulus), writer_hex);json_obj_open(&e);json_kv_int(&e, "schema", 2);json_kv_string(&e, "ts", ts);@@ -51,6 +54,7 @@ static int append_init(const char *path, const char *bound_in,json_kv_string(&e, "phase_before", hex);json_kv_string(&e, "bound_in", bound_in);json_kv_string(&e, "nv_name", name_hex);+ json_kv_string(&e, "nv_writer_public", writer_hex);json_kv_string(&e, "anchor", "shirudo-anchor:test");json_kv_string(&e, "anchor_digest", hex);json_kv_string(&e, "chain", hex);diff --git a/testing/helper/test-register.c b/testing/helper/test-register.cindex 97cbe31b..b5a1f8ff 100644--- a/testing/helper/test-register.c+++ b/testing/helper/test-register.c@@ -110,7 +110,8 @@ static void puthex(const __u8 *b, size_t n)static int append_init(const __u8 event[32],const struct shirudo_chain_init_info *info){- char buf[4096], ts[32], boot_id[64], nv_index[16], hex[256];+ char buf[4096], ts[32], boot_id[64], nv_index[16];+ char hex[2 * SHIRUDO_TPM_NV_RSA_BYTES + 1];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };eventlog_timestamp(ts, sizeof(ts));@@ -135,6 +136,8 @@ static int append_init(const __u8 event[32],json_kv_string(&e, "bound_in", info->initrd ? "initrd" : "root");hex_encode(info->name, info->name_len, hex);json_kv_string(&e, "nv_name", hex);+ hex_encode(info->writer_public, sizeof(info->writer_public), hex);+ json_kv_string(&e, "nv_writer_public", hex);json_kv_string(&e, "anchor", info->anchor);hex_encode(info->anchor_digest, 32, hex);json_kv_string(&e, "anchor_digest", hex);@@ -409,15 +412,20 @@ int main(int argc, char **argv)return 2;memcpy(values, rec.pcr_before, 32);memcpy(values + 32, rec.phase_before, 32);- tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,- branch_written, branch_init,- policy);+ rc = tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,+ rec.writer_public, branch_written, branch_init, policy);+ if (rc) {+ fprintf(stderr, "register policy: %s\n", strerror(-rc));+ return 1;+ }if (tpm_open(&tpm))return 1;- rc = tpm_nv_extend_first(tpm, SHIRUDO_NV_INDEX,- SHIRUDO_CHAIN_PCR_MASK,- branch_written, branch_init,- event);+ rc = tpm_nv_set_writer(tpm, rec.writer_public);+ if (!rc)+ rc = tpm_nv_extend_first(tpm, SHIRUDO_NV_INDEX,+ SHIRUDO_CHAIN_PCR_MASK,+ branch_written, branch_init,+ event);tpm_close(tpm);printf("rc=%d\n", rc);return rc ? 1 : 0;@@ -431,17 +439,18 @@ int main(int argc, char **argv)*/if (strcmp(argv[1], "forge") == 0 && argc == 7) {struct shirudo_tpm_key ak = { .handle = 0 }, ek = { .handle = 0 };- struct shirudo_tpm_key ak_copy = {}, ek_copy = {};- struct shirudo_tpm_attestation quote = {}, certify = {};char hex[2 * SHIRUDO_TPM_ATTEST_MAX + 1], buf[32 * 1024];struct shirudo_json_emit e = { .buf = buf, .cap = sizeof(buf) };- __u8 branch_written[32], branch_init[32], policy[32];+ struct shirudo_tpm_attestation quote = {}, certify = {};__u8 values[24][32] = {}, before[64], anchor_digest[32];+ __u8 branch_written[32], branch_init[32], policy[32];+ __u32 index, pcr, phase, mask, quoted, reset = 0, i;+ struct shirudo_tpm_key ak_copy = {}, ek_copy = {};+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];char ts[32], boot_id[64], nv_index[16];- char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];size_t nonce_len = strlen(argv[6]) / 2;+ char anchor[SHIRUDO_CHAIN_ANCHOR_MAX];struct shirudo_tpm *tpm = NULL;- __u32 index, pcr, phase, mask, quoted, reset = 0, i;__u8 value[32], nonce[64];struct tpm_nv_info nv;@@ -455,18 +464,22 @@ int main(int argc, char **argv)nonce_len == 0 || nonce_len > sizeof(nonce) ||hex_decode(argv[6], nonce, nonce_len))return 2;+ if (nv_writer_read(writer_public))+ return 1;if (tpm_open(&tpm))return 1;- rc = tpm_reset_count(tpm, &reset);+ rc = tpm_nv_set_writer(tpm, writer_public);+ if (!rc)+ rc = tpm_reset_count(tpm, &reset);if (!rc)rc = tpm_pcr_read(tpm, pcr, before);if (!rc)rc = tpm_pcr_read(tpm, phase, before + 32);- if (!rc) {- tpm_register_policy(mask, before, 2, branch_written,- branch_init, policy);+ if (!rc)+ rc = tpm_register_policy(mask, before, 2, writer_public,+ branch_written, branch_init, policy);+ if (!rc)rc = tpm_nv_define(tpm, index, policy, NULL, 0);- }if (!rc)rc = tpm_nv_extend_first(tpm, index, mask,branch_written, branch_init,@@ -581,6 +594,8 @@ int main(int argc, char **argv)json_kv_string(&e, "bound_in", "initrd");hex_encode(nv.name, nv.name_len, hex);json_kv_string(&e, "nv_name", hex);+ hex_encode(writer_public, sizeof(writer_public), hex);+ json_kv_string(&e, "nv_writer_public", hex);json_kv_string(&e, "anchor", anchor);hex_encode(anchor_digest, 32, hex);json_kv_string(&e, "anchor_digest", hex);diff --git a/testing/helper/test-tpm-wire.c b/testing/helper/test-tpm-wire.cindex 298e57cf..3eb4391d 100644--- a/testing/helper/test-tpm-wire.c+++ b/testing/helper/test-tpm-wire.c@@ -121,14 +121,16 @@ int main(int argc, char **argv)puthex(out, 32);return 0;}- if (strcmp(argv[1], "register-policy") == 0 && argc == 5) {+ if (strcmp(argv[1], "register-policy") == 0 && argc == 6) {__u8 w[32], i[32], p[32], values[64];if (hex_decode(argv[3], values, 32) ||- hex_decode(argv[4], values + 32, 32))+ hex_decode(argv[4], values + 32, 32) ||+ hex_decode(argv[5], public, sizeof(public)))return 2;- tpm_register_policy((__u32)strtoul(argv[2], NULL, 0),- values, 2, w, i, p);+ if (tpm_register_policy((__u32)strtoul(argv[2], NULL, 0),+ values, 2, public, w, i, p))+ return 1;puthex(w, 32);puthex(i, 32);puthex(p, 32);diff --git a/testing/tpm/t0033-tpm-wire.sh b/testing/tpm/t0033-tpm-wire.shindex da560bb5..c613ff9c 100644--- a/testing/tpm/t0033-tpm-wire.sh+++ b/testing/tpm/t0033-tpm-wire.sh@@ -72,7 +72,7 @@ print(hashlib.sha256(digest + bytes.fromhex(\"0000016c00000136\")).hexdigest())test_expect_success 'register policy branches match independent vectors' 'v9=$(py "print(\"cd\"*32)") && v11=$(py "print(\"ef\"*32)") &&- out=$(./$H register-policy 0xa00 $v9 $v11) &&+ out=$(./$H register-policy 0xa00 $v9 $v11 $MODULUS) &&want=$(py "import hashlibdef h(data):diff --git a/testing/tpm/t0034-register.sh b/testing/tpm/t0034-register.shindex a6b4c49d..ab6e033a 100644--- a/testing/tpm/t0034-register.sh+++ b/testing/tpm/t0034-register.sh@@ -24,6 +24,7 @@ cleanup() {}trap cleanup EXIT HUP INT TERMregister_writer_start || exit 1+WRITER=$(./$R writer-public) || exit 1mkdir -p "$WORK/keys"head -c 700 /dev/urandom >"$WORK/keys/cert.der": >"$WORK/initrd-release"@@ -73,9 +74,9 @@ test_expect_success 'init binds the register to PCRs 9 and 11 from the initrd anecho "$init" >"$WORK/init" && echo "$name" >"$WORK/name"'-test_expect_success 'predict names the register from the binding values alone' '+test_expect_success 'predict names the register from the binding values and kernel writer' '$S attest predict --cert="$WORK/keys/cert.der" --bind-pcr=9=$Z \- --bind-pcr=11=$PHASE >"$WORK/predict" &&+ --bind-pcr=11=$PHASE --writer-public="$WRITER" >"$WORK/predict" &&grep -qx "register_name: $(cat "$WORK/name")" "$WORK/predict" &&grep -qx "pcr9_after_anchor: $(./$R pcr 9)" "$WORK/predict"'@@ -170,16 +171,20 @@ test_expect_success 'a wrong nonce, key name, binding value or tampered value fagrep -q "^FAIL: register: certified contents" "$WORK/report4"'-# Root on the target defines an index of its own, bound to the debug and-# application PCRs it can extend and reset at will, writes the init event-# the operator expects into it, anchors it there and has the genuine-# attestation key certify it in the real register's place. The evidence-# is consistent in every value it carries; only the handle and the PCR-# numbers, which the verifier must take from its own constants and not-# from the document, give it away.+# The pre-init fixture uses the kernel writer to certify a substitute+# register bound to resettable PCRs. Even otherwise valid writer evidence+# must not select the verifier's register handle or binding PCRs.test_expect_success 'a register of the target'"'"'s own, bound to PCRs it controls, is refused' 'nonce=0102030405060708090a0b0c0d0e0f10 &&./$R forge 01818e01 16 23 "$(cat "$WORK/init")" $nonce >"$WORK/forged.json" &&+ python3 - "$WORK/forged.json" "$WRITER" <<"PY" &&+import json, sys+evidence = json.load(open(sys.argv[1]))+assert evidence["eventlog"][0]["nv_writer_public"] == sys.argv[2]+assert evidence["register"]["nv_index"] == "0x01818E01"+assert evidence["eventlog"][0]["pcr"] == 16+assert evidence["eventlog"][0]["phase_pcr"] == 23+PY[ "$(py "import json; print(json.load(open(\"$WORK/forged.json\"))[\"register\"][\"value\"])")" = \"$(sha "$Z$(cat "$WORK/init")")" ] &&! $S attest verify --evidence="$WORK/forged.json" --nonce=$nonce \@@ -348,6 +353,7 @@ inits = [json.loads(l) for l in open(sys.argv[1]) if json.loads(l)["type"] == "iassert len(inits) == 2, initsassert inits[0]["tpm_reset_count"] != inits[1]["tpm_reset_count"], initsassert inits[0]["bound_in"] == "initrd" and inits[1]["bound_in"] == "root", inits+assert inits[0]["nv_writer_public"] == inits[1]["nv_writer_public"], initsPY'diff --git a/util/attest.c b/util/attest.cindex 3eed9f37..35136f29 100644--- a/util/attest.c+++ b/util/attest.c@@ -430,6 +430,7 @@ struct record {};struct init_record {+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];bool present;bool have_reset_count;bool initrd;@@ -446,6 +447,7 @@ struct init_record {};struct parse {+ char writer_public_hex[2 * SHIRUDO_TPM_NV_RSA_BYTES + 1];char nonce_hex[2 * SHIRUDO_ATTEST_NONCE_MAX + 1];__s64 pcr_mask;char ak_public_hex[2 * SHIRUDO_TPM_PUBLIC_MAX + 1];@@ -534,7 +536,9 @@ static int on_record(void *ctx)SHIRUDO_TPM_NAME_LEN) ||hex_decode(p->anchor_digest_hex, in->anchor_digest,32) ||- hex_decode(p->chain_hex, in->chain, 32))+ hex_decode(p->chain_hex, in->chain, 32) ||+ hex_decode(p->writer_public_hex, in->writer_public,+ sizeof(in->writer_public)))return -EINVAL;memcpy(in->digest, r->digest, 32);in->pcr = (__u32)p->rec_pcr;@@ -604,6 +608,7 @@ static int on_record(void *ctx)p->key_fingerprint[0] = '\0';p->key_sha_hex[0] = '\0';p->anchor[0] = '\0';+ p->writer_public_hex[0] = '\0';p->bound_in[0] = '\0';p->tree_seq = 0;p->rec_pcr = -1;@@ -847,6 +852,9 @@ int attest_verify(const char *evidence, size_t len,sizeof(p->bound_in), 0, NULL, 0 },{ "nv_name", SHIRUDO_JSON_T_STRING, p->nv_name_hex,sizeof(p->nv_name_hex), 0, NULL, 0 },+ { "nv_writer_public", SHIRUDO_JSON_T_STRING,+ p->writer_public_hex, sizeof(p->writer_public_hex),+ 0, NULL, 0 },{ "anchor", SHIRUDO_JSON_T_STRING, p->anchor,sizeof(p->anchor), 0, NULL, 0 },{ "anchor_digest", SHIRUDO_JSON_T_STRING,@@ -1100,8 +1108,13 @@ int attest_verify(const char *evidence, size_t len,SHIRUDO_NV_INDEX, SHIRUDO_CHAIN_PCR, SHIRUDO_CHAIN_PHASE_PCR);memcpy(bind, p->init.pcr_before, 32);memcpy(bind + 32, p->init.phase_before, 32);- tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, bind, 2,- branch_written, branch_init, policy);+ rc = tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, bind, 2,+ p->init.writer_public,+ branch_written, branch_init, policy);+ if (rc) {+ say(&rep, false, "register: cannot compute the signed write policy");+ goto out;+ }tpm_nv_name(SHIRUDO_NV_INDEX,SHIRUDO_TPM_REGISTER_ATTRS | SHIRUDO_TPM_NV_WRITTEN,policy, 32, expected_name);diff --git a/util/chain.c b/util/chain.cindex 58a64211..5e2dc3e0 100644--- a/util/chain.c+++ b/util/chain.c@@ -24,6 +24,7 @@#include "initrd.h"#include "log.h"#include "malloc.h"+#include "nv_writer.h"#include "paths.h"#include "tpm.h"@@ -187,24 +188,31 @@ static void bind_values(const __u8 pcr_before[32],* the written name is the one the record, the anchor and every later* comparison use.*/-static void register_expectation(const __u8 pcr_before[32],- const __u8 phase_before[32],- __u8 branch_written[32],- __u8 branch_init[32],- __u8 policy[32],- __u8 name_unwritten[SHIRUDO_TPM_NAME_LEN],- __u8 name_written[SHIRUDO_TPM_NAME_LEN])+static int register_expectation(const __u8 pcr_before[32],+ const __u8 phase_before[32],+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],+ __u8 branch_written[32],+ __u8 branch_init[32],+ __u8 policy[32],+ __u8 name_unwritten[SHIRUDO_TPM_NAME_LEN],+ __u8 name_written[SHIRUDO_TPM_NAME_LEN]){__u8 values[64];+ int rc;bind_values(pcr_before, phase_before, values);- tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,- branch_written, branch_init, policy);+ rc = tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2, writer_public,+ branch_written, branch_init, policy);+ if (rc) {+ pr_err("tpm: compute signed register policy: %s", strerror(-rc));+ return rc;+ }tpm_nv_name(SHIRUDO_NV_INDEX, SHIRUDO_TPM_REGISTER_ATTRS,policy, SHIRUDO_TPM_NV_DIGEST_LEN, name_unwritten);tpm_nv_name(SHIRUDO_NV_INDEX,SHIRUDO_TPM_REGISTER_ATTRS | SHIRUDO_TPM_NV_WRITTEN,policy, SHIRUDO_TPM_NV_DIGEST_LEN, name_written);+ return 0;}static bool name_equals(const struct tpm_nv_info *info,@@ -235,6 +243,15 @@ out:return rc;}+static int register_writer(struct shirudo_tpm *tpm,+ __u8 public[SHIRUDO_TPM_NV_RSA_BYTES])+{+ int rc;++ rc = nv_writer_read(public);+ return rc ? rc : tpm_nv_set_writer(tpm, public);+}+int chain_init(const __u8 event[32],struct shirudo_chain_init_info *info){@@ -278,6 +295,26 @@ int chain_init(const __u8 event[32],recorded = !rc && (!rec.have_reset_count ||rec.reset_count == info->reset_count);if (recorded) {+ rc = register_writer(tpm, info->writer_public);+ if (rc)+ goto out;+ rc = register_expectation(rec.pcr_before, rec.phase_before,+ info->writer_public, branch_written,+ branch_init, policy, unwritten, written);+ if (rc)+ goto out;+ if (!name_equals(&nv, written)) {+ pr_err("tpm: recorded register has an obsolete policy; "+ "trusted reboot required");+ rc = -EEXIST;+ goto out;+ }+ if (!ct_eq(info->writer_public, rec.writer_public,+ sizeof(info->writer_public))) {+ pr_err("tpm: recorded NV writer differs from the guarded key");+ rc = -EKEYREJECTED;+ goto out;+ }if (nv.defined && nv.written && name_equals(&nv, rec.name)) {rc = tpm_nv_read(tpm, SHIRUDO_NV_INDEX,info->value);@@ -314,10 +351,14 @@ int chain_init(const __u8 event[32],if (rc)goto out;info->initrd = in_initrd();- register_expectation(info->pcr_before, info->phase_before,- branch_written, branch_init, policy, unwritten,- written);- rc = chain_anchor(written, info->anchor, sizeof(info->anchor));+ rc = register_writer(tpm, info->writer_public);+ if (rc)+ goto out;+ rc = register_expectation(info->pcr_before, info->phase_before,+ info->writer_public, branch_written,+ branch_init, policy, unwritten, written);+ if (!rc)+ rc = chain_anchor(written, info->anchor, sizeof(info->anchor));if (rc)goto out;@@ -360,18 +401,24 @@ out:int chain_extend(const __u8 event[32], __u8 out_chain[32]){+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];__u8 branch_written[32], branch_init[32], policy[32], values[64];struct shirudo_eventlog_init rec;struct tpm_nv_info nv;struct shirudo_tpm *tpm = NULL;+ __u32 reset;int rc;rc = eventlog_boot_init(&rec);if (rc)return -ENODEV; /* no register this boot */bind_values(rec.pcr_before, rec.phase_before, values);- tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,- branch_written, branch_init, policy);+ rc = tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2, rec.writer_public,+ branch_written, branch_init, policy);+ if (rc) {+ pr_err("tpm: compute signed register policy: %s", strerror(-rc));+ return rc;+ }rc = tpm_open(&tpm);if (rc)@@ -389,6 +436,27 @@ int chain_extend(const __u8 event[32], __u8 out_chain[32])rc = -EACCES;goto out;}+ if (!ct_eq(nv.policy, policy, sizeof(policy))) {+ pr_err("tpm: register write policy is obsolete or substituted");+ rc = -EACCES;+ goto out;+ }+ rc = tpm_reset_count(tpm, &reset);+ if (rc)+ goto out;+ if (!rec.have_reset_count || reset != rec.reset_count) {+ pr_err("tpm: register reset differs from the init record");+ rc = -EACCES;+ goto out;+ }+ rc = register_writer(tpm, writer_public);+ if (rc)+ goto out;+ if (!ct_eq(writer_public, rec.writer_public, sizeof(writer_public))) {+ pr_err("tpm: recorded NV writer differs from the guarded key");+ rc = -EKEYREJECTED;+ goto out;+ }rc = tpm_nv_extend(tpm, SHIRUDO_NV_INDEX, branch_written,branch_init, event);if (rc) {@@ -432,8 +500,10 @@ bool chain_bound(void)int chain_status(struct chain_status *st){- struct tpm_nv_info nv;+ __u8 branch_written[32], branch_init[32], policy[32], values[64];+ __u8 expected[SHIRUDO_TPM_NAME_LEN];struct shirudo_tpm *tpm = NULL;+ struct tpm_nv_info nv;__u32 reset_count = 0;int rc;@@ -460,6 +530,21 @@ int chain_status(struct chain_status *st)name_equals(&nv, st->init.name) &&(!st->init.have_reset_count ||st->init.reset_count == reset_count);+ if (st->anchored) {+ bind_values(st->init.pcr_before, st->init.phase_before, values);+ rc = tpm_register_policy(SHIRUDO_CHAIN_PCR_MASK, values, 2,+ st->init.writer_public, branch_written,+ branch_init, policy);+ if (rc) {+ pr_err("tpm: compute signed register policy: %s", strerror(-rc));+ st->anchored = false;+ goto out;+ }+ tpm_nv_name(SHIRUDO_NV_INDEX,+ SHIRUDO_TPM_REGISTER_ATTRS |+ SHIRUDO_TPM_NV_WRITTEN, policy, 32, expected);+ st->anchored = name_equals(&nv, expected);+ }out:tpm_close(tpm);return rc;diff --git a/util/chain.h b/util/chain.hindex 219dbcc1..1b298339 100644--- a/util/chain.h+++ b/util/chain.h@@ -72,6 +72,7 @@ int chain_anchor(const __u8 name[SHIRUDO_TPM_NAME_LEN],char *out, size_t cap);struct shirudo_chain_init_info {+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];bool reused; /* written and on record */bool initrd; /* bound while the initrd ran */__u32 reset_count; /* the TPM's, for the record */diff --git a/util/eventlog.c b/util/eventlog.cindex d95408e6..1cd2029a 100644--- a/util/eventlog.c+++ b/util/eventlog.c@@ -337,6 +337,7 @@ int eventlog_replay(__u8 out[32])static int init_line(const char *line, size_t len, bool is_init, void *ctx){+ char writer_public[2 * SHIRUDO_TPM_NV_RSA_BYTES + 1];char pcr_before[2 * 32 + 1], name[2 * SHIRUDO_TPM_NAME_LEN + 1];char phase_before[2 * 32 + 1], anchor[2 * 32 + 1], bound_in[8];struct shirudo_eventlog_init *out = ctx;@@ -355,20 +356,24 @@ static int init_line(const char *line, size_t len, bool is_init, void *ctx)sizeof(phase_before), 0, NULL, 0 },{ "bound_in", SHIRUDO_JSON_T_STRING, bound_in, sizeof(bound_in),0, NULL, 0 },+ { "nv_writer_public", SHIRUDO_JSON_T_STRING, writer_public,+ sizeof(writer_public), 0, NULL, 0 },};bool have_reset_count;if (!is_init)return 0;bound_in[0] = '\0';- if (json_parse(line, len, f, 7) || !f[0].seen || !f[1].seen ||+ if (json_parse(line, len, f, 8) || !f[0].seen || !f[1].seen ||!f[2].seen || !f[3].seen || !f[5].seen || pcr < 0 || pcr >= 24 ||(f[4].seen && (reset_count < 0 || reset_count > UINT32_MAX)) ||strlen(name) != 2 * SHIRUDO_TPM_NAME_LEN ||hex_decode(pcr_before, out->pcr_before, 32) ||hex_decode(phase_before, out->phase_before, 32) ||hex_decode(name, out->name, SHIRUDO_TPM_NAME_LEN) ||- hex_decode(anchor, out->anchor_digest, 32))+ hex_decode(anchor, out->anchor_digest, 32) || !f[7].seen ||+ hex_decode(writer_public, out->writer_public,+ sizeof(out->writer_public)))return -EINVAL;out->initrd = f[6].seen && !strcmp(bound_in, "initrd");/*diff --git a/util/eventlog.h b/util/eventlog.hindex 163f7042..043936cb 100644--- a/util/eventlog.h+++ b/util/eventlog.h@@ -51,6 +51,7 @@ int eventlog_replay(__u8 out[SHIRUDO_TPM_DIGEST_LEN]);* the last one names means the register was bound again.*/struct shirudo_eventlog_init {+ __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES];__u32 pcr;bool initrd; /* bound while the initrd ran */__u8 pcr_before[SHIRUDO_TPM_DIGEST_LEN];diff --git a/util/tpm_wire.c b/util/tpm_wire.cindex 494a052e..f60046f9 100644--- a/util/tpm_wire.c+++ b/util/tpm_wire.c@@ -242,10 +242,11 @@ int tpm_register_policy_final(const __u8 branch_written[32],return 0;}-void tpm_register_policy(__u32 pcr_mask,- const __u8 *pcr_values, size_t n_values,- __u8 branch_written[32],- __u8 branch_init[32], __u8 policy[32])+int tpm_register_policy(__u32 pcr_mask,+ const __u8 *pcr_values, size_t n_values,+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],+ __u8 branch_written[32],+ __u8 branch_init[32], __u8 policy[32]){static const __u8 zero[32];__u8 pcr_digest[32], unwritten[32];@@ -255,6 +256,7 @@ void tpm_register_policy(__u32 pcr_mask,sha256_buffer(pcr_values, 32 * n_values, pcr_digest);tpm_policy_pcr(unwritten, pcr_mask, pcr_digest, branch_init);tpm_policy_or(branch_written, branch_init, policy);+ return 0;}void tpm_nv_name(__u32 index, __u32 attrs,diff --git a/util/tpm_wire.h b/util/tpm_wire.hindex 0d8a3b79..37735fbe 100644--- a/util/tpm_wire.h+++ b/util/tpm_wire.h@@ -101,11 +101,12 @@ void tpm_policy_or(const __u8 a[SHIRUDO_TPM_DIGEST_LEN],* PolicyNvWritten(NO) + PolicyPCR(mask, SHA-256(values))). The two* branch digests are what a session has to present to PolicyOR.*/-void tpm_register_policy(__u32 pcr_mask,- const __u8 *pcr_values, size_t n_values,- __u8 branch_written[32],- __u8 branch_init[32],- __u8 policy[32]);+int tpm_register_policy(__u32 pcr_mask,+ const __u8 *pcr_values, size_t n_values,+ const __u8 writer_public[SHIRUDO_TPM_NV_RSA_BYTES],+ __u8 branch_written[32],+ __u8 branch_init[32],+ __u8 policy[32]);/* Name of an NV index: nameAlg || SHA-256(TPMS_NV_PUBLIC). */void tpm_nv_name(__u32 index, __u32 attrs,