Skip to content
RCE Write-up Advanced

php2shell: anatomy of two PHP lifecycle bugs

Two unauthenticated remote code execution vulnerabilities in PHP arise from how the interpreter handles memory when a request is interrupted by a fatal error: one from out-of-memory corruption during body parsing, and one from stale worker-global state that persists across requests. An attacker could achieve code execution on mod_php, and potentially leak memory or execute code on other SAPIs under certain conditions.

Keiran Smith Follow

Published on offsec.com

How out-of-memory corruption and stale worker state became two critical PHP RCEs and why the original exploit theory got it wrong.

Keiran Smith, on offsec.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the root cause is that PHP's memory manager uses longjmp() to abort on out-of-memory, leaving half-built data structures, and that worker globals pointing into per-request memory are not always reset before the next request starts.

Impact

On Apache mod_php, an attacker could achieve full remote code execution with default settings; on PHP-FPM, a complete exploit has not been demonstrated, but memory disclosure and potential code execution may be possible under certain conditions.

What the attacker needed

PHP version before the fixed releases; mod_php or PHP-FPM with persistent workers; attacker can send HTTP requests; for 619, requests must hit the same worker

The fix

The vulnerabilities were fixed in PHP 8.2.35, 8.3.36, 8.4.27, and 8.5.12 by replacing longjmp() on out-of-memory with a delayed error mechanism and by resetting worker globals that could be accessed before request initialisation.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

PHP normally cleans up after each request, but if a request crashes in a specific way (like running out of memory), some internal pointers may be left pointing to freed memory. The next request on the same worker process can then read or use that stale data, which can lead to an attacker running their own code on the server. This is serious because it happens before any application code runs, so normal security checks in the app cannot stop it.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.