Skip to content

Fix GH-21952: UAF in DOMNotation after DOCTYPE removal. - #21955

Open
devnexen wants to merge 2 commits into
php:PHP-8.6from
devnexen:gh21952
Open

devnexen wants to merge 2 commits into
php:PHP-8.6from
devnexen:gh21952

Conversation

@devnexen

@devnexen devnexen commented May 5, 2026 •

Copy link
Copy Markdown
Member

Commit 9498bc3 wired Dom\Notation to its owning DocType by setting
ret->parent = parent_dtd in create_notation. Because the orphan xmlEntity
allocated for the wrapper is not in any tree, the parent pointer dangles
once the DocType is removed and freed, triggering a use-after-free when
parentNode is read (e.g. via var_dump on the surviving notation).

Drop the parent assignment and align Dom\Notation::$parentNode with the
W3C DOM Level 3 Core specification (always null) and ::$isConnected with
it (always false). ownerDocument and baseURI keep working through doc.

@devnexen devnexen changed the title Gh21952 Fix phpGH-21952: UAF in DOMNotation after DOCTYPE removal. May 5, 2026
@devnexen devnexen changed the title Fix phpGH-21952: UAF in DOMNotation after DOCTYPE removal. Fix GH-21952: UAF in DOMNotation after DOCTYPE removal. May 5, 2026
@devnexen
devnexen marked this pull request as ready for review May 5, 2026 17:50
@Sjord

Sjord commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Looks good to me.

@jordikroon Can you take a look at this? (as author of 9498bc3)

@iliaal

iliaal commented Nov 7, 2026

Copy link
Copy Markdown
Member

The classes registration fix here (dom_modern_notation_prop_handlers) is a crash on stable branches too: reading $notation->publicId on any Dom\Notation segfaults on 8.4+. Could you send that line separately against PHP-8.4? The rest of this PR can stay on master.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the parent assignment fixes it on its own: once parent is NULL, the generic parentNode and isConnected handlers already return null/false, and parentElement, getRootNode() and contains() read the same pointer. So the two new handlers, their declarations and the comment can go. 9498bc3 shipped in 8.6.0, so this should target PHP-8.6 with a NEWS entry.

Commit 9498bc3 wired Dom\Notation to its owning DocType by setting
ret->parent = parent_dtd in create_notation. Because the orphan xmlEntity
allocated for the wrapper is not in any tree, the parent pointer dangles
once the DocType is removed and freed, triggering a use-after-free when
parentNode is read (e.g. via var_dump on the surviving notation).

Drop the parent assignment and align Dom\Notation::$parentNode with the
W3C DOM Level 3 Core specification (always null) and ::$isConnected with
it (always false). ownerDocument and baseURI keep working through doc.
@devnexen
devnexen changed the base branch from master to PHP-8.6 November 27, 2026 04:54
Comment thread ext/dom/dom_iterators.c Outdated
ret->SystemID = xmlStrdup(SystemID);
if (parent_dtd != NULL) {
ret->parent = parent_dtd;
/* Don't store parent_dtd: orphan notation can outlive the DTD when

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: with ret->parent no longer set, the generic parentNode and isConnected handlers already return null/false, so the two notation handlers, their declarations and this comment can go.

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.

[DOM] heap-use-after-free in php_dom_object_get_data when DOMNotation object is accessed after its owning DOCTYPE is removed

3 participants