From 20f8d05e1edf1079624c6885ba027e31e93d6606 Mon Sep 17 00:00:00 2001 From: William Roberts Date: Thu, 9 Sep 2021 13:51:33 -0500 Subject: [PATCH] tpm: fix suspicious sizeof The sizeof on dest in str_padded_copy requires the src to be a fixed buffer. Since the vendor list is an array of const char pointers, sizeof on the value is the char pointer not an array of chars. Fix this by using the _ version where we can control the inputs and use strlen here since the buffer is gaurenteed to be NUL term over sizeof. Signed-off-by: William Roberts --- src/lib/tpm.c | 4 ++-- src/lib/utils.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/tpm.c b/src/lib/tpm.c index 6bf63a68..8159890d 100644 --- a/src/lib/tpm.c +++ b/src/lib/tpm.c @@ -751,8 +751,8 @@ CK_RV tpm_get_token_info (tpm_ctx *ctx, CK_TOKEN_INFO *info) { // otherwise 4 byte ID was already padded and will be used. for (unsigned int i=0; i < ARRAY_LEN(TPM2_MANUFACTURER_MAP); i++){ if (!strncasecmp((char *)info->manufacturerID, TPM2_MANUFACTURER_MAP[i][0], 4)) { - str_padded_copy(info->manufacturerID, - TPM2_MANUFACTURER_MAP[i][1]); + _str_padded_copy(info->manufacturerID, sizeof(info->manufacturerID), + (const CK_UTF8CHAR_PTR)TPM2_MANUFACTURER_MAP[i][1], strlen(TPM2_MANUFACTURER_MAP[i][1])); } } diff --git a/src/lib/utils.h b/src/lib/utils.h index 8dcb5830..a6e90e1e 100644 --- a/src/lib/utils.h +++ b/src/lib/utils.h @@ -40,7 +40,7 @@ int str_to_ul(const char *val, size_t *res); #define str_padded_copy(dst, src) _str_padded_copy(dst, sizeof(dst), (const CK_UTF8CHAR_PTR)src, strnlen((const char *)src, sizeof(src))) -static inline void _str_padded_copy(CK_UTF8CHAR_PTR dst, size_t dst_len, const CK_UTF8CHAR *src, size_t src_len) { +static inline void _str_padded_copy(CK_UTF8CHAR_PTR dst, size_t dst_len, const CK_UTF8CHAR_PTR src, size_t src_len) { memset(dst, ' ', dst_len); memcpy(dst, src, src_len); }