Back Depthfirst phpAfterShell: From Interrupted Session Writes to Remote Code Execution
TL;DR: PHP can update a file-backed session while parsing an unauthenticated multipart request, even before the target script begins execution. We discovered reliable methods to interrupt the file operation during session upload-progress updates, allowing us to splice the prefix of one valid session with the suffix of another. A subsequent session operation treats the corrupted data as legitimate server-side state, giving attackers control over session values and the ability to trigger object deserialization. By chaining this primitive with a memory-corruption in a PHP-native class, we achieved remote code execution. We reported this vulnerability to the PHP team, and it was assigned CVE-2026-126714 , with a critical CVSS score of 9.2 .
php2shell drew attention to an unusual part of PHP’s request lifecycle: while PHP is still parsing an unauthenticated multipart request, before the target script even begins, the upload-progress callback can open and decode a file-backed Session. The original exploit took advantage of objects becoming active during this callback, and PHP addressed it by restricting class resolution during RFC1867 upload-progress updates.
When we started looking into the patched implementation, we were curious what happened to the Session data beyond the deserialization step. Even with the new restrictions in place, the callback still reads the existing Session, adds upload-progress state, serializes the whole thing, and writes it back to the same file. In other words, while the patch changed which objects could be created in memory, the underlying file update behavior remained untouched. This raised a different question: what actually happens to the Session file if the PHP worker dies in the middle of an update?
To answer that, we had to look beyond the PHP code itself. PHP serializes the updated Session into a valid value, but the file handler writes it to the existing inode using pwrite() and ftruncate() . These operations are not atomic, and an interrupted update doesn’t necessarily leave the file in either its old or new state. Depending on where the interruption happens, part of the new Session may already have been written while bytes from the Session remain on disk. The result can be a Session file containing the prefix of the new serialized data and the tail of the old one. Both original Sessions are valid, and surprisingly, their combination can also be valid, but with a completely different meaning.
This is where things got interesting. By carefully choosing the length fields in the new prefix, an attacker can make PHP’s Session decoder interpret leftover bytes that were previously just part of an ordinary string as separate Session entries and serialized objects. Nothing needs to be deserialized during the upload-progress callback itself. The attacker only needs to leave the right bytes on disk and wait for the application to call session_start() , at which point PHP reads the modified Session and deserializes those objects.
This gave us a new exploitation path that the php2shell patch did not address. The patch prevented objects from becoming active during the RFC1867 callback, but it couldn’t prevent an interrupted file update from leaving behind a different, valid Session. What began as a closer look at a PHP patch eventually led us through the Session serialization format, the file handler’s write behavior, and all the way down to how the kernel handles interrupted buffered writes.
Before the Script Begins
Most PHP developers meet file uploads through $_FILES . By the time application code reads that array, PHP has already parsed the multipart body. The work happens during request startup in the RFC1867 parser.
At the start of a file part, the callback constructs a Session value containing byte counters and file metadata. The multipart field name and filename become strings inside that value:
This gave us the first reachability result. An application does not need an upload controller or a call to session_start() on the multipart endpoint. PHP itself is the Session writer. A request only needs to reach the Web SAPI as multipart/form-data with upload progress enabled.
Two Request Paths, One Inode
The callback assigns the selected SID to PS(id) . With session.save_handler=files , the upload request and a later application request resolve the same path of /sess_
The first writer is RFC1867. It opens the file during multipart parsing, reads the current Session, adds or removes the progress key, serializes the result, and writes it back. The second writer is the application. When a later route calls session_start() , it opens the same file and decodes it into $_SESSION .
The default php Session serializer is used on both paths. Upload completion also removes the progress entry and flushes the whole Session again. Each progress update therefore operates on the complete file rather than an isolated progress record.
PHP takes an exclusive flock() while a worker owns the Session. That prevents cooperating workers from writing at the same time. It does not roll back bytes. If the worker disappears, the kernel releases the lock and leaves whatever the inode contains at that moment.
This shifted the question. Concurrency between writers was not required to mix two values. A single worker could modify part of the live inode and die before PHP finished the update.
A Session File Is a Parser Input
The file handler does not store a PHP array as one opaque blob. With session.serialize_handler=php , it concatenates records in this form:
The decoder scans to | for the key, then hands the following bytes to PHP’s unserializer. Once that value ends, the byte starts another Session key. There is no file-level length, checksum, generation number, or commit marker.
A small Session may look like this:
The productions become more significant as the parser moves from simple values to structured state:
N , b , i , and d create scalar values.
s carries a byte length. That length decides where parsing resumes.
a carries an element count and recursively decodes its children.
O , C , and E enter class reconstruction, custom serialization, or enum handling.
R and r connect the new value to something decoded earlier in the stream.
The length fields supplied the missing piece. Bytes can be syntactically active at one offset and inert at another. A serialized object placed inside a sufficiently long string is only data. Change the length in a prefix, and the same bytes can become the beginning of the top-level record.
This produced a concrete target. We did not need arbitrary file contents. We needed a prefix from replacement state R to move the decoder onto a chosen tail from carrier state C .
Following the Update into the Kernel
The file handler writes the serialized replacement into the existing Session inode with pwrite() at offset zero:
At the PHP level, this looks like one operation. At the kernel boundary, a large buffered write may copy many pages into the page cache before the syscall returns. If the process dies during that copy, the inode can already contain a prefix of the new buffer. PHP never receives a short-write result because there is no process left to receive it.
Our first hypothesis used PHP’s execution timeout. It was attractive because the timer is remotely controllable through parser work. Tracing showed why the early experiments were inconsistent. On our Linux test systems, the timeout became actionable after the buffered pwrite() returned. The file contained all of R , not a partial prefix.
That failure narrowed the problem. To interrupt the copy itself, the worker had to disappear inside the syscall. An OOM kill supplied that behavior for one family of releases. A separate source change created another family where a complete pwrite() was enough because truncation happened in a later syscall.
Timeout delivery and an uncatchable OOM kill interact differently with a buffered pwrite() .
The root cause stayed the same in both cases. PHP updated the live inode in place and treated completion of several filesystem operations as one logical Session commit.
Two Ways to Keep the Old Tail
Let C be the carrier Session already stored on disk and R the replacement PHP wants to commit. The order of pwrite() and ftruncate() creates two exploitable states.
Overview: the two interruption points produce different combinations of replacement R and carrier C.
Variant A: The Write Stops Partway
In the first ordering, PHP truncates before writing only when the replacement is shorter than the size cached when the file was opened:
If R is at least as long as C , the old inode remains in place when pwrite() begins. A worker killed after copying k bytes leaves:
Variant A: termination during pwrite() leaves a prefix of replacement R joined to the retained carrier C tail.
The useful insight was that k did not need to be one exact offset. We shaped the serialized grammar so that a range of partial prefixes reached the same retained record. This converted a narrow kernel event into a wider parser acceptance interval.
Variant B: The Write Finishes Before Truncation
A later ordering writes first and truncates the old tail only after pwrite() reports success:
When R is shorter than C , a complete write temporarily creates:
The syscall removes the tail. If the worker terminates after pwrite() and before ftruncate() , the temporary state becomes durable.
Variant B: a complete shorter pwrite() leaves the carrier C tail until ftruncate() .
This changes the exploitation problem. Variant A needs termination during a buffered copy. Variant B needs termination between two syscalls. The second window is still small, but it no longer depends on a partial kernel copy.
Commit c2b02f24dff separates these orderings in the affected source history.
Building a Third Valid Session
The carrier Session C contains the target record inside a length-delimited string. Parsed on its own, the object bytes are inert. The replacement R changes the grammar immediately before the retained tail. After an interrupted update, the decoder reaches the tail as a new top-level Session key.
Both source states are generated by PHP from ordinary multipart fields. Neither source file is malformed. The malformed state exists only at the storage boundary, where bytes from the two serialized values meet.
The public proof uses a harmless object:
A later route contains only normal Session code:
If the splice misses early, the consumer sees the carrier state. If it misses late, it sees the replacement. A hit creates $_SESSION['evil'] as a real object. Those three outcomes gave the exploit a stronger oracle than socket timing alone.
Finding the Windows over HTTP
A fixed delay worked on one machine and failed on another. CPU speed was only part of the reason. Apache scheduling, page-cache state, allocator pressure, multipart parsing, and the exact moment a worker crossed its limit all moved the boundary.
We stopped treating the interruption point as a constant. The controller treated it as a moving boundary that could be measured through application state.
Each candidate uses a fresh SID and three requests against the same sess_ file:
A carrier POST writes C through upload progress.
A replacement POST asks the same callback to write R .
A consumer GET calls session_start() and classifies the resulting Session.
Remote calibration: carrier C, replacement R, and the consumer oracle across both write windows.
The HTTP connection tells us whether a worker completed or disappeared. The consumer tells us what happened to the file. Together they distinguish an early termination, a late termination, and a splice that crossed the parser acceptance interval.
Calibrating Variant A
For the partial-write family, held multipart requests bring the Apache cgroup close to its memory boundary. The carrier and replacement keep their important grammar at a fixed geometry. Two replacement lanes cross a barrier together so that an allocation in one worker can push another worker over the boundary while it is writing.
The controller adjusts pressure at two scales. Coarse slots move in 2 MB steps. A tunable request covers the smaller interval between them. Complete responses and disconnected sockets feed a rolling history. Stable observations move the coarse allocation count. Mixed observations are fitted with a recency-weighted logistic model, then sampled near the estimated transition at 32 KiB offsets.
The model handles a mismatch between two kinds of state. The memory limit is discrete from the attacker’s perspective, while the amount already committed by Apache and PHP is noisy. Re-estimating the transition after each observation is faster than scanning the full range again. Dead pressure connections are replaced so the controller keeps approximately the same background state.
Variant A calibration loop: coarse pressure, a fine allocation, synchronized replacement lanes, and a recency-weighted boundary estimate.
Kernel probes showed partial prefixes near 2 MiB, 4 MiB, and 5.5 MiB. The grammar accepts a span around those regions. In a fresh HTTP run, the object appeared on attempt 11.
Calibrating Variant B
The post-write window needs a different control variable. A legal multipart field name containing repeated bracket notation makes PHP spend CPU in request parsing before it flushes upload progress. The field name never enters the carrier record, so its depth changes timing without changing the target grammar.
Shallow inputs terminate before the replacement is written. Deep inputs pass the truncate. Between them lies the state where pwrite() has completed and ftruncate() has not.
The full depth range is not perfectly monotonic. We first locate a small region where outcomes are locally ordered. A particle filter then tracks the boundary. Its state includes drift and an outlier component because neighboring requests can land on different Apache scheduling histories. Surprise-driven updates widen the after an unexpected result. Systematic resampling keeps candidates concentrated around the most informative depths.
Variant B calibration loop: bracket a local phase axis, probe one depth, classify the Session, and update the particle population.
This approach raised the simulated 24-attempt hit rate from 52.8% to 88.4%. A fresh HTTP run reached the target state in ten attempts after one coarse rebase. The controller learns the current host instead of carrying a timing constant from the development machine.
The Request Changes the Meaning of the Bytes
The interrupted upload leaves the mixed file in session.save_path . The request presents the same SID and calls session_start() . PHP reads the file as trusted server-side Session state and runs the normal decoder.
The earlier php2shell remediation restricts class resolution while the upload-progress callback is active. On patched builds, an object handled during that callback is transported as __PHP_Incomplete_Class . Its serialized representation still reaches the file handler. Once RFC1867 has finished, the restriction is gone. A later session_start() resolves the record through the ordinary class path.
The splice is useful before objects enter the picture. Scalars and arrays can replace identity, authorization, CSRF, password-reset, cart, or workflow state before application code performs its access checks. An object adds a path into magic methods and native decoders.
At this point the storage bug becomes an arbitrary deserialization primitive. The application contributes only its normal use of Sessions.
From a Native Object to RCE
PHP ships internal classes that reconstruct native state from serialized input. SPL containers, iterators, date and time objects, random engines, and extension-owned wrappers cross from PHP values into C structures. Their decoders manipulate zend_object , HashTable , references, native pointers, property tables, and length fields.
We audited that surface as a separate problem. The Session file could carry an object. The remaining question was how much power PHP’s own classes exposed when an attacker controlled its serialized representation.
That audit produced a chain using native PHP decoders. A memory-corruption vulnerability supplies the first primitive. Later stages disclose the addresses required by the worker and redirect an internal call. The object from the Session tail enters the chain during session_start() and ends in code execution inside the PHP process.
The decoder and final heap layout remain reserved for coordinated disclosure. The full chain does not depend on an application-defined gadget class.
Full-chain Demonstration
On Exploitation and Remediation in the AI Era
Over the years, PHP security research has produced all kinds of interesting tricks, often far more memorable than the vulnerabilities themselves. Deserialization techniques evolved from userland gadget chains to PHP-native objects and unexpected entry points like PHAR metadata. Researchers have found clever ways around open_basedir and disable_functions , while combinations of php://filter and character encoding tricks turned seemingly limited LFI vulnerabilities into RCE. Many of these techniques also came out of CTFs, where unusual constraints pushed researchers to explore obscure PHP behaviors and combine things that weren’t originally meant to work together. Much of this work happened long before AI became part of security research, through manual code review, experimentation, and a lot of trial and error. What really gets researchers excited has never been another CVE number or a single bug. It’s those clever tricks that connect things nobody thought were related, or make something that seemed impossible actually work.
AI changes the pace of that work. A model can hold more of the codebase in view at once, keep several competing ideas alive, and make it practical to run far more experiments than you would write harnesses for by hand. On this bug, fixed timing and pressure values kept failing whenever worker scheduling or memory state drifted; treating every HTTP attempt as an observation, estimating where the boundary had moved, and choosing the request from that estimate only came together after iterating through multiple failed models and kernel probes that would have taken weeks to build manually.
Patching has become faster too, and this creates a new problem. AI can follow a known exploit, add a guard, and write a regression test in a short time. That patch may stop the exploit while leaving the deeper bug in place.
The php2shell patch is a good example. It stopped objects from becoming active during the RFC1867 callback. It did not make the Session file update safe when a worker died. The bytes could remain on disk and become active later, when the application called session_start() . The original exploit stopped working, but the underlying problem was still there.
At depthfirst, we do offensive research with a defensive goal: knowing whether the software we protect is genuinely safe after a patch lands, where a broken PoC is only the first check. When both finding a bug and shipping a fix can happen quickly, reviewing the patch deserves the same care as building the exploit, making sure remediation removes the reason the bug was possible rather than just blocking the first path that reached it.
The source history divides into three groups. Earlier releases use ordinary object semantics during RFC1867. The first remediation changes object transport and retains the truncate-before-write ordering. Later branches move truncation after the write, which changes the interruption window without removing the retained-tail primitive.
The ranges describe official php-src trees as of November 17, 2026. The last row refers to the dedicated EOL mitigation branches. Older unpatched 7.4, 8.0, and 8.1 builds retain the original path. Downstream packages may carry the relevant changes without changing PHP_VERSION .
The full story
This article is one source in a clustered incident — the cluster page carries the summary, timeline and every other outlet covering it.
