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

8ad338
commit dbac77ddbccb23d507758c591fad622e2b6e6324
8ad338
Author: Tomas Korbar <tkorbar@redhat.com>
8ad338
Date:   Mon Feb 14 12:20:25 2022 +0100
8ad338
8ad338
    CVE-2021-45960
8ad338
8ad338
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
8ad338
index 2821c6f..c45be0c 100644
8ad338
--- a/lib/xmlparse.c
8ad338
+++ b/lib/xmlparse.c
8ad338
@@ -3341,7 +3341,12 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
8ad338
   if (nPrefixes) {
8ad338
     int j;  /* hash table index */
8ad338
     unsigned long version = parser->m_nsAttsVersion;
8ad338
-    int nsAttsSize = (int)1 << parser->m_nsAttsPower;
8ad338
+    /* Detect and prevent invalid shift */
8ad338
+    if (parser->m_nsAttsPower >= sizeof(unsigned int) * 8 /* bits per byte */) {
8ad338
+      return XML_ERROR_NO_MEMORY;
8ad338
+    }
8ad338
+
8ad338
+    unsigned int nsAttsSize = 1u << parser->m_nsAttsPower;
8ad338
     unsigned char oldNsAttsPower = parser->m_nsAttsPower;
8ad338
     /* size of hash table must be at least 2 * (# of prefixed attributes) */
8ad338
     if ((nPrefixes << 1) >> parser->m_nsAttsPower) {  /* true for m_nsAttsPower = 0 */
8ad338
@@ -3350,7 +3355,28 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
8ad338
       while (nPrefixes >> parser->m_nsAttsPower++);
8ad338
       if (parser->m_nsAttsPower < 3)
8ad338
         parser->m_nsAttsPower = 3;
8ad338
-      nsAttsSize = (int)1 << parser->m_nsAttsPower;
8ad338
+
8ad338
+      /* Detect and prevent invalid shift */
8ad338
+      if (parser->m_nsAttsPower >= sizeof(nsAttsSize) * 8 /* bits per byte */) {
8ad338
+        /* Restore actual size of memory in m_nsAtts */
8ad338
+        parser->m_nsAttsPower = oldNsAttsPower;
8ad338
+        return XML_ERROR_NO_MEMORY;
8ad338
+      }
8ad338
+
8ad338
+      nsAttsSize = 1u << parser->m_nsAttsPower;
8ad338
+
8ad338
+      /* Detect and prevent integer overflow.
8ad338
+       * The preprocessor guard addresses the "always false" warning
8ad338
+       * from -Wtype-limits on platforms where
8ad338
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
8ad338
+#if UINT_MAX >= SIZE_MAX
8ad338
+      if (nsAttsSize > (size_t)(-1) / sizeof(NS_ATT)) {
8ad338
+        /* Restore actual size of memory in m_nsAtts */
8ad338
+        parser->m_nsAttsPower = oldNsAttsPower;
8ad338
+        return XML_ERROR_NO_MEMORY;
8ad338
+      }
8ad338
+#endif
8ad338
+
8ad338
       temp = (NS_ATT *)REALLOC(parser, parser->m_nsAtts, nsAttsSize * sizeof(NS_ATT));
8ad338
       if (!temp) {
8ad338
         /* Restore actual size of memory in m_nsAtts */