Skip to content

Commit 5a09e01

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-16535: UAF when using document as a child Fix GH-16533: Segfault when adding attribute to parent that is not an element
2 parents 713c71a + 51b642f commit 5a09e01

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ PHP NEWS
3232
. Fixed bug GH-16336 (Attribute intern document mismanagement). (nielsdos)
3333
. Fixed bug GH-16338 (Null-dereference in ext/dom/node.c). (nielsdos)
3434
. Fixed bug GH-16473 (dom_import_simplexml stub is wrong). (nielsdos)
35+
. Fixed bug GH-16533 (Segfault when adding attribute to parent that is not
36+
an element). (nielsdos)
37+
. Fixed bug GH-16535 (UAF when using document as a child). (nielsdos)
3538

3639
- EXIF:
3740
. Fixed bug GH-16409 (Segfault in exif_thumbnail when not dealing with a

ext/dom/node.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,17 @@ static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNode
969969
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
970970
return false;
971971
}
972+
/* Attributes must be in elements. */
973+
if (child->type == XML_ATTRIBUTE_NODE && parentp->type != XML_ELEMENT_NODE) {
974+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
975+
return false;
976+
}
977+
978+
/* Documents can never be a child. */
979+
if (child->type == XML_DOCUMENT_NODE || child->type == XML_HTML_DOCUMENT_NODE) {
980+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
981+
return false;
982+
}
972983

973984
return true;
974985
}

ext/dom/tests/gh16533.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-16533 (Segfault when adding attribute to parent that is not an element)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument();
9+
try {
10+
$doc->appendChild($doc->createAttribute('foo'));
11+
} catch (DOMException $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
15+
echo $doc->saveXML();
16+
17+
?>
18+
--EXPECT--
19+
Hierarchy Request Error
20+
<?xml version="1.0"?>

ext/dom/tests/gh16535.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-16535 (UAF when using document as a child)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$v2 = new DOMDocument("t");
9+
10+
$v2->loadHTML("t");
11+
$v4 = $v2->createElement('foo');
12+
try {
13+
$v4->appendChild($v2);
14+
} catch (DOMException $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
$v2->loadHTML("oU");
18+
echo $v2->saveXML();
19+
20+
?>
21+
--EXPECT--
22+
Hierarchy Request Error
23+
<?xml version="1.0" standalone="yes"?>
24+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
25+
<html><body><p>oU</p></body></html>

0 commit comments

Comments
 (0)