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

4e0c08
From 9f93e8036e842329863bf20395b8fb8f73834d9e Mon Sep 17 00:00:00 2001
4e0c08
From: Sebastian Pipping <sebastian@pipping.org>
4e0c08
Date: Thu, 30 Dec 2021 22:46:03 +0100
4e0c08
Subject: [PATCH] lib: Prevent integer overflow at multiple places
4e0c08
 (CVE-2022-22822 to CVE-2022-22827)
4e0c08
4e0c08
The involved functions are:
4e0c08
- addBinding (CVE-2022-22822)
4e0c08
- build_model (CVE-2022-22823)
4e0c08
- defineAttribute (CVE-2022-22824)
4e0c08
- lookup (CVE-2022-22825)
4e0c08
- nextScaffoldPart (CVE-2022-22826)
4e0c08
- storeAtts (CVE-2022-22827)
4e0c08
---
4e0c08
 expat/lib/xmlparse.c | 153 ++++++++++++++++++++++++++++++++++++++++++-
4e0c08
 1 file changed, 151 insertions(+), 2 deletions(-)
4e0c08
4e0c08
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
4e0c08
index 8f243126..575e73ee 100644
4e0c08
--- a/lib/xmlparse.c
4e0c08
+++ b/lib/xmlparse.c
4e0c08
@@ -3261,13 +3261,38 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr,
4e0c08
 
4e0c08
   /* get the attributes from the tokenizer */
4e0c08
   n = XmlGetAttributes(enc, attStr, parser->m_attsSize, parser->m_atts);
4e0c08
+
4e0c08
+  /* Detect and prevent integer overflow */
4e0c08
+  if (n > INT_MAX - nDefaultAtts) {
4e0c08
+    return XML_ERROR_NO_MEMORY;
4e0c08
+  }
4e0c08
+
4e0c08
   if (n + nDefaultAtts > parser->m_attsSize) {
4e0c08
     int oldAttsSize = parser->m_attsSize;
4e0c08
     ATTRIBUTE *temp;
4e0c08
 #ifdef XML_ATTR_INFO
4e0c08
     XML_AttrInfo *temp2;
4e0c08
 #endif
4e0c08
+
4e0c08
+    /* Detect and prevent integer overflow */
4e0c08
+    if ((nDefaultAtts > INT_MAX - INIT_ATTS_SIZE)
4e0c08
+        || (n > INT_MAX - (nDefaultAtts + INIT_ATTS_SIZE))) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+
4e0c08
     parser->m_attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
4e0c08
+
4e0c08
+    /* Detect and prevent integer overflow.
4e0c08
+     * The preprocessor guard addresses the "always false" warning
4e0c08
+     * from -Wtype-limits on platforms where
4e0c08
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+    if ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(ATTRIBUTE)) {
4e0c08
+      parser->m_attsSize = oldAttsSize;
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+#endif
4e0c08
+
4e0c08
     temp = (ATTRIBUTE *)REALLOC(parser, (void *)parser->m_atts,
4e0c08
                                 parser->m_attsSize * sizeof(ATTRIBUTE));
4e0c08
     if (temp == NULL) {
4e0c08
@@ -3276,6 +3301,17 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr,
4e0c08
     }
4e0c08
     parser->m_atts = temp;
4e0c08
 #ifdef XML_ATTR_INFO
4e0c08
+    /* Detect and prevent integer overflow.
4e0c08
+     * The preprocessor guard addresses the "always false" warning
4e0c08
+     * from -Wtype-limits on platforms where
4e0c08
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#  if UINT_MAX >= SIZE_MAX
4e0c08
+    if ((unsigned)parser->m_attsSize > (size_t)(-1) / sizeof(XML_AttrInfo)) {
4e0c08
+      parser->m_attsSize = oldAttsSize;
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+#  endif
4e0c08
+
4e0c08
     temp2 = (XML_AttrInfo *)REALLOC(parser, (void *)parser->m_attInfo,
4e0c08
                                     parser->m_attsSize * sizeof(XML_AttrInfo));
4e0c08
     if (temp2 == NULL) {
4e0c08
@@ -3610,9 +3646,31 @@ storeAtts(XML_Parser parser, const ENCODING *enc, const char *attStr,
4e0c08
   tagNamePtr->prefixLen = prefixLen;
4e0c08
   for (i = 0; localPart[i++];)
4e0c08
     ; /* i includes null terminator */
4e0c08
+
4e0c08
+  /* Detect and prevent integer overflow */
4e0c08
+  if (binding->uriLen > INT_MAX - prefixLen
4e0c08
+      || i > INT_MAX - (binding->uriLen + prefixLen)) {
4e0c08
+    return XML_ERROR_NO_MEMORY;
4e0c08
+  }
4e0c08
+
4e0c08
   n = i + binding->uriLen + prefixLen;
4e0c08
   if (n > binding->uriAlloc) {
4e0c08
     TAG *p;
4e0c08
+
4e0c08
+    /* Detect and prevent integer overflow */
4e0c08
+    if (n > INT_MAX - EXPAND_SPARE) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+    /* Detect and prevent integer overflow.
4e0c08
+     * The preprocessor guard addresses the "always false" warning
4e0c08
+     * from -Wtype-limits on platforms where
4e0c08
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+    if ((unsigned)(n + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+#endif
4e0c08
+
4e0c08
     uri = (XML_Char *)MALLOC(parser, (n + EXPAND_SPARE) * sizeof(XML_Char));
4e0c08
     if (! uri)
4e0c08
       return XML_ERROR_NO_MEMORY;
4e0c08
@@ -3708,6 +3766,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
4e0c08
   if (parser->m_freeBindingList) {
4e0c08
     b = parser->m_freeBindingList;
4e0c08
     if (len > b->uriAlloc) {
4e0c08
+      /* Detect and prevent integer overflow */
4e0c08
+      if (len > INT_MAX - EXPAND_SPARE) {
4e0c08
+        return XML_ERROR_NO_MEMORY;
4e0c08
+      }
4e0c08
+
4e0c08
+      /* Detect and prevent integer overflow.
4e0c08
+       * The preprocessor guard addresses the "always false" warning
4e0c08
+       * from -Wtype-limits on platforms where
4e0c08
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+      if ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4e0c08
+        return XML_ERROR_NO_MEMORY;
4e0c08
+      }
4e0c08
+#endif
4e0c08
+
4e0c08
       XML_Char *temp = (XML_Char *)REALLOC(
4e0c08
           parser, b->uri, sizeof(XML_Char) * (len + EXPAND_SPARE));
4e0c08
       if (temp == NULL)
4e0c08
@@ -3720,6 +3793,21 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
4e0c08
     b = (BINDING *)MALLOC(parser, sizeof(BINDING));
4e0c08
     if (! b)
4e0c08
       return XML_ERROR_NO_MEMORY;
4e0c08
+
4e0c08
+    /* Detect and prevent integer overflow */
4e0c08
+    if (len > INT_MAX - EXPAND_SPARE) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+    /* Detect and prevent integer overflow.
4e0c08
+     * The preprocessor guard addresses the "always false" warning
4e0c08
+     * from -Wtype-limits on platforms where
4e0c08
+     * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+    if ((unsigned)(len + EXPAND_SPARE) > (size_t)(-1) / sizeof(XML_Char)) {
4e0c08
+      return XML_ERROR_NO_MEMORY;
4e0c08
+    }
4e0c08
+#endif
4e0c08
+
4e0c08
     b->uri
4e0c08
         = (XML_Char *)MALLOC(parser, sizeof(XML_Char) * (len + EXPAND_SPARE));
4e0c08
     if (! b->uri) {
4e0c08
@@ -6141,7 +6229,24 @@ defineAttribute(ELEMENT_TYPE *type, ATTRIBUTE_ID *attId, XML_Bool isCdata,
4e0c08
       }
4e0c08
     } else {
4e0c08
       DEFAULT_ATTRIBUTE *temp;
4e0c08
+
4e0c08
+      /* Detect and prevent integer overflow */
4e0c08
+      if (type->allocDefaultAtts > INT_MAX / 2) {
4e0c08
+        return 0;
4e0c08
+      }
4e0c08
+
4e0c08
       int count = type->allocDefaultAtts * 2;
4e0c08
+
4e0c08
+      /* Detect and prevent integer overflow.
4e0c08
+       * The preprocessor guard addresses the "always false" warning
4e0c08
+       * from -Wtype-limits on platforms where
4e0c08
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+      if ((unsigned)count > (size_t)(-1) / sizeof(DEFAULT_ATTRIBUTE)) {
4e0c08
+        return 0;
4e0c08
+      }
4e0c08
+#endif
4e0c08
+
4e0c08
       temp = (DEFAULT_ATTRIBUTE *)REALLOC(parser, type->defaultAtts,
4e0c08
                                           (count * sizeof(DEFAULT_ATTRIBUTE)));
4e0c08
       if (temp == NULL)
4e0c08
@@ -6792,8 +6897,20 @@ lookup(XML_Parser parser, HASH_TABLE *table, KEY name, size_t createSize) {
4e0c08
     /* check for overflow (table is half full) */
4e0c08
     if (table->used >> (table->power - 1)) {
4e0c08
       unsigned char newPower = table->power + 1;
4e0c08
+
4e0c08
+      /* Detect and prevent invalid shift */
4e0c08
+      if (newPower >= sizeof(unsigned long) * 8 /* bits per byte */) {
4e0c08
+        return NULL;
4e0c08
+      }
4e0c08
+
4e0c08
       size_t newSize = (size_t)1 << newPower;
4e0c08
       unsigned long newMask = (unsigned long)newSize - 1;
4e0c08
+
4e0c08
+      /* Detect and prevent integer overflow */
4e0c08
+      if (newSize > (size_t)(-1) / sizeof(NAMED *)) {
4e0c08
+        return NULL;
4e0c08
+      }
4e0c08
+
4e0c08
       size_t tsize = newSize * sizeof(NAMED *);
4e0c08
       NAMED **newV = (NAMED **)table->mem->malloc_fcn(tsize);
4e0c08
       if (! newV)
4e0c08
@@ -7143,6 +7260,20 @@ nextScaffoldPart(XML_Parser parser) {
4e0c08
   if (dtd->scaffCount >= dtd->scaffSize) {
4e0c08
     CONTENT_SCAFFOLD *temp;
4e0c08
     if (dtd->scaffold) {
4e0c08
+      /* Detect and prevent integer overflow */
4e0c08
+      if (dtd->scaffSize > UINT_MAX / 2u) {
4e0c08
+        return -1;
4e0c08
+      }
4e0c08
+      /* Detect and prevent integer overflow.
4e0c08
+       * The preprocessor guard addresses the "always false" warning
4e0c08
+       * from -Wtype-limits on platforms where
4e0c08
+       * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+      if (dtd->scaffSize > (size_t)(-1) / 2u / sizeof(CONTENT_SCAFFOLD)) {
4e0c08
+        return -1;
4e0c08
+      }
4e0c08
+#endif
4e0c08
+
4e0c08
       temp = (CONTENT_SCAFFOLD *)REALLOC(
4e0c08
           parser, dtd->scaffold, dtd->scaffSize * 2 * sizeof(CONTENT_SCAFFOLD));
4e0c08
       if (temp == NULL)
4e0c08
@@ -7212,8 +7343,26 @@ build_model(XML_Parser parser) {
4e0c08
   XML_Content *ret;
4e0c08
   XML_Content *cpos;
4e0c08
   XML_Char *str;
4e0c08
-  int allocsize = (dtd->scaffCount * sizeof(XML_Content)
4e0c08
-                   + (dtd->contentStringLen * sizeof(XML_Char)));
4e0c08
+
4e0c08
+  /* Detect and prevent integer overflow.
4e0c08
+   * The preprocessor guard addresses the "always false" warning
4e0c08
+   * from -Wtype-limits on platforms where
4e0c08
+   * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
4e0c08
+#if UINT_MAX >= SIZE_MAX
4e0c08
+  if (dtd->scaffCount > (size_t)(-1) / sizeof(XML_Content)) {
4e0c08
+    return NULL;
4e0c08
+  }
4e0c08
+  if (dtd->contentStringLen > (size_t)(-1) / sizeof(XML_Char)) {
4e0c08
+    return NULL;
4e0c08
+  }
4e0c08
+#endif
4e0c08
+  if (dtd->scaffCount * sizeof(XML_Content)
4e0c08
+      > (size_t)(-1) - dtd->contentStringLen * sizeof(XML_Char)) {
4e0c08
+    return NULL;
4e0c08
+  }
4e0c08
+
4e0c08
+  const size_t allocsize = (dtd->scaffCount * sizeof(XML_Content)
4e0c08
+                            + (dtd->contentStringLen * sizeof(XML_Char)));
4e0c08
 
4e0c08
   ret = (XML_Content *)MALLOC(parser, allocsize);
4e0c08
   if (! ret)