xzyang / rpms / libxml2

Forked from rpms/libxml2 3 years ago
Clone

Blame SOURCES/libxml2-More-format-string-warnings-with-possible-format-string-vulnerability.patch

6dedca
From 6a2a4e753bbba26b2de3d32f11c28691bec967dc Mon Sep 17 00:00:00 2001
6dedca
From: David Kilzer <ddkilzer@apple.com>
6dedca
Date: Mon, 23 May 2016 14:58:41 +0800
6dedca
Subject: [PATCH] More format string warnings with possible format string
6dedca
 vulnerability
6dedca
To: libvir-list@redhat.com
6dedca
6dedca
For https://bugzilla.gnome.org/show_bug.cgi?id=761029
6dedca
6dedca
adds a new xmlEscapeFormatString() function to escape composed format
6dedca
strings
6dedca
6dedca
Signed-off-by: Daniel Veillard <veillard@redhat.com>
6dedca
---
6dedca
 libxml.h     |  3 +++
6dedca
 relaxng.c    |  3 ++-
6dedca
 xmlschemas.c | 39 ++++++++++++++++++++++++++-------------
6dedca
 xmlstring.c  | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
6dedca
 4 files changed, 86 insertions(+), 14 deletions(-)
6dedca
6dedca
diff --git a/libxml.h b/libxml.h
6dedca
index 4558b70..88e515f 100644
6dedca
--- a/libxml.h
6dedca
+++ b/libxml.h
6dedca
@@ -9,6 +9,8 @@
6dedca
 #ifndef __XML_LIBXML_H__
6dedca
 #define __XML_LIBXML_H__
6dedca
 
6dedca
+#include <libxml/xmlstring.h>
6dedca
+
6dedca
 #ifndef NO_LARGEFILE_SOURCE
6dedca
 #ifndef _LARGEFILE_SOURCE
6dedca
 #define _LARGEFILE_SOURCE
6dedca
@@ -93,6 +95,7 @@ int __xmlInitializeDict(void);
6dedca
 int __xmlRandom(void);
6dedca
 #endif
6dedca
 
6dedca
+XMLPUBFUN xmlChar * XMLCALL xmlEscapeFormatString(xmlChar **msg);
6dedca
 int xmlNop(void);
6dedca
 
6dedca
 #ifdef IN_LIBXML
6dedca
diff --git a/relaxng.c b/relaxng.c
6dedca
index b531081..99e9901 100644
6dedca
--- a/relaxng.c
6dedca
+++ b/relaxng.c
6dedca
@@ -2215,7 +2215,8 @@ xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
6dedca
         snprintf(msg, 1000, "Unknown error code %d\n", err);
6dedca
     }
6dedca
     msg[1000 - 1] = 0;
6dedca
-    return (xmlStrdup((xmlChar *) msg));
6dedca
+    xmlChar *result = xmlCharStrdup(msg);
6dedca
+    return (xmlEscapeFormatString(&result));
6dedca
 }
6dedca
 
6dedca
 /**
6dedca
diff --git a/xmlschemas.c b/xmlschemas.c
6dedca
index 398cdd8..1bcb0f0 100644
6dedca
--- a/xmlschemas.c
6dedca
+++ b/xmlschemas.c
6dedca
@@ -1769,7 +1769,7 @@ xmlSchemaFormatItemForReport(xmlChar **buf,
6dedca
     }
6dedca
     FREE_AND_NULL(str)
6dedca
 
6dedca
-    return (*buf);
6dedca
+    return (xmlEscapeFormatString(buf));
6dedca
 }
6dedca
 
6dedca
 /**
6dedca
@@ -2247,6 +2247,13 @@ xmlSchemaFormatNodeForError(xmlChar ** msg,
6dedca
 	TODO
6dedca
 	return (NULL);
6dedca
     }
6dedca
+
6dedca
+    /*
6dedca
+     * xmlSchemaFormatItemForReport() also returns an escaped format
6dedca
+     * string, so do this before calling it below (in the future).
6dedca
+     */
6dedca
+    xmlEscapeFormatString(msg);
6dedca
+
6dedca
     /*
6dedca
     * VAL TODO: The output of the given schema component is currently
6dedca
     * disabled.
6dedca
@@ -2474,11 +2481,13 @@ xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
6dedca
 	msg = xmlStrcat(msg, BAD_CAST " '");
6dedca
 	if (type->builtInType != 0) {
6dedca
 	    msg = xmlStrcat(msg, BAD_CAST "xs:");
6dedca
-	    msg = xmlStrcat(msg, type->name);
6dedca
-	} else
6dedca
-	    msg = xmlStrcat(msg,
6dedca
-		xmlSchemaFormatQName(&str,
6dedca
-		    type->targetNamespace, type->name));
6dedca
+	    str = xmlStrdup(type->name);
6dedca
+	} else {
6dedca
+	    const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
6dedca
+	    if (!str)
6dedca
+		str = xmlStrdup(qName);
6dedca
+	}
6dedca
+	msg = xmlStrcat(msg, xmlEscapeFormatString(&str);;
6dedca
 	msg = xmlStrcat(msg, BAD_CAST "'");
6dedca
 	FREE_AND_NULL(str);
6dedca
     }
6dedca
@@ -2615,7 +2624,7 @@ xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
6dedca
 		str = xmlStrcat(str, BAD_CAST ", ");
6dedca
 	}
6dedca
 	str = xmlStrcat(str, BAD_CAST " ).\n");
6dedca
-	msg = xmlStrcat(msg, BAD_CAST str);
6dedca
+	msg = xmlStrcat(msg, xmlEscapeFormatString(&str);;
6dedca
 	FREE_AND_NULL(str)
6dedca
     } else
6dedca
       msg = xmlStrcat(msg, BAD_CAST "\n");
6dedca
@@ -3139,11 +3148,13 @@ xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
6dedca
 		msg = xmlStrcat(msg, BAD_CAST " '");
6dedca
 		if (type->builtInType != 0) {
6dedca
 		    msg = xmlStrcat(msg, BAD_CAST "xs:");
6dedca
-		    msg = xmlStrcat(msg, type->name);
6dedca
-		} else
6dedca
-		    msg = xmlStrcat(msg,
6dedca
-			xmlSchemaFormatQName(&str,
6dedca
-			    type->targetNamespace, type->name));
6dedca
+		    str = xmlStrdup(type->name);
6dedca
+		} else {
6dedca
+		    const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
6dedca
+		    if (!str)
6dedca
+			str = xmlStrdup(qName);
6dedca
+		}
6dedca
+		msg = xmlStrcat(msg, xmlEscapeFormatString(&str);;
6dedca
 		msg = xmlStrcat(msg, BAD_CAST "'.");
6dedca
 		FREE_AND_NULL(str);
6dedca
 	    }
6dedca
@@ -3156,7 +3167,9 @@ xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
6dedca
 	}
6dedca
 	if (expected) {
6dedca
 	    msg = xmlStrcat(msg, BAD_CAST " Expected is '");
6dedca
-	    msg = xmlStrcat(msg, BAD_CAST expected);
6dedca
+	    xmlChar *expectedEscaped = xmlCharStrdup(expected);
6dedca
+	    msg = xmlStrcat(msg, xmlEscapeFormatString(&expectedEscaped));
6dedca
+	    FREE_AND_NULL(expectedEscaped);
6dedca
 	    msg = xmlStrcat(msg, BAD_CAST "'.\n");
6dedca
 	} else
6dedca
 	    msg = xmlStrcat(msg, BAD_CAST "\n");
6dedca
diff --git a/xmlstring.c b/xmlstring.c
6dedca
index c66eef3..9164d2e 100644
6dedca
--- a/xmlstring.c
6dedca
+++ b/xmlstring.c
6dedca
@@ -987,5 +987,60 @@ xmlUTF8Strsub(const xmlChar *utf, int start, int len) {
6dedca
     return(xmlUTF8Strndup(utf, len));
6dedca
 }
6dedca
 
6dedca
+/**
6dedca
+ * xmlEscapeFormatString:
6dedca
+ * @msg:  a pointer to the string in which to escape '%' characters.
6dedca
+ * Must be a heap-allocated buffer created by libxml2 that may be
6dedca
+ * returned, or that may be freed and replaced.
6dedca
+ *
6dedca
+ * Replaces the string pointed to by 'msg' with an escaped string.
6dedca
+ * Returns the same string with all '%' characters escaped.
6dedca
+ */
6dedca
+xmlChar *
6dedca
+xmlEscapeFormatString(xmlChar **msg)
6dedca
+{
6dedca
+    xmlChar *msgPtr = NULL;
6dedca
+    xmlChar *result = NULL;
6dedca
+    xmlChar *resultPtr = NULL;
6dedca
+    size_t count = 0;
6dedca
+    size_t msgLen = 0;
6dedca
+    size_t resultLen = 0;
6dedca
+
6dedca
+    if (!msg || !*msg)
6dedca
+        return(NULL);
6dedca
+
6dedca
+    for (msgPtr = *msg; *msgPtr != '\0'; ++msgPtr) {
6dedca
+        ++msgLen;
6dedca
+        if (*msgPtr == '%')
6dedca
+            ++count;
6dedca
+    }
6dedca
+
6dedca
+    if (count == 0)
6dedca
+        return(*msg);
6dedca
+
6dedca
+    resultLen = msgLen + count + 1;
6dedca
+    result = (xmlChar *) xmlMallocAtomic(resultLen * sizeof(xmlChar));
6dedca
+    if (result == NULL) {
6dedca
+        /* Clear *msg to prevent format string vulnerabilities in
6dedca
+           out-of-memory situations. */
6dedca
+        xmlFree(*msg);
6dedca
+        *msg = NULL;
6dedca
+        xmlErrMemory(NULL, NULL);
6dedca
+        return(NULL);
6dedca
+    }
6dedca
+
6dedca
+    for (msgPtr = *msg, resultPtr = result; *msgPtr != '\0'; ++msgPtr, ++resultPtr) {
6dedca
+        *resultPtr = *msgPtr;
6dedca
+        if (*msgPtr == '%')
6dedca
+            *(++resultPtr) = '%';
6dedca
+    }
6dedca
+    result[resultLen - 1] = '\0';
6dedca
+
6dedca
+    xmlFree(*msg);
6dedca
+    *msg = result;
6dedca
+
6dedca
+    return *msg;
6dedca
+}
6dedca
+
6dedca
 #define bottom_xmlstring
6dedca
 #include "elfgcchack.h"
6dedca
-- 
6dedca
2.5.5
6dedca