Blame SOURCES/expat-2.2.5-Prevent-stack-exhaustion-in-build_model.patch

1581cb
commit f1b61e6fbaedbb2bbea736269a015d97d4df46ce
1581cb
Author: Tomas Korbar <tkorbar@redhat.com>
1581cb
Date:   Tue May 3 13:42:54 2022 +0200
1581cb
1581cb
    Fix CVE-2022-25313
1581cb
1581cb
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
1581cb
index ceeec26..d47e42c 100644
1581cb
--- a/lib/xmlparse.c
1581cb
+++ b/lib/xmlparse.c
1581cb
@@ -7458,12 +7458,14 @@ build_node(XML_Parser parser,
1581cb
 }
1581cb
 
1581cb
 static XML_Content *
1581cb
-build_model (XML_Parser parser)
1581cb
-{
1581cb
-  DTD * const dtd = parser->m_dtd;  /* save one level of indirection */
1581cb
+build_model(XML_Parser parser) {
1581cb
+  /* Function build_model transforms the existing parser->m_dtd->scaffold
1581cb
+   * array of CONTENT_SCAFFOLD tree nodes into a new array of
1581cb
+   * XML_Content tree nodes followed by a gapless list of zero-terminated
1581cb
+   * strings. */
1581cb
+  DTD *const dtd = parser->m_dtd; /* save one level of indirection */
1581cb
   XML_Content *ret;
1581cb
-  XML_Content *cpos;
1581cb
-  XML_Char * str;
1581cb
+  XML_Char *str; /* the current string writing location */
1581cb
 
1581cb
   /* Detect and prevent integer overflow.
1581cb
    * The preprocessor guard addresses the "always false" warning
1581cb
@@ -7486,13 +7488,99 @@ build_model (XML_Parser parser)
1581cb
                             + (dtd->contentStringLen * sizeof(XML_Char)));
1581cb
 
1581cb
   ret = (XML_Content *)MALLOC(parser, allocsize);
1581cb
-  if (!ret)
1581cb
+  if (! ret)
1581cb
     return NULL;
1581cb
 
1581cb
-  str =  (XML_Char *) (&ret[dtd->scaffCount]);
1581cb
-  cpos = &ret[1];
1581cb
+  /* What follows is an iterative implementation (of what was previously done
1581cb
+   * recursively in a dedicated function called "build_node".  The old recursive
1581cb
+   * build_node could be forced into stack exhaustion from input as small as a
1581cb
+   * few megabyte, and so that was a security issue.  Hence, a function call
1581cb
+   * stack is avoided now by resolving recursion.)
1581cb
+   *
1581cb
+   * The iterative approach works as follows:
1581cb
+   *
1581cb
+   * - We have two writing pointers, both walking up the result array; one does
1581cb
+   *   the work, the other creates "jobs" for its colleague to do, and leads
1581cb
+   *   the way:
1581cb
+   *
1581cb
+   *   - The faster one, pointer jobDest, always leads and writes "what job
1581cb
+   *     to do" by the other, once they reach that place in the
1581cb
+   *     array: leader "jobDest" stores the source node array index (relative
1581cb
+   *     to array dtd->scaffold) in field "numchildren".
1581cb
+   *
1581cb
+   *   - The slower one, pointer dest, looks at the value stored in the
1581cb
+   *     "numchildren" field (which actually holds a source node array index
1581cb
+   *     at that time) and puts the real data from dtd->scaffold in.
1581cb
+   *
1581cb
+   * - Before the loop starts, jobDest writes source array index 0
1581cb
+   *   (where the root node is located) so that dest will have something to do
1581cb
+   *   when it starts operation.
1581cb
+   *
1581cb
+   * - Whenever nodes with children are encountered, jobDest appends
1581cb
+   *   them as new jobs, in order.  As a result, tree node siblings are
1581cb
+   *   adjacent in the resulting array, for example:
1581cb
+   *
1581cb
+   *     [0] root, has two children
1581cb
+   *       [1] first child of 0, has three children
1581cb
+   *         [3] first child of 1, does not have children
1581cb
+   *         [4] second child of 1, does not have children
1581cb
+   *         [5] third child of 1, does not have children
1581cb
+   *       [2] second child of 0, does not have children
1581cb
+   *
1581cb
+   *   Or (the same data) presented in flat array view:
1581cb
+   *
1581cb
+   *     [0] root, has two children
1581cb
+   *
1581cb
+   *     [1] first child of 0, has three children
1581cb
+   *     [2] second child of 0, does not have children
1581cb
+   *
1581cb
+   *     [3] first child of 1, does not have children
1581cb
+   *     [4] second child of 1, does not have children
1581cb
+   *     [5] third child of 1, does not have children
1581cb
+   *
1581cb
+   * - The algorithm repeats until all target array indices have been processed.
1581cb
+   */
1581cb
+  XML_Content *dest = ret; /* tree node writing location, moves upwards */
1581cb
+  XML_Content *const destLimit = &ret[dtd->scaffCount];
1581cb
+  XML_Content *jobDest = ret; /* next free writing location in target array */
1581cb
+  str = (XML_Char *)&ret[dtd->scaffCount];
1581cb
+
1581cb
+  /* Add the starting job, the root node (index 0) of the source tree  */
1581cb
+  (jobDest++)->numchildren = 0;
1581cb
+
1581cb
+  for (; dest < destLimit; dest++) {
1581cb
+    /* Retrieve source tree array index from job storage */
1581cb
+    const int src_node = (int)dest->numchildren;
1581cb
+
1581cb
+    /* Convert item */
1581cb
+    dest->type = dtd->scaffold[src_node].type;
1581cb
+    dest->quant = dtd->scaffold[src_node].quant;
1581cb
+    if (dest->type == XML_CTYPE_NAME) {
1581cb
+      const XML_Char *src;
1581cb
+      dest->name = str;
1581cb
+      src = dtd->scaffold[src_node].name;
1581cb
+      for (;;) {
1581cb
+        *str++ = *src;
1581cb
+        if (! *src)
1581cb
+          break;
1581cb
+        src++;
1581cb
+      }
1581cb
+      dest->numchildren = 0;
1581cb
+      dest->children = NULL;
1581cb
+    } else {
1581cb
+      unsigned int i;
1581cb
+      int cn;
1581cb
+      dest->name = NULL;
1581cb
+      dest->numchildren = dtd->scaffold[src_node].childcnt;
1581cb
+      dest->children = jobDest;
1581cb
+
1581cb
+      /* Append scaffold indices of children to array */
1581cb
+      for (i = 0, cn = dtd->scaffold[src_node].firstchild;
1581cb
+           i < dest->numchildren; i++, cn = dtd->scaffold[cn].nextsib)
1581cb
+        (jobDest++)->numchildren = (unsigned int)cn;
1581cb
+    }
1581cb
+  }
1581cb
 
1581cb
-  build_node(parser, 0, ret, &cpos, &str);
1581cb
   return ret;
1581cb
 }
1581cb
 
1581cb
diff --git a/tests/runtests.c b/tests/runtests.c
1581cb
index eacd163..569ad8c 100644
1581cb
--- a/tests/runtests.c
1581cb
+++ b/tests/runtests.c
1581cb
@@ -2848,6 +2848,81 @@ START_TEST(test_dtd_elements)
1581cb
 }
1581cb
 END_TEST
1581cb
 
1581cb
+static void XMLCALL
1581cb
+element_decl_check_model(void *UNUSED_P(userData), const XML_Char *name,
1581cb
+                         XML_Content *model) {
1581cb
+  uint32_t errorFlags = 0;
1581cb
+
1581cb
+  /* Expected model array structure is this:
1581cb
+   * [0] (type 6, quant 0)
1581cb
+   *   [1] (type 5, quant 0)
1581cb
+   *     [3] (type 4, quant 0, name "bar")
1581cb
+   *     [4] (type 4, quant 0, name "foo")
1581cb
+   *     [5] (type 4, quant 3, name "xyz")
1581cb
+   *   [2] (type 4, quant 2, name "zebra")
1581cb
+   */
1581cb
+  errorFlags |= ((xcstrcmp(name, XCS("junk")) == 0) ? 0 : (1u << 0));
1581cb
+  errorFlags |= ((model != NULL) ? 0 : (1u << 1));
1581cb
+
1581cb
+  errorFlags |= ((model[0].type == XML_CTYPE_SEQ) ? 0 : (1u << 2));
1581cb
+  errorFlags |= ((model[0].quant == XML_CQUANT_NONE) ? 0 : (1u << 3));
1581cb
+  errorFlags |= ((model[0].numchildren == 2) ? 0 : (1u << 4));
1581cb
+  errorFlags |= ((model[0].children == &model[1]) ? 0 : (1u << 5));
1581cb
+  errorFlags |= ((model[0].name == NULL) ? 0 : (1u << 6));
1581cb
+
1581cb
+  errorFlags |= ((model[1].type == XML_CTYPE_CHOICE) ? 0 : (1u << 7));
1581cb
+  errorFlags |= ((model[1].quant == XML_CQUANT_NONE) ? 0 : (1u << 8));
1581cb
+  errorFlags |= ((model[1].numchildren == 3) ? 0 : (1u << 9));
1581cb
+  errorFlags |= ((model[1].children == &model[3]) ? 0 : (1u << 10));
1581cb
+  errorFlags |= ((model[1].name == NULL) ? 0 : (1u << 11));
1581cb
+
1581cb
+  errorFlags |= ((model[2].type == XML_CTYPE_NAME) ? 0 : (1u << 12));
1581cb
+  errorFlags |= ((model[2].quant == XML_CQUANT_REP) ? 0 : (1u << 13));
1581cb
+  errorFlags |= ((model[2].numchildren == 0) ? 0 : (1u << 14));
1581cb
+  errorFlags |= ((model[2].children == NULL) ? 0 : (1u << 15));
1581cb
+  errorFlags |= ((xcstrcmp(model[2].name, XCS("zebra")) == 0) ? 0 : (1u << 16));
1581cb
+
1581cb
+  errorFlags |= ((model[3].type == XML_CTYPE_NAME) ? 0 : (1u << 17));
1581cb
+  errorFlags |= ((model[3].quant == XML_CQUANT_NONE) ? 0 : (1u << 18));
1581cb
+  errorFlags |= ((model[3].numchildren == 0) ? 0 : (1u << 19));
1581cb
+  errorFlags |= ((model[3].children == NULL) ? 0 : (1u << 20));
1581cb
+  errorFlags |= ((xcstrcmp(model[3].name, XCS("bar")) == 0) ? 0 : (1u << 21));
1581cb
+
1581cb
+  errorFlags |= ((model[4].type == XML_CTYPE_NAME) ? 0 : (1u << 22));
1581cb
+  errorFlags |= ((model[4].quant == XML_CQUANT_NONE) ? 0 : (1u << 23));
1581cb
+  errorFlags |= ((model[4].numchildren == 0) ? 0 : (1u << 24));
1581cb
+  errorFlags |= ((model[4].children == NULL) ? 0 : (1u << 25));
1581cb
+  errorFlags |= ((xcstrcmp(model[4].name, XCS("foo")) == 0) ? 0 : (1u << 26));
1581cb
+
1581cb
+  errorFlags |= ((model[5].type == XML_CTYPE_NAME) ? 0 : (1u << 27));
1581cb
+  errorFlags |= ((model[5].quant == XML_CQUANT_PLUS) ? 0 : (1u << 28));
1581cb
+  errorFlags |= ((model[5].numchildren == 0) ? 0 : (1u << 29));
1581cb
+  errorFlags |= ((model[5].children == NULL) ? 0 : (1u << 30));
1581cb
+  errorFlags |= ((xcstrcmp(model[5].name, XCS("xyz")) == 0) ? 0 : (1u << 31));
1581cb
+
1581cb
+  XML_SetUserData(parser, (void *)(uintptr_t)errorFlags);
1581cb
+  XML_FreeContentModel(parser, model);
1581cb
+}
1581cb
+
1581cb
+START_TEST(test_dtd_elements_nesting) {
1581cb
+  // Payload inspired by a test in Perl's XML::Parser
1581cb
+  const char *text = "
1581cb
+                     "\n"
1581cb
+                     "]>\n"
1581cb
+                     "<foo/>";
1581cb
+
1581cb
+  XML_SetUserData(parser, (void *)(uintptr_t)-1);
1581cb
+
1581cb
+  XML_SetElementDeclHandler(parser, element_decl_check_model);
1581cb
+  if (XML_Parse(parser, text, (int)strlen(text), XML_TRUE)
1581cb
+      == XML_STATUS_ERROR)
1581cb
+    xml_failure(parser);
1581cb
+
1581cb
+  if ((uint32_t)(uintptr_t)XML_GetUserData(parser) != 0)
1581cb
+    fail("Element declaration model regression detected");
1581cb
+}
1581cb
+END_TEST
1581cb
+
1581cb
 /* Test foreign DTD handling */
1581cb
 START_TEST(test_set_foreign_dtd)
1581cb
 {
1581cb
@@ -12256,6 +12331,7 @@ make_suite(void)
1581cb
     tcase_add_test(tc_basic, test_memory_allocation);
1581cb
     tcase_add_test(tc_basic, test_default_current);
1581cb
     tcase_add_test(tc_basic, test_dtd_elements);
1581cb
+    tcase_add_test(tc_basic, test_dtd_elements_nesting);
1581cb
     tcase_add_test(tc_basic, test_set_foreign_dtd);
1581cb
     tcase_add_test(tc_basic, test_foreign_dtd_not_standalone);
1581cb
     tcase_add_test(tc_basic, test_invalid_foreign_dtd);