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

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