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

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