Skip to content
Open
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
38 changes: 38 additions & 0 deletions ext/dom/tests/gh22554.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
GH-22554 (Use-after-free when an XPath callback returns a node from a document created inside the callback)
--CREDITS--
waseem-cve
--EXTENSIONS--
dom
--FILE--
<?php

$doc = new DOMDocument;
$doc->loadXML('<root/>');

$xp = new DOMXPath($doc);
$xp->registerNamespace('my', 'my.ns');

$xp->registerPHPFunctionNS('my.ns', 'include', function () {
$d = new DOMDocument;
$d->loadXML('<r><uaf/></r>');

return $d->documentElement;
});

$xp->registerPHPFunctionNS('my.ns', 'process', function ($arg) {
echo "process arg: ", get_class($arg[0]), " ", $arg[0]->nodeName, "\n";
return 'x';
});

$result = $xp->query('my:process(my:include()/uaf)');
var_dump($result->length);
unset($xp);

echo "Done\n";

?>
--EXPECT--
process arg: DOMElement uaf
int(0)
Done
3 changes: 2 additions & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ static void dom_xpath_proxy_factory(xmlNodePtr node, zval *child, dom_object *in

ZEND_ASSERT(node->type != XML_NAMESPACE_DECL);

php_dom_create_object(node, child, intern);
dom_xpath_object *xobj = php_xpath_obj_from_obj(&intern->std);
php_dom_create_object(node, child, dom_xpath_intern_for_doc(xobj, node->doc));
}

static dom_xpath_object *dom_xpath_ext_fetch_intern(xmlXPathParserContextPtr ctxt)
Expand Down
Loading