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

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