Skip to content

session_reset() destroys $_SESSION while PS(http_session_vars) still points to it: use-after-free when a destructor calls session_unset() #25705

Backlog

Description

@xuziqiang98

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):

  1. zend_array_destroy() (Zend/zend_hash.c:2061) releases boom: object refcount 1 → 0.
  2. zend_objects_store_del() sets the refcount to 1 (Zend/zend_objects_API.c:180) and calls zend_objects_destroy_object(), which takes a second reference (Zend/zend_objects.c:148) and calls __destruct() (Zend/zend_objects.c:172).
  3. session_unset() → zend_hash_clean() releases all three values: boom 2 → 1, filler and filler2 are freed.
  4. __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).
  5. The outer zend_objects_store_del() continues with GC_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.
  6. The outer zend_array_destroy() loop goes on to filler and filler2, 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(); by session_write_close(); session_start();: same report with php_session_start() (ext/session/session.c:1765) in the stack, 20 of 20 runs.
  • After unset($GLOBALS['_SESSION']), so that PS(http_session_vars) is the only holder of the array, session_destroy() gives the same report through php_rshutdown_session_globals() (ext/session/session.c:162), and session_decode() with session.serialize_handler=php_serialize through ps_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 that zend_array_destroy() has already freed (20 of 20 runs).
  • Not affected: session_encode() in the destructor with the element order above; the session_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 and ps_files_open() (ext/session/mod_files.c:160) dereferences a null pointer, reached from php_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 crash on 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/tests on a plain build (5817 tests, no failures) and ext/session/tests Zend/tests on 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() returns true without doing anything, and session_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 in zend_hash_clean() (Double Free) #22061 passes, but the script above still reports the use-after-free at Zend/zend_objects_API.c:182.

Related reports

PHP Version

PHP 8.5.12

Operating System

Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64

Activity

  1. lazerg commented on Nov 24, 2026

    @lazerg
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions