Skip to content

Commit bd724bd

Browse files
committed
Fix GH-15169: stack overflow when var serialization in ext/standard/var
Adding a stack check here as I consider serialization to be a more sensitive place where erroring out with an exception seems appropriate. Closes GH-16159.
1 parent 220c882 commit bd724bd

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ PHP NEWS
5656

5757
- Standard:
5858
. Fixed bug GH-16053 (Assertion failure in Zend/zend_hash.c). (Arnaud)
59+
. Fixed bug GH-15169 (stack overflow when var serialization in
60+
ext/standard/var). (nielsdos)
5961

6062
- Streams:
6163
. Fixed bugs GH-15908 and GH-15026 (leak / assertion failure in streams.c).

Zend/tests/stack_limit/stack_limit_001.phpt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ class Test2 {
2727
}
2828
}
2929

30-
class Test3 {
31-
public function __sleep()
32-
{
33-
serialize($this);
34-
}
35-
}
36-
3730
function replace() {
3831
return preg_replace_callback('#.#', function () {
3932
return replace();
@@ -52,12 +45,6 @@ try {
5245
echo $e->getMessage(), "\n";
5346
}
5447

55-
try {
56-
serialize(new Test3);
57-
} catch (Error $e) {
58-
echo $e->getMessage(), "\n";
59-
}
60-
6148
try {
6249
replace();
6350
} catch (Error $e) {
@@ -79,4 +66,3 @@ array(4) {
7966
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
8067
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
8168
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
82-
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?

Zend/tests/stack_limit/stack_limit_002.phpt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ class Test2 {
2626
}
2727
}
2828

29-
class Test3 {
30-
public function __sleep()
31-
{
32-
serialize($this);
33-
}
34-
}
35-
3629
function replace() {
3730
return preg_replace_callback('#.#', function () {
3831
return replace();
@@ -52,12 +45,6 @@ $fiber = new Fiber(function (): void {
5245
echo $e->getMessage(), "\n";
5346
}
5447

55-
try {
56-
serialize(new Test3);
57-
} catch (Error $e) {
58-
echo $e->getMessage(), "\n";
59-
}
60-
6148
try {
6249
replace();
6350
} catch (Error $e) {
@@ -82,4 +69,3 @@ array(4) {
8269
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
8370
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
8471
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
85-
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-15169 (stack overflow when var serialization in ext/standard/var)
3+
--SKIPIF--
4+
<?php
5+
if (ini_get('zend.max_allowed_stack_size') === false) {
6+
die('skip No stack limit support');
7+
}
8+
if (getenv('SKIP_ASAN')) {
9+
die('skip ASAN needs different stack limit setting due to more stack space usage');
10+
}
11+
?>
12+
--INI--
13+
zend.max_allowed_stack_size=512K
14+
--FILE--
15+
<?php
16+
class Node
17+
{
18+
public $next;
19+
}
20+
$firstNode = new Node();
21+
$node = $firstNode;
22+
for ($i = 0; $i < 30000; $i++) {
23+
$newNode = new Node();
24+
$node->next = $newNode;
25+
$node = $newNode;
26+
}
27+
28+
try {
29+
serialize($firstNode);
30+
} catch (Error $e) {
31+
echo $e->getMessage(), "\n";
32+
}
33+
?>
34+
--EXPECT--
35+
Maximum call stack size reached. Infinite recursion?

ext/standard/var.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,15 @@ static void php_var_serialize_class(smart_str *buf, zval *struc, HashTable *ht,
986986
}
987987
/* }}} */
988988

989+
static zend_always_inline bool php_serialize_check_stack_limit(void)
990+
{
991+
#ifdef ZEND_CHECK_STACK_LIMIT
992+
return zend_call_stack_overflowed(EG(stack_limit));
993+
#else
994+
return false;
995+
#endif
996+
}
997+
989998
static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_data_t var_hash, bool in_rcn_array, bool is_root) /* {{{ */
990999
{
9911000
zend_long var_already;
@@ -995,6 +1004,11 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
9951004
return;
9961005
}
9971006

1007+
if (UNEXPECTED(php_serialize_check_stack_limit())) {
1008+
zend_throw_error(NULL, "Maximum call stack size reached. Infinite recursion?");
1009+
return;
1010+
}
1011+
9981012
if (var_hash && (var_already = php_add_var_hash(var_hash, struc, in_rcn_array))) {
9991013
if (var_already == -1) {
10001014
/* Reference to an object that failed to serialize, replace with null. */

0 commit comments

Comments
 (0)