Blame SOURCES/0001-swtpm-Check-header-size-indicator-against-expected-s.patch

cee5f4
From 9f740868fc36761de27df3935513bdebf8852d19 Mon Sep 17 00:00:00 2001
cee5f4
From: Stefan Berger <stefanb@linux.ibm.com>
cee5f4
Date: Wed, 16 Feb 2022 11:17:47 -0500
cee5f4
Subject: [PATCH] swtpm: Check header size indicator against expected size (CID
cee5f4
 375869)
cee5f4
cee5f4
This fix addresses Coverity issue CID 375869.
cee5f4
cee5f4
Check the header size indicated in the header of the state against the
cee5f4
expected size and return an error code in case the header size indicator
cee5f4
is different. There was only one header size so far since blobheader was
cee5f4
introduced, so we don't need to deal with different sizes.
cee5f4
cee5f4
Without this fix a specially craft header could have cause out-of-bounds
cee5f4
accesses on the byte array containing the swtpm's state.
cee5f4
cee5f4
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
cee5f4
---
cee5f4
 src/swtpm/swtpm_nvstore.c | 11 ++++++++++-
cee5f4
 1 file changed, 10 insertions(+), 1 deletion(-)
cee5f4
cee5f4
diff --git a/src/swtpm/swtpm_nvstore.c b/src/swtpm/swtpm_nvstore.c
cee5f4
index 437088370e11..144d8975ec54 100644
cee5f4
--- a/src/swtpm/swtpm_nvstore.c
cee5f4
+++ b/src/swtpm/swtpm_nvstore.c
cee5f4
@@ -1075,6 +1075,7 @@ SWTPM_NVRAM_CheckHeader(unsigned char *data, uint32_t length,
cee5f4
                         uint8_t *hdrversion, bool quiet)
cee5f4
 {
cee5f4
     blobheader *bh = (blobheader *)data;
cee5f4
+    uint16_t hdrsize;
cee5f4
 
cee5f4
     if (length < sizeof(bh)) {
cee5f4
         if (!quiet)
cee5f4
@@ -1100,8 +1101,16 @@ SWTPM_NVRAM_CheckHeader(unsigned char *data, uint32_t length,
cee5f4
         return TPM_BAD_VERSION;
cee5f4
     }
cee5f4
 
cee5f4
+    hdrsize = ntohs(bh->hdrsize);
cee5f4
+    if (hdrsize != sizeof(blobheader)) {
cee5f4
+        logprintf(STDERR_FILENO,
cee5f4
+                  "bad header size: %u != %zu\n",
cee5f4
+                  hdrsize, sizeof(blobheader));
cee5f4
+        return TPM_BAD_DATASIZE;
cee5f4
+    }
cee5f4
+
cee5f4
     *hdrversion = bh->version;
cee5f4
-    *dataoffset = ntohs(bh->hdrsize);
cee5f4
+    *dataoffset = hdrsize;
cee5f4
     *hdrflags = ntohs(bh->flags);
cee5f4
 
cee5f4
     return TPM_SUCCESS;
cee5f4
-- 
cee5f4
2.34.1.428.gdcc0cd074f0c
cee5f4