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