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

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