Skip to content

UAF in zend_generator_dtor_storage() when a delegated value's destructor re-enters Generator::next() #25923

Duplicate

Description

@xuziqiang98

Description

A generator is parked on yield from [U, 1]. After current() and one next(), U is held only by generator->values. Dropping the generator runs zend_generator_dtor_storage(), which caches ex = generator->execute_data and then destroys generator->values. U::__destruct() retrieves the generator through a WeakReference and calls Generator::next(). The nested resume reaches the delegated-value failure path, closes and frees the generator's execute_data; when the nested call returns, zend_generator_dtor_storage() dereferences the cached ex and reads ex->func from freed memory. ASan reports a heap-use-after-free READ at Zend/zend_generators.c:280; the free stack is zend_generator_close() at Zend/zend_generators.c:169 from ZEND_GENERATOR_RETURN.

Root cause

  • zend_generator_dtor_storage() (Zend/zend_generators.c:280) stores the current execute_data in a local ex before destroying generator->values.
  • generator->values holds the delegated array from yield from; destroying it runs the destructor of U.
  • U::__destruct() uses WeakReference::get() to obtain the still-live generator (weakref cleanup happens at the end of free_storage) and calls next().
  • Because the generator is not marked as running and the delegated values are still present, zend_generator_resume() enters get_next_delegated_value(); with the array exhausted it takes the failure path, calls GENERATOR_RETURN, and zend_generator_close() frees execute_data (Zend/zend_generators.c:169).
  • The nested call returns to zend_generator_dtor_storage(), which still uses the cached ex (reading ex->func at :280; the finally path also writes ex->opline), producing a UAF read and potentially a UAF write.

Reproducer

trigger.php

<?php
/* Cluster 82: generator parked on `yield from [U,1]`.  After current() and
 * one next(), U is held only by generator->values.  Dropping the generator
 * runs zend_generator_dtor_storage: it caches ex, destroys values, U's
 * __destruct re-enters Generator::next() via WeakReference, resume reaches
 * the delegated-value failure path and closes/frees execute_data, then
 * dtor_storage dereferences the stale ex. */
class U {
    public $wr;
    public function __destruct() {
        $g = $this->wr->get();
        if ($g) {
            try {
                $g->next();
            } catch (Throwable $e) {
            }
        }
    }
}
function gen() {
    yield from [new U, 1];
}
$g = gen();
$u = $g->current();
$u->wr = WeakReference::create($g);
$g->next();
unset($u);
unset($g);

Command

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

ASan report

Full ASan report
==2257346==ERROR: AddressSanitizer: heap-use-after-free on address 0x50d000005b58 at pc 0x5c2f21eca006 bp 0x7ffc642d1b70 sp 0x7ffc642d1b60
READ of size 8 at 0x50d000005b58 thread T0
    #0 0x5c2f21eca005 in zend_generator_dtor_storage php-8.6.0/Zend/zend_generators.c:280
    #1 0x5c2f21f7e8bc in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #2 0x5c2f21fc7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #3 0x5c2f21d3270b in ZEND_UNSET_CV_SPEC_CV_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:49792
    #4 0x5c2f21eac982 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:115689
    #5 0x5c2f21e727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #6 0x5c2f21fdf3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #7 0x5c2f21a9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #8 0x5c2f21a9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #9 0x5c2f21fe2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #10 0x5c2f21fe59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #11 0x5c2f21fe074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #12 0x7049c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #13 0x7049c722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #14 0x5c2f21031944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

0x50d000005b58 is located 24 bytes inside of 144-byte region [0x50d000005b40,0x50d000005bd0)
freed by thread T0 here:
    #0 0x7049c78fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x5c2f21c03e65 in __zend_free php-8.6.0/Zend/zend_alloc.c:4100
    #2 0x5c2f21c1385e in _efree php-8.6.0/Zend/zend_alloc.c:3052
    #3 0x5c2f21ec57a5 in zend_generator_close php-8.6.0/Zend/zend_generators.c:169
    #4 0x5c2f21d63a2f in ZEND_GENERATOR_RETURN_SPEC_CONST_HANDLER php-8.6.0/Zend/zend_vm_execute.h:5101
    #5 0x5c2f21e84005 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:111346
    #6 0x5c2f21ec76c7 in zend_generator_resume php-8.6.0/Zend/zend_generators.c:849
    #7 0x5c2f21ecaa68 in zim_Generator_next php-8.6.0/Zend/zend_generators.c:1004
    #8 0x5c2f21e7ebee in ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:2017
    #9 0x5c2f21e7ebee in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110917
    #10 0x5c2f21d04442 in zend_call_function php-8.6.0/Zend/zend_execute_API.c:1082
    #11 0x5c2f21d05ca1 in zend_call_known_function_ex php-8.6.0/Zend/zend_execute_API.c:1187
    #12 0x5c2f21f7fcd2 in zend_call_known_function php-8.6.0/Zend/zend_API.h:930
    #13 0x5c2f21f7fcd2 in zend_call_known_instance_method php-8.6.0/Zend/zend_API.h:958
    #14 0x5c2f21f7fcd2 in zend_call_known_instance_method_with_0_params php-8.6.0/Zend/zend_API.h:964
    #15 0x5c2f21f7fcd2 in zend_objects_destroy_object php-8.6.0/Zend/zend_objects.c:171
    #16 0x5c2f21f7eb7a in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #17 0x5c2f21fc7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #18 0x5c2f21ee3a42 in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #19 0x5c2f21ee3a42 in zend_array_destroy php-8.6.0/Zend/zend_hash.c:2065
    #20 0x5c2f21fc7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #21 0x5c2f21fc815d in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #22 0x5c2f21fc815d in zval_ptr_dtor php-8.6.0/Zend/zend_variables.c:83
    #23 0x5c2f21ec9dc2 in zend_generator_dtor_storage php-8.6.0/Zend/zend_generators.c:266
    #24 0x5c2f21f7e8bc in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #25 0x5c2f21fc7e95 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #26 0x5c2f21d3270b in ZEND_UNSET_CV_SPEC_CV_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:49792
    #27 0x5c2f21eac982 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:115689
    #28 0x5c2f21e727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #29 0x5c2f21fdf3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #30 0x5c2f21a9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #31 0x5c2f21a9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #32 0x5c2f21fe2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #33 0x5c2f21fe59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #34 0x5c2f21fe074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #35 0x7049c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

previously allocated by thread T0 here:
    #0 0x7049c78fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x5c2f21c051e8 in __zend_malloc php-8.6.0/Zend/zend_alloc.c:4071
    #2 0x5c2f21c135b7 in _emalloc php-8.6.0/Zend/zend_alloc.c:3042
    #3 0x5c2f21e6d584 in ZEND_GENERATOR_CREATE_SPEC_HANDLER php-8.6.0/Zend/zend_vm_execute.h:2364
    #4 0x5c2f21e80087 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110932
    #5 0x5c2f21e727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #6 0x5c2f21fdf3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #7 0x5c2f21a9d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #8 0x5c2f21a9dc74 in php_execute_script php-8.6.0/main/main.c:2668
    #9 0x5c2f21fe2f2f in do_cli php-8.6.0/sapi/cli/php_cli.c:944
    #10 0x5c2f21fe59c9 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1361
    #11 0x5c2f21fe074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #12 0x7049c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #13 0x7049c722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #14 0x5c2f21031944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

SUMMARY: AddressSanitizer: heap-use-after-free php-8.6.0/Zend/zend_generators.c:280 in zend_generator_dtor_storage

PHP Version

PHP 8.6.0

Operating System

Ubuntu 24.04

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