Repository navigation
Description
Description
The following code:
No extensions or INI changes are needed. Save as /tmp/c2a-poc/poc.php:
<?php
class C {
public function __destruct() {
// Re-entry: the inner convert_to_array() releases the same object again, then stores an array
settype($GLOBALS['x'], 'array');
}
}
$x = new C;
settype($x, 'array');
echo "SURVIVED - no crash\n";Run with an ASan build and USE_ZEND_ALLOC=0, so that the heap goes through malloc. Nothing is printed to stdout:
USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php /tmp/c2a-poc/poc.phpResulted in this output:
$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php /tmp/c2a-poc/poc.php
=================================================================
==500251==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000038fd0 at pc 0x6444b29aca1b bp 0x7ffe218c1120 sp 0x7ffe218c1110
READ of size 4 at 0x504000038fd0 thread T0
#0 0x6444b29aca1a in zend_gc_delref php-src/Zend/zend_types.h:1395
#1 0x6444b29aca1a in zend_objects_store_del php-src/Zend/zend_objects_API.c:182
#2 0x6444b29bc7c5 in zend_object_release php-src/Zend/zend_objects_API.h:77
#3 0x6444b29bc7c5 in convert_to_array php-src/Zend/zend_operators.c:856
#4 0x6444b23f3c43 in zif_settype php-src/ext/standard/type.c:113
#5 0x6444b28ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER php-src/Zend/zend_vm_execute.h:1360
#6 0x6444b28ab605 in execute_ex php-src/Zend/zend_vm_execute.h:116550
#7 0x6444b28a4148 in zend_execute php-src/Zend/zend_vm_execute.h:122038
#8 0x6444b2a05ad4 in zend_execute_script php-src/Zend/zend.c:1990
#9 0x6444b24cd784 in php_execute_script_ex php-src/main/main.c:2688
#10 0x6444b24cdc42 in php_execute_script php-src/main/main.c:2728
#11 0x6444b2a09723 in do_cli php-src/sapi/cli/php_cli.c:960
#12 0x6444b2a0bff3 in main php-src/sapi/cli/php_cli.c:1383
0x504000038fd0 is located 0 bytes inside of 40-byte region [0x504000038fd0,0x504000038ff8)
freed by thread T0 here:
#0 0x7da1a84fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
#1 0x6444b2623c16 in __zend_free php-src/Zend/zend_alloc.c:3976
#2 0x6444b26325db in _efree php-src/Zend/zend_alloc.c:2928
#3 0x6444b29ac8e3 in zend_objects_store_del php-src/Zend/zend_objects_API.c:200
#4 0x6444b29adad9 in zend_object_release php-src/Zend/zend_objects_API.h:77
#5 0x6444b29adad9 in zend_objects_destroy_object php-src/Zend/zend_objects.c:185
#6 0x6444b29ac9da in zend_objects_store_del php-src/Zend/zend_objects_API.c:181
#7 0x6444b29bc7c5 in zend_object_release php-src/Zend/zend_objects_API.h:77
#8 0x6444b29bc7c5 in convert_to_array php-src/Zend/zend_operators.c:856
#9 0x6444b23f3c43 in zif_settype php-src/ext/standard/type.c:113
...
previously allocated by thread T0 here:
...
#3 0x6444b29adbc5 in zend_objects_new php-src/Zend/zend_objects.c:191
...
#6 0x6444b27b8cfc in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER php-src/Zend/zend_vm_execute.h:11316
...
SUMMARY: AddressSanitizer: heap-use-after-free php-src/Zend/zend_types.h:1395 in zend_gc_delref
==500251==ABORTING
(Exit code 1. Local paths in the ASan report are abbreviated to php-src/, some stack frames are omitted.)
Without the sanitizer the process aborts with exit code 134: the plain build prints zend_mm_heap corrupted with the default allocator and malloc(): unaligned tcache chunk detected with USE_ZEND_ALLOC=0; the ASan build with the default allocator also prints zend_mm_heap corrupted. In gdb both messages come from a later allocation, sapi_send_headers() (main/SAPI.c:887), so the double free itself is not noticed by the allocator.
But I expected this output instead:
SURVIVED - no crash
Exit code 0, with $x being an array afterwards. This is the output of the same script when __destruct() does not call settype() again.
Root cause
settype()converts the value of the reference in place (ext/standard/type.c:100,:113).- For a regular object,
convert_to_array()releases the object and overwrites the operand only afterwards (Zend/zend_operators.c:855-857):
HashTable *ht = zend_std_build_object_properties_array(Z_OBJ_P(op));
OBJ_RELEASE(Z_OBJ_P(op));
ZVAL_ARR(op, ht);While OBJ_RELEASE() runs the destructor, op, the value of $x, still holds the object pointer.
3. __destruct() calls settype() on the same reference. The inner convert_to_array() finds the object again and runs the same OBJ_RELEASE(), which consumes a reference the operand no longer owns.
4. When __destruct() returns, zend_objects_destroy_object() releases its own reference (Zend/zend_objects.c:185). The refcount drops to 0 and the object is freed (Zend/zend_objects_API.c:200). The enclosing zend_objects_store_del() then continues with the freed object; GC_DELREF(object) at :182 is the read reported above, and it would free the object a second time.
Second trigger: settype($x, 'null')
convert_to_null() uses the same order (Zend/zend_operators.c:682-683: zval_ptr_dtor(op); ZVAL_NULL(op);). This script gives the same report:
<?php
class C {
public function __destruct() {
settype($GLOBALS['x'], 'null');
}
}
$x = new C;
settype($x, 'null');
echo "SURVIVED - no crash\n";==500526==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000038f10 at pc 0x6417f5faca1b bp 0x7ffcdadd5a10 sp 0x7ffcdadd5a00
READ of size 4 at 0x504000038f10 thread T0
#0 0x6417f5faca1a in zend_gc_delref php-src/Zend/zend_types.h:1395
#1 0x6417f5faca1a in zend_objects_store_del php-src/Zend/zend_objects_API.c:182
...
#5 0x6417f5fbba02 in convert_to_null php-src/Zend/zend_operators.c:682
#6 0x6417f59f3d2b in zif_settype php-src/ext/standard/type.c:121
...
freed by thread T0 here:
...
#3 0x6417f5fac8e3 in zend_objects_store_del php-src/Zend/zend_objects_API.c:200
#4 0x6417f5fadad9 in zend_object_release php-src/Zend/zend_objects_API.h:77
#5 0x6417f5fadad9 in zend_objects_destroy_object php-src/Zend/zend_objects.c:185
#6 0x6417f5fac9da in zend_objects_store_del php-src/Zend/zend_objects_API.c:181
...
#10 0x6417f5fbba02 in convert_to_null php-src/Zend/zend_operators.c:682
#11 0x6417f59f3d2b in zif_settype php-src/ext/standard/type.c:121
...
PHP Version
PHP 8.5.13
Operating System
Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64
Activity