Skip to content

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

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

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

Conversation

@iliaal

@iliaal iliaal commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

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.

XSLT is split into a follow-up PR (fix/xpath-callback-mutation-refuse-xsl). 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.

@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from 587797a to 669f6bf Compare November 9, 2026 21:33
@iliaal iliaal changed the title ext/dom: Fix use-after-free when an XPath or XSLT callback frees nodes ext/dom: Fix use-after-free when an XPath or XSLT callback modifies a document Nov 9, 2026
@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from 669f6bf to 2b1a1f4 Compare November 9, 2026 23:34
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.
@iliaal
iliaal force-pushed the fix/xpath-callback-result-uaf-master branch from 2b1a1f4 to 4f5a047 Compare November 10, 2026 00:42
@iliaal iliaal changed the title ext/dom: Fix use-after-free when an XPath or XSLT callback modifies a document ext/dom: Fix use-after-free when an XPath callback modifies the document Nov 10, 2026
@iliaal

iliaal commented Nov 10, 2026

Copy link
Copy Markdown
Owner Author

Submitted upstream as php#25357.

@iliaal iliaal closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant