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

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