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

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