Skip to content

ext/dom: Fix use-after-free when an XPath callback modifies the document - #25357

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/xpath-callback-result-uaf-master
Open

iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/xpath-callback-result-uaf-master

Conversation

@iliaal

@iliaal iliaal commented Nov 10, 2026

Copy link
Copy Markdown
Member

A php:function() callback of DOMXPath or Dom\XPath runs while libxml2 builds the node-set, so a callback that removes nodes, assigns textContent, removes attributes or drops the last reference to a detached subtree frees nodes the evaluation still uses. While such an evaluation runs, the trees it can reach are read-only, and modifying them throws a DOMException with NO_MODIFICATION_ALLOWED_ERR (an Error through SimpleXML). New nodes and document properties stay modifiable, so building and returning a fragment from a callback still works. This adds fields to php_libxml_ref_obj, php_libxml_node_ptr and php_dom_xpath_callbacks, so it targets master. XSLT gets the same protection in a follow-up PR. Not covered: an id() node-set spanning two detached subtrees where a callback drops the last reference to the other one, and user error handlers during an evaluation with no PHP functions registered. Reported by Hem1700.

A php:function() callback runs while libxml2 builds an XPath node-set, so
modifying the tree or dropping the last reference to a detached subtree
freed nodes the evaluation still referenced. While DOMXPath or Dom\XPath
evaluates with PHP functions registered, the tree of the evaluated document,
the tree of every node a callback returned and the detached subtrees of the
context node and of callback arguments are read-only: modifying them throws
a DOMException with NO_MODIFICATION_ALLOWED_ERR or, through SimpleXML, an
Error, and the evaluation keeps those subtrees alive until it ends. New
nodes and document properties stay modifiable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant