Skip to content

Commit fa8a9d1

Browse files
eyupcanakmanLamentXU123
authored andcommitted
Fix stack overflow in filter_var() and filter_var_array() (#25349)
Deeply nested arrays could overflow the C stack while being filtered. Check the stack limit before descending, and stop processing array elements and keys once an exception is pending. This prevents repeated stack errors from a second deep entry. Closes #25349
1 parent 6144bac commit fa8a9d1

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ PHP NEWS
4545
. Fixed the argument blamed by the imagepolygon(), imageopenpolygon() and
4646
imagefilledpolygon() too few points error. (lacatoire)
4747

48+
- Filter:
49+
. Fixed stack overflow in filter_var() and filter_var_array() with deeply
50+
nested arrays. (Eyüp Can Akman)
51+
4852
- MBString:
4953
. Fixed stack overflow in mb_check_encoding() and mb_convert_encoding() with
5054
deeply nested arrays. (Eyüp Can Akman)

‎ext/filter/filter.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long f
393393
if (Z_TYPE_P(value) == IS_ARRAY) {
394394
zval *element;
395395

396+
#ifdef ZEND_CHECK_STACK_LIMIT
397+
if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
398+
zend_call_stack_size_error();
399+
return;
400+
}
401+
#endif
402+
396403
if (Z_IS_RECURSIVE_P(value)) {
397404
return;
398405
}
@@ -403,6 +410,10 @@ static void php_zval_filter_recursive(zval *value, zend_long filter, zend_long f
403410
if (Z_TYPE_P(element) == IS_ARRAY) {
404411
SEPARATE_ARRAY(element);
405412
php_zval_filter_recursive(element, filter, flags, options, charset, copy);
413+
if (EG(exception)) {
414+
Z_UNPROTECT_RECURSION_P(value);
415+
return;
416+
}
406417
} else {
407418
php_zval_filter(element, filter, flags, options, charset, copy);
408419
}
@@ -590,6 +601,10 @@ static void php_filter_array_handler(zval *input, HashTable *op_ht, zend_long op
590601
Z_TYPE_P(arg_elm) == IS_ARRAY ? 0 : zval_get_long(arg_elm),
591602
0, FILTER_REQUIRE_SCALAR
592603
);
604+
if (EG(exception)) {
605+
zval_ptr_dtor(&nval);
606+
RETURN_THROWS();
607+
}
593608
zend_hash_update(Z_ARRVAL_P(return_value), arg_key, &nval);
594609
}
595610
} ZEND_HASH_FOREACH_END();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Stack overflow in filter_var() and filter_var_array() with deeply nested arrays
3+
--EXTENSIONS--
4+
filter
5+
--SKIPIF--
6+
<?php
7+
if (ini_get('zend.max_allowed_stack_size') === false) {
8+
die('skip No stack limit support');
9+
}
10+
if (getenv('SKIP_ASAN')) {
11+
die('skip ASAN needs different stack limit setting due to more stack space usage');
12+
}
13+
?>
14+
--INI--
15+
zend.max_allowed_stack_size=256K
16+
--FILE--
17+
<?php
18+
$deep = 'x';
19+
for ($i = 0; $i < 30000; $i++) {
20+
$deep = [$deep];
21+
}
22+
23+
/* Two top-level entries: the second must not be visited once the
24+
* stack limit error has been thrown, so only one Error is thrown. */
25+
try {
26+
filter_var([$deep, $deep], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
27+
} catch (Error $e) {
28+
echo $e->getMessage(), "\n";
29+
var_dump($e->getPrevious());
30+
}
31+
try {
32+
filter_var_array(['a' => $deep, 'b' => $deep], ['a' => ['flags' => FILTER_REQUIRE_ARRAY], 'b' => ['flags' => FILTER_REQUIRE_ARRAY]]);
33+
} catch (Error $e) {
34+
echo $e->getMessage(), "\n";
35+
var_dump($e->getPrevious());
36+
}
37+
38+
unset($e);
39+
while (is_array($deep)) {
40+
$deep = $deep[0];
41+
}
42+
?>
43+
--EXPECTF--
44+
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
45+
NULL
46+
Maximum call stack size of %d bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?
47+
NULL

0 commit comments

Comments
 (0)