Blame SOURCES/0002-utils-fix-stringop-overread-in-str_padded_copy.patch

32281e
From f633526b34c052514f3739cb1e08fdac38603eea Mon Sep 17 00:00:00 2001
32281e
From: William Roberts <william.c.roberts@intel.com>
32281e
Date: Wed, 5 May 2021 11:52:23 -0500
32281e
Subject: [PATCH 2/6] utils: fix stringop-overread in str_padded_copy
32281e
32281e
cc1: all warnings being treated as errors
32281e
| make: *** [Makefile:1953: src/lib/slot.lo] Error 1
32281e
| make: *** Waiting for unfinished jobs....
32281e
| In file included from src/lib/mutex.h:10,
32281e
| from src/lib/session_ctx.h:6,
32281e
| from src/lib/digest.h:13,
32281e
| from src/lib/tpm.c:28:
32281e
| In function 'str_padded_copy',
32281e
| inlined from 'tpm_get_token_info' at src/lib/tpm.c:742:5:
32281e
| src/lib/utils.h:42:5: error: 'strnlen' specified bound 32 exceeds source size 5 [-Werror=stringop-overread]
32281e
| 42 | memcpy(dst, src, strnlen((char *)(src), dst_len));
32281e
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32281e
| src/lib/utils.h: In function 'tpm_get_token_info':
32281e
| src/lib/tpm.c:739:19: note: source object declared here
32281e
| 739 | unsigned char manufacturerID[sizeof(UINT32)+1] = {0}; // 4 bytes + '\0' as temp storage
32281e
| | ^~~~~~~~~~~~~~
32281e
| cc1: all warnings being treated as errors
32281e
| make: *** [Makefile:1953: src/lib/tpm.lo] Error 1
32281e
| WARNING: exit code 1 from a shell command.
32281e
32281e
Fixes #676
32281e
32281e
Signed-off-by: William Roberts <william.c.roberts@intel.com>
32281e
---
32281e
 src/lib/general.c | 8 ++++----
32281e
 src/lib/general.h | 2 +-
32281e
 src/lib/slot.c    | 4 ++--
32281e
 src/lib/token.c   | 4 ++--
32281e
 src/lib/tpm.c     | 7 +++----
32281e
 src/lib/utils.h   | 6 ++++--
32281e
 6 files changed, 16 insertions(+), 15 deletions(-)
32281e
32281e
diff --git a/src/lib/general.c b/src/lib/general.c
32281e
index 9b7327c..eaddaf8 100644
32281e
--- a/src/lib/general.c
32281e
+++ b/src/lib/general.c
32281e
@@ -19,8 +19,8 @@
32281e
   #define VERSION "UNKNOWN"
32281e
 #endif
32281e
 
32281e
-#define LIBRARY_DESCRIPTION (CK_UTF8CHAR_PTR)"TPM2.0 Cryptoki"
32281e
-#define LIBRARY_MANUFACTURER (CK_UTF8CHAR_PTR)"tpm2-software.github.io"
32281e
+static const CK_UTF8CHAR LIBRARY_DESCRIPTION[] = "TPM2.0 Cryptoki";
32281e
+static const CK_UTF8CHAR LIBRARY_MANUFACTURER[] = "tpm2-software.github.io";
32281e
 
32281e
 #define CRYPTOKI_VERSION { \
32281e
            .major = CRYPTOKI_VERSION_MAJOR, \
32281e
@@ -78,8 +78,8 @@ CK_RV general_get_info(CK_INFO *info) {
32281e
 
32281e
     static CK_INFO *_info = NULL;
32281e
     if (!_info) {
32281e
-        str_padded_copy(_info_.manufacturerID, LIBRARY_MANUFACTURER, sizeof(_info_.manufacturerID));
32281e
-        str_padded_copy(_info_.libraryDescription, LIBRARY_DESCRIPTION, sizeof(_info_.libraryDescription));
32281e
+        str_padded_copy(_info_.manufacturerID, LIBRARY_MANUFACTURER);
32281e
+        str_padded_copy(_info_.libraryDescription, LIBRARY_DESCRIPTION);
32281e
 
32281e
         parse_lib_version(&_info_.libraryVersion.major,
32281e
                 &_info_.libraryVersion.minor);
32281e
diff --git a/src/lib/general.h b/src/lib/general.h
32281e
index 14a18e4..356c142 100644
32281e
--- a/src/lib/general.h
32281e
+++ b/src/lib/general.h
32281e
@@ -10,7 +10,7 @@
32281e
 #define TPM2_TOKEN_LABEL                "TPM2 PKCS#11 Token"
32281e
 #define TPM2_TOKEN_MANUFACTURER         "Intel"
32281e
 #define TPM2_TOKEN_MODEL                "TPM2 PKCS#11"
32281e
-#define TPM2_TOKEN_SERIAL_NUMBER        "0000000000000000"
32281e
+static const CK_UTF8CHAR TPM2_TOKEN_SERIAL_NUMBER[] = "0000000000000000";
32281e
 #define TPM2_TOKEN_HW_VERSION           { 0, 0 }
32281e
 #define TPM2_TOKEN_FW_VERSION           { 0, 0 }
32281e
 
32281e
diff --git a/src/lib/slot.c b/src/lib/slot.c
32281e
index 548d22b..6db5bb9 100644
32281e
--- a/src/lib/slot.c
32281e
+++ b/src/lib/slot.c
32281e
@@ -119,8 +119,8 @@ CK_RV slot_get_info (CK_SLOT_ID slot_id, CK_SLOT_INFO *info) {
32281e
         return CKR_GENERAL_ERROR;
32281e
     }
32281e
 
32281e
-    str_padded_copy(info->manufacturerID, token_info.manufacturerID, sizeof(info->manufacturerID));
32281e
-    str_padded_copy(info->slotDescription, token_info.label, sizeof(info->slotDescription));
32281e
+    str_padded_copy(info->manufacturerID, token_info.manufacturerID);
32281e
+    str_padded_copy(info->slotDescription, token_info.label);
32281e
 
32281e
     info->hardwareVersion = token_info.hardwareVersion;
32281e
     info->firmwareVersion = token_info.firmwareVersion;
32281e
diff --git a/src/lib/token.c b/src/lib/token.c
32281e
index 6d7ebd2..c721129 100644
32281e
--- a/src/lib/token.c
32281e
+++ b/src/lib/token.c
32281e
@@ -317,8 +317,8 @@ CK_RV token_get_info (token *t, CK_TOKEN_INFO *info) {
32281e
     }
32281e
 
32281e
     // Identification
32281e
-    str_padded_copy(info->label, t->label, sizeof(info->label));
32281e
-    str_padded_copy(info->serialNumber, (unsigned char*) TPM2_TOKEN_SERIAL_NUMBER, sizeof(info->serialNumber));
32281e
+    str_padded_copy(info->label, t->label);
32281e
+    str_padded_copy(info->serialNumber, TPM2_TOKEN_SERIAL_NUMBER);
32281e
 
32281e
 
32281e
     // Memory: TODO not sure what memory values should go here, the platform?
32281e
diff --git a/src/lib/tpm.c b/src/lib/tpm.c
32281e
index 1639df4..7f9f052 100644
32281e
--- a/src/lib/tpm.c
32281e
+++ b/src/lib/tpm.c
32281e
@@ -740,15 +740,14 @@ CK_RV tpm_get_token_info (tpm_ctx *ctx, CK_TOKEN_INFO *info) {
32281e
     unsigned char manufacturerID[sizeof(UINT32)+1] = {0}; // 4 bytes + '\0' as temp storage
32281e
     UINT32 manufacturer = ntohl(tpmProperties[TPM2_PT_MANUFACTURER - TPM2_PT_FIXED].value);
32281e
     memcpy(manufacturerID, (unsigned char*) &manufacturer, sizeof(uint32_t));
32281e
-    str_padded_copy(info->manufacturerID, manufacturerID, sizeof(info->manufacturerID));
32281e
+    str_padded_copy(info->manufacturerID, manufacturerID);
32281e
 
32281e
     // Map human readable Manufacturer String, if available,
32281e
     // otherwise 4 byte ID was already padded and will be used.
32281e
     for (unsigned int i=0; i < ARRAY_LEN(TPM2_MANUFACTURER_MAP); i++){
32281e
         if (!strncasecmp((char *)info->manufacturerID, TPM2_MANUFACTURER_MAP[i][0], 4)) {
32281e
             str_padded_copy(info->manufacturerID,
32281e
-                            (unsigned char *)TPM2_MANUFACTURER_MAP[i][1],
32281e
-                            sizeof(info->manufacturerID));
32281e
+                            (unsigned char *)TPM2_MANUFACTURER_MAP[i][1]);
32281e
         }
32281e
     }
32281e
 
32281e
@@ -758,7 +757,7 @@ CK_RV tpm_get_token_info (tpm_ctx *ctx, CK_TOKEN_INFO *info) {
32281e
     vendor[1] = ntohl(tpmProperties[TPM2_PT_VENDOR_STRING_2 - TPM2_PT_FIXED].value);
32281e
     vendor[2] = ntohl(tpmProperties[TPM2_PT_VENDOR_STRING_3 - TPM2_PT_FIXED].value);
32281e
     vendor[3] = ntohl(tpmProperties[TPM2_PT_VENDOR_STRING_4 - TPM2_PT_FIXED].value);
32281e
-    str_padded_copy(info->model, (unsigned char*) &vendor, sizeof(info->model));
32281e
+    str_padded_copy(info->model, (unsigned char*) &vendor);
32281e
 
32281e
     return CKR_OK;
32281e
 }
32281e
diff --git a/src/lib/utils.h b/src/lib/utils.h
32281e
index 81c61fa..cf35746 100644
32281e
--- a/src/lib/utils.h
32281e
+++ b/src/lib/utils.h
32281e
@@ -39,9 +39,11 @@
32281e
 
32281e
 int str_to_ul(const char *val, size_t *res);
32281e
 
32281e
-static inline void str_padded_copy(CK_UTF8CHAR_PTR dst, const CK_UTF8CHAR_PTR src, size_t dst_len) {
32281e
+#define str_padded_copy(dst, src) _str_padded_copy(dst, sizeof(dst), src, strnlen((const char *)src, sizeof(src)))
32281e
+static inline void _str_padded_copy(CK_UTF8CHAR_PTR dst, size_t dst_len, const CK_UTF8CHAR *src, size_t src_len) {
32281e
     memset(dst, ' ', dst_len);
32281e
-    memcpy(dst, src, strnlen((char *)(src), dst_len));
32281e
+    memcpy(dst, src, src_len);
32281e
+    LOGE("BILL(%zu): %.*s\n", dst_len, dst_len, dst);
32281e
 }
32281e
 
32281e
 twist utils_hash_pass(const twist pin, const twist salt);
32281e
-- 
32281e
2.38.1
32281e