Skip to content

Cross-request UAF read in opcache_get_configuration() via dangling JIT_G(options) after runtime ini_set('opcache.jit') #25926

Backlog

Description

@xuziqiang98

Description

Request 1 calls ini_set('opcache.jit', <runtime string>). OnUpdateJit() calls zend_jit_config(), which sets JIT_G(enabled)=0 and then OnUpdateString() stores the request-allocated string in JIT_G(options). At request shutdown, zend_ini_deactivate() restores opcache.jit; OnUpdateJit(orig_value, DEACTIVATE) calls zend_jit_config(), which returns FAILURE early because JIT_G(enabled) is now 0, so OnUpdateString() is skipped. zend_restore_ini_entry_cb() nevertheless releases ini_entry->value and frees the request string, leaving JIT_G(options) dangling. Request 2 in the same worker calls opcache_get_configuration(), which passes JIT_G(options) to add_assoc_string(); strlen() reads the freed buffer. ASan reports a heap-use-after-free READ at ext/opcache/zend_accelerator_module.c:883.

Root cause

  • opcache.jit is PHP_INI_ALL (ext/opcache/zend_accelerator_module.c:354), so ini_set() can change it at runtime. OnUpdateJit() (:211) calls zend_jit_config() and, on success, OnUpdateString() stores the new string's raw bytes in JIT_G(options).
  • Request 1 sets a runtime-built "disable" value: zend_jit_config() accepts it, sets JIT_G(enabled)=0/JIT_G(on)=0, and returns SUCCESS; OnUpdateString() stores the request string.
  • At request shutdown, zend_ini_deactivate() iterates EG(modified_ini_directives) and calls OnUpdateJit(orig_value, DEACTIVATE). zend_jit_config() returns FAILURE at the !JIT_G(enabled) early return (ext/opcache/jit/zend_jit.c:3688), so OnUpdateJit() does not call OnUpdateString().
  • zend_restore_ini_entry_cb() does not return early on the DEACTIVATE failure; it releases ini_entry->value (Zend/zend_ini.c:68) and installs the original value. JIT_G(options) still points at the freed request string.
  • Request 2 calls opcache_get_configuration(); add_assoc_string(&directives, "opcache.jit", JIT_G(options)) (ext/opcache/zend_accelerator_module.c:883) computes strlen() on the freed buffer → UAF read (ASan free stack: zend_restore_ini_entry_cb() → zend_ini_deactivate()).

Reproducer

router.php

<?php
/* Cluster 112: request 1 sets opcache.jit=disable at runtime, so OnUpdateJit
 * sets JIT_G(enabled)=0 and stores the request-allocated "disable" bytes in
 * JIT_G(options).  At DEACTIVATE the restore calls OnUpdateJit(orig_value)
 * which fails early because enabled==0, but zend_restore_ini_entry_cb still
 * releases ini_entry->value; JIT_G(options) is left dangling.  Request 2
 * reads it through opcache_get_configuration().  The value is built at
 * runtime so it is a heap zend_string, not an interned literal. */
$uri = $_SERVER['REQUEST_URI'];
if ($uri === '/1') {
    $v = implode('', ['dis', 'able']);
    $old = ini_set('opcache.jit', $v);
    echo "req1 old=" . var_export($old, true) . "\n";
} elseif ($uri === '/2') {
    $cfg = opcache_get_configuration();
    echo "req2 opcache.jit=" . $cfg['directives']['opcache.jit'] . "\n";
} else {
    echo "usage\n";
}

Command

USE_ZEND_ALLOC=0 ASAN_OPTIONS=detect_leaks=0 \
  php -n -d opcache.enable_cli=1 -d opcache.jit=tracing \
         -d opcache.jit_buffer_size=64M -S 127.0.0.1:9301 router.php &
curl -s http://127.0.0.1:9301/1
curl -s http://127.0.0.1:9301/2

Request 1 builds "disable" at runtime (implode('', ['dis','able'])) so the string is request-allocated, not an interned literal; request 2 calls opcache_get_configuration() on the same process.

ASan report

Full ASan report
==2322499==ERROR: AddressSanitizer: heap-use-after-free on address 0x503000020ed8 at pc 0x7845c787d96f bp 0x7ffde08d5890 sp 0x7ffde08d5038
READ of size 8 at 0x503000020ed8 thread T0
    #0 0x7845c787d96e in strlen ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:391
    #1 0x5892a8424efb in add_assoc_string_ex php-8.6.0/Zend/zend_API.c:2069
    #2 0x5892a7bf4c02 in add_assoc_string php-8.6.0/Zend/zend_API.h:654
    #3 0x5892a7bf4c02 in zif_opcache_get_configuration php-8.6.0/ext/opcache/zend_accelerator_module.c:883
    #4 0x5892a867bc9c in ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER php-8.6.0/Zend/zend_vm_execute.h:1393
    #5 0x5892a867bc9c in execute_ex php-8.6.0/Zend/zend_vm_execute.h:110877
    #6 0x5892a86727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #7 0x5892a87df3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #8 0x5892a829d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #9 0x5892a87f1c96 in php_cli_server_dispatch_router php-8.6.0/sapi/cli/php_cli_server.c:2334
    #10 0x5892a87f75c3 in php_cli_server_dispatch php-8.6.0/sapi/cli/php_cli_server.c:2376
    #11 0x5892a87f75c3 in php_cli_server_recv_event_read_request php-8.6.0/sapi/cli/php_cli_server.c:2723
    #12 0x5892a87f3142 in php_cli_server_do_event_for_each_fd_callback php-8.6.0/sapi/cli/php_cli_server.c:2810
    #13 0x5892a87fa5f8 in php_cli_server_poller_iter_on_active php-8.6.0/sapi/cli/php_cli_server.c:918
    #14 0x5892a87fa5f8 in php_cli_server_do_event_for_each_fd php-8.6.0/sapi/cli/php_cli_server.c:2830
    #15 0x5892a87fa5f8 in php_cli_server_do_event_loop php-8.6.0/sapi/cli/php_cli_server.c:2842
    #16 0x5892a87fa5f8 in do_cli_server php-8.6.0/sapi/cli/php_cli_server.c:2974
    #17 0x5892a87e5637 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1364
    #18 0x5892a87e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #19 0x7845c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #20 0x7845c722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #21 0x5892a7831944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

0x503000020ed8 is located 24 bytes inside of 32-byte region [0x503000020ec0,0x503000020ee0)
freed by thread T0 here:
    #0 0x7845c78fc4d8 in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x5892a8403e65 in __zend_free php-8.6.0/Zend/zend_alloc.c:4100
    #2 0x5892a841385e in _efree php-8.6.0/Zend/zend_alloc.c:3052
    #3 0x5892a8724f05 in zend_string_release php-8.6.0/Zend/zend_string.h:484
    #4 0x5892a8724f05 in zend_restore_ini_entry_cb php-8.6.0/Zend/zend_ini.c:68
    #5 0x5892a872717c in zend_ini_deactivate php-8.6.0/Zend/zend_ini.c:135
    #6 0x5892a87dfe7c in zend_deactivate php-8.6.0/Zend/zend.c:1370
    #7 0x5892a829abac in php_request_shutdown php-8.6.0/main/main.c:2006
    #8 0x5892a87f4d35 in php_cli_server_request_shutdown php-8.6.0/sapi/cli/php_cli_server.c:2303
    #9 0x5892a87f8113 in php_cli_server_dispatch php-8.6.0/sapi/cli/php_cli_server.c:2377
    #10 0x5892a87f8113 in php_cli_server_recv_event_read_request php-8.6.0/sapi/cli/php_cli_server.c:2723
    #11 0x5892a87f3142 in php_cli_server_do_event_for_each_fd_callback php-8.6.0/sapi/cli/php_cli_server.c:2810
    #12 0x5892a87fa5f8 in php_cli_server_poller_iter_on_active php-8.6.0/sapi/cli/php_cli_server.c:918
    #13 0x5892a87fa5f8 in php_cli_server_do_event_for_each_fd php-8.6.0/sapi/cli/php_cli_server.c:2830
    #14 0x5892a87fa5f8 in php_cli_server_do_event_loop php-8.6.0/sapi/cli/php_cli_server.c:2842
    #15 0x5892a87fa5f8 in do_cli_server php-8.6.0/sapi/cli/php_cli_server.c:2974
    #16 0x5892a87e5637 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1364
    #17 0x5892a87e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #18 0x7845c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #19 0x7845c722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #20 0x5892a7831944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

previously allocated by thread T0 here:
    #0 0x7845c78fd9c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x5892a84051e8 in __zend_malloc php-8.6.0/Zend/zend_alloc.c:4071
    #2 0x5892a84135b7 in _emalloc php-8.6.0/Zend/zend_alloc.c:3042
    #3 0x5892a84158fa in _safe_emalloc php-8.6.0/Zend/zend_alloc.c:3100
    #4 0x5892a841620a in _safe_femalloc php-8.6.0/Zend/zend_alloc.c:3222
    #5 0x5892a81ba922 in zend_string_safe_falloc php-8.6.0/Zend/zend_string.h:212
    #6 0x5892a81ba922 in php_implode php-8.6.0/ext/standard/string.c:1064
    #7 0x5892a81bb6fe in zflf_implode_2 php-8.6.0/ext/standard/string.c:1187
    #8 0x5892a865a2e6 in ZEND_FRAMELESS_ICALL_2_SPEC_HANDLER php-8.6.0/Zend/zend_vm_execute.h:3909
    #9 0x5892a8680786 in execute_ex php-8.6.0/Zend/zend_vm_execute.h:111052
    #10 0x5892a86727c4 in zend_execute php-8.6.0/Zend/zend_vm_execute.h:116091
    #11 0x5892a87df3a1 in zend_execute_script php-8.6.0/Zend/zend.c:1990
    #12 0x5892a829d7b6 in php_execute_script_ex php-8.6.0/main/main.c:2628
    #13 0x5892a87f1c96 in php_cli_server_dispatch_router php-8.6.0/sapi/cli/php_cli_server.c:2334
    #14 0x5892a87f75c3 in php_cli_server_dispatch php-8.6.0/sapi/cli/php_cli_server.c:2376
    #15 0x5892a87f75c3 in php_cli_server_recv_event_read_request php-8.6.0/sapi/cli/php_cli_server.c:2723
    #16 0x5892a87f3142 in php_cli_server_do_event_for_each_fd_callback php-8.6.0/sapi/cli/php_cli_server.c:2810
    #17 0x5892a87fa5f8 in php_cli_server_poller_iter_on_active php-8.6.0/sapi/cli/php_cli_server.c:918
    #18 0x5892a87fa5f8 in php_cli_server_do_event_for_each_fd php-8.6.0/sapi/cli/php_cli_server.c:2830
    #19 0x5892a87fa5f8 in php_cli_server_do_event_loop php-8.6.0/sapi/cli/php_cli_server.c:2842
    #20 0x5892a87fa5f8 in do_cli_server php-8.6.0/sapi/cli/php_cli_server.c:2974
    #21 0x5892a87e5637 in do_php_cli php-8.6.0/sapi/cli/php_cli.c:1364
    #22 0x5892a87e074e in main php-8.6.0/sapi/cli/php_cli_main.c:18
    #23 0x7845c722a1c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #24 0x7845c722a28a in __libc_start_main_impl ../csu/libc-start.c:360
    #25 0x5892a7831944 in _start (php+0x431944) (BuildId: 827a5048af1c5fe9a5adec2f30e759cf4bc249ed)

SUMMARY: AddressSanitizer: heap-use-after-free ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:391 in strlen

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