Skip to content

Commit 6e82ae9

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix phpGH-16595: Another UAF in DOM -> cloneNode Fix phpGH-16593: Assertion failure in DOM->replaceChild
2 parents 947e319 + ed21ebd commit 6e82ae9

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010

1111
- DOM:
1212
. Fixed bug GH-16594 (Assertion failure in DOM -> before). (nielsdos)
13+
. Fixed bug GH-16593 (Assertion failure in DOM->replaceChild). (nielsdos)
14+
. Fixed bug GH-16595 (Another UAF in DOM -> cloneNode). (nielsdos)
1315

1416
- GD:
1517
. Fixed bug GH-16559 (UBSan abort in ext/gd/libgd/gd_interpolation.c:1007).

ext/dom/node.c

+16-7
Original file line numberDiff line numberDiff line change
@@ -895,21 +895,23 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
895895
RETURN_FALSE;
896896
}
897897

898-
if (child->doc == NULL && parentp->doc != NULL) {
899-
dom_set_document_ref_pointers(child, intern->document);
900-
}
901-
902-
php_libxml_invalidate_node_list_cache(intern->document);
903-
898+
xmlNodePtr refp = NULL;
904899
if (ref != NULL) {
905-
xmlNodePtr refp;
906900
dom_object *refpobj;
907901
DOM_GET_OBJ(refp, ref, xmlNodePtr, refpobj);
908902
if (refp->parent != parentp) {
909903
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
910904
RETURN_FALSE;
911905
}
906+
}
907+
908+
if (child->doc == NULL && parentp->doc != NULL) {
909+
dom_set_document_ref_pointers(child, intern->document);
910+
}
911+
912+
php_libxml_invalidate_node_list_cache(intern->document);
912913

914+
if (ref != NULL) {
913915
if (child->parent != NULL) {
914916
xmlUnlinkNode(child);
915917
}
@@ -1196,6 +1198,13 @@ static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
11961198
RETURN_FALSE;
11971199
}
11981200

1201+
/* This is already disallowed by libxml, but we should check it here to avoid
1202+
* breaking assumptions and assertions. */
1203+
if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) {
1204+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1205+
RETURN_FALSE;
1206+
}
1207+
11991208
if (oldchild->parent != nodep) {
12001209
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
12011210
RETURN_FALSE;

ext/dom/tests/gh16593.phpt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-16593 (Assertion failure in DOM->replaceChild)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument;
9+
$root = $doc->appendChild($doc->createElement('root'));
10+
$child = $root->appendChild($doc->createElement('child'));
11+
try {
12+
$root->replaceChild($doc->createAttribute('foo'), $child);
13+
} catch (DOMException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
echo $doc->saveXML();
17+
18+
?>
19+
--EXPECT--
20+
Hierarchy Request Error
21+
<?xml version="1.0"?>
22+
<root><child/></root>

ext/dom/tests/gh16595.phpt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-16595 (Another UAF in DOM -> cloneNode)
3+
--EXTENSIONS--
4+
dom
5+
--CREDITS--
6+
chibinz
7+
--FILE--
8+
<?php
9+
$v0 = new DOMElement ( "jg" );
10+
$v1 = new DOMDocument ( "Zb" );
11+
$v2 = new DOMElement ( "IU" );
12+
$v7 = new DOMElement ( "L" , null , "df" );
13+
$v9 = new DOMDocument ( );
14+
15+
try { $v1 -> insertBefore ( $v0 , $v9 ); } catch (\Throwable) { }
16+
$v0 -> replaceChildren ( $v7 );
17+
$v7 -> before ( $v2 );
18+
$v1 -> insertBefore ( $v0 );
19+
$v2 -> cloneNode ( );
20+
echo $v1->saveXML();
21+
echo $v9->saveXML();
22+
?>
23+
--EXPECT--
24+
<?xml version="Zb"?>
25+
<jg xmlns:default="df"><IU/><default:L xmlns="df"/></jg>
26+
<?xml version="1.0"?>

0 commit comments

Comments
 (0)