[php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Jakub Zelenka | Date: | Sun, 01 Nov 2026 14:13:10 +0000 |
| Subject: | [php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143151@lists.php.net to get a copy of this message | ||
Author: Jakub Zelenka (bukka)
Date: 2026-11-01T15:13:01+01:00
Commit: https://github.com/php/php-src/commit/43c2c77a180a095c616654c26600140f1ed3d20e
Raw diff: https://github.com/php/php-src/commit/43c2c77a180a095c616654c26600140f1ed3d20e.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
Fix UAF in reflected attribute write paths (#25120)
Changed paths:
A ext/dom/tests/reflected_attribute_write_live_child.phpt
M NEWS
M ext/dom/element.c
M ext/dom/php_dom.c
M ext/dom/php_dom.h
M ext/dom/token_list.c
Diff:
diff --git a/NEWS b/NEWS
index 7d47669d454f..a72e55a0d7c0 100644
--- a/NEWS
+++ b/NEWS
@@ -59,6 +59,8 @@ PHP NEWS
NULL deref on first use after a failed open). (Girgias)
- DOM:
+ . Fixed a use-after-free when reflected attribute setters replace an
+ attribute whose child still has a live wrapper. (Jakub Zelenka)
. Fix error paths in dom_xml_serializing_a_document_node(). (ndossche)
- FFI:
diff --git a/ext/dom/element.c b/ext/dom/element.c
index 8dd7f3756050..260f0f747d57 100644
--- a/ext/dom/element.c
+++ b/ext/dom/element.c
@@ -157,7 +157,7 @@ static xmlAttrPtr dom_element_reflected_attribute_write(dom_object *obj, zval *n
/* Typed property, so it is a string already */
ZEND_ASSERT(Z_TYPE_P(newval) == IS_STRING);
php_libxml_invalidate_node_list_cache(obj->document);
- return xmlSetNsProp(nodep, NULL, (const xmlChar *) name, (const xmlChar *) Z_STRVAL_P(newval));
+ return dom_set_attribute_value(nodep, NULL, (const xmlChar *) name, (const xmlChar *)
Z_STRVAL_P(newval));
}
/* {{{ className string
@@ -1062,11 +1062,7 @@ static void dom_set_attribute_ns_modern(dom_object *intern, xmlNodePtr elemp,
ze
if (errorcode == 0) {
php_dom_libxml_ns_mapper *ns_mapper = php_dom_get_ns_mapper(intern);
xmlNsPtr ns = php_dom_libxml_ns_mapper_get_ns_raw_prefix_string(ns_mapper, prefix,
xmlStrlen(prefix), uri);
- xmlNodePtr existing = (xmlNodePtr) xmlHasNsProp(elemp, localname, ns == NULL ? NULL :
ns->href);
- if (existing != NULL && existing->type != XML_ATTRIBUTE_DECL) {
- node_list_unlink(existing->children);
- }
- xmlAttrPtr attr = xmlSetNsProp(elemp, ns, localname, BAD_CAST value);
+ xmlAttrPtr attr = dom_set_attribute_value(elemp, ns, localname, BAD_CAST value);
if (UNEXPECTED(attr == NULL)) {
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
} else {
diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c
index f034976839a7..8d8bc7fb754a 100644
--- a/ext/dom/php_dom.c
+++ b/ext/dom/php_dom.c
@@ -2345,6 +2345,15 @@ void dom_remove_all_children(xmlNodePtr nodep)
}
}
+xmlAttrPtr dom_set_attribute_value(xmlNodePtr element, xmlNsPtr ns, const xmlChar *name, const
xmlChar *value)
+{
+ xmlAttrPtr existing = xmlHasNsProp(element, name, ns == NULL ? NULL : ns->href);
+ if (existing != NULL && existing->type != XML_ATTRIBUTE_DECL) {
+ node_list_unlink(existing->children);
+ }
+ return xmlSetNsProp(element, ns, name, value);
+}
+
void php_dom_get_content_into_zval(const xmlNode *nodep, zval *return_value, bool null_on_failure)
{
ZEND_ASSERT(nodep != NULL);
diff --git a/ext/dom/php_dom.h b/ext/dom/php_dom.h
index fae232e54f72..095b4cf259e5 100644
--- a/ext/dom/php_dom.h
+++ b/ext/dom/php_dom.h
@@ -115,6 +115,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type);
#endif
bool dom_get_strict_error(php_libxml_ref_obj *document);
void node_list_unlink(xmlNodePtr node);
+xmlAttrPtr dom_set_attribute_value(xmlNodePtr element, xmlNsPtr ns, const xmlChar *name, const
xmlChar *value);
int dom_check_qname(char *qname, char **localname, char **prefix, int uri_len, int name_len);
xmlNsPtr dom_get_ns(xmlNodePtr node, char *uri, int *errorcode, char *prefix);
xmlNsPtr dom_get_ns_unchecked(xmlNodePtr nodep, char *uri, char *prefix);
diff --git a/ext/dom/tests/reflected_attribute_write_live_child.phpt
b/ext/dom/tests/reflected_attribute_write_live_child.phpt
new file mode 100644
index 000000000000..b387784a32ca
--- /dev/null
+++ b/ext/dom/tests/reflected_attribute_write_live_child.phpt
@@ -0,0 +1,63 @@
+--TEST--
+Reflected attribute writes keep attribute children with live wrappers
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+function check(string $label, $doc, Closure $write): void {
+ $element = $doc->documentElement;
+ $text = $element->getAttributeNode('class')->firstChild;
+
+ $write($element);
+
+ echo $label, ": detached: ";
+ var_dump($text->parentNode === null);
+ echo $label, ": text: ";
+ var_dump($text->textContent);
+ echo $label, ": new value: ";
+ var_dump($element->getAttribute('class'));
+}
+
+$doc = new DOMDocument();
+$doc->loadXML('<root class="old"/>');
+check('legacy className', $doc, static fn ($element) => $element->className =
'new');
+
+$doc = Dom\XMLDocument::createFromString('<root class="old"/>');
+check('modern className', $doc, static fn ($element) => $element->className =
'new');
+
+$doc = Dom\XMLDocument::createFromString('<root class="old"/>');
+check('classList value', $doc, static fn ($element) =>
$element->classList->value = 'new');
+
+$doc = Dom\XMLDocument::createFromString('<root class="old"/>');
+check('classList update', $doc, static fn ($element) =>
$element->classList->add('new'));
+
+$doc = Dom\XMLDocument::createFromString('<root class="old"/>');
+$element = $doc->documentElement;
+$element->id = 'old';
+$text = $element->getAttributeNode('id')->firstChild;
+$element->id = 'new';
+echo "id: detached: ";
+var_dump($text->parentNode === null);
+echo "id: text: ";
+var_dump($text->textContent);
+echo "id: new value: ";
+var_dump($element->id);
+
+?>
+--EXPECT--
+legacy className: detached: bool(true)
+legacy className: text: string(3) "old"
+legacy className: new value: string(3) "new"
+modern className: detached: bool(true)
+modern className: text: string(3) "old"
+modern className: new value: string(3) "new"
+classList value: detached: bool(true)
+classList value: text: string(3) "old"
+classList value: new value: string(3) "new"
+classList update: detached: bool(true)
+classList update: text: string(3) "old"
+classList update: new value: string(7) "old new"
+id: detached: bool(true)
+id: text: string(3) "old"
+id: new value: string(3) "new"
diff --git a/ext/dom/token_list.c b/ext/dom/token_list.c
index 34e2aa6b3ca3..0cb7f0225d9f 100644
--- a/ext/dom/token_list.c
+++ b/ext/dom/token_list.c
@@ -194,7 +194,7 @@ static void dom_token_list_update(dom_token_list_object *intern)
/* 2. Set an attribute value for the associated element using associated attribute’s local name
and the result of
* running the ordered set serializer for token set. */
char *value = dom_ordered_set_serializer(token_set);
- xmlSetNsProp(dom_token_list_get_element(intern), NULL, BAD_CAST "class", BAD_CAST
value);
+ dom_set_attribute_value(dom_token_list_get_element(intern), NULL, BAD_CAST "class",
BAD_CAST value);
efree(intern->cached_string);
intern->cached_string = value;
}
@@ -434,7 +434,7 @@ zend_result dom_token_list_value_write(dom_object *obj, zval *newval)
return FAILURE;
}
php_libxml_invalidate_node_list_cache(intern->dom.document);
- xmlSetNsProp(dom_token_list_get_element(intern), NULL, BAD_CAST "class", BAD_CAST
Z_STRVAL_P(newval));
+ dom_set_attribute_value(dom_token_list_get_element(intern), NULL, BAD_CAST "class",
BAD_CAST Z_STRVAL_P(newval));
/* Note: we don't update the set here, the set is always lazily updated for performance
reasons. */
return SUCCESS;
}