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