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

4cc7ea
commit c98f9d35bdd6e4536e8b82898d241a9c99c543f8
4cc7ea
Author: Tomas Korbar <tkorbar@redhat.com>
4cc7ea
Date:   Mon Feb 21 17:27:38 2022 +0100
4cc7ea
4cc7ea
    CVE-2022-22822 to CVE-2022-22827
4cc7ea
4cc7ea
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
4cc7ea
index 1371f61..87d1a98 100644
4cc7ea
--- a/lib/xmlparse.c
4cc7ea
+++ b/lib/xmlparse.c
4cc7ea
@@ -2772,18 +2772,54 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
4cc7ea
 
4cc7ea
   /* get the attributes from the tokenizer */
4cc7ea
   n = XmlGetAttributes(enc, attStr, attsSize, atts);
4cc7ea
+
4cc7ea
+  /* Detect and prevent integer overflow */
4cc7ea
+  if (n > INT_MAX - nDefaultAtts) {
4cc7ea
+    return XML_ERROR_NO_MEMORY;
4cc7ea
+  }
4cc7ea
+
4cc7ea
   if (n + nDefaultAtts > attsSize) {
4cc7ea
     int oldAttsSize = attsSize;
4cc7ea
     ATTRIBUTE *temp;
4cc7ea
 #ifdef XML_ATTR_INFO
4cc7ea
     XML_AttrInfo *temp2;
4cc7ea
 #endif
4cc7ea
+
4cc7ea
+    /* Detect and prevent integer overflow */
4cc7ea
+    if ((nDefaultAtts > INT_MAX - INIT_ATTS_SIZE)
4cc7ea
+        || (n > INT_MAX - (nDefaultAtts + INIT_ATTS_SIZE))) {
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+
4cc7ea
     attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
4cc7ea
+
4cc7ea
+    /* Detect and prevent integer overflow.
4cc7ea
+     * The preprocessor guard addresses the "always false" warning
4cc7ea
+     * from -Wtype-limits on platforms where
4cc7ea
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+    if ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(ATTRIBUTE)) {
4cc7ea
+      parser->m_attsSize = oldAttsSize;
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
     temp = (ATTRIBUTE *)REALLOC((void *)atts, attsSize * sizeof(ATTRIBUTE));
4cc7ea
     if (temp == NULL)
4cc7ea
       return XML_ERROR_NO_MEMORY;
4cc7ea
     atts = temp;
4cc7ea
 #ifdef XML_ATTR_INFO
4cc7ea
+    /* Detect and prevent integer overflow.
4cc7ea
+     * The preprocessor guard addresses the "always false" warning
4cc7ea
+     * from -Wtype-limits on platforms where
4cc7ea
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#  if UINT_MAX >= SIZE_MAX
4cc7ea
+    if ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(XML_AttrInfo)) {
4cc7ea
+      parser->m_attsSize = oldAttsSize;
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+#  endif
4cc7ea
+
4cc7ea
     temp2 = (XML_AttrInfo *)REALLOC((void *)attInfo, attsSize * sizeof(XML_AttrInfo));
4cc7ea
     if (temp2 == NULL)
4cc7ea
       return XML_ERROR_NO_MEMORY;
4cc7ea
@@ -3089,9 +3125,30 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
4cc7ea
   tagNamePtr->prefixLen = prefixLen;
4cc7ea
   for (i = 0; localPart[i++];)
4cc7ea
     ;  /* i includes null terminator */
4cc7ea
+
4cc7ea
+  /* Detect and prevent integer overflow */
4cc7ea
+  if (binding->uriLen > INT_MAX - prefixLen
4cc7ea
+      || i > INT_MAX - (binding->uriLen + prefixLen)) {
4cc7ea
+    return XML_ERROR_NO_MEMORY;
4cc7ea
+  }
4cc7ea
+
4cc7ea
   n = i + binding->uriLen + prefixLen;
4cc7ea
   if (n > binding->uriAlloc) {
4cc7ea
     TAG *p;
4cc7ea
+    /* Detect and prevent integer overflow */
4cc7ea
+    if (n > INT_MAX - EXPAND_SPARE) {
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+    /* Detect and prevent integer overflow.
4cc7ea
+     * The preprocessor guard addresses the "always false" warning
4cc7ea
+     * from -Wtype-limits on platforms where
4cc7ea
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+    if ((unsigned)(n + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
     uri = (XML_Char *)MALLOC((n + EXPAND_SPARE) * sizeof(XML_Char));
4cc7ea
     if (!uri)
4cc7ea
       return XML_ERROR_NO_MEMORY;
4cc7ea
@@ -3192,6 +3249,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
4cc7ea
   if (freeBindingList) {
4cc7ea
     b = freeBindingList;
4cc7ea
     if (len > b->uriAlloc) {
4cc7ea
+      /* Detect and prevent integer overflow */
4cc7ea
+      if (len > INT_MAX - EXPAND_SPARE) {
4cc7ea
+        return XML_ERROR_NO_MEMORY;
4cc7ea
+      }
4cc7ea
+
4cc7ea
+      /* Detect and prevent integer overflow.
4cc7ea
+       * The preprocessor guard addresses the "always false" warning
4cc7ea
+       * from -Wtype-limits on platforms where
4cc7ea
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+      if ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4cc7ea
+        return XML_ERROR_NO_MEMORY;
4cc7ea
+      }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
       XML_Char *temp = (XML_Char *)REALLOC(b->uri,
4cc7ea
                           sizeof(XML_Char) * (len + EXPAND_SPARE));
4cc7ea
       if (temp == NULL)
4cc7ea
@@ -3205,6 +3277,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
4cc7ea
     b = (BINDING *)MALLOC(sizeof(BINDING));
4cc7ea
     if (!b)
4cc7ea
       return XML_ERROR_NO_MEMORY;
4cc7ea
+
4cc7ea
+    /* Detect and prevent integer overflow */
4cc7ea
+    if (len > INT_MAX - EXPAND_SPARE) {
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+    /* Detect and prevent integer overflow.
4cc7ea
+     * The preprocessor guard addresses the "always false" warning
4cc7ea
+     * from -Wtype-limits on platforms where
4cc7ea
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+    if ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4cc7ea
+      return XML_ERROR_NO_MEMORY;
4cc7ea
+    }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
     b->uri = (XML_Char *)MALLOC(sizeof(XML_Char) * (len + EXPAND_SPARE));
4cc7ea
     if (!b->uri) {
4cc7ea
       FREE(b);
4cc7ea
@@ -5471,7 +5558,24 @@ defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
4cc7ea
     }
4cc7ea
     else {
4cc7ea
       DEFAULT_ATTRIBUTE *temp;
4cc7ea
+
4cc7ea
+      /* Detect and prevent integer overflow */
4cc7ea
+      if (type->allocDefaultAtts > INT_MAX / 2) {
4cc7ea
+        return 0;
4cc7ea
+      }
4cc7ea
+
4cc7ea
       int count = type->allocDefaultAtts * 2;
4cc7ea
+
4cc7ea
+      /* Detect and prevent integer overflow.
4cc7ea
+       * The preprocessor guard addresses the "always false" warning
4cc7ea
+       * from -Wtype-limits on platforms where
4cc7ea
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+      if ((unsigned)count > (size_t)(-1) / sizeof(DEFAULT_ATTRIBUTE)) {
4cc7ea
+        return 0;
4cc7ea
+      }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
       temp = (DEFAULT_ATTRIBUTE *)
4cc7ea
         REALLOC(type->defaultAtts, (count * sizeof(DEFAULT_ATTRIBUTE)));
4cc7ea
       if (temp == NULL)
4cc7ea
@@ -6098,8 +6202,20 @@ lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize)
4cc7ea
     /* check for overflow (table is half full) */
4cc7ea
     if (table->used >> (table->power - 1)) {
4cc7ea
       unsigned char newPower = table->power + 1;
4cc7ea
+
4cc7ea
+      /* Detect and prevent invalid shift */
4cc7ea
+      if (newPower >= sizeof(unsigned long) * 8 /* bits per byte */) {
4cc7ea
+        return NULL;
4cc7ea
+      }
4cc7ea
+
4cc7ea
       size_t newSize = (size_t)1 << newPower;
4cc7ea
       unsigned long newMask = (unsigned long)newSize - 1;
4cc7ea
+
4cc7ea
+      /* Detect and prevent integer overflow */
4cc7ea
+      if (newSize > (size_t)(-1) / sizeof(NAMED *)) {
4cc7ea
+        return NULL;
4cc7ea
+      }
4cc7ea
+
4cc7ea
       size_t tsize = newSize * sizeof(NAMED *);
4cc7ea
       NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
4cc7ea
       if (!newV)
4cc7ea
@@ -6390,6 +6506,20 @@ nextScaffoldPart(XML_Parser parser)
4cc7ea
   if (dtd->scaffCount >= dtd->scaffSize) {
4cc7ea
     CONTENT_SCAFFOLD *temp;
4cc7ea
     if (dtd->scaffold) {
4cc7ea
+      /* Detect and prevent integer overflow */
4cc7ea
+      if (dtd->scaffSize > UINT_MAX / 2u) {
4cc7ea
+        return -1;
4cc7ea
+      }
4cc7ea
+      /* Detect and prevent integer overflow.
4cc7ea
+       * The preprocessor guard addresses the "always false" warning
4cc7ea
+       * from -Wtype-limits on platforms where
4cc7ea
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+      if (dtd->scaffSize > (size_t)(-1) / 2u / sizeof(CONTENT_SCAFFOLD)) {
4cc7ea
+        return -1;
4cc7ea
+      }
4cc7ea
+#endif
4cc7ea
+
4cc7ea
       temp = (CONTENT_SCAFFOLD *)
4cc7ea
         REALLOC(dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
4cc7ea
       if (temp == NULL)
4cc7ea
@@ -6466,9 +6596,27 @@ build_model (XML_Parser parser)
4cc7ea
   XML_Content *ret;
4cc7ea
   XML_Content *cpos;
4cc7ea
   XML_Char * str;
4cc7ea
-  int allocsize = (dtd->scaffCount * sizeof(XML_Content)
4cc7ea
-                   + (dtd->contentStringLen * sizeof(XML_Char)));
4cc7ea
 
4cc7ea
+  /* Detect and prevent integer overflow.
4cc7ea
+   * The preprocessor guard addresses the "always false" warning
4cc7ea
+   * from -Wtype-limits on platforms where
4cc7ea
+   * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4cc7ea
+#if UINT_MAX >= SIZE_MAX
4cc7ea
+  if (dtd->scaffCount > (size_t)(-1) / sizeof(XML_Content)) {
4cc7ea
+    return NULL;
4cc7ea
+  }
4cc7ea
+  if (dtd->contentStringLen > (size_t)(-1) / sizeof(XML_Char)) {
4cc7ea
+    return NULL;
4cc7ea
+  }
4cc7ea
+#endif
4cc7ea
+  if (dtd->scaffCount * sizeof(XML_Content)
4cc7ea
+      > (size_t)(-1) - dtd->contentStringLen * sizeof(XML_Char)) {
4cc7ea
+    return NULL;
4cc7ea
+  }
4cc7ea
+
4cc7ea
+  const size_t allocsize = (dtd->scaffCount * sizeof(XML_Content)
4cc7ea
+                            + (dtd->contentStringLen * sizeof(XML_Char)));
4cc7ea
+ 
4cc7ea
   ret = (XML_Content *)MALLOC(allocsize);
4cc7ea
   if (!ret)
4cc7ea
     return NULL;