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

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