From 3ee5e9d5f3eff01f35b12d4bd4133720f05594fb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 24 Oct 2023 22:19:48 +0200 Subject: [PATCH 1/2] tpm2-util: dont't find best PCR bank if no PCRs are selected whatsoever This will otherwise just yield weird log message, complaining that PCRs were not initialized. But which PCRs if we have none selected? --- src/shared/tpm2-util.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 8d50971356..4bb17157e2 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -2539,6 +2539,12 @@ int tpm2_get_best_pcr_bank( assert(c); assert(ret); + if (pcr_mask == 0) { + log_debug("Asked to pick best PCR bank but no PCRs selected we could derive this from. Defaulting to SHA256."); + *ret = TPM2_ALG_SHA256; /* if no PCRs are selected this doesn't matter anyway... */ + return 0; + } + FOREACH_TPMS_PCR_SELECTION_IN_TPML_PCR_SELECTION(selection, &c->capability_pcrs) { TPMI_ALG_HASH hash = selection->hash; int good; From 4af7f27aeb670a80ca2c64967718958ed52870f6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 24 Oct 2023 23:14:23 +0200 Subject: [PATCH 2/2] tpm2-util: add line breaks in compound struct init, like we usually do --- src/shared/tpm2-util.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 4bb17157e2..70912930b8 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -1235,7 +1235,9 @@ int tpm2_get_or_create_srk( return 0; /* 0 → SRK already set up */ /* No SRK, create and persist one */ - TPM2B_PUBLIC template = { .size = sizeof(TPMT_PUBLIC), }; + TPM2B_PUBLIC template = { + .size = sizeof(TPMT_PUBLIC), + }; r = tpm2_get_best_srk_template(c, &template.publicArea); if (r < 0) return log_debug_errno(r, "Could not get best SRK template: %m"); @@ -2764,7 +2766,9 @@ int tpm2_digest_many( if (extend) sha256_process_bytes(digest->buffer, digest->size, &ctx); else { - *digest = (TPM2B_DIGEST){ .size = SHA256_DIGEST_SIZE, }; + *digest = (TPM2B_DIGEST) { + .size = SHA256_DIGEST_SIZE, + }; if (n_data == 0) /* If not extending and no data, return zero hash */ return 0; } @@ -4218,7 +4222,9 @@ int tpm2_unseal(Tpm2Context *c, if (r < 0) return r; } else if (primary_alg != 0) { - TPM2B_PUBLIC template = { .size = sizeof(TPMT_PUBLIC), }; + TPM2B_PUBLIC template = { + .size = sizeof(TPMT_PUBLIC), + }; r = tpm2_get_legacy_template(primary_alg, &template.publicArea); if (r < 0) return log_debug_errno(r, "Could not get legacy template: %m");