From c807d55798f000c8a8eb0141302a66915f10d0a1 Mon Sep 17 00:00:00 2001 From: Sergii Dmytruk Date: Wed, 16 Nov 2022 20:52:47 +0200 Subject: [PATCH] security/tpm/tspi/log.c: fix strncpy() usage Change-Id: Ib24129829bef3764a8ba1e2c0d92bc42c5cfbc8d Signed-off-by: Sergii Dmytruk Reviewed-on: https://review.coreboot.org/c/coreboot/+/69708 Tested-by: build bot (Jenkins) Reviewed-by: Julius Werner --- src/security/tpm/tspi/log.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/security/tpm/tspi/log.c b/src/security/tpm/tspi/log.c index 96c3087f95..e33cacb87a 100644 --- a/src/security/tpm/tspi/log.c +++ b/src/security/tpm/tspi/log.c @@ -77,6 +77,8 @@ void tpm_log_add_table_entry(const char *name, const uint32_t pcr, struct tpm_cb_log_entry *tce = &tclt->entries[tclt->num_entries++]; strncpy(tce->name, name, TPM_CB_LOG_PCR_HASH_NAME - 1); + tce->name[TPM_CB_LOG_PCR_HASH_NAME - 1] = '\0'; + tce->pcr = pcr; if (digest_len > TPM_CB_LOG_DIGEST_MAX_LENGTH) { @@ -143,7 +145,10 @@ void tpm_log_copy_entries(const void *from, void *to) for (i = 0; i < from_log->num_entries; i++) { struct tpm_cb_log_entry *tce = &to_log->entries[to_log->num_entries++]; + strncpy(tce->name, from_log->entries[i].name, TPM_CB_LOG_PCR_HASH_NAME - 1); + tce->name[TPM_CB_LOG_PCR_HASH_NAME - 1] = '\0'; + tce->pcr = from_log->entries[i].pcr; if (from_log->entries[i].digest_length > TPM_CB_LOG_DIGEST_MAX_LENGTH) { @@ -153,6 +158,8 @@ void tpm_log_copy_entries(const void *from, void *to) strncpy(tce->digest_type, from_log->entries[i].digest_type, TPM_CB_LOG_PCR_HASH_LEN - 1); + tce->digest_type[TPM_CB_LOG_PCR_HASH_LEN - 1] = '\0'; + tce->digest_length = MIN(from_log->entries[i].digest_length, TPM_CB_LOG_DIGEST_MAX_LENGTH); memcpy(tce->digest, from_log->entries[i].digest, tce->digest_length);