Skip to content

Commit d3fada3

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix phpGH-16777: Calling the constructor again on a DOM object after it is in a document causes UAF Fix phpGH-16808: Segmentation fault in RecursiveIteratorIterator->current() with a xml element input
2 parents 1b803bc + 18b18f0 commit d3fada3

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

ext/dom/node.c

+3
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
906906
}
907907

908908
if (child->doc == NULL && parentp->doc != NULL) {
909+
xmlSetTreeDoc(child, parentp->doc);
909910
dom_set_document_ref_pointers(child, intern->document);
910911
}
911912

@@ -1212,6 +1213,7 @@ static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
12121213
}
12131214

12141215
if (newchild->doc == NULL && nodep->doc != NULL) {
1216+
xmlSetTreeDoc(newchild, nodep->doc);
12151217
dom_set_document_ref_pointers(newchild, intern->document);
12161218
}
12171219

@@ -1320,6 +1322,7 @@ static void dom_node_append_child_legacy(zval *return_value, dom_object *intern,
13201322
}
13211323

13221324
if (child->doc == NULL && nodep->doc != NULL) {
1325+
xmlSetTreeDoc(child, nodep->doc);
13231326
dom_set_document_ref_pointers(child, intern->document);
13241327
}
13251328

ext/dom/tests/gh16777_1.phpt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$text = new DOMText('my value');
8+
$doc = new DOMDocument();
9+
$doc->appendChild($text);
10+
$text->__construct('my new value');
11+
$doc->appendChild($text);
12+
echo $doc->saveXML();
13+
$dom2 = new DOMDocument();
14+
try {
15+
$dom2->appendChild($text);
16+
} catch (DOMException $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
?>
20+
--EXPECT--
21+
<?xml version="1.0"?>
22+
my value
23+
my new value
24+
Wrong Document Error

ext/dom/tests/gh16777_2.phpt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-16777 (Calling the constructor again on a DOM object after it is in a document causes UAF)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$el = new DOMElement('name');
8+
$el->append($child = new DOMElement('child'));
9+
$doc = new DOMDocument();
10+
$doc->appendChild($el);
11+
$el->__construct('newname');
12+
$doc->appendChild($el);
13+
echo $doc->saveXML();
14+
$dom2 = new DOMDocument();
15+
try {
16+
$dom2->appendChild($el);
17+
} catch (DOMException $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
var_dump($child->ownerDocument === $doc);
21+
?>
22+
--EXPECT--
23+
<?xml version="1.0"?>
24+
<name><child/></name>
25+
<newname/>
26+
Wrong Document Error
27+
bool(true)

ext/simplexml/simplexml.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,11 @@ static zval *php_sxe_iterator_current_data(zend_object_iterator *iter) /* {{{ */
24792479
{
24802480
php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
24812481

2482-
return &iterator->sxe->iter.data;
2482+
zval *data = &iterator->sxe->iter.data;
2483+
if (Z_ISUNDEF_P(data)) {
2484+
return NULL;
2485+
}
2486+
return data;
24832487
}
24842488
/* }}} */
24852489

ext/simplexml/tests/gh16808.phpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-16808 (Segmentation fault in RecursiveIteratorIterator->current() with a xml element input)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
$sxe = new SimpleXMLElement("<root />");
8+
$test = new RecursiveIteratorIterator($sxe);
9+
var_dump($test->current());
10+
?>
11+
--EXPECT--
12+
NULL

0 commit comments

Comments
 (0)