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

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