Attention is currently required from: Michał Żygowski, Maciej Pijanowski, Christian Walter, Krystian Hebel.
Hello Michał Żygowski, Maciej Pijanowski, Krystian Hebel,
I'd like you to do a code review. Please visit
https://review.coreboot.org/c/coreboot/+/68747
to review the following change.
Change subject: [WIP] security/tpm: add TPM log format as per 1.2 spec ......................................................................
[WIP] security/tpm: add TPM log format as per 1.2 spec
Used by default for all boards with TPM1 which don't specify log format explicitly.
Ticket: https://ticket.coreboot.org/issues/423 Change-Id: I89720615a75573d44dd0a39ad3d7faa78f125843 Signed-off-by: Sergii Dmytruk sergii.dmytruk@3mdeb.com --- M src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h M src/security/tpm/Kconfig M src/security/tpm/Makefile.inc A src/security/tpm/tpm12_log_serialized.h M src/security/tpm/tspi.h M src/security/tpm/tspi/crtm.h A src/security/tpm/tspi/log-tpm12.c 7 files changed, 267 insertions(+), 3 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/47/68747/1
diff --git a/src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h b/src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h index 0cf9c7f..6970c63 100644 --- a/src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h +++ b/src/commonlib/bsd/include/commonlib/bsd/cbmem_id.h @@ -59,10 +59,11 @@ #define CBMEM_ID_STAGEx_CACHE 0x57a9e100 #define CBMEM_ID_STAGEx_RAW 0x57a9e200 #define CBMEM_ID_STORAGE_DATA 0x53746f72 -#define CBMEM_ID_TCPA_LOG 0x54435041 -#define CBMEM_ID_TCPA_TCG_LOG 0x54445041 +#define CBMEM_ID_TCPA_LOG 0x54435041 /* TPM log in coreboot-specific format */ +#define CBMEM_ID_TCPA_TCG_LOG 0x54445041 /* TPM1 ACPI log */ +#define CBMEM_ID_TPM_LOG 0x54504D4C /* TPM log per specification (either one) */ #define CBMEM_ID_TIMESTAMP 0x54494d45 -#define CBMEM_ID_TPM2_TCG_LOG 0x54504d32 +#define CBMEM_ID_TPM2_TCG_LOG 0x54504d32 /* TPM2 ACPI log */ #define CBMEM_ID_TPM_PPI 0x54505049 #define CBMEM_ID_VBOOT_HANDOFF 0x780074f0 /* deprecated */ #define CBMEM_ID_VBOOT_SEL_REG 0x780074f1 /* deprecated */ @@ -137,6 +138,7 @@ { CBMEM_ID_STORAGE_DATA, "SD/MMC/eMMC" }, \ { CBMEM_ID_TCPA_LOG, "TCPA LOG " }, \ { CBMEM_ID_TCPA_TCG_LOG, "TCPA TCGLOG" }, \ + { CBMEM_ID_TPM_LOG, "TPM STD LOG" }, \ { CBMEM_ID_TIMESTAMP, "TIME STAMP " }, \ { CBMEM_ID_TPM2_TCG_LOG, "TPM2 TCGLOG" }, \ { CBMEM_ID_TPM_PPI, "TPM PPI " }, \ diff --git a/src/security/tpm/Kconfig b/src/security/tpm/Kconfig index bc52d1a..254a951 100644 --- a/src/security/tpm/Kconfig +++ b/src/security/tpm/Kconfig @@ -102,16 +102,25 @@ config USE_TPM_LOG_CB bool default NEED_VBOOT_COMPATIBILITY +config USE_TPM_LOG_TPM12 + bool + default n
choice prompt "TPM event log format" depends on TPM_MEASURED_BOOT default TPM_LOG_CB if USE_TPM_LOG_CB + default TPM_LOG_TPM12 if TPM1 || USE_TPM_LOG_TPM12
config TPM_LOG_CB bool "coreboot's custom format" help Custom coreboot-specific format of the log derived from TPM1 log format. +config TPM_LOG_TPM12 + bool "TPM 1.2 format" + help + Log per TPM 1.2 specification. + See "TCG PC Client Specific Implementation Specification for Conventional BIOS".
endchoice
diff --git a/src/security/tpm/Makefile.inc b/src/security/tpm/Makefile.inc index 7083c00..4540534 100644 --- a/src/security/tpm/Makefile.inc +++ b/src/security/tpm/Makefile.inc @@ -61,4 +61,10 @@ postcar-$(CONFIG_TPM_LOG_CB) += tspi/log.c bootblock-$(CONFIG_TPM_LOG_CB) += tspi/log.c
+ramstage-$(CONFIG_TPM_LOG_TPM12) += tspi/log-tpm12.c +romstage-$(CONFIG_TPM_LOG_TPM12) += tspi/log-tpm12.c +verstage-$(CONFIG_TPM_LOG_TPM12) += tspi/log-tpm12.c +postcar-$(CONFIG_TPM_LOG_TPM12) += tspi/log-tpm12.c +bootblock-$(CONFIG_TPM_LOG_TPM12) += tspi/log-tpm12.c + endif # CONFIG_TPM_MEASURED_BOOT diff --git a/src/security/tpm/tpm12_log_serialized.h b/src/security/tpm/tpm12_log_serialized.h new file mode 100644 index 0000000..51eb287 --- /dev/null +++ b/src/security/tpm/tpm12_log_serialized.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __TPM12_LOG_SERIALIZED_H__ +#define __TPM12_LOG_SERIALIZED_H__ + +#include <stdint.h> + +#define TCPA_SPEC_ID_EVENT_SIGNATURE "Spec ID Event00" + +#define MAX_TCPA_LOG_ENTRIES 50 +#define TCPA_DIGEST_MAX_LENGTH 20 +#define TCPA_PCR_HASH_NAME 50 +#define TCPA_PCR_HASH_LEN 10 +/* Assumption of 2K TCPA log size reserved for CAR/SRAM */ +#define MAX_PRERAM_TCPA_LOG_ENTRIES 15 + +struct tcpa_entry { + uint32_t pcr; + uint32_t event_type; + uint8_t digest[TCPA_DIGEST_MAX_LENGTH]; + uint32_t name_length; + char name[TCPA_PCR_HASH_NAME]; +} __packed; + +struct spec_id_event_data { + char signature[16]; + uint32_t platform_class; + uint8_t spec_version_minor; + uint8_t spec_version_major; + uint8_t spec_errata; + uint8_t reserved; + uint8_t vendor_info_size; +} __packed; + +struct tcpa_table { + /* The first entry of the log is inlined and describes the log itself */ + uint32_t pcr; + uint32_t event_type; + uint8_t digest[TCPA_DIGEST_MAX_LENGTH]; + uint32_t event_data_size; + struct spec_id_event_data spec_id; + /* Vendor info, outside of spec_id to avoid #ifdef on using tcpa_table */ + uint16_t max_entries; + uint16_t num_entries; + + struct tcpa_entry entries[0]; /* Variable number of entries */ +} __packed; + +#endif diff --git a/src/security/tpm/tspi.h b/src/security/tpm/tspi.h index aae7cb4..09b7fc0 100644 --- a/src/security/tpm/tspi.h +++ b/src/security/tpm/tspi.h @@ -9,6 +9,8 @@
#if CONFIG(TPM_LOG_CB) #include <commonlib/tcpa_log_serialized.h> +#elif CONFIG(TPM_LOG_TPM12) +#include "tpm12_log_serialized.h" #endif
#define TPM_PCR_MAX_LEN 64 diff --git a/src/security/tpm/tspi/crtm.h b/src/security/tpm/tspi/crtm.h index 5615d64..ddc25e5 100644 --- a/src/security/tpm/tspi/crtm.h +++ b/src/security/tpm/tspi/crtm.h @@ -20,6 +20,8 @@ # define TPM_MEASURE_ALGO VB2_HASH_SHA1 #elif CONFIG(TPM_LOG_CB) && CONFIG(TPM2) # define TPM_MEASURE_ALGO VB2_HASH_SHA256 +#elif CONFIG(TPM_LOG_TPM12) +# define TPM_MEASURE_ALGO VB2_HASH_SHA1 #endif
#ifndef TPM_MEASURE_ALGO diff --git a/src/security/tpm/tspi/log-tpm12.c b/src/security/tpm/tspi/log-tpm12.c new file mode 100644 index 0000000..f110978 --- /dev/null +++ b/src/security/tpm/tspi/log-tpm12.c @@ -0,0 +1,180 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Unlike log.c this implements TCPA log according to TPM1.2 specification + * rather then using coreboot-specific log format. + */ + +#include <endian.h> +#include <console/console.h> +#include <security/tpm/tspi.h> +#include <string.h> +#include <symbols.h> +#include <cbmem.h> +#include <bootstate.h> +#include <vb2_sha.h> + +#define EV_NO_ACTION 0x03 +#define EV_ACTION 0x05 + +static struct tcpa_table *tcpa_cbmem_init(void) +{ + static struct tcpa_table *tclt; + if (tclt) + return tclt; + + if (cbmem_possibly_online()) { + size_t tcpa_log_len; + struct spec_id_event_data *hdr; + + tclt = cbmem_find(CBMEM_ID_TPM_LOG); + if (tclt) + return tclt; + + tcpa_log_len = sizeof(*tclt) + MAX_TCPA_LOG_ENTRIES * sizeof(tclt->entries[0]); + tclt = cbmem_add(CBMEM_ID_TPM_LOG, tcpa_log_len); + if (!tclt) + return NULL; + + memset(tclt, 0, sizeof(*tclt)); + hdr = &tclt->spec_id; + + /* Fill in first "header" entry. */ + tclt->event_type = htole32(EV_NO_ACTION); + tclt->event_data_size = htole32(sizeof(*hdr) + hdr->vendor_info_size); + strcpy(hdr->signature, TCPA_SPEC_ID_EVENT_SIGNATURE); + hdr->platform_class = htole32(0x00); // client platform + hdr->spec_version_minor = 0x02; + hdr->spec_version_major = 0x01; + hdr->spec_errata = 0x01; + hdr->vendor_info_size = sizeof(tclt->max_entries) + sizeof(tclt->num_entries); + + tclt->max_entries = MAX_TCPA_LOG_ENTRIES; + tclt->num_entries = 0; + } + + return tclt; +} + +struct tcpa_table *tcpa_log_init(void) +{ + static struct tcpa_table *tclt; + + /* We are dealing here with pre CBMEM environment. + * If cbmem isn't available use CAR or SRAM */ + if (!cbmem_possibly_online() && + !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) { + return (struct tcpa_table *)_tpm_tcpa_log; + } else if (ENV_ROMSTAGE && + !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) { + tclt = tcpa_cbmem_init(); + if (!tclt) + return (struct tcpa_table *)_tpm_tcpa_log; + } else { + tclt = tcpa_cbmem_init(); + } + + return tclt; +} + +void tcpa_log_dump(void *unused) +{ + int i, j; + struct tcpa_table *tclt; + + tclt = tcpa_log_init(); + if (!tclt) + return; + + printk(BIOS_INFO, "coreboot TCPA measurements:\n\n"); + for (i = 0; i < tclt->num_entries; i++) { + struct tcpa_entry *tce = &tclt->entries[i]; + + printk(BIOS_INFO, " PCR-%u ", le32toh(tce->pcr)); + + for (j = 0; j < TCPA_DIGEST_MAX_LENGTH; j++) + printk(BIOS_INFO, "%02x", tce->digest[j]); + + printk(BIOS_INFO, " %s [%s]\n", "SHA1", tce->name); + } + printk(BIOS_INFO, "\n"); +} + +void tcpa_log_add_table_entry(const char *name, const uint32_t pcr, + enum vb2_hash_algorithm digest_algo, + const uint8_t *digest, + const size_t digest_len) +{ + struct tcpa_table *tclt; + struct tcpa_entry *tce; + + tclt = tcpa_log_init(); + if (!tclt) { + printk(BIOS_WARNING, "TCPA: Log non-existent!\n"); + return; + } + + if (!name) { + printk(BIOS_WARNING, "TCPA: TCPA entry name not set\n"); + return; + } + + if (tclt->num_entries >= tclt->max_entries) { + printk(BIOS_WARNING, "TCPA: TCPA log table is full\n"); + return; + } + + tce = &tclt->entries[tclt->num_entries++]; + + tce->pcr = htole32(pcr); + tce->event_type = htole32(EV_ACTION); + + memcpy(tce->digest, digest, digest_len); + + tce->name_length = htole32(TCPA_PCR_HASH_NAME); + strncpy(tce->name, name, TCPA_PCR_HASH_NAME - 1); +} + +void tcpa_preram_log_clear(void) +{ + printk(BIOS_INFO, "TCPA: Clearing coreboot TCPA log\n"); + struct tcpa_table *tclt = (struct tcpa_table *)_tpm_tcpa_log; + tclt->max_entries = MAX_TCPA_LOG_ENTRIES; + tclt->num_entries = 0; +} + +#if !CONFIG(VBOOT_RETURN_FROM_VERSTAGE) +static void recover_tcpa_log(int is_recovery) +{ + struct tcpa_table *preram_log = (struct tcpa_table *)_tpm_tcpa_log; + struct tcpa_table *ram_log = NULL; + int i; + + if (preram_log->num_entries > MAX_PRERAM_TCPA_LOG_ENTRIES) { + printk(BIOS_WARNING, + "TCPA: Pre-RAM TCPA log is too full, possible corruption\n"); + return; + } + + ram_log = tcpa_cbmem_init(); + if (!ram_log) { + printk(BIOS_WARNING, "TCPA: CBMEM not available something went wrong\n"); + return; + } + + for (i = 0; i < preram_log->num_entries; i++) { + struct tcpa_entry *tce = &ram_log->entries[ram_log->num_entries++]; + + tce->pcr = preram_log->entries[i].pcr; + tce->event_type = preram_log->entries[i].event_type; + + memcpy(tce->digest, preram_log->entries[i].digest, TCPA_DIGEST_MAX_LENGTH); + + tce->name_length = htole32(TCPA_PCR_HASH_NAME); + strncpy(tce->name, preram_log->entries[i].name, TCPA_PCR_HASH_NAME - 1); + } +} +CBMEM_CREATION_HOOK(recover_tcpa_log); +#endif + +BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, tcpa_log_dump, NULL);