Blame SOURCES/expat-2.2.5-Prevent-more-integer-overflows.patch

fa9cc6
commit 0f920007dc157e052fed2fc66a83c6c23ccec0aa
fa9cc6
Author: Tomas Korbar <tkorbar@redhat.com>
fa9cc6
Date:   Mon Feb 14 12:41:56 2022 +0100
fa9cc6
fa9cc6
    CVE-2022-22822 to CVE-2022-22827
fa9cc6
fa9cc6
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
fa9cc6
index 22d0a75..6a880af 100644
fa9cc6
--- a/lib/xmlparse.c
fa9cc6
+++ b/lib/xmlparse.c
fa9cc6
@@ -3187,13 +3187,38 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
fa9cc6
 
fa9cc6
   /* get the attributes from the tokenizer */
fa9cc6
   n = XmlGetAttributes(enc, attStr, parser->m_attsSize, parser->m_atts);
fa9cc6
+
fa9cc6
+  /* Detect and prevent integer overflow */
fa9cc6
+  if (n > INT_MAX - nDefaultAtts) {
fa9cc6
+    return XML_ERROR_NO_MEMORY;
fa9cc6
+  }
fa9cc6
+
fa9cc6
   if (n + nDefaultAtts > parser->m_attsSize) {
fa9cc6
     int oldAttsSize = parser->m_attsSize;
fa9cc6
     ATTRIBUTE *temp;
fa9cc6
 #ifdef XML_ATTR_INFO
fa9cc6
     XML_AttrInfo *temp2;
fa9cc6
 #endif
fa9cc6
+
fa9cc6
+    /* Detect and prevent integer overflow */
fa9cc6
+    if ((nDefaultAtts > INT_MAX - INIT_ATTS_SIZE)
fa9cc6
+        || (n > INT_MAX - (nDefaultAtts + INIT_ATTS_SIZE))) {
fa9cc6
+      return XML_ERROR_NO_MEMORY;
fa9cc6
+    }
fa9cc6
+
fa9cc6
     parser->m_attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
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 ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(ATTRIBUTE)) {
fa9cc6
+      parser->m_attsSize = oldAttsSize;
fa9cc6
+      return XML_ERROR_NO_MEMORY;
fa9cc6
+    }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
     temp = (ATTRIBUTE *)REALLOC(parser, (void *)parser->m_atts, parser->m_attsSize * sizeof(ATTRIBUTE));
fa9cc6
     if (temp == NULL) {
fa9cc6
       parser->m_attsSize = oldAttsSize;
fa9cc6
@@ -3201,6 +3226,17 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
fa9cc6
     }
fa9cc6
     parser->m_atts = temp;
fa9cc6
 #ifdef XML_ATTR_INFO
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 ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(XML_AttrInfo)) {
fa9cc6
+      parser->m_attsSize = oldAttsSize;
fa9cc6
+      return XML_ERROR_NO_MEMORY;
fa9cc6
+    }
fa9cc6
+#  endif
fa9cc6
+
fa9cc6
     temp2 = (XML_AttrInfo *)REALLOC(parser, (void *)parser->m_attInfo, parser->m_attsSize * sizeof(XML_AttrInfo));
fa9cc6
     if (temp2 == NULL) {
fa9cc6
       parser->m_attsSize = oldAttsSize;
fa9cc6
@@ -3535,9 +3571,30 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
fa9cc6
   tagNamePtr->prefixLen = prefixLen;
fa9cc6
   for (i = 0; localPart[i++];)
fa9cc6
     ;  /* i includes null terminator */
fa9cc6
+
fa9cc6
+  /* Detect and prevent integer overflow */
fa9cc6
+  if (binding->uriLen > INT_MAX - prefixLen
fa9cc6
+      || i > INT_MAX - (binding->uriLen + prefixLen)) {
fa9cc6
+    return XML_ERROR_NO_MEMORY;
fa9cc6
+  }
fa9cc6
+
fa9cc6
   n = i + binding->uriLen + prefixLen;
fa9cc6
   if (n > binding->uriAlloc) {
fa9cc6
     TAG *p;
fa9cc6
+    /* Detect and prevent integer overflow */
fa9cc6
+    if (n > INT_MAX - EXPAND_SPARE) {
fa9cc6
+      return XML_ERROR_NO_MEMORY;
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 ((unsigned)(n + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
fa9cc6
+      return XML_ERROR_NO_MEMORY;
fa9cc6
+    }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
     uri = (XML_Char *)MALLOC(parser, (n + EXPAND_SPARE) * sizeof(XML_Char));
fa9cc6
     if (!uri)
fa9cc6
       return XML_ERROR_NO_MEMORY;
fa9cc6
@@ -3638,6 +3695,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
fa9cc6
   if (parser->m_freeBindingList) {
fa9cc6
     b = parser->m_freeBindingList;
fa9cc6
     if (len > b->uriAlloc) {
fa9cc6
+      /* Detect and prevent integer overflow */
fa9cc6
+      if (len > INT_MAX - EXPAND_SPARE) {
fa9cc6
+        return XML_ERROR_NO_MEMORY;
fa9cc6
+      }
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 ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
fa9cc6
+        return XML_ERROR_NO_MEMORY;
fa9cc6
+      }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
       XML_Char *temp = (XML_Char *)REALLOC(parser, b->uri,
fa9cc6
                           sizeof(XML_Char) * (len + EXPAND_SPARE));
fa9cc6
       if (temp == NULL)
fa9cc6
@@ -3651,6 +3723,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
fa9cc6
     b = (BINDING *)MALLOC(parser, sizeof(BINDING));
fa9cc6
     if (!b)
fa9cc6
       return XML_ERROR_NO_MEMORY;
fa9cc6
+
fa9cc6
+    /* Detect and prevent integer overflow */
fa9cc6
+    if (len > INT_MAX - EXPAND_SPARE) {
fa9cc6
+      return XML_ERROR_NO_MEMORY;
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 ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
fa9cc6
+      return XML_ERROR_NO_MEMORY;
fa9cc6
+    }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
     b->uri = (XML_Char *)MALLOC(parser, sizeof(XML_Char) * (len + EXPAND_SPARE));
fa9cc6
     if (!b->uri) {
fa9cc6
       FREE(parser, b);
fa9cc6
@@ -6058,7 +6145,24 @@ defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
fa9cc6
     }
fa9cc6
     else {
fa9cc6
       DEFAULT_ATTRIBUTE *temp;
fa9cc6
+
fa9cc6
+      /* Detect and prevent integer overflow */
fa9cc6
+      if (type->allocDefaultAtts > INT_MAX / 2) {
fa9cc6
+        return 0;
fa9cc6
+      }
fa9cc6
+
fa9cc6
       int count = type->allocDefaultAtts * 2;
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 ((unsigned)count > (size_t)(-1) / sizeof(DEFAULT_ATTRIBUTE)) {
fa9cc6
+        return 0;
fa9cc6
+      }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
       temp = (DEFAULT_ATTRIBUTE *)
fa9cc6
         REALLOC(parser, type->defaultAtts, (count * sizeof(DEFAULT_ATTRIBUTE)));
fa9cc6
       if (temp == NULL)
fa9cc6
@@ -6733,8 +6837,20 @@ lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize)
fa9cc6
     /* check for overflow (table is half full) */
fa9cc6
     if (table->used >> (table->power - 1)) {
fa9cc6
       unsigned char newPower = table->power + 1;
fa9cc6
+
fa9cc6
+      /* Detect and prevent invalid shift */
fa9cc6
+      if (newPower >= sizeof(unsigned long) * 8 /* bits per byte */) {
fa9cc6
+        return NULL;
fa9cc6
+      }
fa9cc6
+
fa9cc6
       size_t newSize = (size_t)1 << newPower;
fa9cc6
       unsigned long newMask = (unsigned long)newSize - 1;
fa9cc6
+
fa9cc6
+      /* Detect and prevent integer overflow */
fa9cc6
+      if (newSize > (size_t)(-1) / sizeof(NAMED *)) {
fa9cc6
+        return NULL;
fa9cc6
+      }
fa9cc6
+
fa9cc6
       size_t tsize = newSize * sizeof(NAMED *);
fa9cc6
       NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
fa9cc6
       if (!newV)
fa9cc6
@@ -7100,6 +7216,20 @@ nextScaffoldPart(XML_Parser parser)
fa9cc6
   if (dtd->scaffCount >= dtd->scaffSize) {
fa9cc6
     CONTENT_SCAFFOLD *temp;
fa9cc6
     if (dtd->scaffold) {
fa9cc6
+      /* Detect and prevent integer overflow */
fa9cc6
+      if (dtd->scaffSize > UINT_MAX / 2u) {
fa9cc6
+        return -1;
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 (dtd->scaffSize > (size_t)(-1) / 2u / sizeof(CONTENT_SCAFFOLD)) {
fa9cc6
+        return -1;
fa9cc6
+      }
fa9cc6
+#endif
fa9cc6
+
fa9cc6
       temp = (CONTENT_SCAFFOLD *)
fa9cc6
         REALLOC(parser, dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
fa9cc6
       if (temp == NULL)
fa9cc6
@@ -7176,8 +7306,26 @@ build_model (XML_Parser parser)
fa9cc6
   XML_Content *ret;
fa9cc6
   XML_Content *cpos;
fa9cc6
   XML_Char * str;
fa9cc6
-  int allocsize = (dtd->scaffCount * sizeof(XML_Content)
fa9cc6
-                   + (dtd->contentStringLen * sizeof(XML_Char)));
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 (dtd->scaffCount > (size_t)(-1) / sizeof(XML_Content)) {
fa9cc6
+    return NULL;
fa9cc6
+  }
fa9cc6
+  if (dtd->contentStringLen > (size_t)(-1) / sizeof(XML_Char)) {
fa9cc6
+    return NULL;
fa9cc6
+  }
fa9cc6
+#endif
fa9cc6
+  if (dtd->scaffCount * sizeof(XML_Content)
fa9cc6
+      > (size_t)(-1) - dtd->contentStringLen * sizeof(XML_Char)) {
fa9cc6
+    return NULL;
fa9cc6
+  }
fa9cc6
+
fa9cc6
+  const size_t allocsize = (dtd->scaffCount * sizeof(XML_Content)
fa9cc6
+                            + (dtd->contentStringLen * sizeof(XML_Char)));
fa9cc6
 
fa9cc6
   ret = (XML_Content *)MALLOC(parser, allocsize);
fa9cc6
   if (!ret)