Blame SOURCES/expat-2.2.10-Detect-and-prevent-troublesome-left-shifts.patch

4e0c08
From 0adcb34c49bee5b19bd29b16a578c510c23597ea Mon Sep 17 00:00:00 2001
4e0c08
From: Sebastian Pipping <sebastian@pipping.org>
4e0c08
Date: Mon, 27 Dec 2021 20:15:02 +0100
4e0c08
Subject: [PATCH] lib: Detect and prevent troublesome left shifts in function
4e0c08
 storeAtts (CVE-2021-45960)
4e0c08
4e0c08
---
4e0c08
 expat/lib/xmlparse.c | 31 +++++++++++++++++++++++++++++--
4e0c08
 1 file changed, 29 insertions(+), 2 deletions(-)
4e0c08
4e0c08
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
4e0c08
index d730f41c3..b47c31b05 100644
4e0c08
--- a/lib/xmlparse.c
4e0c08
+++ b/lib/xmlparse.c
4e0c08
@@ -3414,7 +3414,13 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr,
4e0c08
   if (nPrefixes) {
4e0c08
     int j; /* hash table index */
4e0c08
     unsigned long version = parser->m_nsAttsVersion;
4e0c08
-    int nsAttsSize = (int)1 << parser->m_nsAttsPower;
4e0c08
+
4e0c08
+    /* Detect and prevent invalid shift */
4e0c08
+    if (parser->m_nsAttsPower >= sizeof(unsigned int) * 8 /* bits per byte */) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+
4e0c08
+    unsigned int nsAttsSize = 1u << parser->m_nsAttsPower;
4e0c08
     unsigned char oldNsAttsPower = parser->m_nsAttsPower;
4e0c08
     /* size of hash table must be at least 2 * (# of prefixed attributes) */
4e0c08
     if ((nPrefixes << 1)
4e0c08
@@ -3425,7 +3431,28 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr,
4e0c08
         ;
4e0c08
       if (parser->m_nsAttsPower < 3)
4e0c08
         parser->m_nsAttsPower = 3;
4e0c08
-      nsAttsSize = (int)1 << parser->m_nsAttsPower;
4e0c08
+
4e0c08
+      /* Detect and prevent invalid shift */
4e0c08
+      if (parser->m_nsAttsPower >= sizeof(nsAttsSize) * 8 /* bits per byte */) {
4e0c08
+        /* Restore actual size of memory in m_nsAtts */
4e0c08
+        parser->m_nsAttsPower = oldNsAttsPower;
4e0c08
+        return XML_ERROR_NO_MEMORY;
4e0c08
+      }
4e0c08
+
4e0c08
+      nsAttsSize = 1u << parser->m_nsAttsPower;
4e0c08
+
4e0c08
+      /* Detect and prevent integer overflow.
4e0c08
+       * The preprocessor guard addresses the "always false" warning
4e0c08
+       * from -Wtype-limits on platforms where
4e0c08
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+      if (nsAttsSize > (size_t)(-1) / sizeof(NS_ATT)) {
4e0c08
+        /* Restore actual size of memory in m_nsAtts */
4e0c08
+        parser->m_nsAttsPower = oldNsAttsPower;
4e0c08
+        return XML_ERROR_NO_MEMORY;
4e0c08
+      }
4e0c08
+#endif
4e0c08
+
4e0c08
       temp = (NS_ATT *)REALLOC(parser, parser->m_nsAtts,
4e0c08
                                nsAttsSize * sizeof(NS_ATT));
4e0c08
       if (! temp) {