Skip to content

Fix GH-16777: Calling the constructor again on a DOM object after it is in a document causes UAF #16824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ PHP_METHOD(DOMNode, insertBefore)
}

if (child->doc == NULL && parentp->doc != NULL) {
xmlSetTreeDoc(child, parentp->doc);
dom_set_document_ref_pointers(child, intern->document);
}

Expand Down Expand Up @@ -1188,6 +1189,7 @@ PHP_METHOD(DOMNode, replaceChild)
}

if (newchild->doc == NULL && nodep->doc != NULL) {
xmlSetTreeDoc(newchild, nodep->doc);
dom_set_document_ref_pointers(newchild, intern->document);
}

Expand Down Expand Up @@ -1291,6 +1293,7 @@ PHP_METHOD(DOMNode, appendChild)
}

if (child->doc == NULL && nodep->doc != NULL) {
xmlSetTreeDoc(child, nodep->doc);
dom_set_document_ref_pointers(child, intern->document);
}

Expand Down
24 changes: 24 additions & 0 deletions ext/dom/tests/gh16777_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
--EXTENSIONS--
dom
--FILE--
<?php
$text = new DOMText('my value');
$doc = new DOMDocument();
$doc->appendChild($text);
$text->__construct('my new value');
$doc->appendChild($text);
echo $doc->saveXML();
$dom2 = new DOMDocument();
try {
$dom2->appendChild($text);
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
<?xml version="1.0"?>
my value
my new value
Wrong Document Error
27 changes: 27 additions & 0 deletions ext/dom/tests/gh16777_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
--EXTENSIONS--
dom
--FILE--
<?php
$el = new DOMElement('name');
$el->append($child = new DOMElement('child'));
$doc = new DOMDocument();
$doc->appendChild($el);
$el->__construct('newname');
$doc->appendChild($el);
echo $doc->saveXML();
$dom2 = new DOMDocument();
try {
$dom2->appendChild($el);
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
var_dump($child->ownerDocument === $doc);
?>
--EXPECT--
<?xml version="1.0"?>
<name><child/></name>
<newname/>
Wrong Document Error
bool(true)
Loading