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