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

8718de
8718de
https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2019-15903
8718de
8718de
https://github.com/libexpat/libexpat/commit/6da1f19625592bfb928253620cac568d9a9b9c65
8718de
8718de
--- expat-2.1.0/lib/xmlparse.c.cve15903
8718de
+++ expat-2.1.0/lib/xmlparse.c
8718de
@@ -331,7 +331,7 @@
8718de
 static enum XML_Error
8718de
 doProlog(XML_Parser parser, const ENCODING *enc, const char *s,
8718de
          const char *end, int tok, const char *next, const char **nextPtr,
8718de
-         XML_Bool haveMore);
8718de
+         XML_Bool haveMore, XML_Bool allowClosingDoctype);
8718de
 static enum XML_Error
8718de
 processInternalEntity(XML_Parser parser, ENTITY *entity,
8718de
                       XML_Bool betweenDecl);
8718de
@@ -3699,7 +3699,7 @@
8718de
 
8718de
   processor = prologProcessor;
8718de
   return doProlog(parser, encoding, s, end, tok, next,
8718de
-                  nextPtr, (XML_Bool)!ps_finalBuffer);
8718de
+                  nextPtr, (XML_Bool)!ps_finalBuffer, XML_TRUE);
8718de
 }
8718de
 
8718de
 static enum XML_Error PTRCALL
8718de
@@ -3749,7 +3749,7 @@
8718de
   const char *next = s;
8718de
   int tok = XmlPrologTok(encoding, s, end, &next;;
8718de
   return doProlog(parser, encoding, s, end, tok, next,
8718de
-                  nextPtr, (XML_Bool)!ps_finalBuffer);
8718de
+                  nextPtr, (XML_Bool)!ps_finalBuffer, XML_TRUE);
8718de
 }
8718de
 
8718de
 static enum XML_Error
8718de
@@ -3760,7 +3760,8 @@
8718de
          int tok,
8718de
          const char *next,
8718de
          const char **nextPtr,
8718de
-         XML_Bool haveMore)
8718de
+         XML_Bool haveMore,
8718de
+         XML_Bool allowClosingDoctype)
8718de
 {
8718de
 #ifdef XML_DTD
8718de
   static const XML_Char externalSubsetName[] = { ASCII_HASH , '\0' };
8718de
@@ -3936,6 +3937,11 @@
8718de
       }
8718de
       break;
8718de
     case XML_ROLE_DOCTYPE_CLOSE:
8718de
+      if (allowClosingDoctype != XML_TRUE) {
8718de
+        /* Must not close doctype from within expanded parameter entities */
8718de
+        return XML_ERROR_INVALID_TOKEN;
8718de
+      }
8718de
+
8718de
       if (doctypeName) {
8718de
         startDoctypeDeclHandler(handlerArg, doctypeName,
8718de
                                 doctypeSysid, doctypePubid, 0);
8718de
@@ -4837,7 +4843,7 @@
8718de
   if (entity->is_param) {
8718de
     int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next;;
8718de
     result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
8718de
-                      next, &next, XML_FALSE);
8718de
+                      next, &next, XML_FALSE, XML_FALSE);
8718de
   }
8718de
   else
8718de
 #endif /* XML_DTD */
8718de
@@ -4882,7 +4888,7 @@
8718de
   if (entity->is_param) {
8718de
     int tok = XmlPrologTok(internalEncoding, textStart, textEnd, &next;;
8718de
     result = doProlog(parser, internalEncoding, textStart, textEnd, tok,
8718de
-                      next, &next, XML_FALSE);
8718de
+                      next, &next, XML_FALSE, XML_TRUE);
8718de
   }
8718de
   else
8718de
 #endif /* XML_DTD */
8718de
@@ -4909,7 +4915,7 @@
8718de
     processor = prologProcessor;
8718de
     tok = XmlPrologTok(encoding, s, end, &next;;
8718de
     return doProlog(parser, encoding, s, end, tok, next, nextPtr,
8718de
-                    (XML_Bool)!ps_finalBuffer);
8718de
+                    (XML_Bool)!ps_finalBuffer, XML_TRUE);
8718de
   }
8718de
   else
8718de
 #endif /* XML_DTD */
8718de
--- expat-2.1.0/tests/runtests.c.cve15903
8718de
+++ expat-2.1.0/tests/runtests.c
8718de
@@ -1157,6 +1157,69 @@
8718de
     CharData_AppendString(storage, "\n");
8718de
 }
8718de
 
8718de
+#ifdef XML_DTD
8718de
+START_TEST(test_misc_deny_internal_entity_closing_doctype_issue_317) {
8718de
+  const char *const inputOne = "
8718de
+                               "<d/>'>\n"
8718de
+                               "\n"
8718de
+                               "%e;";
8718de
+  const char *const inputTwo = "
8718de
+                               "<d/>'>\n"
8718de
+                               "\n"
8718de
+                               "%e2;";
8718de
+  const char *const inputThree = "
8718de
+                                 "<d'>\n"
8718de
+                                 "\n"
8718de
+                                 "%e;";
8718de
+  const char *const inputIssue317 = "
8718de
+                                    "\n"
8718de
+                                    "<doc>Hell<oc (#PCDATA)*>'>\n"
8718de
+                                    "%foo;\n"
8718de
+                                    "]>\n"
8718de
+                                    "<doc>Hello, world</dVc>";
8718de
+
8718de
+  const char *const inputs[] = {inputOne, inputTwo, inputThree, inputIssue317};
8718de
+  size_t inputIndex = 0;
8718de
+
8718de
+  for (; inputIndex < sizeof(inputs) / sizeof(inputs[0]); inputIndex++) {
8718de
+    XML_Parser parser;
8718de
+    enum XML_Status parseResult;
8718de
+    int setParamEntityResult;
8718de
+    XML_Size lineNumber;
8718de
+    XML_Size columnNumber;
8718de
+    const char *const input = inputs[inputIndex];
8718de
+
8718de
+    parser = XML_ParserCreate(NULL);
8718de
+    setParamEntityResult
8718de
+        = XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
8718de
+    if (setParamEntityResult != 1)
8718de
+      fail("Failed to set XML_PARAM_ENTITY_PARSING_ALWAYS.");
8718de
+
8718de
+    parseResult = XML_Parse(parser, input, (int)strlen(input), 0);
8718de
+    if (parseResult != XML_STATUS_ERROR) {
8718de
+      parseResult = XML_Parse(parser, "", 0, 1);
8718de
+      if (parseResult != XML_STATUS_ERROR) {
8718de
+        fail("Parsing was expected to fail but succeeded.");
8718de
+      }
8718de
+    }
8718de
+
8718de
+    if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)
8718de
+      fail("Error code does not match XML_ERROR_INVALID_TOKEN");
8718de
+
8718de
+    lineNumber = XML_GetCurrentLineNumber(parser);
8718de
+    if (lineNumber != 4)
8718de
+      fail("XML_GetCurrentLineNumber does not work as expected.");
8718de
+
8718de
+    columnNumber = XML_GetCurrentColumnNumber(parser);
8718de
+    if (columnNumber != 0)
8718de
+      fail("XML_GetCurrentColumnNumber does not work as expected.");
8718de
+
8718de
+    XML_ParserFree(parser);
8718de
+  }
8718de
+}
8718de
+END_TEST
8718de
+#endif
8718de
+
8718de
 static void
8718de
 run_ns_tagname_overwrite_test(char *text, char *result)
8718de
 {
8718de
@@ -1479,6 +1542,11 @@
8718de
     tcase_add_test(tc_namespace, test_ns_unbound_prefix_on_attribute);
8718de
     tcase_add_test(tc_namespace, test_ns_unbound_prefix_on_element);
8718de
 
8718de
+#ifdef XML_DTD
8718de
+    tcase_add_test(tc_basic,
8718de
+                   test_misc_deny_internal_entity_closing_doctype_issue_317);
8718de
+#endif
8718de
+
8718de
     return s;
8718de
 }
8718de