Skip to content

Commit f2db1ad

Browse files
committed
ext/dom: Fix use-after-free when an XPath callback modifies the document
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. XSLTProcessor refuses to transform a read-only document in place, because libxslt frees whitespace text nodes of its source for xsl:strip-space.
1 parent 8ea2d41 commit f2db1ad

35 files changed

Lines changed: 964 additions & 15 deletions

‎NEWS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ PHP NEWS
2525
passing an array of CURLFile or CURLStringFile objects to
2626
CURLOPT_POSTFIELDS. (Sjoerd Langkemper)
2727

28+
- DOM:
29+
. Fixed use-after-free when a php:function() callback of DOMXPath or
30+
Dom\XPath modifies nodes that are still being evaluated; such
31+
modifications now throw a DOMException or Error. (Ilia Alshanetsky)
32+
2833
- FPM:
2934
. Use recommended TYPE, HELP order for OpenMetrics metadata.
3035
(Marcel Hernandez)

‎UPGRADING‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ PHP 8.7 UPGRADE NOTES
3232
curl_setopt() return false, instead of being silently interpreted as a
3333
combination of redirect types.
3434

35+
- DOM:
36+
. While DOMXPath or Dom\XPath evaluates an expression with PHP functions
37+
registered, the nodes it can reach are read-only: the tree of the
38+
evaluated document, the tree of every node a php:function() callback
39+
returned, and the detached subtree of the context node or of a node
40+
passed to a callback. Modifying such a node throws a DOMException with
41+
code DOM_NO_MODIFICATION_ALLOWED_ERR (a warning when strictErrorChecking
42+
is disabled), and modifying it through SimpleXML throws an Error.
43+
Creating and modifying new nodes and setting document properties such as
44+
formatOutput or documentURI are still allowed. Transforming the evaluated
45+
document with XSLTProcessor throws the same exception, unless the
46+
processor copies the document (cloneDocument set to true).
47+
3548
- Intl:
3649
. UConverter::setSubstChars() now rejects substitution strings longer than
3750
127 bytes. Previously, the length could be truncated and only a prefix of

‎ext/dom/attr.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ PHP_METHOD(DOMAttr, __construct)
4545
RETURN_THROWS();
4646
}
4747

48+
if (dom_throw_if_node_read_only(Z_DOMOBJ_P(ZEND_THIS)->document, dom_object_get_node(Z_DOMOBJ_P(ZEND_THIS)))) {
49+
return;
50+
}
51+
4852
intern = Z_DOMOBJ_P(ZEND_THIS);
4953

5054
name_valid = xmlValidateName(BAD_CAST name, 0);

‎ext/dom/cdatasection.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ PHP_METHOD(DOMCdataSection, __construct)
4040
RETURN_THROWS();
4141
}
4242

43+
if (dom_throw_if_node_read_only(Z_DOMOBJ_P(ZEND_THIS)->document, dom_object_get_node(Z_DOMOBJ_P(ZEND_THIS)))) {
44+
return;
45+
}
46+
4347
nodep = xmlNewCDataBlock(NULL, BAD_CAST value, value_len);
4448

4549
if (!nodep) {

‎ext/dom/characterdata.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ static void dom_character_data_append_data(INTERNAL_FUNCTION_PARAMETERS)
164164
}
165165

166166
DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
167+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
168+
return;
169+
}
167170
xmlTextConcat(nodep, BAD_CAST arg, arg_len);
168171
}
169172

@@ -202,6 +205,10 @@ static void dom_character_data_insert_data(INTERNAL_FUNCTION_PARAMETERS, bool re
202205

203206
DOM_GET_OBJ(node, id, xmlNodePtr, intern);
204207

208+
if (dom_throw_if_node_read_only(intern->document, node)) {
209+
RETURN_FALSE;
210+
}
211+
205212
const xmlChar *cur = php_dom_get_content_or_empty(node);
206213

207214
length = xmlUTF8Strlen(cur);
@@ -267,6 +274,10 @@ static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool re
267274

268275
DOM_GET_OBJ(node, id, xmlNodePtr, intern);
269276

277+
if (dom_throw_if_node_read_only(intern->document, node)) {
278+
RETURN_FALSE;
279+
}
280+
270281
const xmlChar *cur = php_dom_get_content_or_empty(node);
271282

272283
length = xmlUTF8Strlen(cur);
@@ -337,6 +348,10 @@ static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool r
337348

338349
DOM_GET_OBJ(node, id, xmlNodePtr, intern);
339350

351+
if (dom_throw_if_node_read_only(intern->document, node)) {
352+
RETURN_FALSE;
353+
}
354+
340355
const xmlChar *cur = php_dom_get_content_or_empty(node);
341356

342357
length = xmlUTF8Strlen(cur);

‎ext/dom/comment.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ PHP_METHOD(DOMComment, __construct)
4040
RETURN_THROWS();
4141
}
4242

43+
if (dom_throw_if_node_read_only(Z_DOMOBJ_P(ZEND_THIS)->document, dom_object_get_node(Z_DOMOBJ_P(ZEND_THIS)))) {
44+
return;
45+
}
46+
4347
nodep = xmlNewComment(BAD_CAST value);
4448

4549
if (!nodep) {

‎ext/dom/document.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,10 @@ static void dom_document_adopt_node(INTERNAL_FUNCTION_PARAMETERS, bool modern)
12131213
RETURN_FALSE;
12141214
}
12151215

1216+
if (dom_throw_if_node_read_only(dom_object_nodep->document, nodep)) {
1217+
RETURN_FALSE;
1218+
}
1219+
12161220
xmlDocPtr new_document;
12171221
dom_object *dom_object_new_document;
12181222
zval *new_document_zval = ZEND_THIS;
@@ -1254,6 +1258,10 @@ PHP_METHOD(DOMDocument, normalizeDocument)
12541258

12551259
DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
12561260

1261+
if (dom_throw_if_node_read_only(intern->document, (xmlNodePtr) docp)) {
1262+
return;
1263+
}
1264+
12571265
php_dom_normalize_legacy((xmlNodePtr) docp);
12581266
}
12591267
/* }}} end dom_document_normalize_document */
@@ -1801,6 +1809,10 @@ PHP_METHOD(DOMDocument, xinclude)
18011809

18021810
DOM_GET_OBJ(docp, ZEND_THIS, xmlDocPtr, intern);
18031811

1812+
if (dom_throw_if_node_read_only(intern->document, (xmlNodePtr) docp)) {
1813+
RETURN_FALSE;
1814+
}
1815+
18041816
int err = dom_perform_xinclude(docp, intern, flags);
18051817

18061818
if (err) {
@@ -1827,6 +1839,10 @@ PHP_METHOD(Dom_XMLDocument, xinclude)
18271839

18281840
DOM_GET_OBJ(docp, ZEND_THIS, xmlDocPtr, intern);
18291841

1842+
if (dom_throw_if_node_read_only(intern->document, (xmlNodePtr) docp)) {
1843+
RETURN_THROWS();
1844+
}
1845+
18301846
int err = dom_perform_xinclude(docp, intern, flags);
18311847

18321848
if (err < 0) {

‎ext/dom/documentfragment.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ PHP_METHOD(DOMDocumentFragment, __construct)
3636

3737
ZEND_PARSE_PARAMETERS_NONE();
3838

39+
if (dom_throw_if_node_read_only(Z_DOMOBJ_P(ZEND_THIS)->document, dom_object_get_node(Z_DOMOBJ_P(ZEND_THIS)))) {
40+
return;
41+
}
42+
3943
nodep = xmlNewDocFragment(NULL);
4044

4145
if (!nodep) {
@@ -74,6 +78,10 @@ PHP_METHOD(DOMDocumentFragment, appendXML) {
7478
RETURN_FALSE;
7579
}
7680

81+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
82+
RETURN_FALSE;
83+
}
84+
7785
if (data) {
7886
PHP_LIBXML_SANITIZE_GLOBALS(parse);
7987
err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, BAD_CAST data, &lst);

‎ext/dom/domimplementation.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ PHP_METHOD(DOMImplementation, createDocument)
174174
php_dom_throw_error(WRONG_DOCUMENT_ERR, true);
175175
RETURN_THROWS();
176176
}
177+
if (dom_throw_if_node_read_only(doctobj->document, (xmlNodePtr) doctype)) {
178+
RETURN_THROWS();
179+
}
177180
} else {
178181
doctobj = NULL;
179182
}
@@ -261,6 +264,9 @@ PHP_METHOD(Dom_Implementation, createDocument)
261264

262265
if (dtd != NULL) {
263266
DOM_GET_OBJ(doctype, dtd, xmlDtdPtr, doctobj);
267+
if (dom_throw_if_node_read_only(doctobj->document, (xmlNodePtr) doctype)) {
268+
RETURN_THROWS();
269+
}
264270
}
265271

266272
xmlDocPtr document = NULL;

‎ext/dom/element.c‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ PHP_METHOD(DOMElement, __construct)
5050
RETURN_THROWS();
5151
}
5252

53+
if (dom_throw_if_node_read_only(Z_DOMOBJ_P(ZEND_THIS)->document, dom_object_get_node(Z_DOMOBJ_P(ZEND_THIS)))) {
54+
return;
55+
}
56+
5357
name_valid = xmlValidateName(BAD_CAST name, 0);
5458
if (name_valid != 0) {
5559
php_dom_throw_error(INVALID_CHARACTER_ERR, true);
@@ -429,6 +433,10 @@ PHP_METHOD(DOMElement, setAttribute)
429433

430434
DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
431435

436+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
437+
RETURN_FALSE;
438+
}
439+
432440
if (php_dom_follow_spec_intern(intern)) {
433441
xmlChar *name_processed = BAD_CAST name;
434442
if (php_dom_ns_is_html_and_document_is_html(nodep)) {
@@ -619,6 +627,10 @@ PHP_METHOD(DOMElement, removeAttribute)
619627

620628
DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
621629

630+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
631+
RETURN_FALSE;
632+
}
633+
622634
attrp = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
623635
if (attrp == NULL) {
624636
RETURN_FALSE;
@@ -640,6 +652,10 @@ PHP_METHOD(Dom_Element, removeAttribute)
640652

641653
DOM_GET_OBJ(nodep, ZEND_THIS, xmlNodePtr, intern);
642654

655+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
656+
RETURN_THROWS();
657+
}
658+
643659
attrp = dom_get_attribute_or_nsdecl(intern, nodep, BAD_CAST name, name_len);
644660
if (attrp != NULL) {
645661
dom_remove_attribute(nodep, attrp, intern->document);
@@ -699,6 +715,10 @@ static void dom_element_set_attribute_node_common(INTERNAL_FUNCTION_PARAMETERS,
699715
DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
700716
DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
701717

718+
if (dom_throw_if_node_read_only(intern->document, nodep) || dom_throw_if_node_read_only(attrobj->document, (xmlNodePtr) attrp)) {
719+
RETURN_FALSE;
720+
}
721+
702722
/* ZPP Guarantees that a DOMAttr class is given, as it is converted to a xmlAttr
703723
* to pass to libxml (see http://www.xmlsoft.org/html/libxml-tree.html#xmlAttr)
704724
* if it is not of type XML_ATTRIBUTE_NODE it indicates a bug somewhere */
@@ -795,6 +815,10 @@ static void dom_element_remove_attribute_node(INTERNAL_FUNCTION_PARAMETERS, zend
795815

796816
ZEND_ASSERT(attrp->type == XML_ATTRIBUTE_NODE);
797817

818+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
819+
RETURN_FALSE;
820+
}
821+
798822
if (attrp->parent != nodep) {
799823
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
800824
RETURN_FALSE;
@@ -1095,6 +1119,10 @@ PHP_METHOD(DOMElement, setAttributeNS)
10951119

10961120
DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
10971121

1122+
if (dom_throw_if_node_read_only(intern->document, elemp)) {
1123+
return;
1124+
}
1125+
10981126
if (php_dom_follow_spec_intern(intern)) {
10991127
dom_set_attribute_ns_modern(intern, elemp, uri, name, value);
11001128
} else {
@@ -1185,6 +1213,10 @@ PHP_METHOD(DOMElement, removeAttributeNS)
11851213

11861214
DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
11871215

1216+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
1217+
return;
1218+
}
1219+
11881220
bool follow_spec = php_dom_follow_spec_intern(intern);
11891221
if (follow_spec && uri_len == 0) {
11901222
uri = NULL;
@@ -1408,6 +1440,10 @@ PHP_METHOD(DOMElement, setIdAttribute)
14081440

14091441
DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
14101442

1443+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
1444+
return;
1445+
}
1446+
14111447
attrp = xmlHasNsProp(nodep, BAD_CAST name, NULL);
14121448
if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
14131449
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
@@ -1437,6 +1473,10 @@ PHP_METHOD(DOMElement, setIdAttributeNS)
14371473

14381474
DOM_GET_OBJ(elemp, id, xmlNodePtr, intern);
14391475

1476+
if (dom_throw_if_node_read_only(intern->document, elemp)) {
1477+
return;
1478+
}
1479+
14401480
attrp = xmlHasNsProp(elemp, BAD_CAST name, BAD_CAST uri);
14411481
if (attrp == NULL || attrp->type == XML_ATTRIBUTE_DECL) {
14421482
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
@@ -1465,6 +1505,10 @@ static void dom_element_set_id_attribute_node(INTERNAL_FUNCTION_PARAMETERS, zend
14651505
DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
14661506
DOM_GET_OBJ(attrp, node, xmlAttrPtr, attrobj);
14671507

1508+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
1509+
return;
1510+
}
1511+
14681512
if (attrp->parent != nodep) {
14691513
php_dom_throw_error(NOT_FOUND_ERR, dom_get_strict_error(intern->document));
14701514
} else {
@@ -1665,6 +1709,10 @@ static void dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS, ze
16651709
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
16661710
DOM_GET_OBJ(otherp, element_zval, xmlNodePtr, other_intern);
16671711

1712+
if (dom_throw_if_node_read_only(this_intern->document, thisp) || dom_throw_if_node_read_only(other_intern->document, otherp)) {
1713+
RETURN_NULL();
1714+
}
1715+
16681716
xmlNodePtr result = dom_insert_adjacent(where, thisp, this_intern, otherp);
16691717
if (result == NULL) {
16701718
RETURN_NULL();
@@ -1741,6 +1789,10 @@ static void dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAMETERS, zend_
17411789

17421790
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
17431791

1792+
if (dom_throw_if_node_read_only(this_intern->document, thisp)) {
1793+
return;
1794+
}
1795+
17441796
if (!ZEND_IN_RANGE_OF(ZSTR_LEN(data), int)) {
17451797
zend_argument_value_error(2, "is too long");
17461798
RETURN_THROWS();
@@ -1809,6 +1861,10 @@ PHP_METHOD(Dom_Element, insertAdjacentHTML)
18091861

18101862
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
18111863

1864+
if (dom_throw_if_node_read_only(this_intern->document, thisp)) {
1865+
RETURN_THROWS();
1866+
}
1867+
18121868
/* 1. We don't do injection sinks. */
18131869

18141870
/* 2. Let context be NULL */
@@ -1899,6 +1955,10 @@ PHP_METHOD(DOMElement, toggleAttribute)
18991955

19001956
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, intern);
19011957

1958+
if (dom_throw_if_node_read_only(intern->document, thisp)) {
1959+
RETURN_FALSE;
1960+
}
1961+
19021962
/* Step 1 */
19031963
if (xmlValidateName(BAD_CAST qname, 0) != 0) {
19041964
php_dom_throw_error(INVALID_CHARACTER_ERR, true);
@@ -2177,6 +2237,10 @@ PHP_METHOD(Dom_Element, rename)
21772237
xmlNodePtr nodep;
21782238
DOM_GET_THIS_OBJ(nodep, id, xmlNodePtr, intern);
21792239

2240+
if (dom_throw_if_node_read_only(intern->document, nodep)) {
2241+
RETURN_THROWS();
2242+
}
2243+
21802244
xmlChar *localname = NULL, *prefix = NULL;
21812245
int errorcode = dom_validate_and_extract(namespace_uri, qualified_name, &localname, &prefix);
21822246
if (UNEXPECTED(errorcode != 0)) {

0 commit comments

Comments
 (0)