Blame SOURCES/expat-2.2.5-Protect-against-malicious-namespace-declarations.patch

589ceb
commit fd5473ef5873048eadef344a1f16f71ad8eefe99
589ceb
Author: Tomas Korbar <tkorbar@redhat.com>
589ceb
Date:   Mon Mar 14 12:17:41 2022 +0100
589ceb
589ceb
    Protect against malicious namespace declarations
589ceb
589ceb
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
589ceb
index 581b9a4..6f3510b 100644
589ceb
--- a/lib/xmlparse.c
589ceb
+++ b/lib/xmlparse.c
589ceb
@@ -661,8 +661,7 @@ XML_ParserCreate(const XML_Char *encodingName)
589ceb
 XML_Parser XMLCALL
589ceb
 XML_ParserCreateNS(const XML_Char *encodingName, XML_Char nsSep)
589ceb
 {
589ceb
-  XML_Char tmp[2];
589ceb
-  *tmp = nsSep;
589ceb
+  XML_Char tmp[2] = {nsSep, 0};
589ceb
   return XML_ParserCreate_MM(encodingName, NULL, tmp);
589ceb
 }
589ceb
 
589ceb
@@ -1288,8 +1287,7 @@ XML_ExternalEntityParserCreate(XML_Parser oldParser,
589ceb
      would be otherwise.
589ceb
   */
589ceb
   if (parser->m_ns) {
589ceb
-    XML_Char tmp[2];
589ceb
-    *tmp = parser->m_namespaceSeparator;
589ceb
+    XML_Char tmp[2] = {parser->m_namespaceSeparator, 0};
589ceb
     parser = parserCreate(encodingName, &parser->m_mem, tmp, newDtd);
589ceb
   }
589ceb
   else {
589ceb
@@ -3640,6 +3638,117 @@ storeAtts(XML_Parser parser, const ENCODING *enc,
589ceb
   return XML_ERROR_NONE;
589ceb
 }
589ceb
 
589ceb
+static XML_Bool
589ceb
+is_rfc3986_uri_char(XML_Char candidate) {
589ceb
+  // For the RFC 3986 ANBF grammar see
589ceb
+  // https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
589ceb
+
589ceb
+  switch (candidate) {
589ceb
+  // From rule "ALPHA" (uppercase half)
589ceb
+  case 'A':
589ceb
+  case 'B':
589ceb
+  case 'C':
589ceb
+  case 'D':
589ceb
+  case 'E':
589ceb
+  case 'F':
589ceb
+  case 'G':
589ceb
+  case 'H':
589ceb
+  case 'I':
589ceb
+  case 'J':
589ceb
+  case 'K':
589ceb
+  case 'L':
589ceb
+  case 'M':
589ceb
+  case 'N':
589ceb
+  case 'O':
589ceb
+  case 'P':
589ceb
+  case 'Q':
589ceb
+  case 'R':
589ceb
+  case 'S':
589ceb
+  case 'T':
589ceb
+  case 'U':
589ceb
+  case 'V':
589ceb
+  case 'W':
589ceb
+  case 'X':
589ceb
+  case 'Y':
589ceb
+  case 'Z':
589ceb
+
589ceb
+  // From rule "ALPHA" (lowercase half)
589ceb
+  case 'a':
589ceb
+  case 'b':
589ceb
+  case 'c':
589ceb
+  case 'd':
589ceb
+  case 'e':
589ceb
+  case 'f':
589ceb
+  case 'g':
589ceb
+  case 'h':
589ceb
+  case 'i':
589ceb
+  case 'j':
589ceb
+  case 'k':
589ceb
+  case 'l':
589ceb
+  case 'm':
589ceb
+  case 'n':
589ceb
+  case 'o':
589ceb
+  case 'p':
589ceb
+  case 'q':
589ceb
+  case 'r':
589ceb
+  case 's':
589ceb
+  case 't':
589ceb
+  case 'u':
589ceb
+  case 'v':
589ceb
+  case 'w':
589ceb
+  case 'x':
589ceb
+  case 'y':
589ceb
+  case 'z':
589ceb
+
589ceb
+  // From rule "DIGIT"
589ceb
+  case '0':
589ceb
+  case '1':
589ceb
+  case '2':
589ceb
+  case '3':
589ceb
+  case '4':
589ceb
+  case '5':
589ceb
+  case '6':
589ceb
+  case '7':
589ceb
+  case '8':
589ceb
+  case '9':
589ceb
+
589ceb
+  // From rule "pct-encoded"
589ceb
+  case '%':
589ceb
+
589ceb
+  // From rule "unreserved"
589ceb
+  case '-':
589ceb
+  case '.':
589ceb
+  case '_':
589ceb
+  case '~':
589ceb
+
589ceb
+  // From rule "gen-delims"
589ceb
+  case ':':
589ceb
+  case '/':
589ceb
+  case '?':
589ceb
+  case '#':
589ceb
+  case '[':
589ceb
+  case ']':
589ceb
+  case '@':
589ceb
+
589ceb
+  // From rule "sub-delims"
589ceb
+  case '!':
589ceb
+  case '$':
589ceb
+  case '&':
589ceb
+  case '\'':
589ceb
+  case '(':
589ceb
+  case ')':
589ceb
+  case '*':
589ceb
+  case '+':
589ceb
+  case ',':
589ceb
+  case ';':
589ceb
+  case '=':
589ceb
+    return XML_TRUE;
589ceb
+
589ceb
+  default:
589ceb
+    return XML_FALSE;
589ceb
+  }
589ceb
+}
589ceb
+
589ceb
 /* addBinding() overwrites the value of prefix->binding without checking.
589ceb
    Therefore one must keep track of the old value outside of addBinding().
589ceb
 */
589ceb
@@ -3700,6 +3809,29 @@ addBinding(XML_Parser parser, PREFIX *prefix, const ATTRIBUTE_ID *attId,
589ceb
     if (!mustBeXML && isXMLNS
589ceb
         && (len > xmlnsLen || uri[len] != xmlnsNamespace[len]))
589ceb
       isXMLNS = XML_FALSE;
589ceb
+
589ceb
+    // NOTE: While Expat does not validate namespace URIs against RFC 3986
589ceb
+    //       today (and is not REQUIRED to do so with regard to the XML 1.0
589ceb
+    //       namespaces specification) we have to at least make sure, that
589ceb
+    //       the application on top of Expat (that is likely splitting expanded
589ceb
+    //       element names ("qualified names") of form
589ceb
+    //       "[uri sep] local [sep prefix] '\0'" back into 1, 2 or 3 pieces
589ceb
+    //       in its element handler code) cannot be confused by an attacker
589ceb
+    //       putting additional namespace separator characters into namespace
589ceb
+    //       declarations.  That would be ambiguous and not to be expected.
589ceb
+    //
589ceb
+    //       While the HTML API docs of function XML_ParserCreateNS have been
589ceb
+    //       advising against use of a namespace separator character that can
589ceb
+    //       appear in a URI for >20 years now, some widespread applications
589ceb
+    //       are using URI characters (':' (colon) in particular) for a
589ceb
+    //       namespace separator, in practice.  To keep these applications
589ceb
+    //       functional, we only reject namespaces URIs containing the
589ceb
+    //       application-chosen namespace separator if the chosen separator
589ceb
+    //       is a non-URI character with regard to RFC 3986.
589ceb
+    if (parser->m_ns && (uri[len] == parser->m_namespaceSeparator)
589ceb
+        && ! is_rfc3986_uri_char(uri[len])) {
589ceb
+      return XML_ERROR_SYNTAX;
589ceb
+    }
589ceb
   }
589ceb
   isXML = isXML && len == xmlLen;
589ceb
   isXMLNS = isXMLNS && len == xmlnsLen;
589ceb
diff --git a/tests/runtests.c b/tests/runtests.c
589ceb
index ecc6f47..eabd55d 100644
589ceb
--- a/tests/runtests.c
589ceb
+++ b/tests/runtests.c
589ceb
@@ -7950,6 +7950,38 @@ START_TEST(test_ns_double_colon_doctype)
589ceb
 }
589ceb
 END_TEST
589ceb
 
589ceb
+START_TEST(test_ns_separator_in_uri) {
589ceb
+  struct test_case {
589ceb
+    enum XML_Status expectedStatus;
589ceb
+    const char *doc;
589ceb
+    XML_Char namesep;
589ceb
+  };
589ceb
+  struct test_case cases[] = {
589ceb
+      {XML_STATUS_OK, "<doc xmlns='one_two' />", XCS('\n')},
589ceb
+      {XML_STATUS_ERROR, "<doc xmlns='one
two' />", XCS('\n')},
589ceb
+      {XML_STATUS_OK, "<doc xmlns='one:two' />", XCS(':')},
589ceb
+  };
589ceb
+
589ceb
+  size_t i = 0;
589ceb
+  size_t failCount = 0;
589ceb
+  for (; i < sizeof(cases) / sizeof(cases[0]); i++) {
589ceb
+    XML_Parser parser = XML_ParserCreateNS(NULL, cases[i].namesep);
589ceb
+    XML_SetElementHandler(parser, dummy_start_element, dummy_end_element);
589ceb
+    if (XML_Parse(parser, cases[i].doc, (int)strlen(cases[i].doc),
589ceb
+                  /*isFinal*/ XML_TRUE)
589ceb
+        != cases[i].expectedStatus) {
589ceb
+      failCount++;
589ceb
+    }
589ceb
+    XML_ParserFree(parser);
589ceb
+  }
589ceb
+
589ceb
+  if (failCount) {
589ceb
+    fail("Namespace separator handling is broken");
589ceb
+  }
589ceb
+}
589ceb
+END_TEST
589ceb
+
589ceb
+
589ceb
 /* Control variable; the number of times duff_allocator() will successfully allocate */
589ceb
 #define ALLOC_ALWAYS_SUCCEED (-1)
589ceb
 #define REALLOC_ALWAYS_SUCCEED (-1)
589ceb
@@ -12290,6 +12322,7 @@ make_suite(void)
589ceb
     tcase_add_test(tc_namespace, test_ns_utf16_doctype);
589ceb
     tcase_add_test(tc_namespace, test_ns_invalid_doctype);
589ceb
     tcase_add_test(tc_namespace, test_ns_double_colon_doctype);
589ceb
+    tcase_add_test(tc_namespace, test_ns_separator_in_uri);
589ceb
 
589ceb
     suite_add_tcase(s, tc_misc);
589ceb
     tcase_add_checked_fixture(tc_misc, NULL, basic_teardown);