Description
Description
session_reset() destroys the old $_SESSION array while the session is still active and PS(http_session_vars) still refers to that array. A __destruct() of one of the values therefore runs with session_status() === PHP_SESSION_ACTIVE, and session functions operate on the array that is being destroyed. session_unset() releases every value a second time from inside the destruction loop.
This is not the zend_hash_clean() re-entrancy of GH-22061. The cause is in ext/session, which releases the array while its own global still points to it, and a small change in ext/session fixes it.
The following code:
<?php
class Boom {
public function __destruct() {
// called synchronously while session_reset() is destroying the old session array
session_unset();
}
}
$dir = sys_get_temp_dir() . '/poc103';
@mkdir($dir);
ini_set('session.save_path', $dir);
session_start();
$_SESSION['boom'] = new Boom();
$_SESSION['filler'] = str_repeat('A', 128);
$_SESSION['filler2'] = str_repeat('B', 128);
session_reset();
echo "SURVIVED - no crash\n";Resulted in this output:
$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php -n poc.php
=================================================================
==1555582==ERROR: AddressSanitizer: heap-use-after-free on address 0x504000039390 at pc 0x6164887aca1b bp 0x7ffdc3944450 sp 0x7ffdc3944440
READ of size 4 at 0x504000039390 thread T0
#0 0x6164887aca1a in zend_gc_delref Zend/zend_types.h:1395
#1 0x6164887aca1a in zend_objects_store_del Zend/zend_objects_API.c:182
#2 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#3 0x616488712d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#4 0x616488712d19 in zend_array_destroy Zend/zend_hash.c:2061
#5 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#6 0x6164887ee980 in i_zval_ptr_dtor Zend/zend_variables.h:45
#7 0x6164887ee980 in zend_reference_destroy Zend/zend_variables.c:74
#8 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#9 0x6164887eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#10 0x6164887eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#11 0x616487fed962 in php_session_track_init ext/session/session.c:261
#12 0x61648800272b in php_session_initialize ext/session/session.c:497
#13 0x616488002f7c in php_session_reset ext/session/session.c:1808
#14 0x616488002f7c in zif_session_reset ext/session/session.c:2837
#15 0x6164886ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1360
#16 0x6164886ab605 in execute_ex Zend/zend_vm_execute.h:116550
#17 0x6164886a4148 in zend_execute Zend/zend_vm_execute.h:122038
#18 0x616488805ad4 in zend_execute_script Zend/zend.c:1990
#19 0x6164882cd784 in php_execute_script_ex main/main.c:2688
#20 0x6164882cdc42 in php_execute_script main/main.c:2728
#21 0x616488809723 in do_cli sapi/cli/php_cli.c:960
#22 0x61648880bff3 in main sapi/cli/php_cli.c:1383
0x504000039390 is located 0 bytes inside of 40-byte region [0x504000039390,0x5040000393b8)
freed by thread T0 here:
#1 0x616488423c16 in __zend_free Zend/zend_alloc.c:3976
#2 0x6164884325db in _efree Zend/zend_alloc.c:2928
#3 0x6164887ac8e3 in zend_objects_store_del Zend/zend_objects_API.c:200
#4 0x6164887adad9 in zend_object_release Zend/zend_objects_API.h:77
#5 0x6164887adad9 in zend_objects_destroy_object Zend/zend_objects.c:185
#6 0x6164887ac9da in zend_objects_store_del Zend/zend_objects_API.c:181
#7 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#8 0x616488712d19 in i_zval_ptr_dtor Zend/zend_variables.h:45
#9 0x616488712d19 in zend_array_destroy Zend/zend_hash.c:2061
#10 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#11 0x6164887ee980 in i_zval_ptr_dtor Zend/zend_variables.h:45
#12 0x6164887ee980 in zend_reference_destroy Zend/zend_variables.c:74
#13 0x6164887ee80e in rc_dtor_func Zend/zend_variables.c:57
#14 0x6164887eead6 in i_zval_ptr_dtor Zend/zend_variables.h:45
#15 0x6164887eead6 in zval_ptr_dtor Zend/zend_variables.c:84
#16 0x616487fed962 in php_session_track_init ext/session/session.c:261
#17 0x61648800272b in php_session_initialize ext/session/session.c:497
#18 0x616488002f7c in php_session_reset ext/session/session.c:1808
#19 0x616488002f7c in zif_session_reset ext/session/session.c:2837
#20 0x6164886ab605 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER Zend/zend_vm_execute.h:1360
#21 0x6164886ab605 in execute_ex Zend/zend_vm_execute.h:116550
#22 0x6164886a4148 in zend_execute Zend/zend_vm_execute.h:122038
#23 0x616488805ad4 in zend_execute_script Zend/zend.c:1990
#24 0x6164882cd784 in php_execute_script_ex main/main.c:2688
#25 0x6164882cdc42 in php_execute_script main/main.c:2728
#26 0x616488809723 in do_cli sapi/cli/php_cli.c:960
#27 0x61648880bff3 in main sapi/cli/php_cli.c:1383
previously allocated by thread T0 here:
#1 0x616488424dff in __zend_malloc Zend/zend_alloc.c:3947
#2 0x616488432343 in _emalloc Zend/zend_alloc.c:2918
#3 0x6164887adbc5 in zend_objects_new Zend/zend_objects.c:191
#4 0x616488441460 in _object_and_properties_init Zend/zend_API.c:1854
#5 0x616488441460 in object_init_ex Zend/zend_API.c:1877
#6 0x6164885b8cfc in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER Zend/zend_vm_execute.h:11316
#7 0x6164886b91f3 in execute_ex Zend/zend_vm_execute.h:117699
#8 0x6164886a4148 in zend_execute Zend/zend_vm_execute.h:122038
#9 0x616488805ad4 in zend_execute_script Zend/zend.c:1990
#10 0x6164882cd784 in php_execute_script_ex main/main.c:2688
#11 0x6164882cdc42 in php_execute_script main/main.c:2728
#12 0x616488809723 in do_cli sapi/cli/php_cli.c:960
#13 0x61648880bff3 in main sapi/cli/php_cli.c:1383
SUMMARY: AddressSanitizer: heap-use-after-free Zend/zend_types.h:1395 in zend_gc_delref
==1555582==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 process aborts in both configurations (30 of 30 runs each):
$ /path/to/build-plain/sapi/cli/php -n poc.php
zend_mm_heap corrupted
Aborted (core dumped) (exit code 134)
$ USE_ZEND_ALLOC=0 /path/to/build-plain/sapi/cli/php -n poc.php
malloc(): unaligned tcache chunk detected
Aborted (core dumped) (exit code 134)
But I expected this output instead:
SURVIVED - no crash
Root cause
session_reset() → php_session_reset() (ext/session/session.c:1808) → php_session_initialize() (ext/session/session.c:497) → php_session_track_init() (ext/session/session.c:253):
/* Unconditionally destroy existing array -- possible dirty data */
zend_delete_global_variable(var_name); /* :258 drops the $_SESSION reference */
if (!Z_ISUNDEF(PS(http_session_vars))) {
zval_ptr_dtor(&PS(http_session_vars)); /* :261 last reference: the array is destroyed here ... */
}
array_init(&session_vars); /* :264 */
ZVAL_NEW_REF(&PS(http_session_vars), &session_vars); /* :265 ... PS(http_session_vars) is replaced only here */While zval_ptr_dtor() runs, the session status is still active and PS(http_session_vars) still refers to the array being destroyed. session_unset() (ext/session/session.c:2758) therefore passes its status check, IF_SESSION_VARS() (ext/session/session.c:83) is true, SEPARATE_ARRAY() does not copy an array with refcount 0, and zend_hash_clean() (ext/session/session.c:2773) runs on the dying array.
What happens to the object and the array, in order (checked with watchpoints on the reference counts):
zend_array_destroy()(Zend/zend_hash.c:2061) releasesboom: object refcount 1 → 0.zend_objects_store_del()sets the refcount to 1 (Zend/zend_objects_API.c:180) and callszend_objects_destroy_object(), which takes a second reference (Zend/zend_objects.c:148) and calls__destruct()(Zend/zend_objects.c:172).session_unset()→zend_hash_clean()releases all three values:boom2 → 1,fillerandfiller2are freed.__destruct()returns.OBJ_RELEASE()(Zend/zend_objects.c:185) drops the object to 0 and the object is freed (Zend/zend_objects_API.c:200).- The outer
zend_objects_store_del()continues withGC_DELREF(object)(Zend/zend_objects_API.c:182) and reads the refcount (Zend/zend_objects_API.c:186). This is the use-after-free in the report above. - The outer
zend_array_destroy()loop goes on tofillerandfiller2, which have already been freed, and releases them again.
On plain builds each of these decrements lands on the free-list link that the allocator keeps in freed blocks. The next allocation from that size class aborts with the messages shown above.
Other entry points and variants
- Replace
session_reset();bysession_write_close(); session_start();: same report withphp_session_start()(ext/session/session.c:1765) in the stack, 20 of 20 runs. - After
unset($GLOBALS['_SESSION']), so thatPS(http_session_vars)is the only holder of the array,session_destroy()gives the same report throughphp_rshutdown_session_globals()(ext/session/session.c:162), andsession_decode()withsession.serialize_handler=php_serializethroughps_srlzr_decode_php_serialize()(ext/session/session.c:1011). - With the string stored before the object,
session_unset()itself reports the use-after-free:zend_hash_clean()releases the string thatzend_array_destroy()has already freed (20 of 20 runs). - Not affected:
session_encode()in the destructor with the element order above; thesession_decode()failure path (php_session_cancel_decode()sets the status to none first); a destructor that runs at request shutdown; another variable holding the array; values without destructors. - A destructor that calls
session_write_close()crashes differently:session_reset()continues with the closed save handler andps_files_open()(ext/session/mod_files.c:160) dereferences a null pointer, reached fromphp_session_initialize()(ext/session/session.c:498).
php_session_track_init() is unchanged in php-src master (ext/session/session.c:261), PHP-8.6, PHP-8.5, php-8.5.13, PHP-8.4 and PHP-8.3. zend_array_destroy(), zend_hash_clean() and zend_objects_store_del() are the same on those branches as well; master's zend_array_destroy() only adds an abort() guard. Only 8.5.12 was executed.
Possible fix
Detach PS(http_session_vars) before releasing the old array, in all three places (ext/session/session.c:162, :261, :1011):
if (!Z_ISUNDEF(PS(http_session_vars))) {
zval old_session_vars;
/* Detach the array before destroying it: destructors of its values may call
* session functions, which must not see the array that is being destroyed. */
ZVAL_COPY_VALUE(&old_session_vars, &PS(http_session_vars));
ZVAL_UNDEF(&PS(http_session_vars));
zval_ptr_dtor(&old_session_vars);
}- With this change the script above prints
SURVIVED - no crashon plain and ASan builds (30 of 30 runs each), and none of the entry points listed above reports an error anymore. - Regression tests:
ext/session/tests ext/standard/tests/serialize Zend/testson a plain build (5817 tests, no failures) andext/session/tests Zend/testson an ASan build (5653 tests). The per-test results are identical to an unpatched build; under ASan both fail the same eight stack-limit tests. - Behaviour change: session functions called from such a destructor now behave as if there were no session data.
session_unset()returnstruewithout doing anything, andsession_encode()warns "Cannot encode non-existent session". - The
zend_hash_clean()change of Fix use-after-free when a destructor bails out during zend_hash_clean() #22710 does not cover this. With that change applied to 8.5.12, the PoC of Double free via re-entrant destructor inzend_hash_clean()(Double Free) #22061 passes, but the script above still reports the use-after-free atZend/zend_objects_API.c:182.
Related reports
- Delaying destructors and GC #20001 tracks this class of problems (destructors running while internal code holds a pointer to the data being destroyed). I did not find a report for the
PS(http_session_vars)case there. - Double free via re-entrant destructor in
zend_hash_clean()(Double Free) #22061 is the closest one:session_unset()with a destructor that unsets an element. It was closed as a duplicate and is listed in Delaying destructors and GC #20001. Its PoC still reproduces on 8.5.12 and is not addressed by the fix above. - UAF in session_encode() #16590 was a use-after-free in
session_encode()with a different cause, fixed by taking a reference inPS_ENCODE_LOOP(cc39bc2).
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 array is being freed), so a fix local to the session code probably won't be accepted until that lands.