Anatomy of PHP2Shell (CVE-2026-109618 & CVE-2026-109619)
A study reference on how PHP2Shell (CVE-2026-109618 & CVE-2026-109619) actually works inside php-src, tracing both CVEs down to C structs and exploit mechanics.
Anatomy of PHP2Shell (CVE-2026-109618 & CVE-2026-109619)
When PHP2Shell became public in late October 2026, GitHub filled up overnight with fake proof-of-concept scripts promising instant remote shells. Most of them sent random oversized POST bodies and did nothing more than crash a local test container. The real vulnerability chain is much sharper, and far harder to pull off, than those scripts suggest.
This guide is written as a study reference on how PHP2Shell actually works inside php-src. Part 1 contains concepts required to understand the CVEs and the exploits: subsystem globals, persistent worker pools, setjmp/longjmp bailouts, the Pre-RINIT startup window, the Zend Memory Manager (Zend MM), and the exact byte layouts of PHP’s internal values and hash tables. Part 2 then traces both CVEs down to their C structs and walks step by step through how an attacker chains them from an unauthenticated multipart/form-data request to arbitrary code execution. Part 3 closes with the upstream patch and defensive mitigations.
What actually broke in the Zend Engine?
I have a better one: what is Zend? The name comes from the creators (Zeev + Andi = Zend) of PHP’s “new” compiler and virtual machine, released with PHP since version 4.0.
On October 21, 2026, the PHP project shipped emergency updates (8.5.12, 8.4.27, 8.3.36, and 8.2.35) for two memory-safety flaws in the Zend Engine: CVE-2026-109618 (GHSA-w8hw-w9fx-j79w) and CVE-2026-109619 (GHSA-6wmf-4rv5-p3rp). Both carry a CVSS v4.0 score of 9.2. Together, they are known as PHP2Shell.
- CVE-2026-109618 (Intra-request out-of-memory bailout corruption): When an allocation surpasses
memory_limit, PHP aborts execution with_zend_bailout()(via alongjmp). If that jump happens mid-way through inserting an entry into aHashTable(calledzend_array) or initializing a composite structure, PHP increments the table’s active element counter before populating the slot itself. During request shutdown, PHP cleanup walks every “active” slot and passes uninitialized residual heap bytes to destructors. - CVE-2026-109619 (Cross-request stale global state): When an HTTP upload triggers
ext/sessionupload progress tracking during early request startup, and then aborts before modules finish initializing, PHP wipes the per-request heap without running module shutdown callbacks. Persistent globals in the worker process, specificallyPS(id)andPS(http_session_vars), retain dangling pointers into freed memory. The next HTTP request handled by that same worker reads and destroys those pointers before normal session initialization resets them.
Neither flaw is a string-handling mistake or a userland logic bug. PHP2Shell emerges where four design choices in the PHP runtime intersect:
- per-worker global structs;
- persistent worker processes;
- error recovery via
setjmp/longjmp; - non-zeroed memory recycling in the Zend Memory Manager.
If you already know PHP Zend Engine internals well, you can jump to Part 2; otherwise, every piece introduced in Part 1 pays off directly when building the exploit chain.
Part 1: Engine internals and memory layout
Subsystem global macros
PHP is written in C and predates modern thread-safe runtime data structures. Instead of passing a context pointer into every function, the core engine, and its bundled extensions (called also modules), store state inside subsystem global structs accessed through short C macros:
| Macro | Underlying C struct | Role and fields relevant to PHP2Shell |
|---|---|---|
PS(...) | php_ps_globals | Session extension state: PS(rfc1867_enabled), PS(rfc1867_progress), PS(session_status), PS(id), PS(send_cookie), PS(mod_data), and PS(http_session_vars). |
EG(...) | zend_executor_globals | Zend VM runtime state: symbol tables, object store, current call frame, and the EG(bailout) jump buffer. |
PG(...) | php_core_globals | Core directives and lifecycle flags: PG(memory_limit), PG(max_input_vars), PG(modules_activated), and PG(http_globals)[6]. |
SG(...) | sapi_globals_struct | Web server request metadata: SG(post_max_size), SG(request_info).content_length, Content-Type, and POST reader callbacks. |
CG(...) | zend_compiler_globals | Compiler state and CG(function_table), from which disable_functions entries are removed at startup. |
Notice that these structs mix together “entities” with different lifetimes. A single struct like php_ps_globals holds long-lived php.ini settings (PS(rfc1867_enabled)) right beside temporary per-request (raw) pointers (PS(id) and PS(http_session_vars)).
In non-threaded builds (standard for example for php-fpm on Linux), each global struct sits in the worker process’s .bss segment for the entire life of the process. Many of the pointers inside those structs, however, point to short-lived buffers allocated on the temporary per-request heap. That lifetime mismatch sets a trap: if a request aborts early and wipes the per-request heap without resetting .bss pointers back to NULL or IS_UNDEF, the worker enters the next HTTP request holding dangling pointers.
Persistent web workers
Production deployments such as php-fpm, Apache mod_php, and LiteSpeed do not spawn a fresh OS process per HTTP request. Instead, a master process forks a pool of persistent worker processes.
One worker accepts one request at a time, runs the PHP lifecycle, returns the HTTP response, and loops right back to accept() for the next connection. Because the OS process does not exit() between requests, two things stay constant across sequential requests on the same worker PID:
- Process address space (ASLR and PIE): The base addresses of the
php-fpmbinary, shared libraries (libc.so), and the main Zend heap chunk (see “The Zend Memory Manager” below) remain fixed until the worker process dies or is recycled by the master. - Global
.bssstate: Any pointer left behind inphp_ps_globalsat the end of Request $N$ is still sitting there at the start of Request $N+1$.
This is the lifecycle of a Worker.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Process Starts (Worker forked)
│
▼
1. MINIT (Module Init) ──> Runs ONCE on process startup
│ (loads php.ini, registers classes)
├─ [ HTTP Request Starts ]
│ │
│ ▼
│ 2. RINIT (Request Init) ──> Runs at the START of every request
│ │ (sets up per-request buffers)
│ ▼ [ PHP Script Runs ]
│ │
│ 3. RSHUTDOWN (Request End) ──> Runs at the END of every request
│ (cleans up request memory)
├─ [ Loops back to RINIT for next request... ]
│
▼
4. MSHUTDOWN (Module Shutdown) ──> Runs ONCE when the process exits
The poor man’s exception handling: setjmp(), longjmp(), and _zend_bailout()
Because C has no try/catch mechanism, PHP/Zend recovers from fatal runtime errors and out-of-memory conditions using <setjmp.h>:
int setjmp(jmp_buf env)saves the current CPU register state intoenvand returns0.void longjmp(jmp_buf env, int val)restores those saved registers and jumps straight back to thesetjmp(env)call site, causingsetjmp()to return a second time withval(or1ifval == 0).
On x86_64 (System V ABI), jmp_buf stores the callee-saved registers (RBX, RBP, R12..R15), the stack pointer (RSP), and the instruction pointer (RIP).
Unlike a C++ exception, longjmp() never unwinds intermediate stack frames. It teleports RSP and RIP directly to the saved checkpoint, abandoning every C function currently in progress:
Call Stack During _zend_bailout() (grows downward)
+-----------------------------------+
| php_request_startup() |
| JMP_BUF __bailout |
+-----------------------------------+ <-- 2. RSP restored
| rfc1867_post_handler() | here! Frames
| -> php_register_variable() | below are
| -> (HashTable half-updated!) | abandoned
+-----------------------------------+ without
| _emalloc() -> _zend_bailout() | cleanup.
| calls LONGJMP(bailout, -1) ---- | --+
+-----------------------------------+ |
^-- 1. RSP at OOM +-> Jumps back up
PHP/Zend wraps setjmp() and longjmp() in three macros (zend_try, zend_catch, zend_end_try()) “stacked” through EG(bailout) so nested “try/catch”es can push and pop recovery points:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Zend/zend.h: How PHP implements zend_try / zend_catch */
#define zend_try \
{ \
JMP_BUF *__orig_bailout = EG(bailout); \
JMP_BUF __bailout; \
\
EG(bailout) = &__bailout; \
if (SETJMP(__bailout) == 0) {
#define zend_catch \
} else { \
EG(bailout) = __orig_bailout;
#define zend_end_try() \
} \
EG(bailout) = __orig_bailout; \
}
When PHP hits a fatal error or exceeds PG(memory_limit), it calls _zend_bailout():
1
2
3
4
5
6
7
8
9
10
11
12
/* Zend/zend.c: Non-local jump on fatal / OOM error */
ZEND_API ZEND_NORETURN void _zend_bailout(const char *filename, uint32_t lineno)
{
if (!EG(bailout)) {
zend_output_debug_string(true, "%s(%d) : Bailing out without a bailout address!", filename, lineno);
exit(-1);
}
CG(unclean_shutdown) = 1;
CG(in_compilation) = 0;
EG(current_execute_data) = NULL;
LONGJMP(*EG(bailout), FAILURE); /* Teleports RSP/RIP back to zend_try */
}
Keep this fact in mind for Part 2: any C function that calls emalloc() can vanish mid-flight. If line 1 increments an array length or attaches a pointer to a global struct, and line 2 calls emalloc() and hits memory_limit, line 3 never executes.
Request lifecycle and the Pre-RINIT window
A natural question when studying PHP2Shell is how the entire exploit chain (both the ASLR leak and the final RCE sink) can run without a single line of userland PHP code being executed, and why simple configuration mitigations can block that path. The reason is in php_request_startup() (main/main.c), which prepares the engine before PHP compiles or executes the target .php file.
The Server Application Programming Interface (SAPI) “bridges” the web server (php-fpm, Apache) and the Zend Engine. On every incoming HTTP request, the SAPI calls php_request_startup():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* main/main.c: Request startup sequence in vulnerable PHP versions */
int php_request_startup(void) {
int retval = SUCCESS;
PG(modules_activated) = 0;
zend_try {
php_output_activate();
zend_activate(); /* 1. Resets GC, compiler & executor globals */
sapi_activate(); /* 2. Checks post_max_size & selects POST handler */
php_hash_environment(); /* 3. PRE-RINIT: Parses multipart POST and
* fires upload hooks! */
/* 4. Module RINIT (including PHP_RINIT_FUNCTION(session)) runs AFTER
* POST parsing */
zend_activate_modules();
PG(modules_activated) = 1;
} zend_catch {
retval = FAILURE;
} zend_end_try();
return retval;
}
Both CVEs live in the Pre-RINIT window between
zend_activate()andzend_activate_modules().
sapi_activate() validates headers and picks a body parser
Inside sapi_activate(), sapi_read_post_data() inspects the Content-Type header.
When it sees multipart/form-data, it verifies that SG(request_info).content_length does not exceed SG(post_max_size) (8 MB by default) and registers rfc1867_post_handler as the POST handler.
Because multipart/form-data has post_reader = NULL, the body stream is not parsed inside sapi_activate() itself, but it is deferred to the next step.
php_hash_environment() parses the multipart body and triggers ext/session callbacks
php_hash_environment() allocates PHP’s six built-in superglobal arrays (i.e., 6 arrays namely $_POST, $_GET, $_COOKIE, $_SERVER, $_ENV, $_FILES, stored in PG(http_globals)[0..5]).
To populate $_POST PHP invokes sapi_handle_post(), which runs rfc1867_post_handler() (main/rfc1867.c) over the raw multipart stream.
This function is where we can trigger the OOM condition, thanks to a trick explained in Part 2.
As rfc1867_post_handler() reads form fields and file parts, it fires event hooks into php_session_rfc1867_callback() inside ext/session. All of this runs before any PHP script runs and before zend_activate_modules() calls RINIT.
PG(modules_activated) gates RSHUTDOWN during teardown
As the code snippet above shows, PG(modules_activated) is initially set at 0 and only flips to 1 after zend_activate_modules() returns. If startup fails early (i.e., modules_activated == 0), PHP doesn’t call module shutdown (RSHUTDOWN) functions for extensions whose RINIT never ran.
Specifically, in php_request_shutdown() when _zend_bailout() aborts step 3 (php_hash_environment()):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* main/main.c: Simplified shutdown sequence after a Pre-RINIT bailout */
void php_request_shutdown(void *dummy) {
/* 1. SKIPPED when PG(modules_activated) == 0
* PHP_RSHUTDOWN_FUNCTION(session) never runs to clear PS(id) or
* PS(http_session_vars)
*/
if (PG(modules_activated)) {
zend_deactivate_modules();
}
/* ... zend_deactivate() tears down executor state ... */
/* 2. Destroys $_POST, $_GET, $_COOKIE, $_SERVER, $_ENV, $_FILES */
for (int i = 0; i < NUM_TRACK_VARS; i++) {
zval_ptr_dtor(&PG(http_globals)[i]); /* -> triggers CVE-2026-109618! */
ZVAL_UNDEF(&PG(http_globals)[i]);
}
/* 3. Unconditionally wipes the entire request heap! */
shutdown_memory_manager(CG(unclean_shutdown), 0); /* -> arms CVE-2026-109619! */
}
That three-step shutdown explains how the two vulnerabilities fit together: step 1 skips ext/session’s cleanup routine, step 2 walks half-built $_POST/$_FILES tables (CVE-2026-109618), and step 3 wipes the request heap while leaving session pointers in .bss orphaned (CVE-2026-109619).
The Zend Memory Manager (Zend MM)
PHP/Zend does not call libc’s malloc() and free() for ordinary PHP variables. A single web request allocates and frees tens of thousands of strings, arrays, and objects, so PHP uses its own slab allocator split into two lifetimes:
| Allocator API | Lifetime | Cleanup mechanism |
|---|---|---|
pemalloc(..., 1) / malloc() / … | Worker-persistent | Lives until explicitly freed with pefree(..., 1) / free() or worker exit. |
emalloc() / ecalloc() / estrndup() / efree() / … | Request-scoped | Bound to the current HTTP request; wiped in bulk by shutdown_memory_manager(). |
Zend MM requests memory from the kernel via mmap() in 2 MB chunks (ZEND_MM_CHUNK_SIZE = 0x200000), aligned to a strict 2 MB virtual address boundary (meaning that the least significant 21 bits of every chunk base address are zero: 0x...00000 or 0x...200000, 0x...400000, etc.).
Each 2 MB chunk is divided into 512 pages of 4 KB (0x1000 bytes):
- Page 0 (
+0x0000..+0x0fff) is special; it holds thezend_mm_chunkheader, page allocation bitmaps (Bit = 0: The 4 KB page is free, Bit = 1: The 4 KB page is allocated), and metadata. - Pages 1..511 (
+0x1000..+0x1ff000) serve request allocations.
The first 2 MB chunk (mapped when a worker boots) is heap->main_chunk. If a large request exceeds 2 MB, Zend MM maps additional secondary 2 MB chunks and links them into a circular doubly linked list.
For fast small allocations, any request of 3072 bytes or less is rounded up to one of 30 fixed-size bins (0..29), each backed by a singly linked LIFO freelist (heap->free_slot[bin_num]):
| Bin # | Slot size | Notable PHP structures that land in this bin |
|---|---|---|
#1 | 16 B | zval (16 B), raw estrndup() buffers of 8..15 chars |
#3 | 32 B | zend_reference (32 B), single Bucket (32 B), raw estrndup() buffers of 24..31 chars |
#6 | 56 B | HashTable / zend_array (56 B), base zend_object (56 B) |
#7 | 64 B | 32-character session ID zend_string (24 B header + 32 B + NUL = 57 B) |
#11 | 128 B | Raw buffers of 113..128 B, or zend_string values of 88..103 chars |
#12 | 160 B | php_session_rfc1867_progress (144 B / 136 B), forged Step 4 object/call-frame payload (129..160 B) |
#16 | 320 B | Minimum initialized HashTable arData block (8 buckets + hash index = 288 B) |
Free lists contain unallocated or freed slots, and can be populated even before the first free is called.
When a worker starts (or when the memory manager resets), every entry in heap->free_slot[0..29] is initialized to NULL:
memset(heap->free_slot, 0, sizeof(heap->free_slot))
Then, for example, when a PHP script makes its very first 32-byte allocation (Bin 3):
- Zend MM checks
heap->free_slot[3]. Suppose it is NULL. - It takes the “slow path” (
zend_mm_alloc_small_slow), which carves off a fresh 4 KB page from the 2 MB chunk. - A 4 KB page fits 128 slots of 32 bytes (== 4096 / 32, so we have Slot 0 to Slot 127):
- It gives Slot 0 to the PHP script immediately.
- It pre-links all the remaining untouched slots in a singly linked list (Slot 1 -> Slot 2 -> … -> Slot 127 -> NULL) with head in
heap->free_slot[3]. On 64-bit architectures, the first 8 bytes (+0x00..+0x07) of each free slot hold a plain heap pointer to the next slot, while (under defaultZEND_MM_HEAP_PROTECTION = 1) the last 8 bytes hold an encoded shadow copy of that pointer.
[ Fresh 4 KB Page Carved ]
├ Slot 0 ──> Handed immediately to the caller of emalloc()
│
└ Slot 1..127 chained into heap->free_slot[3]:
heap->free_slot[M] ─> [Slot 1] ─> [Slot 2] ─> ... ─> [Slot 127] ─> NULL
To continue with the example, this could be the Main chunk layout after some allocations have been served:
2 MB Zend MM Main Chunk (Illustrative 2 MB-aligned base: 0x7f8000200000)
+---------------------------------------------------------------------------+
| Page 0 (+0x0000): zend_mm_chunk Header & Page Allocation Bitmaps |
+---------------------------------------------------------------------------+
| Page 1 (+0x1000): 4 KB Run of 160-Byte Slots (Bin #12) |
| +-----------------------+-----------------------+-----------------------+ |
| | Slot 0 (+0x1000) | Slot 1 (+0x10a0) | Slot 2 (+0x1140) ... | |
| +-----------------------+-----------------------+-----------------------+ |
+---------------------------------------------------------------------------+
| Page 2 (+0x2000): 4 KB Run of 32-Byte Slots (Bin #3: zend_reference, ...) |
+---------------------------------------------------------------------------+
| Pages 3 .. 511: Remaining 4 KB Pages |
+---------------------------------------------------------------------------+
Three Zend MM properties are interesting for the PHP2Shell exploit.
main_chunk survives request shutdown, secondary chunks do not
At the end of every request, shutdown_memory_manager() calls munmap() on every secondary 2 MB chunk allocated during that request, returning them to the OS.
However, it keeps heap->main_chunk permanently mapped at the exact same virtual address (to avoid mmap() syscall overhead on the next request) resetting only its page bitmaps and bin heads.
Any pointer into a secondary chunk becomes unmapped memory on Request $N+1$, but any pointer into heap->main_chunk remains valid, mapped, and immediately reusable.
2 MB chunk alignment turns any leaked main_chunk pointer into the exact heap base
Because heap->main_chunk is aligned to 0x200000 (2 MB), leaking any pointer inside main_chunk yields the chunk base with a single bitwise mask: chunk_base = leaked_ptr & ~0x1fffffUL.
Once chunk_base is known, Page 1 (chunk_base + 0x1000), Page 2 (chunk_base + 0x2000), and every fresh bin run is at a deterministic offset.
Small-bin allocations are LIFO and non-zeroed.
Calling efree(ptr) writes a plain next_free_slot pointer into bytes 0x00..0x07 and (with default ZEND_MM_HEAP_PROTECTION = 1, which raises the minimum usable 64-bit bin to 16 B) an encoded shadow pointer into the last 8 bytes (bin_size - 8 .. bin_size - 1), leaving all middle bytes 0x08 .. bin_size - 9 untouched.
Calling emalloc() pops that slot without zeroing it. Any field left unwritten before an OOM bailout still holds whatever pointers or integers lived in that slot previously.
Core Zend data structures
To understand Part 2, you need five C structures from Zend/zend_types.h. Every size and hex offset below is for x86_64 PHP 8.2–8.5 builds.
zval (16 bytes ⇒ bin #1) and zend_refcounted_h (8 bytes)
Every PHP variable, array element, or parameter is stored in a 16-byte zval:
16-Byte zval (sizeof = 0x10, Bin #1)
+----------------------------------+---------------------+--------------------+
| +0x00: zend_value value (8B) | +0x08: u1.type_info | +0x0c: u2.extra |
| (int64, double, or heap pointer) | (4B type + flags) | (4B next/num_args) |
+----------------------------------+---------------------+--------------------+
Bytes +0x00..+0x07 hold an 8-byte union (lval, dval, str, arr, obj, ref, res, counted, ast, zv, ptr, ce, func, and ww ).
Bytes +0x08..+0x0b (u1.type_info) combine a 1-byte base type in the lowest byte (IS_UNDEF = 0, IS_NULL = 1, IS_STRING = 6, IS_ARRAY = 7, IS_OBJECT = 8, IS_REFERENCE = 10) with capability flags in byte 1 (IS_TYPE_REFCOUNTED = 1 << 0, IS_TYPE_COLLECTABLE = 1 << 1). For example:
- An
IS_OBJECTzvalhasu1.type_info = IS_OBJECT_EX = 0x0308(8 | (0x03 << 8)). - An
IS_REFERENCEzval(e.g.,$a = &$b) hasu1.type_info = IS_REFERENCE_EX = 0x010a(10 | (0x01 << 8)).
A 16-byte zval is just a small container:
- For simple types (
int,float,bool,null), the value fits right inside thezval’s first 8 bytes. No extra heap allocation is needed, and nothing is reference-counted. - For complex types (
zend_string,zend_array,zend_object,zend_reference), the data is too big to fit in 8 bytes. So thezval’s first 8 bytes store a pointer to a separate structure on the heap.
Note that
IS_STRINGis something relevant for the exploit. Azvalof that type encapsulates a pointer to azend_string.
Because multiple zvals can point to the same heap structure (for example, $a = $b), PHP needs to know how many zvals are currently pointing to that heap structure so it knows when it is safe to efree() it.
To do that uniformly, every heap-allocated Zend structure starts with the exact same 8-byte header at offset +0x00 (named zend_refcounted_h gc in the C structs):
zend_refcounted_h gc (First 8 bytes of every heap-allocated Zend struct)
+-------------------------------+-------------------------------+
| +0x00: uint32_t refcount (4B) | +0x04: uint32_t type_info (4B)|
+-------------------------------+-------------------------------+
This header is always included in the structures used for complex types (see below).
zend_string (24-byte header)
PHP represents userland strings on the heap with zend_string, which prepends a 24-byte (0x18) header ahead of the character array:
1
2
3
4
5
6
7
8
9
10
zend_string on the Heap (24B header + len + 1B NUL)
+-----------------------------------------------+
| +0x00: gc (8B) |
+-----------------------------------------------+
| +0x08: zend_ulong h (cached string hash, 8B) |
+-----------------------------------------------+
| +0x10: size_t len (byte length, 8B) |
+-----------------------------------------------+
| +0x18: char val[0..len] (controlled bytes) |
+-----------------------------------------------+
Because the engine populates gc (+0x00..+0x07), the cached hash h (+0x08..+0x0f), and the byte length len (+0x10..+0x17), the actual string characters (val[]) always start at offset +0x18.
zend_reference (32 bytes, Bin #3)
When two places in PHP share a variable by reference (such as the userland $_SESSION superglobal and the session module’s internal PS(http_session_vars) slot), both point to a 32-byte zend_reference wrapper on the heap:
32-Byte zend_reference (sizeof = 0x20 => Bin #3)
+--------------------------------+---------------------+
| +0x00: gc (8B) | +0x08: val.value(8B)|
+----------------+---------------+---------------------+
| +0x10: val.u1 | +0x14: val.u2 | +0x18: sources (8B) |
+----------------+---------------+---------------------+
Notice that zend_reference includes an inner zval val spanning bytes +0x08..+0x17.
When the outer zend_reference’s gc.refcount drops to 0, PHP destroys zend_reference by calling i_zval_ptr_dtor(&ref->val) on that inner zval
HashTable (zend_array, 56 bytes, Bin #6) and Bucket (32 bytes)
Every PHP array ($_POST, $_FILES, $_SESSION) is a 56-byte HashTable (struct _zend_array) pointing to a contiguous array of 32-byte Bucket entries (ht->arData):
56-Byte HashTable (sizeof = 0x38, Bin #6)
+-----------------------+-----------------------+
| +0x00: gc (8B) | +0x08: flags/mask (8B)|
+-----------------------+-----------------------+
| +0x10: Bucket *arData | +0x18: nNumUsed (4B) |
| (8B pointer) | +0x1c: nNumOfElts (4B)|
+-----------------------+-----------------------+
| +0x20..+0x2f: nTableSize, nNextFreeElement |
+-----------------------------------------------+
| +0x30: dtor_func_t pDestructor (8B fn ptr) |
+-----------------------------------------------+
|
v ht->arData[idx]
32-Byte Bucket Entry (sizeof = 0x20)
+-----------------------------------------------+
| +0x00: zval val (16B: value + u1.type_info) |
+-----------------------+-----------------------+
| +0x10: ulong h (8B) | +0x18: *key (8B ptr) |
+-----------------------+-----------------------+
When a HashTable is first initialized (HT_MIN_SIZE = 8), ht->arData allocates 32 + (8 * 32) = 288 bytes (Bin #16: 320 B). ht->nNumUsed tracks the high-water mark of arData[0 .. nNumUsed - 1] slots that zend_array_destroy() will inspect at teardown.
Packed array (zend_array, 56 bytes, Bin #6) and zval (16 bytes)
An array built from contiguous integer keys (a[0], a[1], …) is the same 56-byte HashTable (struct _zend_array), but its +0x10 field is zval *arPacked: a flat run of 16-byte zval entries with no Bucket wrappers and only a minimal 2-slot sentinel prefix (nTableMask = HT_MIN_MASK = (uint32_t)-2):
56-Byte HashTable (sizeof = 0x38, Bin #6)
+-----------------------+-----------------------+
| +0x00: gc (8B) | +0x08: flags/mask (8B)|
+-----------------------+-----------------------+
| +0x10: zval *arPacked | +0x18: nNumUsed (4B) |
| (8B pointer) | +0x1c: nNumOfElts (4B)|
+-----------------------+-----------------------+
| +0x20..+0x2f: nTableSize, nNextFreeElement |
+-----------------------------------------------+
| +0x30: dtor_func_t pDestructor (8B fn ptr) |
+-----------------------------------------------+
|
v arPacked[idx]
16-Byte zval Entry (sizeof = 0x10)
+-----------------------+-----------------------+
| +0x00: zend_value | +0x08: u1.type_info |
| (8B: tagged | (4B: type + |
| union) | flags) |
+-----------------------+-----------------------+
| +0x0c: u2 (4B) | |
+-----------------------+-----------------------+
ht->nTableSize is still a power of two (starting at HT_MIN_SIZE = 8 and doubling as elements are inserted), and the backing buffer allocates HT_PACKED_SIZE_EX(nTableSize, HT_MIN_MASK) = 8 + (nTableSize * 16) bytes (rounded up to the Zend MM bin), with ht->arPacked pointing +0x08 bytes past that 8-byte sentinel prefix.
Each zval’s 8-byte zend_value is a tagged union: for refcounted types (IS_STRING, IS_ARRAY, IS_OBJECT, IS_RESOURCE, IS_REFERENCE) it holds a pointer to a separately allocated value; for IS_LONG / IS_DOUBLE it stores the scalar inline in the same 8 bytes.
The type byte (low byte of u1.type_info) selects the interpretation, and the IS_TYPE_REFCOUNTED flag bit (e.g. 0x06 | 0x01 << 8 = 0x0106 for a string) marks the pointer case. There is no per-element Bucket and no full hash index, only the fixed 8-byte HT_MIN_MASK prefix ahead of arPacked[0] so HT_GET_DATA_ADDR(ht) ((char *)ht->arPacked - 8) and lookup macros work uniformly.
zend_object (56 bytes, Bin #6) and destructor dispatch
For an IS_OBJECT zval (u1.type_info = 0x0308), zval.value.obj points to a 56-byte zend_object on the heap, whose handlers pointer at +0x18 points to a zend_object_handlers virtual function table (normally std_object_handlers in the PHP binary’s read-only .rodata segment):
56-Byte zend_object (sizeof = 0x38, Bin #6)
+-----------------------+-----------------------+
| +0x00: gc (8B) | +0x08: handle (4B) |
+-----------------------+-----------------------+
| +0x10: *ce (8B) | +0x18: *handlers (8B) |--\
+-----------------------+-----------------------+ |
| +0x20: HashTable *properties (8B) | |
+-----------------------------------------------+ |
| +0x28: zval properties_table[1] (16B) | |
+-----------------------------------------------+ |
|
+-----------------------------------------/
v
zend_object_handlers Table (.rodata or forged)
+-----------------------------------------------+
| +0x00: int offset |
| +0x08: free_obj(zend_object *object) |
| +0x10: dtor_obj(zend_object *object) |
+-----------------------------------------------+
Whenever PHP destroys a zval via i_zval_ptr_dtor(zval *zval_ptr), the engine runs the following chain:
- Checks
Z_REFCOUNTED_P(zval_ptr)(testing byte 1 ofu1.type_info); - Follows
ref = zval_ptr->value.countedand decrementsref->gc.refcount(--refcount); - Only if
refcountreaches0, it callsrc_dtor_func(ref); - If
refis anIS_OBJECT,rc_dtor_func()callszend_objects_store_del(obj), which temporarily bumpsobj->gc.refcountback to1(GC_ADDREF(obj)) and invokesobj->handlers->dtor_obj(obj)(at*(obj->handlers + 0x10)), passingobjin theRDIregister as the first argument.
That
dtor_obj(obj)call is the ultimate control-flow gadget in Part 2.
Part 2: Vulnerabilities and how the exploit is built
Armed with the knowledge from Part 1 we can examine each CVE separately, verify why CVE-2026-109618 is unreliable on its own, and then trace how chaining it with CVE-2026-109619 yields a repeatable exploit.
CVE-2026-109618: Out-of-memory bailout inside HashTable updates
In vulnerable PHP versions, PG(memory_limit) acted as an instant tripwire inside Zend/zend_alloc.c. Whenever zend_mm_alloc_pages() could not satisfy an emalloc() call without exceeding heap->limit, it called zend_mm_safe_error():
1
2
3
4
5
6
/* Zend/zend_alloc.c: Pre-patch OOM handling */
static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
const char *format, size_t limit, size_t size) {
heap->overflow = 1;
zend_error_noreturn(E_ERROR, format, limit, size); /* -> calls _zend_bailout()! */
}
Now look at what happens inside Zend/zend_hash.c when PHP inserts a string-keyed entry into a HashTable (for example, while php_register_variable_ex() populates $_POST or $_FILES from an incoming HTTP body):
1
2
3
4
5
6
7
8
9
10
11
12
/* Zend/zend_hash.c: Excerpt from _zend_hash_str_add_or_update_i() */
idx = ht->nNumUsed++;
ht->nNumOfElements++;
p = ht->arData + idx;
/* OOM HAZARD: ht->nNumUsed was ALREADY incremented above!
* If zend_string_init() crosses memory_limit, _zend_bailout() longjmps out
* BEFORE p->key or p->val are ever written!
*/
p->key = zend_string_init(str, len, 0);
p->h = h = ZSTR_H(p->key);
ZVAL_COPY_VALUE(&p->val, pData);
Trace the above, line by line:
idx = ht->nNumUsed++;claimsht->arData[idx]as an active bucket.p->key = zend_string_init(str, len, 0);callsemalloc()to allocate azend_stringfor the new array key.- If that
emalloc()hitsmemory_limit,_zend_bailout()firesLONGJMP(*EG(bailout), FAILURE). Execution teleports straight back tophp_request_startup()’szend_catchblock, skippingp->key = ...andZVAL_COPY_VALUE(&p->val, pData). - What sits inside
ht->arData[idx]right now? Because Zend MM never zeroes small-bin or page allocations,p->val(+0x00..+0x0f) andp->key(+0x18..+0x1f) contain raw residual bytes left over from whatever occupied that memory previously.
Immediately after php_request_startup() returns FAILURE, the SAPI worker calls php_request_shutdown(), which destroys PG(http_globals)[0..5] ($_POST, $_FILES, etc.) via zend_array_destroy():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Zend/zend_hash.c: Array destruction walks 0 .. nNumUsed - 1 */
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
{
Bucket *p, *end;
if (HT_HAS_STATIC_KEYS_ONLY(ht)) { /* ... */ }
p = ht->arData;
end = p + ht->nNumUsed;
for (; p != end; p++) {
if (Z_TYPE(p->val) != IS_UNDEF) {
/* Evaluates uninitialized residual bytes in p->val and p->key! */
if (p->key) {
zend_string_release_ex(p->key, 0);
}
i_zval_ptr_dtor(&p->val);
}
}
efree(HT_GET_DATA_ADDR(ht));
efree(ht);
}
Because ht->nNumUsed includes the half-built slot idx, zend_array_destroy() inspects p->val and p->key as if they were real Zend objects and strings:
Uninitialized Bucket ht->arData[idx] Evaluated by zend_array_destroy():
+-----------------------------------+-------------------+-------------------+
| +0x00: p->val.value (8B) | +0x08: type_info | +0x0c: u2 (4B) |
| (Residual ptr -> fake zend_object)| (e.g. 0x0308 OBJ) | |
+-----------------------------------+-------------------+-------------------+
| +0x10: p->h (8B residual hash) | +0x18: p->key (8B residual string ptr)|
+-----------------------------------+---------------------------------------+
The same partial-initialization pattern repeats in several places across php-src (allocating nested $_POST sub-arrays in php_register_variable_ex(), resizing strings, or building error backtraces).
Why CVE-2026-109618 is rarely enough by itself
On paper, CVE-2026-109618 already gives an attacker destructor dispatch (i_zval_ptr_dtor(&p->val)) when residual bytes in ht->arData[idx] carry a refcounted type_info such as IS_OBJECT_EX (0x0308).
Why not exploit CVE-2026-109618 on its own, either in a single request or across two requests on the same worker?
Try building that exploit using only CVE-2026-109618 and you hit two walls.
CVE-2026-109618 is a teardown-only sink with no info-leak channel
Forging p->val.value.obj requires knowing a valid heap address (heap->main_chunk), and forging obj->handlers->dtor_obj requires knowing the PIE base address of the PHP binary (or any dynamic library).
Because CVE-2026-109618 only triggers inside zend_array_destroy() while an OOM’d Pre-RINIT request is dying, no PHP script ever runs, no data from the uninitialized Bucket is returned in the HTTP response, and shutdown_memory_manager() wipes the HashTable immediately afterward.
You cannot use a first CVE-2026-109618 request to disclose addresses for a second request, and if you guess an address blindly, i_zval_ptr_dtor() dereferences an unmapped pointer, killing the worker with SIGSEGV and re-randomizing ASLR on the replacement child.
Even after you know ASLR (by some other means), triggering dtor_obj via zend_array_destroy() is brittle
Once you have leaked addresses, you could groom heap->main_chunk so that a subsequent CVE-2026-109618 OOM lands ht->arData[idx] on top of residual fake Bucket bytes.
However, surviving zend_array_destroy() requires aligning the entire arData allocation and ensuring both p->key (zend_string_release_ex) and p->val (i_zval_ptr_dtor) hold valid pointers or NULL/IS_UNDEF simultaneously, or teardown crashes before reaching your target destructor.
To turn an out-of-memory bailout into a reliable multi-request weapon on the same worker, you first need persistent state that survives across HTTP requests; both to read back raw heap/PIE pointers into an HTTP response (defeating ASLR) and to provide a clean, single-slot dangling pointer to hijack on the follow-up request.
That cross-request state is exactly what CVE-2026-109619 provides.
CVE-2026-109619: Cross-request dangling globals in ext/session
By default, PHP ships with session.upload_progress.enabled = 1. This feature lets a web application show an upload progress bar by writing byte counts into $_SESSION while a multipart/form-data upload is still streaming in over the network.
Because upload progress must update during upload reception, ext/session registers php_session_rfc1867_callback() (ext/session/session.c) with the SAPI multipart parser.
As we saw in Part 1, rfc1867_post_handler() invokes this callback inside php_hash_environment(), during Pre-RINIT (PG(modules_activated) == 0), before PHP_RINIT_FUNCTION(session) has run.
Upload tracking uses a 144-byte (0x90) scratch struct on PHP 8.2–8.4 (136 bytes / 0x88 on PHP 8.5, where sname_len was removed after PS(session_name) became a zend_string *; both land in Zend MM Bin #12: 160 bytes) stored in PS(rfc1867_progress), alongside the main session globals PS(session_status), PS(id), and PS(http_session_vars):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* ext/session/session.c (PHP 8.2–8.4):
* Real 144-byte (0x90) upload progress struct -> Bin #12 (160B)
* (On PHP 8.5, sname_len is removed, shifting offsets by -0x08 -> sizeof = 0x88) */
typedef struct _php_session_rfc1867_progress {
size_t sname_len; /* +0x00 (8B: strlen(PS(session_name)), PHP 8.2–8.4) */
zval sid; /* +0x08 (16B: captured PHPSESSID string) */
smart_str key; /* +0x18 (16B: "upload_progress_<name>") */
zend_long update_step; /* +0x28 (8B) */
zend_long next_update; /* +0x30 (8B) */
double next_update_time; /* +0x38 (8B) */
bool cancel_upload; /* +0x40 (1B) */
bool apply_trans_sid; /* +0x41 (1B + 6B alignment padding) */
size_t content_length; /* +0x48 (8B) */
zval data; /* +0x50 (16B: IS_ARRAY progress table) */
zval files; /* +0x60 (16B: IS_ARRAY files sub-table) */
zval *post_bytes_processed; /* +0x70 (8B raw zval ptr into data) */
zval *current_file_bytes_processed; /* +0x78 (8B raw zval ptr
* into current_file) */
zval current_file; /* +0x80 (16B) -> Total sizeof = 0x90 */
} php_session_rfc1867_progress;
Now look at what php_session_rfc1867_callback() actually does across the multipart events of an upload that includes a PHPSESSID cookie, a PHP_SESSION_UPLOAD_PROGRESS form field, and a file part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ext/session/session.c: Simplified from vulnerable
* php_session_rfc1867_callback() */
static int php_session_rfc1867_callback(unsigned int event, void *event_data,
void **extra) {
php_session_rfc1867_progress *progress = PS(rfc1867_progress);
switch (event) {
case MULTIPART_EVENT_START:
/* BUG #1: PHP_RINIT_FUNCTION(session) has NOT run yet!
* Notice PS(rfc1867_progress) is freshly allocated here,
* BUT stale PS(id), PS(session_status), and PS(http_session_vars)
* from an aborted prior request are NEVER cleared!
*/
if (!PS(rfc1867_enabled)) {
return SUCCESS;
}
progress = ecalloc(1, sizeof(php_session_rfc1867_progress));
PS(rfc1867_progress) = progress;
progress->sname_len = strlen(PS(session_name)); /* PHP 8.2–8.4 */
break;
case MULTIPART_EVENT_FORMDATA:
/* Captures PHPSESSID (or cookie) into progress->sid and
* PHP_SESSION_UPLOAD_PROGRESS value into progress->key.
*/
break;
case MULTIPART_EVENT_FILE_START:
/* The moment the first file part starts, ext/session boots a real
* PHP session right inside Pre-RINIT! In 8.4.26 the code calls
* php_rinit_session(0), then assigns PS(id) and clears
* PS(send_cookie), before php_session_rfc1867_update():
* -> php_session_initialize() sets PS(id) (Bin #7, 64B) and
PS(session_status) = active
* -> php_session_track_init() sets PS(http_session_vars)
(Bin #3, 32B zend_reference)
*/
if (Z_TYPE(progress->sid) && progress->key.s) {
php_rinit_session(0);
PS(id) = zend_string_init(Z_STRVAL(progress->sid),
Z_STRLEN(progress->sid), 0);
PS(send_cookie) = 0; /* <-- cleared here, BUT flipped back to 1
* inside php_session_initialize() when
* PHPSESSID="\x00" (see Step 2)! */
php_session_rfc1867_update(progress, 0);
}
break;
case MULTIPART_EVENT_END:
/* Normal cleanup when multipart parsing finishes cleanly */
if (PS(rfc1867_progress)) {
if (Z_TYPE(progress->sid) && progress->key.s) {
php_session_rfc1867_cleanup(progress);
}
zval_ptr_dtor(&progress->sid);
smart_str_free(&progress->key);
efree(progress);
PS(rfc1867_progress) = NULL;
}
break;
}
return SUCCESS;
}
Look closely at what happens when Request $N$ sends PHP_SESSION_UPLOAD_PROGRESS, triggers MULTIPART_EVENT_FILE_START, and then hits memory_limit on subsequent form fields:
- At
MULTIPART_EVENT_FILE_START,php_rinit_session(0)andphp_session_rfc1867_update()start the session early. In persistent.bss(php_ps_globals), it sets:PS(session_status) = php_session_activePS(id)= pointer to azend_string(Bin#7,64 B) on Request $N$’sheap->main_chunkPS(http_session_vars)=IS_REFERENCE_EXzvalpointing to azend_reference(Bin#3,32 B) on Request $N$’sheap->main_chunk
- Later in the same multipart body, additional form parts (with a trick explained below) push Zend MM past
PG(memory_limit)and trigger_zend_bailout(). MULTIPART_EVENT_ENDis skipped becauserfc1867_post_handler()was abandoned mid-loop bylongjmp().php_request_shutdown()checksif (PG(modules_activated))(that is0 == false) and skipsPHP_RSHUTDOWN_FUNCTION(session)(includingphp_rshutdown_session_globals()).shutdown_memory_manager()wipes Request $N$’s heap, keepingheap->main_chunkmapped at its 2 MB base address with every page marked FREE.- When Request $N+1$ arrives on the same worker with a
multipart/form-databody,MULTIPART_EVENT_STARToverwritesPS(rfc1867_progress)with a newecalloc(), but leavesPS(session_status),PS(id)(a pointer tozend_string), andPS(http_session_vars), untouched and pointing straight intoheap->main_chunk.
Worker Memory Across Request N -> Request N+1 (Same Worker PID)
Worker .bss (php_ps_globals) heap->main_chunk (0x7f8000200000)
(Persists across requests) (Stays mapped; reset on shutdown)
[1] REQUEST N (Pre-RINIT: FILE_START runs, then OOM -> _zend_bailout)
+------------------------------------+ +-------------------------------+
| PS(session_status) = active | | +0x0000: zend_mm_chunk header |
| PS(id) = 0x7f8000203000|--->| +0x3000 (Bin #7, 64B): |
| PS(http_session_vars) | | zend_string session ID |
| .value.ref = 0x7f8000202000|-+ +-------------------------------+
| .u1.type_info = IS_REFERENCE | +->| +0x2000 (Bin #3, 32B): |
+------------------------------------+ | zend_reference -> $_SESSION |
+-------------------------------+
[2] REQUEST N SHUTDOWN (PG(modules_activated)==0 -> RSHUTDOWN skipped!)
+------------------------------------+ +-------------------------------+
| PS(session_status) = active | | +0x3000 (Bin #7): [ FREE ] |
| PS(id) = DANGLING! -+--->| +0x2000 (Bin #3): [ FREE ] |
| PS(http_session_vars) = DANGLING! -+--> +-------------------------------+
+------------------------------------+
[3] REQUEST N+1 (Pre-RINIT: Multipart parsed BEFORE session RINIT!)
+------------------------------------+ +-------------------------------+
| PS(id) = 0x7f8000203000 -+--->| +0x3000: Reclaimed by Req N+1 |
| PS(http_session_vars) | | arrays/objects (ASLR leak!) |
| .value.ref = 0x7f8000202000 -+\ +-------------------------------+
+------------------------------------+ +->| +0x2000: Reclaimed by later |
| 4KB $_POST page (hijack!) |
+-------------------------------+
Note: Under normal circumstances, PHP relies on a contract between GINIT (in
MINIT, so only once in the lifetime of the process) and RSHUTDOWN to ensure thatPS(id)is alwaysNULLbefore any request’s Pre-RINIT runs. The exploit leverages “exception handling” to make sure that contract is not fulfilled. The same applies forPS(http_session_vars).
Other CVE-2026-109619 surfaces: Upstream advisory GHSA-6wmf-4rv5-p3rp also notes two related stale-state paths after an OOM bailout: PHP 8.5’s fatal_error_backtraces recorder (which could inspect dangling call frames) and userland session_start() / unserialize() state after a caught or late bailout. Pre-RINIT upload_progress gets all the attention in PHP2Shell because it is reachable on default PHP installations before a single line of application code runs.
Step-by-step: How the PHP2Shell exploit chain is built
Now we can assemble every piece from Part 1 and Part 2 into the complete multi-request exploit architecture. Holding the big picture in mind makes each individual step much easier to follow:
End-to-End Exploit Roadmap (Sent Over Keep-Alive to One Persistent Worker PID)
+-------------+-------------------------------------------------+----------------------------------------+
| Request 1 | Pre-RINIT OOM (FILE_START + empty-upload amp) | Leaves PS(id) dangling in main_chunk |
+-------------+-------------------------------------------------+----------------------------------------+
| Request 2 | Pre-RINIT arPacked / HashTable heap groom | Leaks main_chunk_base & php_pie_base |
+-------------+-------------------------------------------------+----------------------------------------+
| Request 3 | Pre-RINIT OOM (nudged +1 slot, same amplifier) | Re-arms PS(http_session_vars) @ Page K |
+-------------+-------------------------------------------------+----------------------------------------+
| Request 4 | 4 KB $_POST page + fake object/frame -> END | dtor_obj(P) -> zif_system($command)! |
+-------------+-------------------------------------------------+----------------------------------------+
Let’s walk through all four steps in order and check every engine constraint as an exploit developer has to solve it.
Step 1: Crossing memory_limit (128M) inside post_max_size (8M) while pinning pointers in main_chunk
The first obstacle is a size paradox: default PHP sets post_max_size = 8M and memory_limit = 128M.
If you send a 129 MB HTTP body to exhaust memory_limit, sapi_read_post_data() rejects the request during sapi_activate() before rfc1867_post_handler() ever starts.
So, how do you allocate more than 128 MB of Zend MM memory from an HTTP body smaller than 8 MB?
Simple (LOL)! By exploiting HashTable structural overhead in php_register_variable_ex() (main/php_variables.c).
Whenever PHP parses a form variable with nested single-character string keys like a[a][a][a]... (where 'a' uses PHP’s interned ZSTR_CHAR table at zero extra cost; empty brackets a[][][]... would instead initialize smaller 160-byte packed arrays), every [a] level allocates two heap blocks:
- A new
HashTablestruct: 56 bytes (rounded to Bin#6: 56 B). - That table’s initial 8-slot
arDatabucket array (HT_MIN_SIZE = 8):32 Bhash index +8 * 32 Bbuckets = 288 bytes (rounded to Bin#16: 320 B).
376 bytes of Zend MM heap for every 3 bytes ([a]) on the wire is a ~125x memory amplification ratio!
However, plain nested POST fields alone cannot reach 128 MB under default PHP limits: max_input_vars = 1000 caps the number of parts and max_input_nesting_level = 64 caps the bracket depth, so it tops out around 24 MB (1000 * 64 * 376 B).
The amplifier that actually crosses 128 MB within the defaults is the empty-upload trick.
A multipart/form-data file part with filename="" is not counted toward max_file_uploads, yet rfc1867_post_handler() still registers all six $_FILES sub-entries (name, full_path, type, tmp_name, error, size) under the nested field name.
Each of those six entries walks the same nested HashTable hierarchy, so one short empty-upload record (g[i][a][a]...[a] with filename="") allocates roughly 6x the HashTable/Bucket overhead of a plain POST field.
Around 990 such records at depth 62 (990 * 62 * 6 * 376 B =~ 132 MB) blow past memory_limit while the complete body stays under 1 MB; well inside post_max_size = 8M, max_input_vars = 1000, and max_input_nesting_level = 64.
Ordering inside the OOM request’s multipart body is critical here. Remember from Part 1 that shutdown_memory_manager() munmap()s secondary 2 MB chunks while keeping heap->main_chunk mapped. If FILE_START naively allocated PS(id) or PS(http_session_vars) after the amplifier had already moved Zend MM into chunk #63, both pointers would land in unmapped memory and crash on the next request.
Depending on whether FILE_START should finish before the OOM (as in Request 3, which re-arms PS(http_session_vars) cleanly) or bail out mid-execution (as in Request 1, which bails out inside php_session_send_cookie() so PS(send_cookie) = 1 survives for Step 2), two multipart orderings pin those allocations inside heap->main_chunk:
Pattern A: Early FILE_START (Used in Request 3 to re-arm PS(http_session_vars))
+----------------------------------+----------------------------------+
| Part 1: UPLOAD_PROGRESS + SID | Sets progress->key & progress->sid|
+----------------------------------+----------------------------------+
| Part 2: name="f"; filename="a" | Fires MULTIPART_EVENT_FILE_START |
| (1-byte file upload) | Allocates session_vars LOW in |
| | main_chunk & closes PS(mod_data) |
+----------------------------------+----------------------------------+
| Parts 3..K: Empty-upload records | ~990 parts (62-deep, filename="")|
| (Amplification payload) | Allocates 63 secondary chunks |
| | -> OOM triggers _zend_bailout()! |
+----------------------------------+----------------------------------+
Pattern B: LIFO Stash + Late FILE_START (Used in Request 1 for Set-Cookie leak)
+----------------------------------+----------------------------------+
| Parts 1..S: Dummy $_POST strings | Carves small-bin slots LOW in |
| (s7, s4, b3, ...) | heap->main_chunk |
+----------------------------------+----------------------------------+
| Parts S+1..K: Empty-upload amp | Fills all 64 chunks to 128 MB |
| + 64 KB / 4 KB LRUN page mops | (0 free 4 KB pages left!) |
+----------------------------------+----------------------------------+
| Parts K+1..R: Overwrite s7="" .. | Frees those main_chunk slots |
| (ZVAL_EMPTY_STRING = 0 B!) | onto LIFO heads of free_slot[]! |
+----------------------------------+----------------------------------+
| Final 3 parts: UPLOAD_PROGRESS + | s_create_sid() pops main_chunk, |
| PHPSESSID="\x00" + filename="a"| sets send_cookie=1 -> OOMs! |
+----------------------------------+----------------------------------+
Notice how Pattern B solves an apparent catch-22: overwriting an existing $_POST key with an empty value "" (ZVAL_EMPTY_STRING, an interned singleton that allocates zero heap bytes) calls zval_ptr_dtor() on the earlier main_chunk string and pushes its freed slot directly onto the LIFO head of heap->free_slot[bin], letting FILE_START allocate inside heap->main_chunk even when the heap is already 99.99% full!
Step 2: Defeating ASLR and PIE via dangling PS(id)
Over the same HTTP/1.1 Keep-Alive TCP socket (which, on direct worker SAPIs such as Apache mod_php prefork, routes the next request onto the exact same worker PID), the client sends Request $N+1$.
At startup, PS(session_status) is already php_session_active and PS(id) still points to its freed 64-byte slot (Bin #7) inside heap->main_chunk (say 0x7f8000203000).
Look at how PHP interprets PS(id) as a zend_string:
Dangling PS(id) (zend_string ptr at 0x7f8000203000, Bin #7: 64B)
+-----------------------------+---------------------------------+
| +0x00..+0x0f: gc + hash | +0x10..+0x17: size_t len |
+-----------------------------+---------------------------------+
| +0x18..+0x3f: char val[0..len-1] |
| <-- PHP reads PS(id) string bytes starting here! |
+---------------------------------------------------------------+
When early allocations in Request 2 (or residual structures left in heap->main_chunk) populate the 4 KB page containing PS(id), two classes of internal pointers sit inside those reclaimed bytes:
- Binary
.text/.rodatapointers (for PIE): Every 56-byteHashTable($_POSTsub-array, Bin#6) deterministically stores&zval_ptr_dtorinht->pDestructor(+0x30), pointing straight into the PHP binary’s.textsegment even on a freshly forked worker. Likewise, any standardzend_objectleft over from prior requests stores&std_object_handlersat+0x18(.rodata). - Heap pointers (for
main_chunk):HashTable.arData(+0x10),Bucket.key(+0x18), andarPackedzval.value.strentries point directly intoheap->main_chunk. (Plain Zend MMnext_free_slotlinks sit at+0x00, outsideZSTR_VAL’s+0x18..+0x3fwindow, while the trailing shadow pointer at+0x38..+0x3fis byte-swapped and XOR-obfuscated withshadow_keyon PHP 8.4+ and the slot address on PHP 8.5+, so freed-slot metadata alone does not yield a plain heap pointer.)
How do we exfiltrate those reclaimed PS(id) bytes over HTTP without relying on any userland PHP code? At first glance, MULTIPART_EVENT_FILE_START appears to block the obvious Set-Cookie: PHPSESSID= response header by setting PS(send_cookie) = 0 right before calling php_session_rfc1867_update(). Look inside php_session_initialize() (ext/session/session.c), however, at what happens when Request 1 passes PHPSESSID=\x00 (a 1-byte string whose first character is a NUL terminator):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* ext/session/session.c: Inside php_session_initialize() */
if (!PS(id) || !ZSTR_VAL(PS(id))[0]) {
if (PS(id)) {
zend_string_release_ex(PS(id), false);
PS(id) = NULL;
}
PS(id) = PS(mod)->s_create_sid(&PS(mod_data)); /* 64B Bin #7 in main_chunk */
/* ... */
if (PS(use_cookies)) {
PS(send_cookie) = true; /* <-- Flips PS(send_cookie) BACK to 1! */
}
}
/* Immediately calls php_session_reset_id(): */
if (PS(use_cookies) && PS(send_cookie)) {
zend_result cookies_sent = php_session_send_cookie(); /* <-- OOM bailout here! */
/* ... */
PS(send_cookie) = false; /* <-- Skipped when _zend_bailout() jumps out! */
}
That PHPSESSID=\x00 trick does double duty:
- Application-agnostic
Set-Cookieleak (zero userland code): If Request 1 pre-stashes free small-bin slots inheap->main_chunk(forPS(id)as well asPS(mod_data)anddata->basedir, since skippingphp_session_flush(1)leavesPS(mod_data)open too) and pushes heap usage right up tomemory_limitbeforeFILE_START,s_create_sid()pops amain_chunkslot without needing a new page, flipsPS(send_cookie) = true, and_zend_bailout()fires insidephp_session_send_cookie()(when allocating the 256B Bin#15smart_strheader buffer) beforePS(send_cookie) = false;can run. BothPS(id)andPS(send_cookie) = 1survive in.bss! On Request 2,ps_open_files()cleanly closes the intactPS(mod_data)slot, andphp_session_reset_id()seesPS(send_cookie) == 1, callsphp_session_send_cookie(), URL-encodes all 262 raw bytes ofZSTR_VAL(PS(id))(php_url_encode(), turning\x00,\x7f, etc. into%00,%7F), and emitsSet-Cookie: PHPSESSID=<URL-encoded heap bytes>in the HTTP response headers of any.phpendpoint (even an empty file!). - Simplified lab
SIDleak (no byte-exact OOM tuning needed): Even if Request 1 letsphp_session_reset_id()finish (resettingPS(send_cookie) = 0) and triggers OOM on later parts, Request 2’sphp_session_reset_id()still builds theSIDconstant ("PHPSESSID=" + ZSTR_VAL(PS(id))wheneverPS(define_sid)is true) beforeRINITclearsPS(id). In a local testbed, a one-line<?php echo SID;page reflects the same raw bytes without needing to calibrate Request 1’s OOM down to the exact allocation insidephp_session_send_cookie().
Either way, the reflected PS(id) bytes disclose two 8-byte pointers that give the entire process memory map:
leaked_heap_ptr: leaked whenPS(id)reads across anarPackedzvalorHashTable.arData.leaked_pie_ptr: leaked whenPS(id)reads acrossHashTable.pDestructor(&zval_ptr_dtor) or a residualzend_object’s&std_object_handlers.
From those two pointers, two arithmetic operations defeat both Heap ASLR and PIE:
1
2
3
4
5
6
/* 1. Defeat Heap ASLR (chunks are 2 MB-aligned): */
uint64_t main_chunk_base = leaked_heap_ptr & ~0x1fffffULL;
/* 2. Defeat PIE ASLR (e.g., from HashTable.pDestructor = &zval_ptr_dtor): */
uint64_t php_pie_base = leaked_pie_ptr - OFFSET_ZVAL_PTR_DTOR;
uint64_t addr_zif_system = php_pie_base + OFFSET_ZIF_SYSTEM;
With main_chunk_base and addr_zif_system known, every 4 KB page in heap->main_chunk (main_chunk_base + 0x1000, + 0x2000, …) and every function in the PHP binary now has a known virtual address.
A runnable, zero-userland-code demonstration of Heap ASLR disclosure over Set-Cookie: PHPSESSID=, including an automated Docker testbed (Apache mod_php prefork, default php.ini, inert index.php), is available at https://github.com/dguerri/php2leak.git. Look at why its heap-grooming layout works so cleanly:
Request 1 (Arming dangling PS(id) and PS(send_cookie) = 1)
Using Pattern B from Step 1, Request 1 stashes small-bin strings in heap->main_chunk, exhausts all remaining 4 KB pages across all 64 chunks (128 MB) via the empty-upload amplifier and page mops (leaving one free 256B Bin #15 slot for progress->key), and overwrites the stashed strings with "" to push their main_chunk slots onto Zend MM’s LIFO free_slot[] heads.
Sending PHPSESSID=\x00 and filename="a" then fires MULTIPART_EVENT_FILE_START -> php_session_initialize():
PS(mod)->s_open(&PS(mod_data), ...)(ps_open_files()inext/session/mod_files.c) pops stashedmain_chunkslots forPS(mod_data)(ps_files,40 BBin#4) anddata->basedir("/tmp",32 BBin#3; stashing 16Bin #3slots ensures the 11add_assoc_*strings insideFILE_STARTdo not drainfree_slot[3]beforebasediris allocated!).s_create_sid()pops a stashedmain_chunkslot forPS(id)(64 BBin#7) and setsPS(send_cookie) = 1.php_session_send_cookie()allocates its 256-bytesmart_strbuffer (Bin#15), findsfree_slot[15] == NULLwith zero free 4 KB pages left, and calls_zend_bailout()beforePS(send_cookie) = 0orphp_session_flush(1)(s_close) can execute.
One subtle detail in ext/session/mod_files.c is worth calling out: because s_close was skipped on Request 1, PS(mod_data) stays non-NULL in .bss. On Request 2, ps_open_files() sees non-NULL PS(mod_data) and calls ps_close_files(&PS(mod_data)) before php_session_send_cookie(), dereferencing data->last_key (+0x00), data->basedir (+0x08), and calling efree(data). Releasing even-indexed Bin #4 slots (s4[2,4,6,8,10]) places PS(mod_data) at page offset +0x410, tucked safely between Bin #6 slot 18’s next_free_slot (+0x3f0) and its trailing shadow pointer (+0x420) on Request 2 so last_key == NULL and basedir survive untouched!
Request 2 (Reclaiming PS(id)’s page to leak 262 bytes via Set-Cookie)
Over the same Keep-Alive TCP connection (same worker), we send PHP_SESSION_UPLOAD_PROGRESS and PHPSESSID=Z without a file part (so FILE_START never overwrites PS(id)), ensuring Z_TYPE(progress->sid) && progress->key.s holds at MULTIPART_EVENT_END and enters php_session_rfc1867_cleanup(progress) -> php_session_initialize() -> php_session_reset_id().
Because shutdown_memory_manager() reset the page bitmaps at the end of Request 1, the 4 KB page holding PS(id) is no longer tied to Bin #7: any new allocation can claim it! Sending a 64-element packed array d0[0]...d0[63] (each holding "V"*50 in Bin #8 so PS(mod_data)’s neighboring Bin #6 page stays untouched) allocates d0’s arPacked buffer (8 + 64 * 16 = 1032 B, Bin #24: 1280 B) right on top of PS(id):
1
2
3
4
5
6
7
8
Memory Overlay at Dangling PS(id) During Request 2:
Offset Read as PS(id) (zend_string) Written by d0's arPacked allocation
+0x00..+0x07 gc (8B) <- d0[k-1].u1 = 0x0106 (or HT_MIN_MASK at +0x00)
+0x08..+0x0f h (8B) <- d0[k].value.str (ptr -> "V"*50)
+0x10..+0x17 len (8B) <- d0[k].u1 = 0x0106 (IS_STRING_EX), u2 = 0
==> PS(id)->len becomes 0x106 = 262 bytes!
+0x18..+0x1f val[0..7] (1st leaked qword) <- d0[k+1].value.str (8-byte HEAP PTR in main_chunk!)
+0x20..+0x11d val[8..261] (over-read window)<- d0[k+1..k+17] & adjacent heap structures
Notice the modulo-16 alignment created by arPacked’s 8-byte HT_MIN_MASK prefix: for any 64-byte Bin #7 slot (0x00 mod 0x40), PS(id) + 0x10 (ZSTR_LEN(PS(id))) lands directly on d0[k].u1.type_info (0x0106), turning Set-Cookie: PHPSESSID= (php_url_encode(ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)))) into a 262-byte over-read whose very first 8 bytes (+0x18..+0x1f) hold d0[k+1].value.str (leaked_heap_ptr, with \x00 bytes preserved as %00). Reading across into neighboring HashTable structures (or running one additional 2-request groom with nested sub-arrays so PS(id) overlaps 56-byte HashTables and reads ht->pDestructor = &zval_ptr_dtor at +0x30) similarly yields leaked_pie_ptr.
Step 3: Reclaiming PS(http_session_vars) across page reset with a binary-safe $_POST page
Because Request 2 ran cleanly through normal request completion, RSHUTDOWN cleared the session globals at the end of Request 2. That is not a problem: the worker process never crashed, so main_chunk_base and addr_zif_system remain completely valid while we send Request 3 (a Pattern A OOM request from Step 1) to re-arm PS(http_session_vars) in .bss (u1.type_info = IS_REFERENCE_EX = 0x010a). By including a single dummy 32-byte form field before Part 2 (filename="a"), we nudge PS(http_session_vars).value.ref past offset +0x000 so it lands at Slot 1+ (Page K + 0x020 or higher) of a known 4 KB page Page K inside heap->main_chunk.
Now, in Request 4 (our RCE request), how do we trigger destruction of that dangling PS(http_session_vars) before PHP_RINIT_FUNCTION(session) runs (which would silently overwrite PS(http_session_vars) with ZVAL_UNDEF without calling any destructor)? Using the exact same MULTIPART_EVENT_END path from Step 2! Sending PHP_SESSION_UPLOAD_PROGRESS and PHPSESSID with no file part enters php_session_rfc1867_cleanup(progress) -> php_session_initialize() -> php_session_track_init():
1
2
3
4
/* ext/session/session.c: Inside php_session_track_init() */
if (!Z_ISUNDEF(PS(http_session_vars))) {
zval_ptr_dtor(&PS(http_session_vars)); /* <-- Pre-RINIT UAF destructor sink! */
}
Look at what zval_ptr_dtor(&PS(http_session_vars)) expects to find at ref = PS(http_session_vars).value.ref:
Forged 32-Byte zend_reference Needed at ref (PS(http_session_vars).value.ref)
+-----------------------------------+-----------------------------------+
| +0x00: gc.refcount = 1 (4B) | +0x04: gc.u.type_info = 0x0a (4B) |
| (--refcount == 0 -> dtor!) | (IS_REFERENCE) |
+-----------------------------------+-----------------------------------+
| +0x08: val.value.obj = P (8B ptr to forged zend_object, Step 4) |
+-----------------------------------+-----------------------------------+
| +0x10: val.u1.type_info = 0x0308 | +0x14: val.u2.extra = 0 (4B) |
| (IS_OBJECT_EX, 4B) | |
+-----------------------------------+-----------------------------------+
| +0x18: sources.ptr = NULL (8B) |
+-----------------------------------------------------------------------+
GC_DELREF(ref)decrementsref->gc.refcount(+0x00..+0x03). Only if it drops from1to0does PHP invokerc_dtor_func(ref), which checksGC_TYPE(ref)(+0x04=IS_REFERENCE = 0x0a) and callszend_reference_destroy(ref).zend_reference_destroy(ref)immediately runsi_zval_ptr_dtor(&ref->val)on the innerzvalat+0x08..+0x17.- Seeing
val.u1.type_info = IS_OBJECT_EX(0x0308) at+0x10, PHP followsval.value.obj(P, at+0x08..+0x0f) into object destruction.
Why staying inside Bin #3 (32 B) hits a three-way dead end
At first glance, since PS(http_session_vars) was a Bin #3 (32 B) allocation in Request 3, it is tempting to reclaim it with another 32-byte allocation in Request 4. Try all three 32-byte candidates in rfc1867_post_handler() and every one of them fails on a different byte constraint:
- A short
$_POSTzend_string(0..7chars ->32 B): Its 24-bytezend_stringheader occupies+0x00..+0x17, forcingIS_STRINGat+0x04and overwriting+0x08..+0x17withhandlen. Controlled bytes only begin at+0x18, completely missing the innerzval! - A raw
rfc1867form body (multipart_buffer_read_body(),25..32 B): Allocates a headerlesschar *buffer viaerealloc()+memcpy(), so it can write arbitrary0x00bytes starting at+0x00. Butrfc1867_post_handler()callsefree(value)right at the end of that form part, overwriting+0x00..+0x07withnext_free_slotand destroyinggc.refcount = 1beforeMULTIPART_EVENT_ENDever runs! - A live
rfc1867MIME header or name buffer (estrdup()/estrndup(),24..31chars): Stays allocated pastMULTIPART_EVENT_END, butstrlen()stops on0x00bytes. Any string long enough to land in Bin#3(>= 24non-zero chars) overwrites+0x00..+0x17with non-zero bytes, making it impossible to encodegc.refcount = 0x00000001(+0x01..+0x03=0x00) or the upper zero bytes of a canonical 64-bit pointerP = 0x00007f...(+0x0e..+0x0f=0x00)!
Breaking out of Bin #3 via cross-request page reassignment
The clean way out is the exact same Zend MM rule that powered php2leak in Step 2: at the end of Request 3, shutdown_memory_manager() reset main_chunk’s page bitmaps, turning Page K back into an untyped free 4 KB page!
Instead of fighting 32-byte small bins in Request 4, we send a normal binary-safe $_POST form variable (name="spray") whose value is ~3,500 bytes long. Because 24 + 3500 + 1 = 3525 B exceeds Zend MM’s small-bin ceiling (3072 B), zend_mm_alloc_pages() hands that zend_string all of Page K (4096 B):
Page K (main_chunk_base + K * 0x1000) Reclaimed by 4 KB $_POST["spray"] zend_string
+-----------------------------------------------------------------------------+
| +0x000..+0x017: zend_string header (gc, h, len = 3500) |
| (Harmless! Request 3 nudged PS(http_session_vars) past Slot 0 to >=+0x020)|
+-----------------------------------------------------------------------------+
| +0x018..+0x01f: 8-byte alignment pad inside ZSTR_VAL |
+-----------------------------------------------------------------------------+
| +0x020..+0x03f: Forged 32B zend_reference (refcount=1, 0x0a, obj=P, 0x0308) | <--\
+-----------------------------------------------------------------------------+ | Tiled every
| +0x040..+0x05f: Forged 32B zend_reference (identical copy) | <--+ 0x20 bytes!
+-----------------------------------------------------------------------------+ | Any Slot 1..N
| ... (Repeated across ZSTR_VAL - binary-safe & LIVE in $_POST!) | <--/ hits P!
+-----------------------------------------------------------------------------+
Look at what this achieves in one stroke:
$_POST["spray"]is 100% binary-safe (ZSTR_VALcarries explicitlen = 3500, so all0x00bytes ingc.refcount = 1andP = 0x00007f...survive verbatim).$_POST["spray"]stays live inPG(http_globals)[TRACK_VARS_POST]acrossMULTIPART_EVENT_END, so nothing callsefree()on it beforephp_session_track_init()runs.- Tiling the 32-byte forged
zend_referenceevery0x20bytes fromPage K + 0x020onward means we do not even need to know which 32-byte slot index (Slot 1,Slot 4,Slot 12, …)PS(http_session_vars)landed on withinPage Kduring Request 3; every single one of them forwards control cleanly toP!
Step 4: Turning dtor_obj(obj) into zif_system() via a zend_object / zend_execute_data polyglot
Once zend_reference_destroy() calls i_zval_ptr_dtor(&ref->val), PHP follows our controlled pointer P = ref->val.value.obj. Because P can point anywhere inside heap->main_chunk, we stage the fake object, its fake handler table, and the command string inside binary-safe $_POST zend_strings in Request 4 (for example, a 120-byte $_POST string S in Bin #12: 160 B at a known heap->main_chunk offset, setting P = S + 0x18 right at ZSTR_VAL(S), or even at a fixed high offset inside Page K itself!).
Now trace what happens when i_zval_ptr_dtor(&ref->val) decrements P->gc.refcount (1 -> 0) and invokes zend_objects_store_del(P) (Zend/zend_objects_API.c):
- Checks
!(OBJ_FLAGS(P) & IS_OBJ_DESTRUCTOR_CALLED)atP + 0x04(IS_OBJECT = 0x08, bit clear -> passes) and setsIS_OBJ_DESTRUCTOR_CALLED. - Checks
P->handlers->dtor_obj != zend_objects_destroy_object(true, short-circuiting|| P->ce->destructorsoP->ceatP + 0x10is never dereferenced). - Executes
GC_ADDREF(P), writing1back into[P + 0x00]. - Calls
P->handlers->dtor_obj(P)(*(handlers + 0x10)), passingPinRDI.
Pointing dtor_obj directly at addr_zif_system (PHP_FUNCTION(system) in .text) bypasses disable_functions completely: CG(function_table) is never consulted when jumping straight to the C symbol. However, compare the C signatures of dtor_obj and zif_system on x86_64 System V ABI:
1
2
3
4
5
6
/* Caller (zend_objects_store_del): passes 1 arg in RDI; RSI is residual */
void dtor_obj(zend_object *object /* RDI */);
/* Callee (PHP_FUNCTION(system)): reads call frame from RDI! */
void ZEND_FASTCALL zif_system(zend_execute_data *execute_data, /* RDI */
zval *return_value); /* RSI */
Notice the type mismatch on the RDI register:
- Before the call,
zend_objects_store_del()treatsRDI(P) as azend_object *(0x38bytes). - Inside
zif_system, PHP treats that exact same addressRDI(P) as azend_execute_data *(0x50bytes): the Zend VM’s call-frame structure (Zend/zend_compile.h) that tracks an active PHP function call and stores its passedzvalarguments immediately after the frame header starting atRDI + 0x50(ZEND_CALL_ARG(execute_data, 1)).
That brings us to the polyglot in this section’s title. In security research, a file polyglot (such as a file that is simultaneously a valid PNG image and a valid ZIP archive) is a single byte sequence crafted so two different parsers both accept it as valid. An in-memory struct polyglot applies the exact same trick to C structs: we craft a single 0x60-byte (96 B) buffer at P whose bytes simultaneously look like a valid zend_object to zend_objects_store_del() and like a valid zend_execute_data call frame (with argument #1 $command at +0x50) to zif_system()!
With the 24-byte fake zend_object_handlers vtable tucked right behind it at H = P + 0x60, the entire 120-byte payload looks like this:
120-Byte Payload Inside ZSTR_VAL(S) (S in Bin #12: 160B, P = S + 0x18)
+-------------------------------------------------------------------------+
| Offset | Read as zend_object (0x38B) | Read as zend_execute_data + arg1|
+---------+-----------------------------+---------------------------------+
| +0x00 | gc.refcount = 1 (4B) | const zend_op *opline (8B) |
| +0x04 | gc.u.type_info = 0x08 (4B) | (GC_ADDREF writes 1 at +0x00; |
| | (IS_OBJECT) | unused by internal C funcs!) |
+---------+-----------------------------+---------------------------------+
| +0x08 | handle = 0 (4B) + pad (4B) | zend_execute_data *call = NULL |
+---------+-----------------------------+---------------------------------+
| +0x10 | zend_class_entry *ce = NULL | zval *return_value = NULL |
| | (short-circuited!) | (zif_system uses RSI instead) |
+---------+-----------------------------+---------------------------------+
| +0x18 | *handlers = P + 0x60 -------+-> zend_function *func |
| | (points to fake vtable H) | (untouched when args match!) |
+---------+-----------------------------+---------------------------------+
| +0x20 | *properties = NULL (8B) | This.value = NULL (8B) |
+---------+-----------------------------+---------------------------------+
| +0x28 | properties_table[0] (low 8B)| This.u1.type_info = 0 (4B) |
| +0x2c | (untouched before dtor) | This.u2.num_args = 1 (4B) <--! |
+---------+-----------------------------+---------------------------------+
| +0x30 | properties_table[0] (hi 8B) | *prev_execute_data = NULL (8B) |
+=========+=============================+=================================+
| +0x38 | (past sizeof(zend_object)) | *symbol_table = NULL (8B) |
| +0x40 | | **run_time_cache = NULL (8B) |
| +0x48 | | *extra_named_params = NULL (8B) |
+---------+-----------------------------+---------------------------------+
| +0x50 | ZEND_CALL_ARG(execute_data, 1): zval $command (16B) |
| | +0x50: value.str = ptr to command zend_string on main_chunk |
| | +0x58: u1.type_info = IS_STRING (0x06), +0x5c: u2 = 0 |
+=========+===============================================================+
| +0x60 | Forged zend_object_handlers H (24B: +0x60..+0x77): |
| | +0x60: offset = 0 (8B) |
| | +0x68: free_obj = NULL (8B) |
| | +0x70: dtor_obj = addr_zif_system (8B) <-- RIP hijack! |
+---------+---------------------------------------------------------------+
Check why every single overlapping field, and the uncontrolled RSI register, works cleanly to achieve RCE:
+0x00..+0x07(gcvs.opline): Right before callingdtor_obj,GC_ADDREF(P)overwrites[P + 0x00]with1. For a userland PHP function that non-pointeroplinewould segfault immediately, butzif_systemis an internal C function (ZEND_INTERNAL_FUNCTION) that never touchesexecute_data->oplineunless argument parsing raises an error.+0x18..+0x1f(handlersvs.func):zend_objects_store_del()dereferencesP->handlers(P + 0x60) to loaddtor_obj(addr_zif_system). Insidezif_system,ZEND_PARSE_PARAMETERS_START(1, 2)never dereferencesexecute_data->funcas long as argument count and types succeed.+0x28..+0x2f(properties_table[0]vs.This.u1/This.u2.num_args): SettingThis.u1.type_info = 0(+0x28) clearsZEND_CALL_HAS_EXTRA_NAMED_PARAMS, and settingThis.u2.num_args = 1(+0x2c) tellszif_system(string $command, int &$result_code = null)that exactly one argument was passed (skipping the optional pass-by-reference$result_codewriteback).+0x50..+0x5f(ZEND_CALL_ARG(execute_data, 1)) and residualRSI:Z_PARAM_PATH(cmd, cmd_len)reads ourIS_STRINGzvalatP + 0x50and callsphp_exec()(popen("/bin/sh -c ...", "r")) before ever touchingreturn_value(RSI). The shell command executes in full inside Pre-RINIT before a single line of.phpscript is compiled! Only afterpclose()returns doeszif_systemattempt to write its return string/boolean to*return_value([RSI]), meaning a non-pointer residualRSIat worst crashes that single worker after the payload has already executed (whereupon the master process immediately forks a clean replacement worker).
Why real servers break naive exploits, and where AI actually fits
Reading the four steps above makes it clear why every one-request “curl PoC” posted after October 21 was fake, and why turning a lab exploit into something that survives a production server is an uphill engineering problem.
Four practical hurdles trip up naive exploits immediately:
- Calibrating the narrow
memory_limittripwire blindly. On a target whosephp.iniis unknown,memory_limitcould be128M,256M,512M, or-1(unlimited, disabling OOM bailout altogether!) whilepost_max_sizecould be8Mor2M. Especially for the application-agnosticSet-Cookie: PHPSESSID=leak, Request 1 has to hitmemory_limitinside the tight window betweenPS(send_cookie) = trueandPS(send_cookie) = falseinsidephp_session_reset_id(), without exceedingpost_max_sizeormax_input_vars. - Pinning the exact worker PID across four requests. Behind Nginx, Envoy, or a cloud load balancer, a
php-fpmpool often runs 20 to 200 worker processes, and reverse proxies frequently multiplex upstream connections. If Request 1 or Request 3 arms worker PID4182, and a normal visitor’s page load hits PID4182a millisecond before the attacker’s next request,zend_activate_modules()runsPHP_RINIT_FUNCTION(session), resetsPS(id)andPS(http_session_vars), and disarms the UAF harmlessly. - Null bytes and post-command worker crash (
RSI). Canonical 64-bit Linux user-space addresses (0x00007f8000204000) carry two upper0x00bytes,gc.refcount = 1carries three0x00bytes, and fields likeextra_named_params(+0x48) must beNULL. That rules outrfc1867’sstrlen()-bounded header strings in Bin#3and forces page-level binary-safe$_POSTzend_stringreclamation. Furthermore, althoughzif_systemexecutes the shell command before touchingreturn_value(RSI), oncepclose()returns, writing*return_valueinto an uncontrolled residualRSItypically crashes that worker withSIGSEGV, leaving a telltale crash log entry even after the command ran. - Extension heap noise and symbol table variance. Every production extension (
opcache,redis,igbinary, Datadog or New Relic agents) allocates and frees pages and small bins during startup, shifting page offsets insideheap->main_chunk. And computingaddr_zif_systemfrom&zval_ptr_dtoror&std_object_handlersrequires fingerprinted symbol offsets for that exact distro binary (for instance, Ubuntu 24.04php8.3-fpmvs. official Dockerphp:8.4-fpm-alpine).
How AI changes exploit development without replacing systems expertise
Historically, building an exploit chain like PHP2Shell took a small team weeks of manual work: expanding nested C macros by hand, auditing hundreds of emalloc() paths across php-src, and writing one-off GDB scripts to watch heap->free_slot[] transitions.
Coding agents shorten the mechanical parts of that workflow dramatically. An agent can scan all 1,500+ C files in php-src in minutes to list every site where nNumUsed++ or a PS(...) assignment precedes an allocation, verify byte alignments between zend_object (0x38 bytes) and zend_execute_data (0x50 bytes) with a quick offsetof() harness, and generate GDB Python scripts to record every Bin #3 and Bin #12 allocation inside rfc1867_post_handler() across ten Docker images in parallel.
What AI cannot do on its own is invent a sound mental model when its assumptions about the runtime are wrong. Without an engineer who checks the actual php-src control flow, an LLM happily assumes MULTIPART_EVENT_START leaves PS(rfc1867_progress) intact, tries to overwrite offset +0x08 using a zend_string whose header spans +0x00..+0x17, or forgets that GC_ADDREF(object) clobbers [RDI + 0x00] right before dtor_obj. Tools accelerate the search and verification loop, while systems knowledge still separates a working exploit from a segfault.
Part 3: Upstream fix and remediation
How commit 01a40e89fb fixes the allocator and session lifecycle
On October 21, 2026, PHP core developers Arnaud Le Blanc and Ilija Tovilo (with contributions from Jakub Zelenka, Nora Dossche, and Nicolas Grekas) shipped commit 01a40e89fb (“Avoid state corruption via bailout”) in PHP 8.2.35, 8.3.36, 8.4.27, and 8.5.12.
Auditing every single emalloc() call across PHP and third-party C extensions would have been error-prone and incomplete. Instead, the patch fixes the architectural root causes inside the Zend Memory Manager, input variable parsing, and ext/session.
Two-tier memory limits (Zend/zend_alloc.c)
The patch splits memory_limit into a soft ceiling and a hard ceiling controlled by a new php.ini directive, memory_hard_limit_percent:
- Crossing the soft limit (
memory_limit) inside ordinary, non-fallibleemalloc()calls no longer fires_zend_bailout()mid-function. Instead, Zend MM satisfies the small structural allocation, records a delayed OOM flag (ZEND_HAS_OOM()), and defers the fatal out-of-memory error until execution reaches a safe Zend VM check point where C data structures are consistent. - The hard limit (
memory_hard_limit_percent) defaults to-1(meaning 200% ofmemory_limitwhenmemory_limit <= 1G, such as a256Mhard ceiling for a128Msoft limit, ormemory_limit + 1Gabove1G). If runaway C allocations cross this hard ceiling before reaching a VM check point, PHP refuses to calllongjmp()or run destructors on half-built structures. Instead,zend_mm_hard_limit_error()terminates the worker process immediately via_exit(137)so thephp-fpmmaster spawns a clean worker:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Zend/zend_alloc.c: Patched two-tier OOM handling (commit 01a40e89fb) */
static ZEND_COLD ZEND_NORETURN void zend_mm_hard_limit_error(
zend_mm_heap *heap, size_t add_size ZEND_FILE_LINE_DC)
{
/* ... resolves script_filename ("Unknown" in Pre-RINIT) and script_lineno (0) ... */
/* Do NOT longjmp or run destructors on potentially half-built state.
* Terminate the worker process immediately so the master spawns a clean worker.
*/
fprintf(stderr,
"Hard memory limit of %zu bytes exhausted (tried to allocate %zu bytes) in %s on line %d\n",
heap->hard_limit, add_size, ZSTR_VAL(script_filename), script_lineno);
_exit(137);
}
Explicit fallible allocators and singleton input arrays (Zend/zend_alloc.c, main/php_variables.c)
Where C parsers or built-in functions genuinely allocate large user-controlled buffers that should fail right at the soft memory_limit without killing the worker via _exit(137), the patch introduces explicit fallible allocation functions (femalloc(), fecalloc(), safe_ferealloc(), zend_string_finit()). Unlike emalloc(), these return NULL on soft-limit exhaustion so the caller can roll back local state cleanly before raising an error.
In main/php_variables.c, php_register_variable_ex() and php_default_treat_data() also replace array_init() with array_init_singleton() (zend_new_array_singleton()), which initializes newly created input sub-arrays with nTableSize = 1 instead of HT_MIN_SIZE = 8, blunting the nested-bracket g[i][a]...[a] memory amplification primitive from Part 2. Meanwhile, rfc1867_post_handler() in main/rfc1867.c checks if (ZEND_HAS_OOM()) goto fileupload_done; after registering form variables so a soft OOM during multipart parsing still runs MULTIPART_EVENT_END cleanup instead of jumping over it via longjmp().
Pre-RINIT session reset and lifecycle hardening (ext/session/session.c)
To close CVE-2026-109619, commit 01a40e89fb hardens ext/session/session.c in three ways:
First, php_session_rfc1867_callback() refuses to run if PG(modules_activated) is already set (protecting active sessions when request_parse_body() is called from userland), calls php_rinit_session_globals() at the start of MULTIPART_EVENT_START to clear any leftover PS(id), PS(session_status), or PS(http_session_vars) before RINIT, and deletes $_SESSION from $GLOBALS at MULTIPART_EVENT_END:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* ext/session/session.c: Patched RFC 1867 callback */
static zend_result php_session_rfc1867_callback(unsigned int event, void *event_data, void **extra)
{
if (!PS(rfc1867_enabled)) {
return SUCCESS;
}
if (PG(modules_activated)) {
/* request_parse_body(). Do not run here as it corrupts the session. */
return SUCCESS;
}
switch (event) {
case MULTIPART_EVENT_START: {
/* This runs before RINIT, so we need to clear these early */
php_rinit_session_globals();
/* ... */
}
break;
case MULTIPART_EVENT_END: {
/* ... php_session_rfc1867_cleanup(progress) ... */
/* $_SESSION was populated in incomplete_classes_only mode */
zend_string *session_var_name = ZSTR_INIT_LITERAL("_SESSION", 0);
zend_delete_global_variable(session_var_name);
zend_string_release_ex(session_var_name, 0);
}
break;
}
}
Second, php_session_rfc1867_update() and php_session_rfc1867_cleanup() no longer ignore the return value of php_session_initialize(). Previously, both functions unconditionally forced PS(session_status) = php_session_active and called php_session_flush(1) even when session initialization failed. Both functions now abort immediately when php_session_initialize() fails:
1
2
3
4
5
6
/* ext/session/session.c: Patched php_session_rfc1867_update() & php_session_rfc1867_cleanup() */
PS(in_rfc1867_update) = true;
if (php_session_initialize() == FAILURE || PS(session_status) != php_session_active) {
PS(in_rfc1867_update) = false;
return;
}
Third, while PS(in_rfc1867_update) is true, the new helpers php_session_unserialize_init() and php_session_serialize_init() call php_var_unserialize_set_incomplete_classes_only(*var_hash, true) and php_var_serialize_set_incomplete_classes_only(*var_hash, true). Any objects stored in the session are decoded and re-encoded as __PHP_Incomplete_Class, preventing class autoloaders, unserialize_callback_func, __wakeup(), and __unserialize() from executing before RINIT.
Defensive mitigations when immediate patching is delayed
Upgrading to PHP 8.2.35+, 8.3.36+, 8.4.27+, or 8.5.12+ is the only complete fix because HashTable and unserialize() OOM paths also exist after startup when PHP code processes untrusted input.
If a production fleet cannot be upgraded immediately, apply these php.ini mitigations to cut off the unauthenticated Pre-RINIT attack surface:
; Disables php_session_rfc1867_callback() in Pre-RINIT (blocks the primary PHP2Shell vector)
session.upload_progress.enabled = 0
; On PHP 8.5 only: disables the secondary fatal-error backtrace stale-frame surface
fatal_error_backtraces = 0
In php-fpm pool configs (www.conf), keeping pm.max_requests moderate and monitoring worker logs for repeated Allowed memory size of ... bytes exhausted errors or SIGSEGV (exit code 139 / signal 11) during Unknown on line 0 (PHP’s signature log location for Pre-RINIT crashes before any script is loaded) also gives defenders a high-signal indicator of active probing.
