[CVE-2020-24977] Fix out-of-bounds read with 'xmllint --htmlout' 31/253731/1 accepted/tizen/base/tool/20210223.010101 submit/tizen_base/20210217.050546
authorNick Wellnhofer <wellnhofer@aevum.de>
Fri, 7 Aug 2020 19:54:27 +0000 (21:54 +0200)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Wed, 17 Feb 2021 04:55:21 +0000 (13:55 +0900)
Make sure that truncated UTF-8 sequences don't cause an out-of-bounds
array access.

Thanks to @SuhwanSong and the Agency for Defense Development (ADD) for
the report.

Fixes #178.

Change-Id: Ibae2db998ba4c0f4ba1130869b4cec77a65d648e
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
xmllint.c

index 735d951..c071267 100644 (file)
--- a/xmllint.c
+++ b/xmllint.c
@@ -528,6 +528,12 @@ static void
 xmlHTMLEncodeSend(void) {
     char *result;
 
+    /*
+     * xmlEncodeEntitiesReentrant assumes valid UTF-8, but the buffer might
+     * end with a truncated UTF-8 sequence. This is a hack to at least avoid
+     * an out-of-bounds read.
+     */
+    memset(&buffer[sizeof(buffer)-4], 0, 4);
     result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
     if (result) {
        xmlGenericError(xmlGenericErrorContext, "%s", result);