From 9746e29c1e0216674d1e068320f250b89cb41415 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Thu, 19 Nov 2026 08:36:38 -0500 Subject: [PATCH] ext/dom: Fix crash in named node map iterator key() without a current item php_dom_iterator_current_key() read the object out of iterator->curobj for attribute and DTD named node maps without checking that the iterator has a current item. On an empty attribute map the object pointer is NULL and InternalIterator::key() crashes; once the iterator has moved past the last item it reads the wrapper move_forward() has already released. Return null in that case, as valid() and current() already treat an undefined curobj as the end. An entity or notation map of a DTD without an internal subset has no hash table and was keyed like a node list, returning int(0); select the name-keyed maps by node type instead, as move_forward() does. Integer-keyed node lists are unaffected. --- NEWS | 2 + ext/dom/dom_iterators.c | 4 +- .../DOMNamedNodeMap_iterator_key_empty.phpt | 28 ++++++++ ...OMNamedNodeMap_iterator_key_empty_dtd.phpt | 34 ++++++++++ ...OMNamedNodeMap_iterator_key_exhausted.phpt | 66 +++++++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt create mode 100644 ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt create mode 100644 ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt diff --git a/NEWS b/NEWS index 84b2777e2587..c7c23c6cf83e 100644 --- a/NEWS +++ b/NEWS @@ -51,6 +51,8 @@ PHP NEWS . Fixed crash when registerNodeClass() is given a class with a constant expression that cannot be evaluated; registerNodeClass() now throws the evaluation Error instead. (Ilia Alshanetsky) + . Fixed a crash and a use-after-free in key() of an attribute or DTD named + node map iterator that has no current item. (Ilia Alshanetsky) - Filter: . Fixed stack overflow in filter_var() and filter_var_array() with deeply diff --git a/ext/dom/dom_iterators.c b/ext/dom/dom_iterators.c index 5b9051f49b1b..d2a9bfb54862 100644 --- a/ext/dom/dom_iterators.c +++ b/ext/dom/dom_iterators.c @@ -131,8 +131,10 @@ static void php_dom_iterator_current_key(zend_object_iterator *iter, zval *key) /* Only dtd named node maps, i.e. the ones based on a libxml hash table or attribute collections, * are keyed by the name because in that case the name is unique. */ - if (!objmap->ht && objmap->nodetype != XML_ATTRIBUTE_NODE) { + if (objmap->nodetype != XML_ATTRIBUTE_NODE && objmap->nodetype != XML_ENTITY_NODE && objmap->nodetype != XML_NOTATION_NODE) { ZVAL_LONG(key, iterator->index); + } else if (Z_TYPE(iterator->curobj) != IS_OBJECT) { + ZVAL_NULL(key); } else { dom_object *intern = Z_DOMOBJ_P(&iterator->curobj); diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt new file mode 100644 index 000000000000..b3a333b8da73 --- /dev/null +++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty.phpt @@ -0,0 +1,28 @@ +--TEST-- +DOMNamedNodeMap iterator key() on an empty attribute map +--EXTENSIONS-- +dom +--FILE-- +loadXML(''); +$it = $doc->documentElement->attributes->getIterator(); +echo "DOMNamedNodeMap valid: "; +var_dump($it->valid()); +echo "DOMNamedNodeMap key: "; +var_dump($it->key()); +echo "DOMNamedNodeMap current: "; +var_dump($it->current()); + +$doc = Dom\XMLDocument::createFromString(''); +$it = $doc->documentElement->attributes->getIterator(); +echo "Dom\NamedNodeMap key: "; +var_dump($it->key()); + +?> +--EXPECT-- +DOMNamedNodeMap valid: bool(false) +DOMNamedNodeMap key: NULL +DOMNamedNodeMap current: NULL +Dom\NamedNodeMap key: NULL diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt new file mode 100644 index 000000000000..fbd853ee65b1 --- /dev/null +++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_empty_dtd.phpt @@ -0,0 +1,34 @@ +--TEST-- +DOMNamedNodeMap iterator key() on an empty DTD named node map +--EXTENSIONS-- +dom +--FILE-- +'; + +$doc = new DOMDocument(); +$doc->loadXML($xml); +echo "DOMNamedNodeMap entities valid: "; +var_dump($doc->doctype->entities->getIterator()->valid()); +echo "DOMNamedNodeMap entities key: "; +var_dump($doc->doctype->entities->getIterator()->key()); +echo "DOMNamedNodeMap notations key: "; +var_dump($doc->doctype->notations->getIterator()->key()); + +$doc = Dom\XMLDocument::createFromString($xml); +echo "Dom\DtdNamedNodeMap entities valid: "; +var_dump($doc->doctype->entities->getIterator()->valid()); +echo "Dom\DtdNamedNodeMap entities key: "; +var_dump($doc->doctype->entities->getIterator()->key()); +echo "Dom\DtdNamedNodeMap notations key: "; +var_dump($doc->doctype->notations->getIterator()->key()); + +?> +--EXPECT-- +DOMNamedNodeMap entities valid: bool(false) +DOMNamedNodeMap entities key: NULL +DOMNamedNodeMap notations key: NULL +Dom\DtdNamedNodeMap entities valid: bool(false) +Dom\DtdNamedNodeMap entities key: NULL +Dom\DtdNamedNodeMap notations key: NULL diff --git a/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt b/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt new file mode 100644 index 000000000000..bb256a3156f8 --- /dev/null +++ b/ext/dom/tests/DOMNamedNodeMap_iterator_key_exhausted.phpt @@ -0,0 +1,66 @@ +--TEST-- +DOMNamedNodeMap iterator key() after the last item +--EXTENSIONS-- +dom +--FILE-- +loadXML(']>'); +$it = $doc->documentElement->attributes->getIterator(); +echo "DOMNamedNodeMap attributes first key: "; +var_dump($it->key()); +$it->next(); +echo "DOMNamedNodeMap attributes key after end: "; +var_dump($it->key()); + +$it = $doc->doctype->entities->getIterator(); +echo "DOMNamedNodeMap entities first key: "; +var_dump($it->key()); +$it->next(); +echo "DOMNamedNodeMap entities key after end: "; +var_dump($it->key()); + +$it = $doc->doctype->notations->getIterator(); +echo "DOMNamedNodeMap notations first key: "; +var_dump($it->key()); +$it->next(); +echo "DOMNamedNodeMap notations key after end: "; +var_dump($it->key()); + +$doc = Dom\XMLDocument::createFromString(']>'); +$it = $doc->documentElement->attributes->getIterator(); +echo "Dom\NamedNodeMap first key: "; +var_dump($it->key()); +$it->next(); +echo "Dom\NamedNodeMap key after end: "; +var_dump($it->key()); + +$it = $doc->doctype->entities->getIterator(); +echo "Dom\DtdNamedNodeMap entities first key: "; +var_dump($it->key()); +$it->next(); +echo "Dom\DtdNamedNodeMap entities key after end: "; +var_dump($it->key()); + +$it = $doc->doctype->notations->getIterator(); +echo "Dom\DtdNamedNodeMap notations first key: "; +var_dump($it->key()); +$it->next(); +echo "Dom\DtdNamedNodeMap notations key after end: "; +var_dump($it->key()); + +?> +--EXPECT-- +DOMNamedNodeMap attributes first key: string(1) "a" +DOMNamedNodeMap attributes key after end: NULL +DOMNamedNodeMap entities first key: string(1) "e" +DOMNamedNodeMap entities key after end: NULL +DOMNamedNodeMap notations first key: string(1) "n" +DOMNamedNodeMap notations key after end: NULL +Dom\NamedNodeMap first key: string(1) "a" +Dom\NamedNodeMap key after end: NULL +Dom\DtdNamedNodeMap entities first key: string(1) "e" +Dom\DtdNamedNodeMap entities key after end: NULL +Dom\DtdNamedNodeMap notations first key: string(1) "n" +Dom\DtdNamedNodeMap notations key after end: NULL