Skip to content

UAF in php_free_shutdown_functions() when a destructor calls register_shutdown_function() during HashTable destruction #25920

In review

Description

@xuziqiang98

Description

session_set_save_handler($handler, true) seeds the shutdown-function hash as a mixed table with a string key (session_shutdown). A header callback runs late in request shutdown, after the destructor sweep but before php_free_shutdown_functions(), and registers a shutdown function whose object argument plus fillers grow the table to its resize boundary. When php_free_shutdown_functions() destroys the table, the object's __destruct() calls register_shutdown_function() again. The insertion resizes the table and pefree()s the old bucket array while zend_hash_destroy() is iterating it with cached p/end pointers; the next iteration reads p->key / calls the destructor through the freed bucket array. ASan reports a heap-use-after-free READ in zend_hash_destroy() at Zend/zend_hash.c:2009.

Root cause

  • php_request_shutdown() step 1 runs the registered shutdown functions; session_set_save_handler(..., true) registered a session_shutdown entry in BG(user_shutdown_function_names). The string key makes the table mixed, not packed (ext/session/session.c; ext/standard/basic_functions.c).
  • Step 6 (php_output_deactivate()) invokes the retained header callback. The callback registers strlen with an object argument P plus 29 string fillers; the table reaches 32/32 used.
  • Step 7 php_free_shutdown_functions() calls zend_hash_destroy(BG(user_shutdown_function_names)) (ext/standard/basic_functions.c:1682). The mixed destroy loop snapshots p = ht->arData and end = p + ht->nNumUsed (Zend/zend_hash.c:2009).
  • When the object's bucket is destroyed, user_shutdown_function_dtor() drops the object argument's last reference; P::__destruct() calls register_shutdown_function('strlen', 'x'). BG is still non-NULL (it is cleared only after FREE_HASHTABLE), so the insertion goes into the table currently being destroyed. Because nNumUsed >= nTableSize, zend_hash_do_resize() allocates a new bucket array, copies the live buckets, and pefree()s the old arData (Zend/zend_hash.c:1553).
  • The destroy loop resumes with its cached p/end pointing into the freed array and reads p->key / calls the destructor on the next iteration.

Reproducer

trigger.php

<?php
/* Cluster 76: session_set_save_handler(..., true) creates the mixed
 * shutdown table with string key "session_shutdown" (bucket 0).  No output is
 * produced, so the header callback registered below still runs at
 * php_output_deactivate (step 6, after the destructor sweep).  It registers an
 * object with a __destruct plus 29 fillers, growing the table to 32/32.  Step
 * 7's zend_hash_destroy then hits the object entry; its __destruct calls
 * register_shutdown_function, which resizes the mixed table and frees the
 * bucket array that the destroy loop is iterating. */
class H 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 'sid'; }
    public function validateId(string $id): bool { return true; }
    public function updateTimestamp(string $id, string $data): bool { return true; }
}
class P {
    public function __destruct() {
        register_shutdown_function('strlen', 'x');
    }
}
session_set_save_handler(new H, true);
header_register_callback(function () {
    register_shutdown_function('strlen', new P);
    for ($i = 0; $i < 29; $i++) {
        register_shutdown_function('strlen', 'x');
    }
});

Command

USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 php -n trigger.php

No output is produced before shutdown, so the header callback still runs at php_output_deactivate() with SG(headers_sent) false.

ASan report

Full ASan report
==2291302==ERROR: AddressSanitizer: heap-use-after-free on address 0x51a00000dfd8 at pc 0x59b1724e350b bp 0x7ffdb4c23410 sp 0x7ffdb4c23400
READ of size 8 at 0x51a00000dfd8 thread T0
    #0 0x59b1724e350a in zend_hash_destroy php-8.6.0/Zend/zend_hash.c:2009
    #1 0x59b171edb12f in php_free_shutdown_functions php-8.6.0/ext/standard/basic_functions.c:1682
    #2 0x59b17209a99f in php_request_shutdown php-8.6.0/main/main.c:1988
    #3 0x59b1725e4639 in do_cli php-8.6.0/sapi/cli/php_cli.c:1161
    #4 0x59b1725e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #5 0x59b1725e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #6 0x71000602a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #7 0x71000602a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #8 0x59b171631944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

0x51a00000dfd8 is located 344 bytes inside of 1280-byte region [0x51a00000de80,0x51a00000e380)
freed by thread T0 here:
    #0 0x7100064fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x59b172203e65 in __zend_free php-8.6.0/Zend/zend_alloc.c:4100
    #2 0x59b17221385e in _efree php-8.6.0/Zend/zend_alloc.c:3052
    #3 0x59b1724d3c06 in zend_hash_do_fresize php-8.6.0/Zend/zend_hash.c:1553
    #4 0x59b1724db960 in _zend_hash_index_add_or_update_i php-8.6.0/Zend/zend_hash.c:1378
    #5 0x59b1724db960 in zend_hash_next_index_insert php-8.6.0/Zend/zend_hash.c:1441
    #6 0x59b171edb56e in zend_hash_next_index_insert_mem php-8.6.0/Zend/zend_hash.h:940
    #7 0x59b171edb56e in append_user_shutdown_function php-8.6.0/ext/standard/basic_functions.c:1752
    #8 0x59b171edbbcd in zif_register_shutdown_function php-8.6.0/ext/standard/basic_functions.c:1718
    #9 0x59b17247b460 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1323
    #10 0x59b17247b460 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110872
    #11 0x59b172304442 in zend_call_function php-8.6.0/Zend/zend_execute_API.c:1082
    #12 0x59b172305ca1 in zend_call_known_function_ex php-8.6.0/Zend/zend_execute_API.c:1187
    #13 0x59b17257fcd2 in zend_call_known_function php-8.6.0/Zend/zend_API.h:930
    #14 0x59b17257fcd2 in zend_call_known_instance_method php-8.6.0/Zend/zend_API.h:958
    #15 0x59b17257fcd2 in zend_call_known_instance_method_with_0_params php-8.6.0/Zend/zend_API.h:964
    #16 0x59b17257fcd2 in zend_objects_destroy_object php-8.6.0/Zend/zend_objects.c:171
    #17 0x59b17257eb7a in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #18 0x59b1725c7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #19 0x59b1725c815d in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #20 0x59b1725c815d in zval_ptr_dtor php-8.6.0/Zend/zend_variables.c:83
    #21 0x59b171ecc318 in user_shutdown_function_dtor php-8.6.0/ext/standard/basic_functions.c:1612
    #22 0x59b1724e3539 in zend_hash_destroy php-8.6.0/Zend/zend_hash.c:2008
    #23 0x59b171edb12f in php_free_shutdown_functions php-8.6.0/ext/standard/basic_functions.c:1682
    #24 0x59b17209a99f in php_request_shutdown php-8.6.0/main/main.c:1988
    #25 0x59b1725e4639 in do_cli php-8.6.0/sapi/cli/php_cli.c:1161
    #26 0x59b1725e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #27 0x59b1725e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #28 0x71000602a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #29 0x71000602a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #30 0x59b171631944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

previously allocated by thread T0 here:
    #0 0x7100064fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x59b1722051e8 in __zend_malloc php-8.6.0/Zend/zend_alloc.c:4071
    #2 0x59b1722135b7 in _emalloc php-8.6.0/Zend/zend_alloc.c:3042
    #3 0x59b1724d3a78 in zend_hash_do_fresize php-8.6.0/Zend/zend_hash.c:1545
    #4 0x59b1724db960 in _zend_hash_index_add_or_update_i php-8.6.0/Zend/zend_hash.c:1378
    #5 0x59b1724db960 in zend_hash_next_index_insert php-8.6.0/Zend/zend_hash.c:1441
    #6 0x59b171edb56e in zend_hash_next_index_insert_mem php-8.6.0/Zend/zend_hash.h:940
    #7 0x59b171edb56e in append_user_shutdown_function php-8.6.0/ext/standard/basic_functions.c:1752
    #8 0x59b171edbbcd in zif_register_shutdown_function php-8.6.0/ext/standard/basic_functions.c:1718
    #9 0x59b17247b460 in ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1323
    #10 0x59b17247b460 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110872
    #11 0x59b172304442 in zend_call_function php-8.6.0/Zend/zend_execute_API.c:1082
    #12 0x59b172305ca1 in zend_call_known_function_ex php-8.6.0/Zend/zend_execute_API.c:1187
    #13 0x59b1720c9032 in zend_call_known_fcc_ex php-8.6.0/Zend/zend_API.h:944
    #14 0x59b1720c9032 in zend_call_known_fcc php-8.6.0/Zend/zend_API.h:950
    #15 0x59b1720c9032 in sapi_send_headers php-8.6.0/main/SAPI.c:875
    #16 0x59b171f251c7 in php_header php-8.6.0/ext/standard/head.c:70
    #17 0x59b1720a41ae in php_output_header php-8.6.0/main/output.c:119
    #18 0x59b1720a68b3 in php_output_deactivate php-8.6.0/main/output.c:179
    #19 0x59b17209aae1 in php_request_shutdown php-8.6.0/main/main.c:1983
    #20 0x59b1725e4639 in do_cli php-8.6.0/sapi/cli/php_cli.c:1161
    #21 0x59b1725e59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #22 0x59b1725e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #23 0x71000602a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #24 0x71000602a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #25 0x59b171631944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

SUMMARY: AddressSanitizer: heap-use-after-free php-8.6.0/Zend/zend_hash.c:2009 in zend_hash_destroy

PHP Version

PHP 8.6.0

Operating System

Ubuntu 24.04

Activity

  1. self-assigned this
    on Nov 30, 2026
  2. added a commit that references this issue on Nov 30, 2026
    2353f9a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions