[php-src] PR #12758: fix segfault in `ZEND_BIND_STATIC`
| From: | realFlowControl | Date: | Thu, 23 Nov 2023 15:59:48 +0000 |
| Subject: | [php-src] PR #12758: fix segfault in `ZEND_BIND_STATIC` | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-24293@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/12758
Author: realFlowControl
In case a
ZEND_BIND_STATIC is being executed, while the current chunk is full, the
zend_array_dup() call will trigger a OOM in ZendMM which will crash, as the opline
might be a dangling pointer.
I am currently working on a stable test for this, but it is reproducible with the following PHP
code:
```php
function &ref() {
static $a = 5;
return $a;
}
class Foo {
public static int $i;
public static string $s = "x";
}
var_dump(Foo::$i = "1");
var_dump(Foo::$s, Foo::$i);
$a = range(0, 20000);
gc_mem_caches();
for ($i = 0; $i < 10364; ++$i) {
$a[$i] = "abcdefghijklmnopqrstuvwxyz" . ($i % 2);
}
var_dump(ref());
```
The 10364 in the for loop is the "magic" number to fill up all the bins in
the current chunk (this might vary).
Now in the call to ref() PHP will execute the ZEND_BIND_STATIC opcode and
choose the path via zend_array_dub() in https://github.com/php/php-src/blob/PHP-8.1/Zend/zend_vm_def.h#L8793
which will try to allocate a new chunk, failing todo so as the memory limit is reached and then
crash while trying to follow the opline pointer in
zend_get_executed_lineno() while trying to create the "Allowed memory size of
..."-Error message.
```bash
$ php -n -dmemory_limit=2M x.php
int(1)
string(1) "x"
int(1)
Segmentation fault (core dumped)
$ gdb php core
...
Core was generated by `php -n -dmemory_limit=2M x.php'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 zend_get_executed_lineno () at /usr/local/src/php/Zend/zend_execute_API.c:654
654 return ex->opline->lineno;
(gdb) bt
#0 zend_get_executed_lineno () at /usr/local/src/php/Zend/zend_execute_API.c:654
#1 0x0000004000582b42 in zend_get_executed_lineno () at
/usr/local/src/php/Zend/zend_execute_API.c:636
#2 0x000000400018a2a7 in get_filename_lineno (type=type@entry=1,
filename=filename@entry=0x40028f4540, lineno=lineno@entry=0x40028f453c) at
/usr/local/src/php/Zend/zend.c:1548
#3 0x000000400018ac1d in zend_error_noreturn (type=type@entry=1, format=format@entry=0x4000f12460
"Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)")
at /usr/local/src/php/Zend/zend.c:1623
#4 0x0000004000187f34 in zend_mm_safe_error (format=format@entry=0x4000f12460 "Allowed memory
size of %zu bytes exhausted (tried to allocate %zu bytes)", limit=2097152,
size=size@entry=4096,
heap=<optimized out>) at /usr/local/src/php/Zend/zend_alloc.c:383
#5 0x0000004000188004 in zend_mm_alloc_pages (heap=<optimized out>, pages_count=<optimized
out>) at /usr/local/src/php/Zend/zend_alloc.c:1016
#6 0x0000004000567a62 in zend_mm_alloc_small_slow (heap=0x4006600040, bin_num=6) at
/usr/local/src/php/Zend/zend_alloc.c:1254
#7 0x00000040005a46e2 in zend_array_dup (source=0x40066571c0) at
/usr/local/src/php/Zend/zend_hash.c:2330
#8 0x00000040005ba71e in ZEND_BIND_STATIC_SPEC_CV_UNUSED_HANDLER () at
/usr/local/src/php/Zend/zend_vm_execute.h:48686
#9 0x00000040005f5958 in execute_ex (ex=0x1) at /usr/local/src/php/Zend/zend_vm_execute.h:60095
#10 0x00000040005fd530 in zend_execute (op_array=0x400667e100, return_value=0x0) at
/usr/local/src/php/Zend/zend_vm_execute.h:60408
#11 0x000000400059202b in zend_execute_scripts (type=type@entry=8, retval=retval@entry=0x0,
file_count=file_count@entry=3) at /usr/local/src/php/Zend/zend.c:1827
#12 0x000000400052e056 in php_execute_script (primary_file=<optimized out>) at
/usr/local/src/php/main/main.c:2542
#13 0x000000400066e012 in do_cli (argc=4, argv=0x40011258d0) at
/usr/local/src/php/sapi/cli/php_cli.c:964
#14 0x00000040001a6225 in main (argc=4, argv=0x40011258d0) at
/usr/local/src/php/sapi/cli/php_cli.c:1333
```
I am currently working on a test, to reproduce this in a stable manner and not using a magic number
that might change.