Skip to content

Commit b0c38ac

Browse files
committed
ext/dom: Fix use-after-free in getElementById() after removing an ID attribute
php_libxml_node_free_resource() freed a detached ID attribute's children before xmlFreeProp() called xmlRemoveID(), which reads the ID value from those children, so the entry was not found and the document's ID table kept pointing at the freed attribute. Remove the ID before freeing the children, as php_libxml_node_free_list() already does. This also covers removeAttribute() while a reference to the attribute node is still held.
1 parent c93b3d1 commit b0c38ac

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ PHP NEWS
6262
starts with a non-ASCII character). (David Carlier)
6363
. Fixed a crash and a use-after-free in key() of an attribute or DTD named
6464
node map iterator that has no current item. (Ilia Alshanetsky)
65+
. Fixed use-after-free in getElementById() after a removed ID attribute is
66+
freed. (Ilia Alshanetsky)
6567

6668
- Filter:
6769
. Fixed stack overflow in filter_var() and filter_var_array() with deeply
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
getElementById() after a removed ID attribute is freed
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument();
9+
$doc->loadXML('<root><a xml:id="x"/><b xml:id="y"/></root>');
10+
$a = $doc->documentElement->firstChild;
11+
$attr = $a->getAttributeNode('xml:id');
12+
$a->removeAttributeNode($attr);
13+
unset($attr);
14+
echo "removed id: ";
15+
var_dump($doc->getElementById('x'));
16+
echo "remaining id: ", $doc->getElementById('y')->nodeName, "\n";
17+
18+
$doc = new DOMDocument();
19+
$doc->loadXML('<!DOCTYPE root [<!ATTLIST e id ID #IMPLIED>]><root><e id="x"/><e id="y"/></root>');
20+
$e = $doc->documentElement->firstChild;
21+
$attr = $e->getAttributeNode('id');
22+
$e->removeAttributeNode($attr);
23+
unset($attr);
24+
echo "removed DTD id: ";
25+
var_dump($doc->getElementById('x'));
26+
echo "remaining DTD id: ", $doc->getElementById('y')->nodeName, "\n";
27+
28+
$doc = Dom\XMLDocument::createFromString('<root><a xml:id="x"/><b xml:id="y"/></root>');
29+
$a = $doc->documentElement->firstChild;
30+
$attr = $a->getAttributeNode('xml:id');
31+
$a->removeAttributeNode($attr);
32+
unset($attr);
33+
echo "removed Dom\\XMLDocument id: ";
34+
var_dump($doc->getElementById('x'));
35+
echo "remaining Dom\\XMLDocument id: ", $doc->getElementById('y')->nodeName, "\n";
36+
37+
$doc = new DOMDocument();
38+
$doc->loadXML('<root><a xml:id="x"/><b xml:id="y"/></root>');
39+
$a = $doc->documentElement->firstChild;
40+
$attr = $a->getAttributeNode('xml:id');
41+
$a->removeAttribute('xml:id');
42+
unset($attr);
43+
echo "removeAttribute() id: ";
44+
var_dump($doc->getElementById('x'));
45+
echo "remaining removeAttribute() id: ", $doc->getElementById('y')->nodeName, "\n";
46+
47+
?>
48+
--EXPECT--
49+
removed id: NULL
50+
remaining id: b
51+
removed DTD id: NULL
52+
remaining DTD id: e
53+
removed Dom\XMLDocument id: NULL
54+
remaining Dom\XMLDocument id: b
55+
removeAttribute() id: NULL
56+
remaining removeAttribute() id: b

‎ext/libxml/libxml.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,9 @@ PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node)
14801480
break;
14811481
default:
14821482
if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) {
1483+
if (node->type == XML_ATTRIBUTE_NODE && node->doc != NULL && ((xmlAttrPtr) node)->atype == XML_ATTRIBUTE_ID) {
1484+
xmlRemoveID(node->doc, (xmlAttrPtr) node);
1485+
}
14831486
php_libxml_node_free_list((xmlNodePtr) node->children);
14841487
if (node->type == XML_ELEMENT_NODE) {
14851488
php_libxml_node_free_list((xmlNodePtr) node->properties);

0 commit comments

Comments
 (0)