[php-src] PHP-8.5: ext/dom: fix UAF on namespace axis results with foreign-doc XPath callback nodes
| From: | David Carlier | Date: | Wed, 28 Oct 2026 03:18:42 +0000 |
| Subject: | [php-src] PHP-8.5: ext/dom: fix UAF on namespace axis results with foreign-doc XPath callback nodes | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-142864@lists.php.net to get a copy of this message | ||
Author: David Carlier (devnexen)
Date: 2026-10-28T03:14:25Z
Commit: https://github.com/php/php-src/commit/98b580b0d26645a2c26fb7459f6c16d0d086b207
Raw diff: https://github.com/php/php-src/commit/98b580b0d26645a2c26fb7459f6c16d0d086b207.diff
ext/dom: fix UAF on namespace axis results with foreign-doc XPath callback nodes
Follow-up to https://github.com/php/php-src/pull/24570#issuecomment-6375965729
The namespace node branch of php_xpath_eval() still created the parent
element with the DOMXPath's own dom, so with a foreign-doc callback node
the parent proxy referenced the wrong document. Use
dom_xpath_intern_for_doc() there too.
Close GH-24629
Changed paths:
A ext/dom/tests/xpath_php_function_foreign_doc_namespace_axis.phpt
M NEWS
M ext/dom/xpath.c
Diff:
diff --git a/NEWS b/NEWS
index 070df0bbb0ea..79fdd4b7ef67 100644
--- a/NEWS
+++ b/NEWS
@@ -65,6 +65,8 @@ PHP NEWS
. Fixed DOMXPath crash wrapping a foreign node after a nodeset callback.
(Ilia Alshanetsky)
. Fix error paths in dom_xml_serializing_a_document_node(). (ndossche)
+ . Fixed use-after-free on namespace axis results with a foreign-document
+ XPath callback node. (David Carlier)
- FFI:
. Fixed bug GH-24582 (Memory leaks in ext/ffi parser when finalize_type()
diff --git a/ext/dom/tests/xpath_php_function_foreign_doc_namespace_axis.phpt
b/ext/dom/tests/xpath_php_function_foreign_doc_namespace_axis.phpt
new file mode 100644
index 000000000000..ad77a052d454
--- /dev/null
+++ b/ext/dom/tests/xpath_php_function_foreign_doc_namespace_axis.phpt
@@ -0,0 +1,36 @@
+--TEST--
+DOMXPath: namespace axis result on a node from a document created inside an XPath callback
+--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 xmlns:a="urn:a"/></r>');
+ return $d->documentElement;
+});
+
+$result = $xp->query('my:include()/uaf/namespace::*');
+var_dump($result->length);
+unset($xp);
+
+foreach ($result as $ns) {
+ var_dump($ns->nodeName, $ns->parentNode->nodeName);
+}
+
+echo "Done\n";
+
+?>
+--EXPECT--
+int(2)
+string(9) "xmlns:xml"
+string(3) "uaf"
+string(7) "xmlns:a"
+string(3) "uaf"
+Done
diff --git a/ext/dom/xpath.c b/ext/dom/xpath.c
index a9832f4c3e22..af5d7eacc2ed 100644
--- a/ext/dom/xpath.c
+++ b/ext/dom/xpath.c
@@ -411,7 +411,7 @@ static void php_xpath_eval(INTERNAL_FUNCTION_PARAMETERS, int type, bool modern)
/* Make sure parent dom object exists, so we can take an extra reference. */
zval parent_zval; /* don't destroy me, my lifetime is transfered to the fake namespace
decl */
- php_dom_create_object(nsparent, &parent_zval, &intern->dom);
+ php_dom_create_object(nsparent, &parent_zval, dom_xpath_intern_for_doc(intern,
nsparent->doc));
dom_object *parent_intern = Z_DOMOBJ_P(&parent_zval);
node = php_dom_create_fake_namespace_decl(nsparent, original, &child, parent_intern);