Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ PHP NEWS
. Fixed leak on double DatePeriod::__construct() call. (ilutov)

- DOM:
. Fixed bug GH-23116 (Stack overflow when normalizing a deeply nested
DOMDocument). (Lazizbek Ergashev)
. Fixed bug GH-23117 (Stack overflow when normalizing a deeply nested
Dom\XMLDocument). (Lazizbek Ergashev)
. Fixed bug GH-22825 (DOMElement::setAttribute() fails silently when the DTD
declares a default value for the attribute). (iliaal)
. Fixed bug GH-22447 (UAF at dom_objects_free_storage when setting an
Expand Down
21 changes: 21 additions & 0 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1966,9 +1966,26 @@ static void dom_merge_adjacent_exclusive_text_nodes(xmlNodePtr node)
}
}

static zend_always_inline bool dom_normalize_check_stack_limit(void)
{
#ifdef ZEND_CHECK_STACK_LIMIT
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
if (!EG(exception)) {
zend_throw_error(NULL, "Maximum call stack size reached. Infinite recursion?");
}
return true;
}
#endif
return false;
}

/* {{{ void php_dom_normalize_legacy(xmlNodePtr nodep) */
void php_dom_normalize_legacy(xmlNodePtr nodep)
{
if (UNEXPECTED(dom_normalize_check_stack_limit())) {
return;
}

xmlNodePtr child = nodep->children;
while(child != NULL) {
switch (child->type) {
Expand Down Expand Up @@ -2001,6 +2018,10 @@ void php_dom_normalize_legacy(xmlNodePtr nodep)
/* https://dom.spec.whatwg.org/#dom-node-normalize */
void php_dom_normalize_modern(xmlNodePtr this)
{
if (UNEXPECTED(dom_normalize_check_stack_limit())) {
return;
}

/* for each descendant exclusive Text node node of this: */
xmlNodePtr node = this->children;
while (node != NULL) {
Expand Down
50 changes: 50 additions & 0 deletions ext/dom/tests/gh23116.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
GH-23116 (Stack overflow when normalizing a deeply nested DOMDocument)
--EXTENSIONS--
dom
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting due to more stack space usage');
}
?>
--INI--
zend.max_allowed_stack_size=256K
--FILE--
<?php
// Build bottom-up so the insertion cycle-check stays O(1); top-down is O(n^2).
$doc = new DOMDocument();
$root = $doc->createElement('root');
for ($s = 0; $s < 2; $s++) {
$node = $doc->createElement('a');
for ($i = 0; $i < 25000; $i++) {
$parent = $doc->createElement('a');
$parent->appendChild($node);
$node = $parent;
}
$root->appendChild($node);
}
$doc->appendChild($root);

try {
$doc->normalize();
} catch (\Error $e) {
echo "normalize: ", $e::class, ": ", $e->getMessage(), "\n";
var_dump($e->getPrevious());
}

try {
$doc->normalizeDocument();
} catch (\Error $e) {
echo "normalizeDocument: ", $e::class, ": ", $e->getMessage(), "\n";
var_dump($e->getPrevious());
}
?>
--EXPECT--
normalize: Error: Maximum call stack size reached. Infinite recursion?
NULL
normalizeDocument: Error: Maximum call stack size reached. Infinite recursion?
NULL
41 changes: 41 additions & 0 deletions ext/dom/tests/modern/spec/gh23117.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
GH-23117 (Stack overflow when normalizing a deeply nested Dom\XMLDocument)
--EXTENSIONS--
dom
--SKIPIF--
<?php
if (ini_get('zend.max_allowed_stack_size') === false) {
die('skip No stack limit support');
}
if (getenv('SKIP_ASAN')) {
die('skip ASAN needs different stack limit setting due to more stack space usage');
}
?>
--INI--
zend.max_allowed_stack_size=256K
--FILE--
<?php
// Build bottom-up so the insertion cycle-check stays O(1); top-down is O(n^2).
$doc = Dom\XMLDocument::createEmpty();
$root = $doc->createElement('root');
for ($s = 0; $s < 2; $s++) {
$node = $doc->createElement('a');
for ($i = 0; $i < 25000; $i++) {
$parent = $doc->createElement('a');
$parent->appendChild($node);
$node = $parent;
}
$root->appendChild($node);
}
$doc->appendChild($root);

try {
$doc->normalize();
} catch (\Error $e) {
echo "normalize: ", $e::class, ": ", $e->getMessage(), "\n";
var_dump($e->getPrevious());
}
?>
--EXPECT--
normalize: Error: Maximum call stack size reached. Infinite recursion?
NULL
Loading