Blame SOURCES/libxml2-2.9.7-CVE-2022-40304.patch

e5eacc
From a8fa5f7b5c3c745397b3178405d6be9fdb3cfcbc Mon Sep 17 00:00:00 2001
e5eacc
From: Nick Wellnhofer <wellnhofer@aevum.de>
e5eacc
Date: Wed, 31 Aug 2022 22:11:25 +0200
e5eacc
Subject: [PATCH 2/2] Fix dict corruption caused by entity reference cycles
e5eacc
e5eacc
When an entity reference cycle is detected, the entity content is
e5eacc
cleared by setting its first byte to zero. But the entity content might
e5eacc
be allocated from a dict. In this case, the dict entry becomes corrupted
e5eacc
leading to all kinds of logic errors, including memory errors like
e5eacc
double-frees.
e5eacc
e5eacc
Stop storing entity content, orig, ExternalID and SystemID in a dict.
e5eacc
These values are unlikely to occur multiple times in a document, so they
e5eacc
shouldn't have been stored in a dict in the first place.
e5eacc
e5eacc
Thanks to Ned Williamson and Nathan Wachholz working with Google Project
e5eacc
Zero for the report!
e5eacc
---
e5eacc
 entities.c | 55 ++++++++++++++++--------------------------------------
e5eacc
 1 file changed, 16 insertions(+), 39 deletions(-)
e5eacc
e5eacc
diff --git a/entities.c b/entities.c
e5eacc
index c8193376..3bf1c3ce 100644
e5eacc
--- a/entities.c
e5eacc
+++ b/entities.c
e5eacc
@@ -112,36 +112,19 @@ xmlFreeEntity(xmlEntityPtr entity)
e5eacc
     if ((entity->children) && (entity->owner == 1) &&
e5eacc
         (entity == (xmlEntityPtr) entity->children->parent))
e5eacc
         xmlFreeNodeList(entity->children);
e5eacc
-    if (dict != NULL) {
e5eacc
-        if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
e5eacc
-            xmlFree((char *) entity->name);
e5eacc
-        if ((entity->ExternalID != NULL) &&
e5eacc
-	    (!xmlDictOwns(dict, entity->ExternalID)))
e5eacc
-            xmlFree((char *) entity->ExternalID);
e5eacc
-        if ((entity->SystemID != NULL) &&
e5eacc
-	    (!xmlDictOwns(dict, entity->SystemID)))
e5eacc
-            xmlFree((char *) entity->SystemID);
e5eacc
-        if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
e5eacc
-            xmlFree((char *) entity->URI);
e5eacc
-        if ((entity->content != NULL)
e5eacc
-            && (!xmlDictOwns(dict, entity->content)))
e5eacc
-            xmlFree((char *) entity->content);
e5eacc
-        if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
e5eacc
-            xmlFree((char *) entity->orig);
e5eacc
-    } else {
e5eacc
-        if (entity->name != NULL)
e5eacc
-            xmlFree((char *) entity->name);
e5eacc
-        if (entity->ExternalID != NULL)
e5eacc
-            xmlFree((char *) entity->ExternalID);
e5eacc
-        if (entity->SystemID != NULL)
e5eacc
-            xmlFree((char *) entity->SystemID);
e5eacc
-        if (entity->URI != NULL)
e5eacc
-            xmlFree((char *) entity->URI);
e5eacc
-        if (entity->content != NULL)
e5eacc
-            xmlFree((char *) entity->content);
e5eacc
-        if (entity->orig != NULL)
e5eacc
-            xmlFree((char *) entity->orig);
e5eacc
-    }
e5eacc
+    if ((entity->name != NULL) &&
e5eacc
+        ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
e5eacc
+        xmlFree((char *) entity->name);
e5eacc
+    if (entity->ExternalID != NULL)
e5eacc
+        xmlFree((char *) entity->ExternalID);
e5eacc
+    if (entity->SystemID != NULL)
e5eacc
+        xmlFree((char *) entity->SystemID);
e5eacc
+    if (entity->URI != NULL)
e5eacc
+        xmlFree((char *) entity->URI);
e5eacc
+    if (entity->content != NULL)
e5eacc
+        xmlFree((char *) entity->content);
e5eacc
+    if (entity->orig != NULL)
e5eacc
+        xmlFree((char *) entity->orig);
e5eacc
     xmlFree(entity);
e5eacc
 }
e5eacc
 
e5eacc
@@ -177,18 +160,12 @@ xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
e5eacc
 	    ret->SystemID = xmlStrdup(SystemID);
e5eacc
     } else {
e5eacc
         ret->name = xmlDictLookup(dict, name, -1);
e5eacc
-	if (ExternalID != NULL)
e5eacc
-	    ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
e5eacc
-	if (SystemID != NULL)
e5eacc
-	    ret->SystemID = xmlDictLookup(dict, SystemID, -1);
e5eacc
+	ret->ExternalID = xmlStrdup(ExternalID);
e5eacc
+	ret->SystemID = xmlStrdup(SystemID);
e5eacc
     }
e5eacc
     if (content != NULL) {
e5eacc
         ret->length = xmlStrlen(content);
e5eacc
-	if ((dict != NULL) && (ret->length < 5))
e5eacc
-	    ret->content = (xmlChar *)
e5eacc
-	                   xmlDictLookup(dict, content, ret->length);
e5eacc
-	else
e5eacc
-	    ret->content = xmlStrndup(content, ret->length);
e5eacc
+	ret->content = xmlStrndup(content, ret->length);
e5eacc
      } else {
e5eacc
         ret->length = 0;
e5eacc
         ret->content = NULL;
e5eacc
-- 
e5eacc
2.39.0
e5eacc