From 839b89e678b5265a0e6b0477410e64fac669d578 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 3 Jun 2017 02:01:29 +0200 Subject: [PATCH 4/7] Fix buffer size checks in xmlSnprintfElementContent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xmlSnprintfElementContent failed to correctly check the available buffer space in two locations. Fixes bug 781333 (CVE-2017-9047) and bug 781701 (CVE-2017-9048). Thanks to Marcel Böhme and Thuan Pham for the report. --- result/valid/781333.xml | 5 +++++ result/valid/781333.xml.err | 3 +++ result/valid/781333.xml.err.rdr | 6 ++++++ test/valid/781333.xml | 4 ++++ valid.c | 20 +++++++++++--------- 5 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 result/valid/781333.xml create mode 100644 result/valid/781333.xml.err create mode 100644 result/valid/781333.xml.err.rdr create mode 100644 test/valid/781333.xml diff --git a/result/valid/781333.xml b/result/valid/781333.xml new file mode 100644 index 00000000..45dc451d --- /dev/null +++ b/result/valid/781333.xml @@ -0,0 +1,5 @@ + + +]> + diff --git a/result/valid/781333.xml.err b/result/valid/781333.xml.err new file mode 100644 index 00000000..b401b49a --- /dev/null +++ b/result/valid/781333.xml.err @@ -0,0 +1,3 @@ +./test/valid/781333.xml:4: element a: validity error : Element a content does not follow the DTD, expecting ( ..., got + + ^ diff --git a/result/valid/781333.xml.err.rdr b/result/valid/781333.xml.err.rdr new file mode 100644 index 00000000..5ff56992 --- /dev/null +++ b/result/valid/781333.xml.err.rdr @@ -0,0 +1,6 @@ +./test/valid/781333.xml:4: element a: validity error : Element a content does not follow the DTD, expecting ( ..., got + + ^ +./test/valid/781333.xml:5: element a: validity error : Element a content does not follow the DTD, Expecting more child + +^ diff --git a/test/valid/781333.xml b/test/valid/781333.xml new file mode 100644 index 00000000..b29e5a68 --- /dev/null +++ b/test/valid/781333.xml @@ -0,0 +1,4 @@ + +]> + diff --git a/valid.c b/valid.c index 0a8e58ab..8075d3a0 100644 --- a/valid.c +++ b/valid.c @@ -1266,22 +1266,23 @@ xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int case XML_ELEMENT_CONTENT_PCDATA: strcat(buf, "#PCDATA"); break; - case XML_ELEMENT_CONTENT_ELEMENT: + case XML_ELEMENT_CONTENT_ELEMENT: { + int qnameLen = xmlStrlen(content->name); + + if (content->prefix != NULL) + qnameLen += xmlStrlen(content->prefix) + 1; + if (size - len < qnameLen + 10) { + strcat(buf, " ..."); + return; + } if (content->prefix != NULL) { - if (size - len < xmlStrlen(content->prefix) + 10) { - strcat(buf, " ..."); - return; - } strcat(buf, (char *) content->prefix); strcat(buf, ":"); } - if (size - len < xmlStrlen(content->name) + 10) { - strcat(buf, " ..."); - return; - } if (content->name != NULL) strcat(buf, (char *) content->name); break; + } case XML_ELEMENT_CONTENT_SEQ: if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) @@ -1323,6 +1324,7 @@ xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int xmlSnprintfElementContent(buf, size, content->c2, 0); break; } + if (size - strlen(buf) <= 2) return; if (englob) strcat(buf, ")"); switch (content->ocur) { -- 2.14.1