bcd558
# erAck: backport of expat CVE-2023-52425 DoS fix
bcd558
# https://github.com/libexpat/libexpat/commit/34b598c5f594b015c513c73f06e7ced3323edbf1
bcd558
#
bcd558
--- firefox-115.9.0/parser/expat/lib/expat.h.expat-CVE-2023-52425	2024-03-11 20:36:11.000000000 +0100
bcd558
+++ firefox-115.9.0/parser/expat/lib/expat.h	2024-03-13 20:46:45.648505015 +0100
bcd558
@@ -1045,6 +1045,10 @@ XMLPARSEAPI(const XML_Feature *)
bcd558
 XML_GetFeatureList(void);
bcd558
 
bcd558
 
bcd558
+/* Added in Expat 2.6.0. */
bcd558
+XMLPARSEAPI(XML_Bool)
bcd558
+XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled);
bcd558
+
bcd558
 /* Expat follows the semantic versioning convention.
bcd558
    See http://semver.org.
bcd558
 */
bcd558
--- firefox-115.9.0/parser/expat/lib/internal.h.expat-CVE-2023-52425	2024-03-11 20:36:11.000000000 +0100
bcd558
+++ firefox-115.9.0/parser/expat/lib/internal.h	2024-03-14 00:14:39.334319725 +0100
bcd558
@@ -80,6 +80,7 @@
bcd558
 # endif
bcd558
 #endif
bcd558
 
bcd558
+#include "expat.h" // so we can use type XML_Parser below
bcd558
 
bcd558
 #ifdef __cplusplus
bcd558
 extern "C" {
bcd558
@@ -90,6 +91,9 @@ void
bcd558
 align_limit_to_full_utf8_characters(const char * from, const char ** fromLimRef);
bcd558
 
bcd558
 
bcd558
+extern XML_Bool g_reparseDeferralEnabledDefault; // written ONLY in runtests.c
bcd558
+extern unsigned int g_parseAttempts;             // used for testing only
bcd558
+
bcd558
 #ifdef __cplusplus
bcd558
 }
bcd558
 #endif
bcd558
--- firefox-115.9.0/parser/expat/lib/xmlparse.c.expat-CVE-2023-52425	2024-03-11 20:36:11.000000000 +0100
bcd558
+++ firefox-115.9.0/parser/expat/lib/xmlparse.c	2024-03-13 22:55:14.844756009 +0100
bcd558
@@ -6,6 +6,7 @@
bcd558
 
bcd558
 #define _GNU_SOURCE                     /* syscall prototype */
bcd558
 
bcd558
+#include <stdbool.h>
bcd558
 #include <stddef.h>
bcd558
 #include <string.h>                     /* memset(), memcpy() */
bcd558
 #include <assert.h>
bcd558
@@ -89,6 +90,9 @@ typedef char ICHAR;
bcd558
 /* Round up n to be a multiple of sz, where sz is a power of 2. */
bcd558
 #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
bcd558
 
bcd558
+/* Do safe (NULL-aware) pointer arithmetic */
bcd558
+#define EXPAT_SAFE_PTR_DIFF(p, q) (((p) && (q)) ? ((p) - (q)) : 0)
bcd558
+
bcd558
 /* Handle the case where memmove() doesn't exist. */
bcd558
 #ifndef HAVE_MEMMOVE
bcd558
 #ifdef HAVE_BCOPY
bcd558
@@ -98,6 +102,8 @@ typedef char ICHAR;
bcd558
 #endif /* HAVE_BCOPY */
bcd558
 #endif /* HAVE_MEMMOVE */
bcd558
 
bcd558
+#define EXPAT_MIN(a, b) (((a) < (b)) ? (a) : (b))
bcd558
+
bcd558
 #include "internal.h"
bcd558
 #include "xmltok.h"
bcd558
 #include "xmlrole.h"
bcd558
@@ -476,6 +482,9 @@ parserInit(XML_Parser parser, const XML_
bcd558
    ? 0 \
bcd558
    : ((*((pool)->ptr)++ = c), 1))
bcd558
 
bcd558
+XML_Bool g_reparseDeferralEnabledDefault = XML_TRUE; // write ONLY in runtests.c
bcd558
+unsigned int g_parseAttempts = 0;                    // used for testing only
bcd558
+
bcd558
 struct XML_ParserStruct {
bcd558
   /* The first member must be userData so that the XML_GetUserData
bcd558
      macro works. */
bcd558
@@ -491,6 +500,9 @@ struct XML_ParserStruct {
bcd558
   const char *m_bufferLim;
bcd558
   XML_Index m_parseEndByteIndex;
bcd558
   const char *m_parseEndPtr;
bcd558
+  size_t m_partialTokenBytesBefore; /* used in heuristic to avoid O(n^2) */
bcd558
+  XML_Bool m_reparseDeferralEnabled;
bcd558
+  int m_lastBufferRequestSize;
bcd558
   XML_Char *m_dataBuf;
bcd558
   XML_Char *m_dataBufEnd;
bcd558
   XML_StartElementHandler m_startElementHandler;
bcd558
@@ -647,6 +659,9 @@ struct XML_ParserStruct {
bcd558
 #define bufferEnd (parser->m_bufferEnd)
bcd558
 #define parseEndByteIndex (parser->m_parseEndByteIndex)
bcd558
 #define parseEndPtr (parser->m_parseEndPtr)
bcd558
+#define partialTokenBytesBefore (parser->m_partialTokenBytesBefore)
bcd558
+#define reparseDeferralEnabled (parser->m_reparseDeferralEnabled)
bcd558
+#define lastBufferRequestSize (parser->m_lastBufferRequestSize)
bcd558
 #define bufferLim (parser->m_bufferLim)
bcd558
 #define dataBuf (parser->m_dataBuf)
bcd558
 #define dataBufEnd (parser->m_dataBufEnd)
bcd558
@@ -887,6 +902,47 @@ get_hash_secret_salt(XML_Parser parser)
bcd558
   return parser->m_hash_secret_salt;
bcd558
 }
bcd558
 
bcd558
+static enum XML_Error
bcd558
+callProcessor(XML_Parser parser, const char *start, const char *end,
bcd558
+              const char **endPtr) {
bcd558
+  const size_t have_now = EXPAT_SAFE_PTR_DIFF(end, start);
bcd558
+
bcd558
+  if (parser->m_reparseDeferralEnabled
bcd558
+      && ! parser->m_parsingStatus.finalBuffer) {
bcd558
+    // Heuristic: don't try to parse a partial token again until the amount of
bcd558
+    // available data has increased significantly.
bcd558
+    const size_t had_before = parser->m_partialTokenBytesBefore;
bcd558
+    // ...but *do* try anyway if we're close to causing a reallocation.
bcd558
+    size_t available_buffer
bcd558
+        = EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer);
bcd558
+#if XML_CONTEXT_BYTES > 0
bcd558
+    available_buffer -= EXPAT_MIN(available_buffer, XML_CONTEXT_BYTES);
bcd558
+#endif
bcd558
+    available_buffer
bcd558
+        += EXPAT_SAFE_PTR_DIFF(parser->m_bufferLim, parser->m_bufferEnd);
bcd558
+    // m_lastBufferRequestSize is never assigned a value < 0, so the cast is ok
bcd558
+    const bool enough
bcd558
+        = (have_now >= 2 * had_before)
bcd558
+          || ((size_t)parser->m_lastBufferRequestSize > available_buffer);
bcd558
+
bcd558
+    if (! enough) {
bcd558
+      *endPtr = start; // callers may expect this to be set
bcd558
+      return XML_ERROR_NONE;
bcd558
+    }
bcd558
+  }
bcd558
+  g_parseAttempts += 1;
bcd558
+  const enum XML_Error ret = parser->m_processor(parser, start, end, endPtr);
bcd558
+  if (ret == XML_ERROR_NONE) {
bcd558
+    // if we consumed nothing, remember what we had on this parse attempt.
bcd558
+    if (*endPtr == start) {
bcd558
+      parser->m_partialTokenBytesBefore = have_now;
bcd558
+    } else {
bcd558
+      parser->m_partialTokenBytesBefore = 0;
bcd558
+    }
bcd558
+  }
bcd558
+  return ret;
bcd558
+}
bcd558
+
bcd558
 static XML_Bool  /* only valid for root parser */
bcd558
 startParsing(XML_Parser parser)
bcd558
 {
bcd558
@@ -1075,6 +1131,9 @@ parserInit(XML_Parser parser, const XML_
bcd558
   bufferEnd = buffer;
bcd558
   parseEndByteIndex = 0;
bcd558
   parseEndPtr = NULL;
bcd558
+  partialTokenBytesBefore = 0;
bcd558
+  reparseDeferralEnabled = g_reparseDeferralEnabledDefault;
bcd558
+  lastBufferRequestSize = 0;
bcd558
   declElementType = NULL;
bcd558
   declAttributeId = NULL;
bcd558
   declEntity = NULL;
bcd558
@@ -1232,6 +1291,7 @@ XML_ExternalEntityParserCreate(XML_Parse
bcd558
      to worry which hash secrets each table has.
bcd558
   */
bcd558
   unsigned long oldhash_secret_salt;
bcd558
+  XML_Bool oldReparseDeferralEnabled;
bcd558
 
bcd558
   /* Validate the oldParser parameter before we pull everything out of it */
bcd558
   if (oldParser == NULL)
bcd558
@@ -1276,6 +1336,7 @@ XML_ExternalEntityParserCreate(XML_Parse
bcd558
      to worry which hash secrets each table has.
bcd558
   */
bcd558
   oldhash_secret_salt = hash_secret_salt;
bcd558
+  oldReparseDeferralEnabled = reparseDeferralEnabled;
bcd558
 
bcd558
 #ifdef XML_DTD
bcd558
   if (!context)
bcd558
@@ -1330,6 +1391,7 @@ XML_ExternalEntityParserCreate(XML_Parse
bcd558
   defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
bcd558
   ns_triplets = oldns_triplets;
bcd558
   hash_secret_salt = oldhash_secret_salt;
bcd558
+  reparseDeferralEnabled = oldReparseDeferralEnabled;
bcd558
   parentParser = oldParser;
bcd558
 #ifdef XML_DTD
bcd558
   paramEntityParsing = oldParamEntityParsing;
bcd558
@@ -1850,39 +1912,8 @@ XML_Parse(XML_Parser parser, const char
bcd558
     ps_parsing = XML_PARSING;
bcd558
   }
bcd558
 
bcd558
-  if (len == 0) {
bcd558
-    ps_finalBuffer = (XML_Bool)isFinal;
bcd558
-    if (!isFinal)
bcd558
-      return XML_STATUS_OK;
bcd558
-    positionPtr = bufferPtr;
bcd558
-    parseEndPtr = bufferEnd;
bcd558
-
bcd558
-    /* If data are left over from last buffer, and we now know that these
bcd558
-       data are the final chunk of input, then we have to check them again
bcd558
-       to detect errors based on that fact.
bcd558
-    */
bcd558
-    errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
bcd558
-
bcd558
-    if (errorCode == XML_ERROR_NONE) {
bcd558
-      switch (ps_parsing) {
bcd558
-      case XML_SUSPENDED:
bcd558
-        XmlUpdatePosition(encoding, positionPtr, bufferPtr, &position);
bcd558
-        positionPtr = bufferPtr;
bcd558
-        return XML_STATUS_SUSPENDED;
bcd558
-      case XML_INITIALIZED:
bcd558
-      case XML_PARSING:
bcd558
-        ps_parsing = XML_FINISHED;
bcd558
-        /* fall through */
bcd558
-      default:
bcd558
-        return XML_STATUS_OK;
bcd558
-      }
bcd558
-    }
bcd558
-    eventEndPtr = eventPtr;
bcd558
-    processor = errorProcessor;
bcd558
-    return XML_STATUS_ERROR;
bcd558
-  }
bcd558
 #ifndef XML_CONTEXT_BYTES
bcd558
-  else if (bufferPtr == bufferEnd) {
bcd558
+  if (bufferPtr == bufferEnd) {
bcd558
     const char *end;
bcd558
     int nLeftOver;
bcd558
     enum XML_Status result;
bcd558
@@ -1899,11 +1930,14 @@ XML_Parse(XML_Parser parser, const char
bcd558
        processor = errorProcessor;
bcd558
        return XML_STATUS_ERROR;
bcd558
     }
bcd558
+    // though this isn't a buffer request, we assume that `len` is the app's
bcd558
+    // preferred buffer fill size, and therefore save it here.
bcd558
+    lastBufferRequestSize = len;
bcd558
     parseEndByteIndex += len;
bcd558
     positionPtr = s;
bcd558
     ps_finalBuffer = (XML_Bool)isFinal;
bcd558
 
bcd558
-    errorCode = processor(parser, s, parseEndPtr = s + len, &end;;
bcd558
+    errorCode = callProcessor(parser, s, parseEndPtr = s + len, &end;;
bcd558
 
bcd558
     if (errorCode != XML_ERROR_NONE) {
bcd558
       eventEndPtr = eventPtr;
bcd558
@@ -1930,6 +1964,8 @@ XML_Parse(XML_Parser parser, const char
bcd558
     XmlUpdatePosition(encoding, positionPtr, end, &position);
bcd558
     nLeftOver = s + len - end;
bcd558
     if (nLeftOver) {
bcd558
+#if 0
bcd558
+// erAck: replace with XML_GetBuffer() below.
bcd558
       if (buffer == NULL || nLeftOver > bufferLim - buffer) {
bcd558
         /* avoid _signed_ integer overflow */
bcd558
         char *temp = NULL;
bcd558
@@ -1939,6 +1975,28 @@ XML_Parse(XML_Parser parser, const char
bcd558
                 ? (char *)MALLOC(bytesToAllocate)
bcd558
                 : (char *)REALLOC(buffer, bytesToAllocate));
bcd558
         }
bcd558
+#endif
bcd558
+#if 1
bcd558
+// erAck: the original patch context had a call to XML_GetBuffer() instead:
bcd558
+      // Back up and restore the parsing status to avoid XML_ERROR_SUSPENDED
bcd558
+      // (and XML_ERROR_FINISHED) from XML_GetBuffer.
bcd558
+      const enum XML_Parsing originalStatus = ps_parsing;
bcd558
+      ps_parsing = XML_PARSING;
bcd558
+      void *const temp = XML_GetBuffer(parser, nLeftOver);
bcd558
+      ps_parsing = originalStatus;
bcd558
+#endif
bcd558
+      // GetBuffer may have overwritten this, but we want to remember what the
bcd558
+      // app requested, not how many bytes were left over after parsing.
bcd558
+      lastBufferRequestSize = len;
bcd558
+#if 1
bcd558
+      if (temp == NULL) {
bcd558
+        // NOTE: parser->m_errorCode has already been set by XML_GetBuffer().
bcd558
+        eventPtr = eventEndPtr = NULL;
bcd558
+        processor = errorProcessor;
bcd558
+        return XML_STATUS_ERROR;
bcd558
+      }
bcd558
+#endif
bcd558
+#if 0
bcd558
         if (temp == NULL) {
bcd558
           errorCode = XML_ERROR_NO_MEMORY;
bcd558
           eventPtr = eventEndPtr = NULL;
bcd558
@@ -1948,6 +2006,7 @@ XML_Parse(XML_Parser parser, const char
bcd558
         buffer = temp;
bcd558
         bufferLim = buffer + bytesToAllocate;
bcd558
       }
bcd558
+#endif
bcd558
       memcpy(buffer, end, nLeftOver);
bcd558
     }
bcd558
     bufferPtr = buffer;
bcd558
@@ -1959,15 +2018,14 @@ XML_Parse(XML_Parser parser, const char
bcd558
     return result;
bcd558
   }
bcd558
 #endif  /* not defined XML_CONTEXT_BYTES */
bcd558
-  else {
bcd558
-    void *buff = XML_GetBuffer(parser, len);
bcd558
-    if (buff == NULL)
bcd558
-      return XML_STATUS_ERROR;
bcd558
-    else {
bcd558
-      memcpy(buff, s, len);
bcd558
-      return XML_ParseBuffer(parser, len, isFinal);
bcd558
-    }
bcd558
+  void *buff = XML_GetBuffer(parser, len);
bcd558
+  if (buff == NULL)
bcd558
+    return XML_STATUS_ERROR;
bcd558
+  if (len > 0) {
bcd558
+    assert(s != NULL); // make sure s==NULL && len!=0 was rejected above
bcd558
+    memcpy(buff, s, len);
bcd558
   }
bcd558
+  return XML_ParseBuffer(parser, len, isFinal);
bcd558
 }
bcd558
 
bcd558
 enum XML_Status XMLCALL
bcd558
@@ -2001,7 +2059,7 @@ XML_ParseBuffer(XML_Parser parser, int l
bcd558
   parseEndByteIndex += len;
bcd558
   ps_finalBuffer = (XML_Bool)isFinal;
bcd558
 
bcd558
-  errorCode = processor(parser, start, parseEndPtr, &bufferPtr);
bcd558
+  errorCode = callProcessor(parser, start, parseEndPtr, &bufferPtr);
bcd558
 
bcd558
   if (errorCode != XML_ERROR_NONE) {
bcd558
     eventEndPtr = eventPtr;
bcd558
@@ -2047,7 +2105,11 @@ XML_GetBuffer(XML_Parser parser, int len
bcd558
   default: ;
bcd558
   }
bcd558
 
bcd558
-  if (len > bufferLim - bufferEnd) {
bcd558
+  // whether or not the request succeeds, `len` seems to be the app's preferred
bcd558
+  // buffer fill size; remember it.
bcd558
+  lastBufferRequestSize = len;
bcd558
+  if (len > EXPAT_SAFE_PTR_DIFF(bufferLim, bufferEnd)
bcd558
+      || buffer == NULL) {
bcd558
 #ifdef XML_CONTEXT_BYTES
bcd558
     int keep;
bcd558
 #endif  /* defined XML_CONTEXT_BYTES */
bcd558
@@ -2063,7 +2125,9 @@ XML_GetBuffer(XML_Parser parser, int len
bcd558
       keep = XML_CONTEXT_BYTES;
bcd558
     neededSize += keep;
bcd558
 #endif  /* defined XML_CONTEXT_BYTES */
bcd558
-    if (neededSize  <= bufferLim - buffer) {
bcd558
+    if (buffer && bufferPtr
bcd558
+        && neededSize
bcd558
+               <= EXPAT_SAFE_PTR_DIFF(bufferLim, buffer)) {
bcd558
 #ifdef XML_CONTEXT_BYTES
bcd558
       if (keep < bufferPtr - buffer) {
bcd558
         int offset = (int)(bufferPtr - buffer) - keep;
bcd558
@@ -2072,8 +2136,11 @@ XML_GetBuffer(XML_Parser parser, int len
bcd558
         bufferPtr -= offset;
bcd558
       }
bcd558
 #else
bcd558
-      memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
bcd558
-      bufferEnd = buffer + (bufferEnd - bufferPtr);
bcd558
+      memmove(buffer, bufferPtr,
bcd558
+              EXPAT_SAFE_PTR_DIFF(bufferEnd, bufferPtr));
bcd558
+      bufferEnd
bcd558
+          = buffer
bcd558
+            + EXPAT_SAFE_PTR_DIFF(bufferEnd, bufferPtr);
bcd558
       bufferPtr = buffer;
bcd558
 #endif  /* not defined XML_CONTEXT_BYTES */
bcd558
     }
bcd558
@@ -2171,7 +2238,7 @@ XML_ResumeParser(XML_Parser parser)
bcd558
   }
bcd558
   ps_parsing = XML_PARSING;
bcd558
 
bcd558
-  errorCode = processor(parser, bufferPtr, parseEndPtr, &bufferPtr);
bcd558
+  errorCode = callProcessor(parser, bufferPtr, parseEndPtr, &bufferPtr);
bcd558
 
bcd558
   if (errorCode != XML_ERROR_NONE) {
bcd558
     eventEndPtr = eventPtr;
bcd558
@@ -2481,6 +2548,15 @@ MOZ_XML_ProcessingEntityValue(XML_Parser
bcd558
 }
bcd558
 /* END MOZILLA CHANGE */
bcd558
 
bcd558
+XML_Bool XMLCALL
bcd558
+XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled) {
bcd558
+  if (parser != NULL && (enabled == XML_TRUE || enabled == XML_FALSE)) {
bcd558
+    parser->m_reparseDeferralEnabled = enabled;
bcd558
+    return XML_TRUE;
bcd558
+  }
bcd558
+  return XML_FALSE;
bcd558
+}
bcd558
+
bcd558
 /* Initially tag->rawName always points into the parse buffer;
bcd558
    for those TAG instances opened while the current parse buffer was
bcd558
    processed, and not yet closed, we need to store tag->rawName in a more