Skip to content

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

Merged
iliaal merged 1 commit into
php:masterfrom
iliaal:fix/xpath-callback-result-uaf-master
Nov 27, 2026
Merged

iliaal merged 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. Transforming such a document with XSLTProcessor throws the same exception unless the processor copies it, because xsl:strip-space makes libxslt free whitespace text nodes of its source. Protection for XSLT callbacks follows in a separate 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.

@devnexen

Copy link
Copy Markdown
Member

The read-only state seems bypassable via ext/xsl: a callback running a transformation with xsl:strip-space on the same document lets libxslt free whitespace text nodes the query still uses (ASAN heap-use-after-free in xsltApplyStripSpaces).

can the transformation be refused when the source document is read-only and not copied ?

@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from 4f5a047 to 3ed48b4 Compare November 21, 2026 16:25
@iliaal

iliaal commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Yes, the transformation now throws the same DOMException unless the processor copies the document (cloneDocument). Added your xsl:strip-space case as a test.

@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from 3ed48b4 to f2db1ad Compare November 27, 2026 00:08
Comment thread UPGRADING Resolved
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. XSLTProcessor refuses to
transform a read-only document in place, because libxslt frees whitespace
text nodes of its source for xsl:strip-space.
@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from f2db1ad to 08da9a8 Compare November 27, 2026 13:15
@iliaal
iliaal merged commit 8f4ae6d into php:master Nov 27, 2026
18 checks passed
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.

2 participants