Skip to content
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
20 changes: 20 additions & 0 deletions ext/dom/tests/gh22077.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-22077 (UAF in custom XPath function)
--FILE--
<?php
$document = new DOMDocument;
$xpath = new DOMXPath($document);
$xpath->registerNamespace("my", "my.ns");
$xpath->registerPHPFunctionNS('my.ns', 'include', function(): DOMElement {
$includedDocument = new DOMDocument;
$includedDocument->loadXML('<root><uaf/><node/><uaf/></root>');
return $includedDocument->documentElement;
});
$nodeset = $xpath->query('my:include()/uaf');
$node = $nodeset->item(0);
var_dump($nodeset->length);
var_dump($node->ownerDocument->saveXML($node));
?>
--EXPECT--
int(2)
string(6) "<uaf/>"
21 changes: 20 additions & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@

#ifdef LIBXML_XPATH_ENABLED

static dom_object *dom_xpath_intern_for_doc(dom_xpath_object *xpath_obj, xmlDocPtr doc)
{
if (xpath_obj->dom.document && xpath_obj->dom.document->ptr == doc) {
return &xpath_obj->dom;
}
HashTable *node_list = xpath_obj->xpath_callbacks.node_list;
if (node_list) {
zval *entry;
ZEND_HASH_PACKED_FOREACH_VAL(node_list, entry) {
dom_object *obj = Z_DOMOBJ_P(entry);
if (obj->document && obj->document->ptr == doc) {
return obj;
}
} ZEND_HASH_FOREACH_END();
}
return &xpath_obj->dom;
}

void dom_xpath_objects_free_storage(zend_object *object)
{
dom_xpath_object *intern = php_xpath_obj_from_obj(object);
Expand Down Expand Up @@ -357,7 +375,8 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)

node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);
} else {
php_dom_create_object(node, &child, &intern->dom);
dom_object *parent = dom_xpath_intern_for_doc(intern, node->doc);
php_dom_create_object(node, &child, parent);
}
add_next_index_zval(&retval, &child);
}
Expand Down
Loading