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

e56760
commit b463b1beeba2ad7f9eb456bdbdc136cbbdd1dec8
e56760
Author: Tomas Korbar <tkorbar@redhat.com>
e56760
Date:   Fri Nov 11 10:46:36 2022 +0100
e56760
e56760
    Fix CVE-2022-43680
e56760
e56760
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
e56760
index c0bece5..a73a1bf 100644
e56760
--- a/lib/xmlparse.c
e56760
+++ b/lib/xmlparse.c
e56760
@@ -1068,6 +1068,14 @@ parserCreate(const XML_Char *encodingName,
e56760
   parserInit(parser, encodingName);
e56760
 
e56760
   if (encodingName && ! parser->m_protocolEncodingName) {
e56760
+    if (dtd) {
e56760
+      // We need to stop the upcoming call to XML_ParserFree from happily
e56760
+      // destroying parser->m_dtd because the DTD is shared with the parent
e56760
+      // parser and the only guard that keeps XML_ParserFree from destroying
e56760
+      // parser->m_dtd is parser->m_isParamEntity but it will be set to
e56760
+      // XML_TRUE only later in XML_ExternalEntityParserCreate (or not at all).
e56760
+      parser->m_dtd = NULL;
e56760
+    }
e56760
     XML_ParserFree(parser);
e56760
     return NULL;
e56760
   }
e56760
diff --git a/tests/runtests.c b/tests/runtests.c
e56760
index 530f184..cbfe420 100644
e56760
--- a/tests/runtests.c
e56760
+++ b/tests/runtests.c
e56760
@@ -10090,6 +10090,53 @@ START_TEST(test_alloc_long_notation) {
e56760
 }
e56760
 END_TEST
e56760
 
e56760
+static int XMLCALL
e56760
+external_entity_parser_create_alloc_fail_handler(XML_Parser parser,
e56760
+                                                 const XML_Char *context,
e56760
+                                                 const XML_Char *base,
e56760
+                                                 const XML_Char *systemId,
e56760
+                                                 const XML_Char *publicId) {
e56760
+  UNUSED_P(base);
e56760
+  UNUSED_P(systemId);
e56760
+  UNUSED_P(publicId);
e56760
+
e56760
+  if (context != NULL)
e56760
+    fail("Unexpected non-NULL context");
e56760
+
e56760
+  // The following number intends to fail the upcoming allocation in line
e56760
+  // "parser->m_protocolEncodingName = copyString(encodingName,
e56760
+  // &(parser->m_mem));" in function parserInit.
e56760
+  allocation_count = 3;
e56760
+
e56760
+  const XML_Char *const encodingName = XCS("UTF-8"); // needs something non-NULL
e56760
+  const XML_Parser ext_parser
e56760
+      = XML_ExternalEntityParserCreate(parser, context, encodingName);
e56760
+  if (ext_parser != NULL)
e56760
+    fail(
e56760
+        "Call to XML_ExternalEntityParserCreate was expected to fail out-of-memory");
e56760
+
e56760
+  allocation_count = ALLOC_ALWAYS_SUCCEED;
e56760
+  return XML_STATUS_ERROR;
e56760
+}
e56760
+
e56760
+START_TEST(test_alloc_reset_after_external_entity_parser_create_fail) {
e56760
+  const char *const text = "<doc/>";
e56760
+
e56760
+  XML_SetExternalEntityRefHandler(
e56760
+      g_parser, external_entity_parser_create_alloc_fail_handler);
e56760
+  XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
e56760
+
e56760
+  if (XML_Parse(g_parser, text, (int)strlen(text), XML_TRUE)
e56760
+      != XML_STATUS_ERROR)
e56760
+    fail("Call to parse was expected to fail");
e56760
+
e56760
+  if (XML_GetErrorCode(g_parser) != XML_ERROR_EXTERNAL_ENTITY_HANDLING)
e56760
+    fail("Call to parse was expected to fail from the external entity handler");
e56760
+
e56760
+  XML_ParserReset(g_parser, NULL);
e56760
+}
e56760
+END_TEST
e56760
+
e56760
 static void
e56760
 nsalloc_setup(void) {
e56760
   XML_Memory_Handling_Suite memsuite = {duff_allocator, duff_reallocator, free};
e56760
@@ -12279,6 +12326,8 @@ make_suite(void) {
e56760
   tcase_add_test(tc_alloc, test_alloc_long_public_id);
e56760
   tcase_add_test(tc_alloc, test_alloc_long_entity_value);
e56760
   tcase_add_test(tc_alloc, test_alloc_long_notation);
e56760
+  tcase_add_test__ifdef_xml_dtd(
e56760
+      tc_alloc, test_alloc_reset_after_external_entity_parser_create_fail);
e56760
 
e56760
   suite_add_tcase(s, tc_nsalloc);
e56760
   tcase_add_checked_fixture(tc_nsalloc, nsalloc_setup, nsalloc_teardown);