Blame SOURCES/expat-2.2.5-CVE-2022-43680.patch

2f0e98
commit a739613cfb5ee60919bd5ad545a5582fa8a6dad9
2f0e98
Author: Tomas Korbar <tkorbar@redhat.com>
2f0e98
Date:   Mon Nov 14 12:37:16 2022 +0100
2f0e98
2f0e98
    Fix CVE-2022-43680
2f0e98
2f0e98
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
2f0e98
index 0cc24f6..3f765f7 100644
2f0e98
--- a/lib/xmlparse.c
2f0e98
+++ b/lib/xmlparse.c
2f0e98
@@ -1016,6 +1016,14 @@ parserCreate(const XML_Char *encodingName,
2f0e98
   parserInit(parser, encodingName);
2f0e98
 
2f0e98
   if (encodingName && !parser->m_protocolEncodingName) {
2f0e98
+    if (dtd) {
2f0e98
+      // We need to stop the upcoming call to XML_ParserFree from happily
2f0e98
+      // destroying parser->m_dtd because the DTD is shared with the parent
2f0e98
+      // parser and the only guard that keeps XML_ParserFree from destroying
2f0e98
+      // parser->m_dtd is parser->m_isParamEntity but it will be set to
2f0e98
+      // XML_TRUE only later in XML_ExternalEntityParserCreate (or not at all).
2f0e98
+      parser->m_dtd = NULL;
2f0e98
+    }
2f0e98
     XML_ParserFree(parser);
2f0e98
     return NULL;
2f0e98
   }
2f0e98
diff --git a/tests/runtests.c b/tests/runtests.c
2f0e98
index f3ebbd7..f58f794 100644
2f0e98
--- a/tests/runtests.c
2f0e98
+++ b/tests/runtests.c
2f0e98
@@ -10819,6 +10819,48 @@ START_TEST(test_alloc_long_notation)
2f0e98
 }
2f0e98
 END_TEST
2f0e98
 
2f0e98
+static int XMLCALL
2f0e98
+external_entity_parser_create_alloc_fail_handler(XML_Parser parser,
2f0e98
+                                                 const XML_Char *context,
2f0e98
+                                                 const XML_Char *UNUSED_P(base),
2f0e98
+                                                 const XML_Char *UNUSED_P(systemId),
2f0e98
+                                                 const XML_Char *UNUSED_P(publicId)) {
2f0e98
+  if (context != NULL)
2f0e98
+    fail("Unexpected non-NULL context");
2f0e98
+
2f0e98
+  // The following number intends to fail the upcoming allocation in line
2f0e98
+  // "parser->m_protocolEncodingName = copyString(encodingName,
2f0e98
+  // &(parser->m_mem));" in function parserInit.
2f0e98
+  allocation_count = 3;
2f0e98
+
2f0e98
+  const XML_Char *const encodingName = XCS("UTF-8"); // needs something non-NULL
2f0e98
+  const XML_Parser ext_parser
2f0e98
+      = XML_ExternalEntityParserCreate(parser, context, encodingName);
2f0e98
+  if (ext_parser != NULL)
2f0e98
+    fail(
2f0e98
+        "Call to XML_ExternalEntityParserCreate was expected to fail out-of-memory");
2f0e98
+
2f0e98
+  allocation_count = ALLOC_ALWAYS_SUCCEED;
2f0e98
+  return XML_STATUS_ERROR;
2f0e98
+}
2f0e98
+
2f0e98
+START_TEST(test_alloc_reset_after_external_entity_parser_create_fail) {
2f0e98
+  const char *const text = "<doc/>";
2f0e98
+
2f0e98
+  XML_SetExternalEntityRefHandler(
2f0e98
+      parser, external_entity_parser_create_alloc_fail_handler);
2f0e98
+  XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2f0e98
+
2f0e98
+  if (XML_Parse(parser, text, (int)strlen(text), XML_TRUE)
2f0e98
+      != XML_STATUS_ERROR)
2f0e98
+    fail("Call to parse was expected to fail");
2f0e98
+
2f0e98
+  if (XML_GetErrorCode(parser) != XML_ERROR_EXTERNAL_ENTITY_HANDLING)
2f0e98
+    fail("Call to parse was expected to fail from the external entity handler");
2f0e98
+
2f0e98
+  XML_ParserReset(parser, NULL);
2f0e98
+}
2f0e98
+END_TEST
2f0e98
 
2f0e98
 static void
2f0e98
 nsalloc_setup(void)
2f0e98
@@ -12653,6 +12695,10 @@ make_suite(void)
2f0e98
     tcase_add_test(tc_alloc, test_alloc_long_entity_value);
2f0e98
     tcase_add_test(tc_alloc, test_alloc_long_notation);
2f0e98
 
2f0e98
+    #ifdef XML_DTD
2f0e98
+    tcase_add_test(tc_alloc,
2f0e98
+                   test_alloc_reset_after_external_entity_parser_create_fail);
2f0e98
+    #endif
2f0e98
     suite_add_tcase(s, tc_nsalloc);
2f0e98
     tcase_add_checked_fixture(tc_nsalloc, nsalloc_setup, nsalloc_teardown);
2f0e98
     tcase_add_test(tc_nsalloc, test_nsalloc_xmlns);