Skip to content

Double free and use-after-free: unregister_tick_function() called from a destructor while RSHUTDOWN destroys the tick function list #25681

Open

Description

@xuziqiang98

Description

The following code:

No extensions or INI changes are needed, and declare(ticks) is not needed either. Save as /tmp/tick-poc/poc.php:

<?php
class T {
    public function tick() {}
    public function __destruct() {
        unregister_tick_function([$this, 'tick']);
    }
}
// The callback runs during request shutdown (php_output_end_all), after the destructor pass.
ob_start(function () {
    $t = new T;
    register_tick_function([$t, 'tick']);
    return '';
});
// Neither flush nor ob_end_clean(): the buffer is left for shutdown.
echo "end of script\n";

Run with an ASan build and USE_ZEND_ALLOC=0, so that the heap goes through malloc. The script produces no output (the callback returns an empty string); the error happens during request shutdown:

USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php /tmp/tick-poc/poc.php

Resulted in this output:

$ USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 /path/to/build-asan/sapi/cli/php /tmp/tick-poc/poc.php
=================================================================
==459788==ERROR: AddressSanitizer: heap-use-after-free on address 0x5040000390d0 at pc 0x5e0cbd5aca1b bp 0x7fff037157c0 sp 0x7fff037157b0
READ of size 4 at 0x5040000390d0 thread T0
    #0 0x5e0cbd5aca1a in zend_gc_delref php-src/Zend/zend_types.h:1395
    #1 0x5e0cbd5aca1a in zend_objects_store_del php-src/Zend/zend_objects_API.c:182
    #2 0x5e0cbcee3cc6 in zend_object_release php-src/Zend/zend_objects_API.h:77
    #3 0x5e0cbcee3cc6 in zend_fcc_dtor php-src/Zend/zend_API.h:840
    #4 0x5e0cbcee3cc6 in user_tick_function_dtor php-src/ext/standard/basic_functions.c:1621
    #5 0x5e0cbd596d3a in zend_llist_destroy php-src/Zend/zend_llist.c:109
    #6 0x5e0cbcedb38e in zm_deactivate_basic php-src/ext/standard/basic_functions.c:474
    #7 0x5e0cbd24e250 in zend_deactivate_modules php-src/Zend/zend_API.c:3472
    #8 0x5e0cbd0caa4b in php_request_shutdown php-src/main/main.c:2038
    #9 0x5e0cbd60adef in do_cli php-src/sapi/cli/php_cli.c:1179
    #10 0x5e0cbd60bff3 in main php-src/sapi/cli/php_cli.c:1383
0x5040000390d0 is located 0 bytes inside of 40-byte region [0x5040000390d0,0x5040000390f8)
freed by thread T0 here:
    #0 0x7bf7af4fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x5e0cbd223c16 in __zend_free php-src/Zend/zend_alloc.c:3976
    #2 0x5e0cbd2325db in _efree php-src/Zend/zend_alloc.c:2928
    #3 0x5e0cbd5ac8e3 in zend_objects_store_del php-src/Zend/zend_objects_API.c:200
    #4 0x5e0cbd5adad9 in zend_object_release php-src/Zend/zend_objects_API.h:77
    #5 0x5e0cbd5adad9 in zend_objects_destroy_object php-src/Zend/zend_objects.c:185
    #6 0x5e0cbd5ac9da in zend_objects_store_del php-src/Zend/zend_objects_API.c:181
    #7 0x5e0cbcee3cc6 in zend_object_release php-src/Zend/zend_objects_API.h:77
    #8 0x5e0cbcee3cc6 in zend_fcc_dtor php-src/Zend/zend_API.h:840
    #9 0x5e0cbcee3cc6 in user_tick_function_dtor php-src/ext/standard/basic_functions.c:1621
    #10 0x5e0cbd596d3a in zend_llist_destroy php-src/Zend/zend_llist.c:109
    #11 0x5e0cbcedb38e in zm_deactivate_basic php-src/ext/standard/basic_functions.c:474
    ...
previously allocated by thread T0 here:
    ...
    #3 0x5e0cbd5adbc5 in zend_objects_new php-src/Zend/zend_objects.c:191
    #4 0x5e0cbd241460 in _object_and_properties_init php-src/Zend/zend_API.c:1854
    ...
    #9 0x5e0cbd0d63ee in php_output_handler_op php-src/main/output.c:979
    #10 0x5e0cbd0d8c44 in php_output_stack_pop php-src/main/output.c:1286
    #11 0x5e0cbd0d8f6d in php_output_end_all php-src/main/output.c:328
    #12 0x5e0cbd0ca648 in php_request_shutdown php-src/main/main.c:2026
    ...
SUMMARY: AddressSanitizer: heap-use-after-free php-src/Zend/zend_types.h:1395 in zend_gc_delref
==459788==ABORTING

(No stdout, exit code 1. Local paths in the ASan report are abbreviated to php-src/, some stack frames are omitted.)

Without the sanitizer the process aborts in every configuration I tried, with exit code 134:

  • Plain build, default allocator: zend_mm_heap corrupted on stderr.
  • Plain build, USE_ZEND_ALLOC=0: free(): double free detected in tcache 2 on stderr. In gdb the failing free() is pefree(current, ...) in zend_llist_destroy() (Zend/zend_llist.c:111).
  • ASan build, default allocator: zend_mm_heap corrupted.

But I expected this output instead:

(no output, exit code 0)

The script should just shut down. The same script with the unregister_tick_function() call removed from __destruct() prints nothing and exits with code 0.

Root cause

  1. php_request_shutdown() runs the output callback in step 3 (main/main.c:2026), after the destructor pass of step 2 (:2021). From then on the only owner of the T object is the tick function entry.
  2. Step 5, RSHUTDOWN, destroys the list without marking it as being destroyed (ext/standard/basic_functions.c:473-477):
	if (BG(user_tick_functions)) {
		zend_llist_destroy(BG(user_tick_functions));
		efree(BG(user_tick_functions));
		BG(user_tick_functions) = NULL;
	}
  1. zend_llist_destroy() unlinks nothing up front and frees each element only after its destructor returns (Zend/zend_llist.c:106-112):
	while (current) {
		next = current->next;
		if (l->dtor) {
			l->dtor(current->data);
		}
		pefree(current, l->persistent);
		current = next;
	}
  1. The entry destructor releases the entry's object reference (user_tick_function_dtor(), basic_functions.c:1621 -> zend_fcc_dtor(), Zend/zend_API.h:840), which runs T::__destruct(). At this point BG(user_tick_functions) is still set and the element is still linked, so unregister_tick_function() finds the same entry (basic_functions.c:2339-2340) and deletes it. DEL_LLIST_ELEMENT (Zend/zend_llist.c:81-84) runs the entry destructor a second time and frees the element.
  2. Two things are now released twice:
    • The entry's object reference. Because of the second zend_fcc_dtor() the object's refcount is one too low, so it is freed as soon as __destruct() returns (OBJ_RELEASE() at Zend/zend_objects.c:185, freed at Zend/zend_objects_API.c:200). The enclosing zend_objects_store_del() then continues with the freed object; GC_DELREF(object) at :182 is the read reported above.
    • The list element, by the enclosing zend_llist_destroy() at :111.

user_tick_function_compare() only refuses to unregister a tick function while it is being executed (calling flag, basic_functions.c:1655-1657). Nothing covers the destruction at RSHUTDOWN.

PHP Version

PHP 8.5.13

Operating System

Ubuntu 24.04.5 LTS, Linux 7.0.0-38-generic x86_64

Activity

  1. Girgias commented on Nov 23, 2026

    @Girgias
    Member

    Seems related to #20001

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