Blame SOURCES/expat-2.2.10-Prevent-integer-overflow-on-m_groupSize-in-function.patch

4e0c08
From 85ae9a2d7d0e9358f356b33977b842df8ebaec2b Mon Sep 17 00:00:00 2001
4e0c08
From: Sebastian Pipping <sebastian@pipping.org>
4e0c08
Date: Sat, 25 Dec 2021 20:52:08 +0100
4e0c08
Subject: [PATCH] lib: Prevent integer overflow on m_groupSize in function
4e0c08
 doProlog (CVE-2021-46143)
4e0c08
4e0c08
---
4e0c08
 expat/lib/xmlparse.c | 15 +++++++++++++++
4e0c08
 1 file changed, 15 insertions(+)
4e0c08
4e0c08
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
4e0c08
index b47c31b0..8f243126 100644
4e0c08
--- a/lib/xmlparse.c
4e0c08
+++ b/lib/xmlparse.c
4e0c08
@@ -5046,6 +5046,11 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end,
4e0c08
       if (parser->m_prologState.level >= parser->m_groupSize) {
4e0c08
         if (parser->m_groupSize) {
4e0c08
           {
4e0c08
+            /* Detect and prevent integer overflow */
4e0c08
+            if (parser->m_groupSize > (unsigned int)(-1) / 2u) {
4e0c08
+              return XML_ERROR_NO_MEMORY;
4e0c08
+            }
4e0c08
+
4e0c08
             char *const new_connector = (char *)REALLOC(
4e0c08
                 parser, parser->m_groupConnector, parser->m_groupSize *= 2);
4e0c08
             if (new_connector == NULL) {
4e0c08
@@ -5056,6 +5061,16 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end,
4e0c08
           }
4e0c08
 
4e0c08
           if (dtd->scaffIndex) {
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 (parser->m_groupSize > (size_t)(-1) / sizeof(int)) {
4e0c08
+              return XML_ERROR_NO_MEMORY;
4e0c08
+            }
4e0c08
+#endif
4e0c08
+
4e0c08
             int *const new_scaff_index = (int *)REALLOC(
4e0c08
                 parser, dtd->scaffIndex, parser->m_groupSize * sizeof(int));
4e0c08
             if (new_scaff_index == NULL)