Skip to content

Commit a08847a

Browse files
committed
Fix #66783: UAF when appending DOMDocument to element
According to the DOM standard, elements may only contain element, text, processing instruction and comment nodes[1]. It is also specified that a HierarchyRequestError should be thrown if a document is to be inserted[2]. We follow that standard, and prevent the use-after-free this way. [1] <https://dom.spec.whatwg.org/#node-trees> [2] <https://dom.spec.whatwg.org/#mutation-algorithms> Closes GH-6765.
1 parent 4adc08a commit a08847a

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2021, PHP 7.4.18
44

5+
- DOM:
6+
. Fixed bug #66783 (UAF when appending DOMDocument to element). (cmb)
57

68
01 Apr 2021, PHP 7.4.17
79

ext/dom/php_dom.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,13 @@ int dom_hierarchy(xmlNodePtr parent, xmlNodePtr child)
13021302
{
13031303
xmlNodePtr nodep;
13041304

1305-
if (parent == NULL || child == NULL || child->doc != parent->doc) {
1306-
return SUCCESS;
1307-
}
1305+
if (parent == NULL || child == NULL || child->doc != parent->doc) {
1306+
return SUCCESS;
1307+
}
1308+
1309+
if (child->type == XML_DOCUMENT_NODE) {
1310+
return FAILURE;
1311+
}
13081312

13091313
nodep = parent;
13101314

ext/dom/tests/bug66783.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #66783 (UAF when appending DOMDocument to element)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('dom')) die('skip dom extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$doc = new DomDocument;
10+
$doc->loadXML('<root></root>');
11+
$e = $doc->createElement('e');
12+
try {
13+
$e->appendChild($doc);
14+
} catch (DOMException $ex) {
15+
echo $ex->getMessage(), PHP_EOL;
16+
}
17+
?>
18+
--EXPECTF--
19+
Hierarchy Request Error

0 commit comments

Comments
 (0)