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

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