Skip to content

Commit d88efc0

Browse files
nwellnhofdahlstrom-g
authored andcommitted
[CVE-2022-40304] Fix dict corruption caused by entity reference cycles
When an entity reference cycle is detected, the entity content is cleared by setting its first byte to zero. But the entity content might be allocated from a dict. In this case, the dict entry becomes corrupted leading to all kinds of logic errors, including memory errors like double-frees. Stop storing entity content, orig, ExternalID and SystemID in a dict. These values are unlikely to occur multiple times in a document, so they shouldn't have been stored in a dict in the first place. Thanks to Ned Williamson and Nathan Wachholz working with Google Project Zero for the report! (cherry picked from commit 1b41ec4e9433b05bb0376be4725804c54ef1d80b)
1 parent 6455c41 commit d88efc0

1 file changed

Lines changed: 16 additions & 39 deletions

File tree

‎third_party/libxml/src/entities.c‎

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,19 @@ xmlFreeEntity(xmlEntityPtr entity)
128128
if ((entity->children) && (entity->owner == 1) &&
129129
(entity == (xmlEntityPtr) entity->children->parent))
130130
xmlFreeNodeList(entity->children);
131-
if (dict != NULL) {
132-
if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
133-
xmlFree((char *) entity->name);
134-
if ((entity->ExternalID != NULL) &&
135-
(!xmlDictOwns(dict, entity->ExternalID)))
136-
xmlFree((char *) entity->ExternalID);
137-
if ((entity->SystemID != NULL) &&
138-
(!xmlDictOwns(dict, entity->SystemID)))
139-
xmlFree((char *) entity->SystemID);
140-
if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
141-
xmlFree((char *) entity->URI);
142-
if ((entity->content != NULL)
143-
&& (!xmlDictOwns(dict, entity->content)))
144-
xmlFree((char *) entity->content);
145-
if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
146-
xmlFree((char *) entity->orig);
147-
} else {
148-
if (entity->name != NULL)
149-
xmlFree((char *) entity->name);
150-
if (entity->ExternalID != NULL)
151-
xmlFree((char *) entity->ExternalID);
152-
if (entity->SystemID != NULL)
153-
xmlFree((char *) entity->SystemID);
154-
if (entity->URI != NULL)
155-
xmlFree((char *) entity->URI);
156-
if (entity->content != NULL)
157-
xmlFree((char *) entity->content);
158-
if (entity->orig != NULL)
159-
xmlFree((char *) entity->orig);
160-
}
131+
if ((entity->name != NULL) &&
132+
((dict == NULL) || (!xmlDictOwns(dict, entity->name))))
133+
xmlFree((char *) entity->name);
134+
if (entity->ExternalID != NULL)
135+
xmlFree((char *) entity->ExternalID);
136+
if (entity->SystemID != NULL)
137+
xmlFree((char *) entity->SystemID);
138+
if (entity->URI != NULL)
139+
xmlFree((char *) entity->URI);
140+
if (entity->content != NULL)
141+
xmlFree((char *) entity->content);
142+
if (entity->orig != NULL)
143+
xmlFree((char *) entity->orig);
161144
xmlFree(entity);
162145
}
163146

@@ -194,18 +177,12 @@ xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
194177
ret->SystemID = xmlStrdup(SystemID);
195178
} else {
196179
ret->name = xmlDictLookup(dict, name, -1);
197-
if (ExternalID != NULL)
198-
ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
199-
if (SystemID != NULL)
200-
ret->SystemID = xmlDictLookup(dict, SystemID, -1);
180+
ret->ExternalID = xmlStrdup(ExternalID);
181+
ret->SystemID = xmlStrdup(SystemID);
201182
}
202183
if (content != NULL) {
203184
ret->length = xmlStrlen(content);
204-
if ((dict != NULL) && (ret->length < 5))
205-
ret->content = (xmlChar *)
206-
xmlDictLookup(dict, content, ret->length);
207-
else
208-
ret->content = xmlStrndup(content, ret->length);
185+
ret->content = xmlStrndup(content, ret->length);
209186
} else {
210187
ret->length = 0;
211188
ret->content = NULL;

0 commit comments

Comments
 (0)