Skip to content

Commit 18b18f0

Browse files
committed
Fix phpGH-16777: Calling the constructor again on a DOM object after it is in a document causes UAF
Closes phpGH-16824.
1 parent 2ba1859 commit 18b18f0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ PHP NEWS
1515
- Curl:
1616
. Fixed bug GH-16802 (open_basedir bypass using curl extension). (nielsdos)
1717

18+
- DOM:
19+
. Fixed bug GH-16777 (Calling the constructor again on a DOM object after it
20+
is in a document causes UAF). (nielsdos)
21+
1822
- FPM:
1923
. Fixed GH-16432 (PHP-FPM 8.2 SIGSEGV in fpm_get_status). (Jakub Zelenka)
2024

ext/dom/node.c

+3
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ PHP_METHOD(DOMNode, insertBefore)
10241024
}
10251025

10261026
if (child->doc == NULL && parentp->doc != NULL) {
1027+
xmlSetTreeDoc(child, parentp->doc);
10271028
dom_set_document_ref_pointers(child, intern->document);
10281029
}
10291030

@@ -1188,6 +1189,7 @@ PHP_METHOD(DOMNode, replaceChild)
11881189
}
11891190

11901191
if (newchild->doc == NULL && nodep->doc != NULL) {
1192+
xmlSetTreeDoc(newchild, nodep->doc);
11911193
dom_set_document_ref_pointers(newchild, intern->document);
11921194
}
11931195

@@ -1291,6 +1293,7 @@ PHP_METHOD(DOMNode, appendChild)
12911293
}
12921294

12931295
if (child->doc == NULL && nodep->doc != NULL) {
1296+
xmlSetTreeDoc(child, nodep->doc);
12941297
dom_set_document_ref_pointers(child, intern->document);
12951298
}
12961299

ext/dom/tests/gh16777_1.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$text = new DOMText('my value');
8+
$doc = new DOMDocument();
9+
$doc->appendChild($text);
10+
$text->__construct('my new value');
11+
$doc->appendChild($text);
12+
echo $doc->saveXML();
13+
$dom2 = new DOMDocument();
14+
try {
15+
$dom2->appendChild($text);
16+
} catch (DOMException $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
?>
20+
--EXPECT--
21+
<?xml version="1.0"?>
22+
my value
23+
my new value
24+
Wrong Document Error

ext/dom/tests/gh16777_2.phpt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$el = new DOMElement('name');
8+
$el->append($child = new DOMElement('child'));
9+
$doc = new DOMDocument();
10+
$doc->appendChild($el);
11+
$el->__construct('newname');
12+
$doc->appendChild($el);
13+
echo $doc->saveXML();
14+
$dom2 = new DOMDocument();
15+
try {
16+
$dom2->appendChild($el);
17+
} catch (DOMException $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
var_dump($child->ownerDocument === $doc);
21+
?>
22+
--EXPECT--
23+
<?xml version="1.0"?>
24+
<name><child/></name>
25+
<newname/>
26+
Wrong Document Error
27+
bool(true)

0 commit comments

Comments
 (0)