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

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