Blame SOURCES/expat-2.2.5-CVE-2019-15903.patch

721c48
721c48
https://bugzilla.redhat.com/show_bug.cgi?id=1752592
721c48
721c48
https://github.com/libexpat/libexpat/commit/6da1f19625592bfb928253620cac568d9a9b9c65
721c48
721c48
--- libexpat-R_2_2_5/expat/lib/xmlparse.c.cve15903
721c48
+++ libexpat-R_2_2_5/expat/lib/xmlparse.c
721c48
@@ -411,7 +411,7 @@ initializeEncoding(XML_Parser parser);
721c48
 static enum XML_Error
721c48
 doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
721c48
          const char *end, int tok, const char *next, const char **nextPtr,
721c48
-         XML_Bool haveMore);
721c48
+                               XML_Bool haveMore, XML_Bool allowClosingDoctype);
721c48
 static enum XML_Error
721c48
 processInternalEntity(XML_Parser parser, ENTITY *entity,
721c48
                       XML_Bool betweenDecl);
721c48
@@ -4218,7 +4218,7 @@ externalParEntProcessor(XML_Parser parse
721c48
 
721c48
   parser->m_processor = prologProcessor;
721c48
   return doProlog(parser, parser->m_encoding, s, end, tok, next,
721c48
-                  nextPtr, (XML_Bool)!parser->m_parsingStatus.finalBuffer);
721c48
+                  nextPtr, (XML_Bool)!parser->m_parsingStatus.finalBuffer, XML_TRUE);
721c48
 }
721c48
 
721c48
 static enum XML_Error PTRCALL
721c48
@@ -4268,19 +4268,13 @@ prologProcessor(XML_Parser parser,
721c48
   const char *next = s;
721c48
   int tok = XmlPrologTok(parser->m_encoding, s, end, &next;;
721c48
   return doProlog(parser, parser->m_encoding, s, end, tok, next,
721c48
-                  nextPtr, (XML_Bool)!parser->m_parsingStatus.finalBuffer);
721c48
+                  nextPtr, (XML_Bool)!parser->m_parsingStatus.finalBuffer, XML_TRUE);
721c48
 }
721c48
 
721c48
 static enum XML_Error
721c48
-doProlog(XML_Parser parser,
721c48
-         const ENCODING *enc,
721c48
-         const char *s,
721c48
-         const char *end,
721c48
-         int tok,
721c48
-         const char *next,
721c48
-         const char **nextPtr,
721c48
-         XML_Bool haveMore)
721c48
-{
721c48
+doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end,
721c48
+         int tok, const char *next, const char **nextPtr, XML_Bool haveMore,
721c48
+         XML_Bool allowClosingDoctype) {
721c48
 #ifdef XML_DTD
721c48
   static const XML_Char externalSubsetName[] = { ASCII_HASH , '\0' };
721c48
 #endif /* XML_DTD */
721c48
@@ -4458,6 +4452,11 @@ doProlog(XML_Parser parser,
721c48
       }
721c48
       break;
721c48
     case XML_ROLE_DOCTYPE_CLOSE:
721c48
+      if (allowClosingDoctype != XML_TRUE) {
721c48
+        /* Must not close doctype from within expanded parameter entities */
721c48
+        return XML_ERROR_INVALID_TOKEN;
721c48
+      }
721c48
+
721c48
       if (parser->m_doctypeName) {
721c48
         parser->m_startDoctypeDeclHandler(parser->m_handlerArg, parser->m_doctypeName,
721c48
                                 parser->m_doctypeSysid, parser->m_doctypePubid, 0);
721c48
@@ -5395,7 +5394,7 @@ processInternalEntity(XML_Parser parser,
721c48
   if (entity->is_param) {
721c48
     int tok = XmlPrologTok(parser->m_internalEncoding, textStart, textEnd, &next;;
721c48
     result = doProlog(parser, parser->m_internalEncoding, textStart, textEnd, tok,
721c48
-                      next, &next, XML_FALSE);
721c48
+                      next, &next, XML_FALSE, XML_FALSE);
721c48
   }
721c48
   else
721c48
 #endif /* XML_DTD */
721c48
@@ -5442,7 +5441,7 @@ internalEntityProcessor(XML_Parser parse
721c48
   if (entity->is_param) {
721c48
     int tok = XmlPrologTok(parser->m_internalEncoding, textStart, textEnd, &next;;
721c48
     result = doProlog(parser, parser->m_internalEncoding, textStart, textEnd, tok,
721c48
-                      next, &next, XML_FALSE);
721c48
+                      next, &next, XML_FALSE, XML_TRUE);
721c48
   }
721c48
   else
721c48
 #endif /* XML_DTD */
721c48
@@ -5469,7 +5468,7 @@ internalEntityProcessor(XML_Parser parse
721c48
     parser->m_processor = prologProcessor;
721c48
     tok = XmlPrologTok(parser->m_encoding, s, end, &next;;
721c48
     return doProlog(parser, parser->m_encoding, s, end, tok, next, nextPtr,
721c48
-                    (XML_Bool)!parser->m_parsingStatus.finalBuffer);
721c48
+                    (XML_Bool)!parser->m_parsingStatus.finalBuffer, XML_TRUE);
721c48
   }
721c48
   else
721c48
 #endif /* XML_DTD */
721c48
--- libexpat-R_2_2_5/expat/tests/runtests.c.cve15903
721c48
+++ libexpat-R_2_2_5/expat/tests/runtests.c
721c48
@@ -7193,6 +7193,69 @@ overwrite_end_checker(void *userData, co
721c48
     CharData_AppendXMLChars(storage, XCS("\n"), 1);
721c48
 }
721c48
 
721c48
+#ifdef XML_DTD
721c48
+START_TEST(test_misc_deny_internal_entity_closing_doctype_issue_317) {
721c48
+  const char *const inputOne = "
721c48
+                               "<d/>'>\n"
721c48
+                               "\n"
721c48
+                               "%e;";
721c48
+  const char *const inputTwo = "
721c48
+                               "<d/>'>\n"
721c48
+                               "\n"
721c48
+                               "%e2;";
721c48
+  const char *const inputThree = "
721c48
+                                 "<d'>\n"
721c48
+                                 "\n"
721c48
+                                 "%e;";
721c48
+  const char *const inputIssue317 = "
721c48
+                                    "\n"
721c48
+                                    "<doc>Hell<oc (#PCDATA)*>'>\n"
721c48
+                                    "%foo;\n"
721c48
+                                    "]>\n"
721c48
+                                    "<doc>Hello, world</dVc>";
721c48
+
721c48
+  const char *const inputs[] = {inputOne, inputTwo, inputThree, inputIssue317};
721c48
+  size_t inputIndex = 0;
721c48
+
721c48
+  for (; inputIndex < sizeof(inputs) / sizeof(inputs[0]); inputIndex++) {
721c48
+    XML_Parser parser;
721c48
+    enum XML_Status parseResult;
721c48
+    int setParamEntityResult;
721c48
+    XML_Size lineNumber;
721c48
+    XML_Size columnNumber;
721c48
+    const char *const input = inputs[inputIndex];
721c48
+
721c48
+    parser = XML_ParserCreate(NULL);
721c48
+    setParamEntityResult
721c48
+        = XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
721c48
+    if (setParamEntityResult != 1)
721c48
+      fail("Failed to set XML_PARAM_ENTITY_PARSING_ALWAYS.");
721c48
+
721c48
+    parseResult = XML_Parse(parser, input, (int)strlen(input), 0);
721c48
+    if (parseResult != XML_STATUS_ERROR) {
721c48
+      parseResult = XML_Parse(parser, "", 0, 1);
721c48
+      if (parseResult != XML_STATUS_ERROR) {
721c48
+        fail("Parsing was expected to fail but succeeded.");
721c48
+      }
721c48
+    }
721c48
+
721c48
+    if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)
721c48
+      fail("Error code does not match XML_ERROR_INVALID_TOKEN");
721c48
+
721c48
+    lineNumber = XML_GetCurrentLineNumber(parser);
721c48
+    if (lineNumber != 4)
721c48
+      fail("XML_GetCurrentLineNumber does not work as expected.");
721c48
+
721c48
+    columnNumber = XML_GetCurrentColumnNumber(parser);
721c48
+    if (columnNumber != 0)
721c48
+      fail("XML_GetCurrentColumnNumber does not work as expected.");
721c48
+
721c48
+    XML_ParserFree(parser);
721c48
+  }
721c48
+}
721c48
+END_TEST
721c48
+#endif
721c48
+
721c48
 static void
721c48
 run_ns_tagname_overwrite_test(const char *text, const XML_Char *result)
721c48
 {
721c48
@@ -12210,6 +12273,10 @@ make_suite(void)
721c48
     tcase_add_test(tc_misc, test_misc_features);
721c48
     tcase_add_test(tc_misc, test_misc_attribute_leak);
721c48
     tcase_add_test(tc_misc, test_misc_utf16le);
721c48
+#ifdef XML_DTD
721c48
+    tcase_add_test(tc_misc,
721c48
+                   test_misc_deny_internal_entity_closing_doctype_issue_317);
721c48
+#endif
721c48
 
721c48
     suite_add_tcase(s, tc_alloc);
721c48
     tcase_add_checked_fixture(tc_alloc, alloc_setup, alloc_teardown);