Skip to content

UAF write in _zend_hash_add_or_update_i() via php_auto_globals_create_server() and reentrant $GLOBALS writes #25921

Backlog

Description

@xuziqiang98

Description

With auto_globals_jit=1, _SERVER is armed but not created. A script can write $GLOBALS['_SERVER'] = [new U] through ZEND_FETCH_GLOBAL without triggering the auto-global. A later filter_input(INPUT_SERVER, ...) calls php_auto_globals_create_server(), which does zend_hash_update(&EG(symbol_table), "_SERVER", A). _zend_hash_add_or_update_i() caches data = &p->val, then calls the bucket destructor on the old value U; U::__destruct() adds 300 globals, resizing and freeing symbol_table->arData. Back in _zend_hash_add_or_update_i(), the new value is written through the stale data pointer, producing a heap-use-after-free WRITE at Zend/zend_hash.c:1034. FPM is required because the CLI SAPI initializes _SERVER early.

Root cause

  • $GLOBALS['_SERVER'] = [new U] compiles to ZEND_FETCH_GLOBAL + assign; it creates a direct symbol-table slot and does not run the _SERVER auto-global callback.
  • filter_input(INPUT_SERVER, ...) calls php_filter_get_storage(), which calls zend_is_auto_global(_SERVER) and triggers php_auto_globals_create_server() (main/php_variables.c:900).
  • The callback builds the new _SERVER array A and calls zend_hash_update(&EG(symbol_table), name, &A) (main/php_variables.c:926).
  • _zend_hash_add_or_update_i() finds the existing _SERVER bucket, caches data = &p->val, and invokes ht->pDestructor(data) on the old value U (Zend/zend_hash.c:1034 region). Destroying U runs U::__destruct(), which writes many $GLOBALS['pad*'] entries and forces zend_hash_do_resize(); the resize pefree()s the old arData (Zend/zend_hash.c:1553).
  • When the destructor returns, the update path continues and stores A through the cached data pointer, which now points into the freed bucket array. The ASan report is a WRITE of size 8 at Zend/zend_hash.c:1034, with the free stack in zend_hash_do_fresize()/_zend_hash_add_or_update_i().

Reproducer

trigger.php

<?php
/* Cluster 78: $GLOBALS['_SERVER'] is written through ZEND_FETCH_GLOBAL and
 * does not initialize the armed _SERVER auto-global.  filter_input() then
 * runs php_auto_globals_create_server -> zend_hash_update(symbol_table,
 * "_SERVER") which destroys the user array first; the array's U::__destruct
 * inserts many globals, resizing/freeing symbol_table->arData while
 * _zend_hash_add_or_update_i still holds data=&p->val. */
class U {
    public function __destruct() {
        for ($i = 0; $i < 300; $i++) {
            $GLOBALS["pad$i"] = 1;
        }
    }
}
$GLOBALS['_SERVER'] = [new U];
var_dump(filter_input(INPUT_SERVER, 'NO_SUCH_KEY'));

fpm.conf

[global]
error_log = /tmp/php-uaf-fpm.log
daemonize = no
log_level = notice

[www]
listen = 127.0.0.1:9202
pm = static
pm.max_children = 1
catch_workers_output = yes

Command

USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 \
  php-fpm -F -y fpm.conf &
# send trigger.php to 127.0.0.1:9202 with a FastCGI client

The original harness used a small fcgi_client.py helper; any FastCGI client that executes the script in a PHP-FPM worker is sufficient. The worker's stderr contains the ASan report (with catch_workers_output=yes).

ASan report

Full ASan report
==2302081==ERROR: AddressSanitizer: heap-use-after-free on address 0x51e00000db00 at pc 0x5da71ded4c1b bp 0x7ffc241677d0 sp 0x7ffc241677c0
WRITE of size 8 at 0x51e00000db00 thread T0
    #0 0x5da71ded4c1a in _zend_hash_add_or_update_i php-8.6.0/Zend/zend_hash.c:1034
    #1 0x5da71ded4c1a in zend_hash_update php-8.6.0/Zend/zend_hash.c:1165
    #2 0x5da71dabe33a in php_auto_globals_create_server php-8.6.0/main/php_variables.c:926
    #3 0x5da71dca1c72 in zend_auto_global_check php-8.6.0/Zend/zend_compile.c:2012
    #4 0x5da71dca1c72 in zend_is_auto_global php-8.6.0/Zend/zend_compile.c:2025
    #5 0x5da71d1e8fe2 in php_filter_get_storage php-8.6.0/ext/filter/filter.c:462
    #6 0x5da71d1ea117 in zif_filter_input php-8.6.0/ext/filter/filter.c:692
    #7 0x5da71de7bedc in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1393
    #8 0x5da71de7bedc in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110877
    #9 0x5da71de72a04 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #10 0x5da71dfdf5e1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #11 0x5da71da9d9f6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #12 0x5da71da9deb4 in php_execute_script php-8.6.0/main/main.c:2668
    #13 0x5da71dffeb8a in main php-8.6.0/sapi/fpm/fpm/fpm_main.c:1877
    #14 0x72c160e2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #15 0x72c160e2a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #16 0x5da71d031b84 in _start (php-fpm+0x431b84) (BuildId: 80a45556894f5194902c5ae4a2f3803c1d4451f1)
0x51e00000db00 is located 640 bytes inside of 2560-byte region [0x51e00000d880,0x51e00000e280)
freed by thread T0 here:
    #0 0x72c1612fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x5da71dc040a5 in __zend_free php-8.6.0/Zend/zend_alloc.c:4100
    #2 0x5da71dc13a9e in _efree php-8.6.0/Zend/zend_alloc.c:3052
    #3 0x5da71ded3e46 in zend_hash_do_fresize php-8.6.0/Zend/zend_hash.c:1553
    #4 0x5da71ded5df5 in _zend_hash_add_or_update_i php-8.6.0/Zend/zend_hash.c:1039
    #5 0x5da71ded5df5 in zend_hash_add_new php-8.6.0/Zend/zend_hash.c:1180
    #6 0x5da71dd7657b in zend_fetch_var_address_helper_SPEC_TMP_UNUSED php-8.6.0/Zend/zend_vm_execute.h:21496
    #7 0x5da71dd76c1a in ZEND_FETCH_W_SPEC_TMP_UNUSED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:21562
    #8 0x5da71de98df1 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:113370
    #9 0x5da71dd04682 in zend_call_function php-8.6.0/Zend/zend_execute_API.c:1082
    #10 0x5da71dd05ee1 in zend_call_known_function_ex php-8.6.0/Zend/zend_execute_API.c:1187
    #11 0x5da71df7ff12 in zend_call_known_function php-8.6.0/Zend/zend_API.h:930
    #12 0x5da71df7ff12 in zend_call_known_instance_method php-8.6.0/Zend/zend_API.h:958
    #13 0x5da71df7ff12 in zend_call_known_instance_method_with_0_params php-8.6.0/Zend/zend_API.h:964
    #14 0x5da71df7ff12 in zend_objects_destroy_object php-8.6.0/Zend/zend_objects.c:171
    #15 0x5da71df7edba in zend_objects_store_del php-8.6.0/Zend/zend_objects_API.c:178
    #16 0x5da71dfc80d5 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #17 0x5da71dee3c82 in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #18 0x5da71dee3c82 in zend_array_destroy php-8.6.0/Zend/zend_hash.c:2065
    #19 0x5da71dfc80d5 in rc_dtor_func php-8.6.0/Zend/zend_variables.c:56
    #20 0x5da71dfc839d in i_zval_ptr_dtor php-8.6.0/Zend/zend_variables.h:44
    #21 0x5da71dfc839d in zval_ptr_dtor php-8.6.0/Zend/zend_variables.c:83
    #22 0x5da71ded4713 in _zend_hash_add_or_update_i php-8.6.0/Zend/zend_hash.c:1032
    #23 0x5da71ded4713 in zend_hash_update php-8.6.0/Zend/zend_hash.c:1165
    #24 0x5da71dabe33a in php_auto_globals_create_server php-8.6.0/main/php_variables.c:926
    #25 0x5da71dca1c72 in zend_auto_global_check php-8.6.0/Zend/zend_compile.c:2012
    #26 0x5da71dca1c72 in zend_is_auto_global php-8.6.0/Zend/zend_compile.c:2025
    #27 0x5da71d1e8fe2 in php_filter_get_storage php-8.6.0/ext/filter/filter.c:462
    #28 0x5da71d1ea117 in zif_filter_input php-8.6.0/ext/filter/filter.c:692
    #29 0x5da71de7bedc in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1393
    #30 0x5da71de7bedc in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110877
    #31 0x5da71de72a04 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #32 0x5da71dfdf5e1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #33 0x5da71da9d9f6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #34 0x5da71da9deb4 in php_execute_script php-8.6.0/main/main.c:2668
    #35 0x5da71dffeb8a in main php-8.6.0/sapi/fpm/fpm/fpm_main.c:1877
    #36 0x72c160e2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #37 0x72c160e2a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #38 0x5da71d031b84 in _start (php-fpm+0x431b84) (BuildId: 80a45556894f5194902c5ae4a2f3803c1d4451f1)
previously allocated by thread T0 here:
    #0 0x72c1612fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x5da71dc05428 in __zend_malloc php-8.6.0/Zend/zend_alloc.c:4071
    #2 0x5da71dc137f7 in _emalloc php-8.6.0/Zend/zend_alloc.c:3042
    #3 0x5da71decfdd8 in zend_hash_real_finit_mixed_ex php-8.6.0/Zend/zend_hash.c:243
    #4 0x5da71decfdd8 in zend_hash_real_finit_mixed php-8.6.0/Zend/zend_hash.c:455
    #5 0x5da71ded4805 in _zend_hash_add_or_update_i php-8.6.0/Zend/zend_hash.c:990
    #6 0x5da71ded4805 in zend_hash_update php-8.6.0/Zend/zend_hash.c:1165
    #7 0x5da71dab7613 in php_auto_globals_create_get php-8.6.0/main/php_variables.c:832
    #8 0x5da71dca2c94 in zend_activate_auto_globals php-8.6.0/Zend/zend_compile.c:2054
    #9 0x5da71dabed4d in php_hash_environment php-8.6.0/main/php_variables.c:818
    #10 0x5da71da9a3d0 in php_request_startup php-8.6.0/main/main.c:1916
    #11 0x5da71dffea7a in main php-8.6.0/sapi/fpm/fpm/fpm_main.c:1816
    #12 0x72c160e2a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #13 0x72c160e2a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #14 0x5da71d031b84 in _start (php-fpm+0x431b84) (BuildId: 80a45556894f5194902c5ae4a2f3803c1d4451f1)
SUMMARY: AddressSanitizer: heap-use-after-free php-8.6.0/Zend/zend_hash.c:1034 in _zend_hash_add_or_update_i

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