Skip to content

UAF in session_set_save_handler() when a handler object's destructor re-enters registration #25927

Backlog

Description

@xuziqiang98

Description

Procedural session_set_save_handler($e, $e, $e, $e, $e, $e) stores the same Evil object in six mandatory handler slots, each with a ZVAL_COPY reference. After the external reference is unset, the six slots are the only owners. A second session_set_save_handler('h_open', ..., 'h_gc') releases each slot; on the last slot (ps_gc), the refcount reaches 0 and Evil::__destruct() runs. The destructor calls session_set_save_handler(new Good) while PS(mod_user_names).ps_gc still points at the object being destroyed. The reentrant call reaches ps_gc again and releases that slot while the object is still inside its destructor (refcount drops from 2 to 1). When Evil::__destruct() returns, the outer zend_objects_store_del() releases the final reference, frees the object, and then continues GC_DELREF/GC_REFCOUNT on the freed allocation. ASan reports a heap-use-after-free READ in zend_gc_delref() at Zend/zend_types.h:848, called from zif_session_set_save_handler() at ext/session/session.c:2345.

Root cause

  • SESSION_SET_USER_HANDLER_PROCEDURAL(struct_name, fci) (ext/session/session.c:2177) does:
    if (!Z_ISUNDEF(PS(mod_user_names).struct_name)) zval_ptr_dtor(...); ZVAL_COPY(...).
    The old slot is released before the new value is copied into it.
  • The same Evil object is stored in all six mandatory slots; unset($e) leaves the slots as sole owners.
  • The second session_set_save_handler() releases ps_open … ps_destroy, then ps_gc (ext/session/session.c:2345). The last release drops the refcount to 0 and calls Evil::__destruct() from zend_objects_store_del().
  • Evil::__destruct() calls session_set_save_handler(new Good). can_session_handler_be_changed() passes (no active session, no output). The reentrant call reaches ps_gc while the slot is still non-UNDEF and points at Evil; the macro calls zval_ptr_dtor() on it again, dropping the temporary destroy-object reference from 2 to 1.
  • The inner release does not free the object yet. When Evil::__destruct() returns, zend_objects_destroy_object()'s OBJ_RELEASE drops the last reference and frees the object. The outer zend_objects_store_del() then executes GC_DELREF(object) and reads GC_REFCOUNT(object) on the freed allocation (Zend/zend_objects_API.c:179); the free stack shows zend_objects_store_del() at :197 via zend_objects_destroy_object().

Reproducer

trigger.php

<?php
/* Cluster 114: procedural registration stores the same callable object Evil
 * in the six mandatory slots (six ZVAL_COPY refs).  unset() leaves the slots as
 * the only owners.  A second session_set_save_handler() releases the slots;
 * the last one runs Evil::__destruct, which re-enters
 * session_set_save_handler(new Good) while the ps_gc slot still points at the
 * object being destroyed. */
class Good implements SessionHandlerInterface {
    public function open(string $path, string $name): bool { return true; }
    public function close(): bool { return true; }
    public function read(string $id): string|false { return ''; }
    public function write(string $id, string $data): bool { return true; }
    public function destroy(string $id): bool { return true; }
    public function gc(int $max_lifetime): int|false { return 0; }
    public function create_sid(): string { return 'goodsid'; }
    public function validateId(string $id): bool { return true; }
    public function updateTimestamp(string $id, string $data): bool { return true; }
}

class Evil {
    public function __invoke(...$args) { return true; }
    public function __destruct() {
        $ok = session_set_save_handler(new Good);
        echo "Evil::__destruct inner session_set_save_handler=", var_export($ok, true), "\n";
    }
}

function h_open($p, $n) { return true; }
function h_close() { return true; }
function h_read($id) { return ''; }
function h_write($id, $d) { return true; }
function h_destroy($id) { return true; }
function h_gc($m) { return 0; }

$e = new Evil;
$ok1 = session_set_save_handler($e, $e, $e, $e, $e, $e);
unset($e);
var_dump(session_set_save_handler('h_open', 'h_close', 'h_read', 'h_write', 'h_destroy', 'h_gc'));
echo "done\n";

Command

USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 \
  php -n -d display_errors=0 -d log_errors=0 trigger.php

ASan report

Full ASan report
==2338645==ERROR: AddressSanitizer: heap-use-after-free on address 0x5040000538d0 at pc 0x6077e517ebbb bp 0x7ffdc26b0040 sp 0x7ffdc26b0030
READ of size 4 at 0x5040000538d0 thread T0
    #0 0x6077e517ebba in zend_gc_delref php-8.6.0/Zend/zend_types.h:848
    #1 0x6077e517ebba in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:179
    #2 0x6077e51c7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #3 0x6077e51c815d in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #4 0x6077e51c815d in zval_ptr_dtor php-8.6.0/Zend/zend_variables.c:83
    #5 0x6077e4928a7f in zif_session_set_save_handler php-8.6.0/ext/session/session.c:2345
    #6 0x6077e507bc9c in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1393
    #7 0x6077e507bc9c in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110877
    #8 0x6077e50727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #9 0x6077e51df3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #10 0x6077e4c9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #11 0x6077e4c9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #12 0x6077e51e2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #13 0x6077e51e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #14 0x6077e51e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #15 0x71304722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #16 0x71304722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #17 0x6077e4231944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

0x5040000538d0 is located 0 bytes inside of 40-byte region [0x5040000538d0,0x5040000538f8)
freed by thread T0 here:
    #0 0x7130476fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x6077e4e03e65 in __zend_free php-8.6.0/Zend/zend_alloc.c:4100
    #2 0x6077e4e1385e in _efree php-8.6.0/Zend/zend_alloc.c:3052
    #3 0x6077e517ea83 in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:197
    #4 0x6077e517fc8f in zend_object_release php-8.6.0/Zend/zend_objects_API.h:76
    #5 0x6077e517fc8f in zend_objects_destroy_object php-8.6.0/Zend/zend_objects.c:184
    #6 0x6077e517eb7a in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #7 0x6077e51c7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #8 0x6077e51c815d in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #9 0x6077e51c815d in zval_ptr_dtor php-8.6.0/Zend/zend_variables.c:83
    #10 0x6077e4928a7f in zif_session_set_save_handler php-8.6.0/ext/session/session.c:2345
    #11 0x6077e507bc9c in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1393
    #12 0x6077e507bc9c in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110877
    #13 0x6077e50727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #14 0x6077e51df3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #15 0x6077e4c9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #16 0x6077e4c9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #17 0x6077e51e2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #18 0x6077e51e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #19 0x6077e51e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #20 0x71304722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #21 0x71304722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #22 0x6077e4231944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

previously allocated by thread T0 here:
    #0 0x7130476fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x6077e4e051e8 in __zend_malloc php-8.6.0/Zend/zend_alloc.c:4071
    #2 0x6077e4e135b7 in _emalloc php-8.6.0/Zend/zend_alloc.c:3042
    #3 0x6077e517fd82 in zend_objects_new php-8.6.0/Zend/zend_objects.c:190
    #4 0x6077e4e23151 in _object_and_properties_init php-8.6.0/Zend/zend_API.c:1899
    #5 0x6077e4e23151 in object_init_ex php-8.6.0/Zend/zend_API.c:1922
    #6 0x6077e4f9b088 in ZEND_NEW_SPEC_CONST_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:11454
    #7 0x6077e508a3f4 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:112026
    #8 0x6077e50727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #9 0x6077e51df3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #10 0x6077e4c9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #11 0x6077e4c9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #12 0x6077e51e2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #13 0x6077e51e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #14 0x6077e51e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #15 0x71304722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #16 0x71304722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #17 0x6077e4231944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

SUMMARY: AddressSanitizer: heap-use-after-free php-8.6.0/Zend/zend_types.h:848 in zend_gc_delref

PHP Version

PHP 8.6.0

Operating System

Ubuntu 24.04

Activity

  1. jayssSmm commented on Nov 30, 2026

    @jayssSmm

    Hi, I'd like to take this issue and work on a fix. I'll open a PR once I've validated the reproducer. I'm currently trying to reproduce it on master with ASan and will update this thread if the behaviour doesn't match the report.

    Root cause (my understanding)

    SESSION_SET_USER_HANDLER_PROCEDURAL calls zval_ptr_dtor() on the old slot while the slot still points to the old value.

    If the destructor executes user code, that code can re-enter session_set_save_handler() and release the same slot again, potentially causing a double release.

    Proposed fix

    Make the slot update re-entrancy-safe by moving the old value out of the slot before releasing it:

    zval old;
    ZVAL_COPY_VALUE(&old, &PS(mod_user_names).struct_name);
    ZVAL_COPY(&PS(mod_user_names).struct_name, &fci);
    zval_ptr_dtor(&old);

    This ensures that when the destructor executes, the slot already contains the new value, while the old value is held only by the local variable. A re-entrant call therefore cannot release the old value again.

    This follows the swap-then-release pattern used elsewhere in PHP for values with user-visible destructors.

    Things I'll investigate in the same PR

    1. Check the other slots updated by the macro (create_sid, validate_sid, update_timestamp) and the OO path (PS(mod_user_class_name)) for the same pattern.
    2. Verify whether an outer call overwriting values assigned by a re-entrant call is acceptable. The proposed fix makes this memory-safe, but the final handler state depends on call order.
    3. Review the session_set_save_handler() registration paths to ensure no failure path leaves a slot partially updated.

    Tests

    I'll add a .phpt test under ext/session/tests/ based on the reproducer.

    I'll also document the USE_ZEND_ALLOC=0 requirement for ASan testing and check whether the regression can be made observable on non-ASan builds as well.

    I'll target the lowest supported branch containing the bug and then merge the fix forward.

    Before proceeding: Could you confirm that nobody is already working on this issue and that you're happy for me to take ownership?

    Please let me know if you'd prefer a different approach, such as deferring destructors. Thanks!

  2. xuziqiang98 commented on Nov 30, 2026

    @xuziqiang98
    Author

    I'm not planning to fix or make a PR for this issue, thank you for your contribution:)

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