From 9845d53e407bf6b3f5f04302545f41d6be5962fd Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 5 May 2026 18:05:04 +0100 Subject: [PATCH 1/2] Fix GH-21952: UAF in DOMNotation after DOCTYPE removal. Commit 9498bc3ee13 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. --- ext/dom/dom_iterators.c | 5 +++- ext/dom/dom_properties.h | 2 ++ ext/dom/notation.c | 13 +++++++++ ext/dom/php_dom.c | 4 +++ ext/dom/tests/gh21952.phpt | 29 +++++++++++++++++++ ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt | 4 +-- .../xml/XMLDocument_node_notation_wiring.phpt | 24 +++++++-------- 7 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 ext/dom/tests/gh21952.phpt diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c index b50fe8679150..be29d3284d05 100644 --- a/ext/dom/dom_iterators.c +++ b/ext/dom/dom_iterators.c @@ -63,7 +63,10 @@ xmlNodePtr create_notation(xmlDtdPtr parent_dtd, const xmlChar *name, const xmlC ret->ExternalID = xmlStrdup(ExternalID); ret->SystemID = xmlStrdup(SystemID); if (parent_dtd != NULL) { - ret->parent = parent_dtd; + /* Don't store parent_dtd: orphan notation can outlive the DTD when + * removeChild($doctype) frees it. parentNode/isConnected return spec- + * mandated null/false unconditionally; ownerDocument keeps working + * via ret->doc. */ ret->doc = parent_dtd->doc; } return (xmlNodePtr) ret; diff --git a/ext/dom/dom_properties.h b/ext/dom/dom_properties.h index 918bf0c62f78..85ee0260e4b4 100644 --- a/ext/dom/dom_properties.h +++ b/ext/dom/dom_properties.h @@ -147,6 +147,8 @@ zend_result dom_nodelist_length_read(dom_object *obj, zval *retval); /* notation properties */ zend_result dom_notation_public_id_read(dom_object *obj, zval *retval); zend_result dom_notation_system_id_read(dom_object *obj, zval *retval); +zend_result dom_notation_parent_node_read(dom_object *obj, zval *retval); +zend_result dom_notation_is_connected_read(dom_object *obj, zval *retval); /* processinginstruction properties */ zend_result dom_processinginstruction_target_read(dom_object *obj, zval *retval); diff --git a/ext/dom/notation.c b/ext/dom/notation.c index 73d0d6b083a5..01cf4be4b924 100644 --- a/ext/dom/notation.c +++ b/ext/dom/notation.c @@ -69,4 +69,17 @@ zend_result dom_notation_system_id_read(dom_object *obj, zval *retval) /* }}} */ +zend_result dom_notation_parent_node_read(dom_object *obj, zval *retval) +{ + DOM_PROP_NODE(xmlNodePtr, nodep, obj); + ZVAL_NULL(retval); + return SUCCESS; +} + +zend_result dom_notation_is_connected_read(dom_object *obj, zval *retval) +{ + DOM_PROP_NODE(xmlNodePtr, nodep, obj); + ZVAL_FALSE(retval); + return SUCCESS; +} #endif diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index efa47c5c31a8..f9defb2d4bb4 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -1251,6 +1251,8 @@ PHP_MINIT_FUNCTION(dom) DOM_REGISTER_PROP_HANDLER(&dom_notation_prop_handlers, "publicId", dom_notation_public_id_read, NULL); DOM_REGISTER_PROP_HANDLER(&dom_notation_prop_handlers, "systemId", dom_notation_system_id_read, NULL); zend_hash_merge(&dom_notation_prop_handlers, &dom_node_prop_handlers, NULL, false); + DOM_OVERWRITE_PROP_HANDLER(&dom_notation_prop_handlers, "parentNode", dom_notation_parent_node_read, NULL); + DOM_OVERWRITE_PROP_HANDLER(&dom_notation_prop_handlers, "isConnected", dom_notation_is_connected_read, NULL); zend_hash_add_new_ptr(&classes, dom_notation_class_entry->name, &dom_notation_prop_handlers); dom_modern_notation_class_entry = register_class_Dom_Notation(dom_modern_node_class_entry); @@ -1261,6 +1263,8 @@ PHP_MINIT_FUNCTION(dom) DOM_REGISTER_PROP_HANDLER(&dom_modern_notation_prop_handlers, "publicId", dom_notation_public_id_read, NULL); DOM_REGISTER_PROP_HANDLER(&dom_modern_notation_prop_handlers, "systemId", dom_notation_system_id_read, NULL); zend_hash_merge(&dom_modern_notation_prop_handlers, &dom_modern_node_prop_handlers, NULL, false); + DOM_OVERWRITE_PROP_HANDLER(&dom_modern_notation_prop_handlers, "parentNode", dom_notation_parent_node_read, NULL); + DOM_OVERWRITE_PROP_HANDLER(&dom_modern_notation_prop_handlers, "isConnected", dom_notation_is_connected_read, NULL); zend_hash_add_new_ptr(&classes, dom_modern_notation_class_entry->name, &dom_modern_notation_prop_handlers); dom_entity_class_entry = register_class_DOMEntity(dom_node_class_entry); diff --git a/ext/dom/tests/gh21952.phpt b/ext/dom/tests/gh21952.phpt new file mode 100644 index 000000000000..1869d5dee128 --- /dev/null +++ b/ext/dom/tests/gh21952.phpt @@ -0,0 +1,29 @@ +--TEST-- +GH-21952 (UAF in php_dom_object_get_data when DOMNotation outlives owning DOCTYPE) +--EXTENSIONS-- +dom +--FILE-- +loadXML(<<<'XML' + +]> + +XML); +$notation = $doc->doctype->notations[0]; +var_dump($notation->parentNode); +var_dump($notation->isConnected); +$doc->removeChild($doc->doctype); +var_dump($notation->nodeName); +var_dump($notation->systemId); +var_dump($notation->parentNode); +var_dump($notation->isConnected); +?> +--EXPECT-- +NULL +bool(false) +string(10) "myNotation" +string(8) "test.dtd" +NULL +bool(false) diff --git a/ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt b/ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt index c7ec88f077a8..59d6196f45bc 100644 --- a/ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt +++ b/ext/dom/tests/modern/xml/DTDNamedNodeMap.phpt @@ -153,11 +153,11 @@ object(Dom\Notation)#4 (16) { ["baseURI"]=> string(%d) "%s" ["isConnected"]=> - bool(true) + bool(false) ["ownerDocument"]=> string(22) "(object value omitted)" ["parentNode"]=> - string(22) "(object value omitted)" + NULL ["parentElement"]=> NULL ["childNodes"]=> diff --git a/ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt b/ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt index dd74a2737594..3beab4252f53 100644 --- a/ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt +++ b/ext/dom/tests/modern/xml/XMLDocument_node_notation_wiring.phpt @@ -55,47 +55,47 @@ XML; string(3) "GIF" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL string(3) "GIF" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL bool(true) === JPEG === string(4) "JPEG" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL string(4) "JPEG" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL bool(true) === HTML === string(4) "HTML" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL string(4) "HTML" NULL NULL +bool(false) bool(true) -bool(true) -bool(true) +bool(false) NULL bool(true) From fee51ee450cb55f00e29dec75750ae55901ee9f8 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 27 Nov 2026 13:02:16 +0000 Subject: [PATCH 2/2] feedback --- ext/dom/dom_iterators.c | 4 ---- ext/dom/dom_properties.h | 2 -- ext/dom/notation.c | 13 ------------- ext/dom/php_dom.c | 4 ---- 4 files changed, 23 deletions(-) diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c index be29d3284d05..d298bc02fb74 100644 --- a/ext/dom/dom_iterators.c +++ b/ext/dom/dom_iterators.c @@ -63,10 +63,6 @@ xmlNodePtr create_notation(xmlDtdPtr parent_dtd, const xmlChar *name, const xmlC ret->ExternalID = xmlStrdup(ExternalID); ret->SystemID = xmlStrdup(SystemID); if (parent_dtd != NULL) { - /* Don't store parent_dtd: orphan notation can outlive the DTD when - * removeChild($doctype) frees it. parentNode/isConnected return spec- - * mandated null/false unconditionally; ownerDocument keeps working - * via ret->doc. */ ret->doc = parent_dtd->doc; } return (xmlNodePtr) ret; diff --git a/ext/dom/dom_properties.h b/ext/dom/dom_properties.h index 85ee0260e4b4..918bf0c62f78 100644 --- a/ext/dom/dom_properties.h +++ b/ext/dom/dom_properties.h @@ -147,8 +147,6 @@ zend_result dom_nodelist_length_read(dom_object *obj, zval *retval); /* notation properties */ zend_result dom_notation_public_id_read(dom_object *obj, zval *retval); zend_result dom_notation_system_id_read(dom_object *obj, zval *retval); -zend_result dom_notation_parent_node_read(dom_object *obj, zval *retval); -zend_result dom_notation_is_connected_read(dom_object *obj, zval *retval); /* processinginstruction properties */ zend_result dom_processinginstruction_target_read(dom_object *obj, zval *retval); diff --git a/ext/dom/notation.c b/ext/dom/notation.c index 01cf4be4b924..73d0d6b083a5 100644 --- a/ext/dom/notation.c +++ b/ext/dom/notation.c @@ -69,17 +69,4 @@ zend_result dom_notation_system_id_read(dom_object *obj, zval *retval) /* }}} */ -zend_result dom_notation_parent_node_read(dom_object *obj, zval *retval) -{ - DOM_PROP_NODE(xmlNodePtr, nodep, obj); - ZVAL_NULL(retval); - return SUCCESS; -} - -zend_result dom_notation_is_connected_read(dom_object *obj, zval *retval) -{ - DOM_PROP_NODE(xmlNodePtr, nodep, obj); - ZVAL_FALSE(retval); - return SUCCESS; -} #endif diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c index f9defb2d4bb4..efa47c5c31a8 100644 --- a/ext/dom/php_dom.c +++ b/ext/dom/php_dom.c @@ -1251,8 +1251,6 @@ PHP_MINIT_FUNCTION(dom) DOM_REGISTER_PROP_HANDLER(&dom_notation_prop_handlers, "publicId", dom_notation_public_id_read, NULL); DOM_REGISTER_PROP_HANDLER(&dom_notation_prop_handlers, "systemId", dom_notation_system_id_read, NULL); zend_hash_merge(&dom_notation_prop_handlers, &dom_node_prop_handlers, NULL, false); - DOM_OVERWRITE_PROP_HANDLER(&dom_notation_prop_handlers, "parentNode", dom_notation_parent_node_read, NULL); - DOM_OVERWRITE_PROP_HANDLER(&dom_notation_prop_handlers, "isConnected", dom_notation_is_connected_read, NULL); zend_hash_add_new_ptr(&classes, dom_notation_class_entry->name, &dom_notation_prop_handlers); dom_modern_notation_class_entry = register_class_Dom_Notation(dom_modern_node_class_entry); @@ -1263,8 +1261,6 @@ PHP_MINIT_FUNCTION(dom) DOM_REGISTER_PROP_HANDLER(&dom_modern_notation_prop_handlers, "publicId", dom_notation_public_id_read, NULL); DOM_REGISTER_PROP_HANDLER(&dom_modern_notation_prop_handlers, "systemId", dom_notation_system_id_read, NULL); zend_hash_merge(&dom_modern_notation_prop_handlers, &dom_modern_node_prop_handlers, NULL, false); - DOM_OVERWRITE_PROP_HANDLER(&dom_modern_notation_prop_handlers, "parentNode", dom_notation_parent_node_read, NULL); - DOM_OVERWRITE_PROP_HANDLER(&dom_modern_notation_prop_handlers, "isConnected", dom_notation_is_connected_read, NULL); zend_hash_add_new_ptr(&classes, dom_modern_notation_class_entry->name, &dom_modern_notation_prop_handlers); dom_entity_class_entry = register_class_DOMEntity(dom_node_class_entry);