Inside PHP2Shell: the PHP source code and the security patches
Part 2 of our PHP2Shell investigation into CVE-2026-109618 and CVE-2026-109619 - what changed in the PHP source between 8.4.26 and 8.4.27, read file by file, and what a diff can and cannot tell you about a crash.
Key takeaways
- PHP 8.4.26 and 8.4.27 are 166 commits apart. Five core files carry the changes that matter here, covering the allocator, the hash table, the multipart parser, the session extension and request startup.
- In 8.4.26, crossing memory_limit raises a fatal error on the spot. In 8.4.27 the allocation can complete while the error is queued and raised later.
- The hash insertion still counts a slot before it fills it. That code is identical in both releases, so the fix is not a reordering.
- The multipart parser now checks for a pending out-of-memory error after each step and leaves through its normal cleanup path.
- A diff shows what changed and what was intended. It does not show which change stopped the crash in our lab.
On this page4 sections
Recap: what we are looking for
Part 1 left three ideas on the table. This part takes them to the PHP source and to the patch that changed them:
- a lifecycle inconsistency: a request can end with its request heap discarded but without the extension cleanup that normally runs first;
- a counted-but-unfilled slot: a hash insertion increments
nNumUsedbefore the bucket is filled, and teardown trusts the counter; - a nonlocal jump:
zend_bailout()can leave an interrupted function without finishing it.
The question for this part is narrow, and the source can answer it: what is different between PHP 8.4.26, the vulnerable build we studied, and PHP 8.4.27, the patched release, and what does that difference establish?
We keep one distinction in front of us throughout. Reading a diff tells you what the maintainers changed and, from the comments and the shape of the change, what they intended. It does not tell you which changed line stopped a particular crash in a particular lab. That second question is an experiment, and it belongs to Part 3.
What the two advisories say
The PHP project published two advisories on 21 October 2026. Both are rated critical.
| CVE | Advisory | The project's title |
|---|---|---|
| CVE-2026-109618 | GHSA-w8hw-w9fx-j79w | RCE via OOM corruption |
| CVE-2026-109619 | GHSA-6wmf-4rv5-p3rp | RCE via stale global state |
In our words: the first says PHP leaves an out-of-memory error by jumping out of whatever it was doing, and that request memory can be reachable for a moment before it is fully initialized. If an allocation fails in that moment, request shutdown may read memory that was never completed. The second says a worker keeps global state, such as error, session and extension state, that points into one request's memory, and that some operations run before the next request has reset it.
Both advisories describe remote code execution as the worst case. Our lab reproduced the corruption and a worker abort. It did not reproduce code execution. The advisories are public on php-src: GHSA-w8hw-w9fx-j79w and GHSA-6wmf-4rv5-p3rp.
6. Source review and patch comparison
6.1 The code before the patch
Four short excerpts from PHP 8.4.26 set up everything that follows. Part 1 introduced three of them one at a time. Here they are together, as the baseline before the patch.
The lifecycle flag, and the shutdown that depends on it. PHP marks extension startup complete only after it has run, and only runs extension shutdown when that flag is set:
/* main/main.c, php_request_startup() - lines 1873 to 1875 */
php_hash_environment(); /* parses POST, multipart, uploads */
zend_activate_modules(); /* RINIT for every extension */
PG(modules_activated)=1;
/* main/main.c, php_request_shutdown() - lines 1928 to 1930 */
if (PG(modules_activated)) {
zend_deactivate_modules();
}If a fatal error occurs during request parsing, before zend_activate_modules(), extension shutdown is skipped for that request.
A session string allocated during parsing. The upload-progress callback can initialize session state before the application script runs, on the request heap:
/* ext/session/session.c, MULTIPART_EVENT_FILE_START - lines 3287 to 3309, abbreviated */
if (Z_ISUNDEF(progress->data)) {
...
php_rinit_session(0);
PS(id) = zend_string_init(Z_STRVAL(progress->sid), Z_STRLEN(progress->sid), 0);
...
}This runs inside php_hash_environment(), before the flag above is set. The cleanup that would release PS(id) lives in php_rshutdown_session_globals(), an extension shutdown handler, on the gated path.
A hash insertion that counts before it fills, and a teardown that trusts the count:
/* Zend/zend_hash.c, _zend_hash_str_add_or_update_i() - from line 961, abbreviated */
add_to_hash:
idx = ht->nNumUsed++; /* count first */
ht->nNumOfElements++;
p = ht->arData + idx;
p->key = key = zend_string_init(str, len, ...); /* can fail */
...
p->h = ZSTR_H(key) = h;
ZVAL_COPY_VALUE(&p->val, pData);
/* Zend/zend_hash.c, zend_array_destroy() - teardown, simplified */
for (p = ht->arData; p != ht->arData + ht->nNumUsed; p++) {
i_zval_ptr_dtor(&p->val);
if (p->key) { zend_string_release(p->key); }
}The allocator's limit check. In 8.4.26, an allocation that would cross memory_limit calls a function that raises the fatal error and never returns:
/* Zend/zend_alloc.c, zend_mm_alloc_pages() - line 1051, abbreviated */
zend_mm_safe_error(heap,
"Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)",
heap->limit, ZEND_MM_PAGE_SIZE * pages_count); /* ZEND_NORETURN */The fallible mode, the hard limit and the delayed error that Part 1 described in section 5.4 do not exist in this build. The file contains none of them.
Put together, these describe a conditional hazard. A hash insertion interrupted by a bailout between the counter increment and the bucket fill would leave a counted-but-unfilled slot. A request that fails during early parsing would skip the session cleanup that owns PS(id). Whether either one produces an exploitable inconsistency is what the patch, and Part 3, speak to.
6.2 What we compared
We compared the two release tags directly:
git clone --filter=blob:none https://github.com/php/php-src.git
cd php-src
git fetch --depth 1 origin tag php-8.4.26 tag php-8.4.27
git rev-parse php-8.4.26^{} php-8.4.27^{} # peeled commit hashes
git diff --stat php-8.4.26 php-8.4.27 -- Zend ext mainAt the time of writing this resolves to:
php-8.4.26 tag object e857632a369b1c390720d4358fe029afe7af4408
commit c31ae58ecbedc7a85dac0eee5a6a88da73543e70
php-8.4.27 tag object aa58018e5a2d69f3c497aa22a085fc897b5c3df8
commit 00d8bae801541db0d95a698e108bb4b1b273fe6eRecord these when you do this. Release tags can be moved or re-cut in principle, and a report that says "8.4.26 vs 8.4.27" without hashes is harder to audit than one that pins both ends.
The two tags are 166 commits apart, and the comparison lists more than 300 changed files. Most of them are tests. This is an ordinary release with a security fix inside it, not a one-file patch. Five core files carry the changes this series is about:
| File | What it holds | Lines removed | Lines added |
|---|---|---|---|
| Zend/zend_alloc.c | the memory manager | 127 | 532 |
| Zend/zend_hash.c | hash tables, so PHP arrays | 43 | 259 |
| main/rfc1867.c | the multipart parser | 27 | 189 |
| ext/session/session.c | the session extension | 12 | 61 |
| main/main.c | request startup and shutdown | 2 | 48 |
The counts are from a plain diff of each file at the two tags. A different diff tool or option will give slightly different numbers.
The release notes for 8.4.27 credit the fix in one entry, dated 22 October 2026: both advisory identifiers, both CVE numbers, and the phrase "Avoid state corruption via bailout".
6.3 Fallible operations
The hash-table code gains a fallible mode: a flag (HASH_FALLIBLE), a sentinel return value (HASH_NOMEM), and variants of the table operations whose callers can check for failure. zend_hash.c refers to the flag and the sentinel 31 times in 8.4.27 and not at all in 8.4.26.
/* shape of the change, simplified */
if (!create_record()) {
return ERROR;
}An operation that can report failure does not have to jump out half-way. It can leave the table as it was and tell its caller.
There is one thing the patch did not do, and it is worth being exact about. The insertion sequence from section 6.1 is identical in 8.4.27. It starts at line 1122 instead of line 961, and the code is the same: the counter is still incremented first, and the key is still allocated with the ordinary, non-fallible zend_string_init().
So the fix is not a reordering of that insertion, and that insertion does not use the new failure-reporting variants. Whatever protects it has to come from the allocation in the middle no longer jumping out. That is the next change.
6.4 Delayed OOM
The allocator gains the branch Part 1 showed in section 5.4. In 8.4.27 it is zend_mm_check_limit(), at line 533 of Zend/zend_alloc.c:
/* Zend/zend_alloc.c (8.4.27), zend_mm_check_limit() - abbreviated */
if (!heap->fallible && add_size > heap->hard_limit - heap->real_size) {
zend_mm_hard_limit_error(...); /* does not return */
}
if (!ZEND_HAS_OOM()) { zend_mm_delayed_error(...); } /* record it, carry on */
return heap->fallible ? FAILURE : SUCCESS;A non-fallible allocation that crosses the soft limit records the error and returns success. The error is raised later, at a point where the engine checks for it.
| Step | Where |
|---|---|
| Recorded, and a VM interrupt requested | Zend/zend_alloc.c, lines 445 to 447 |
| Raised, by zend_handle_delayed_oom() | called from Zend/zend.c:1944, Zend/zend_execute.c:4087 and Zend/zend_vm_def.h:10431 |
| Cleared | main/main.c, lines 1930 and 2025, in request shutdown |
The consequence for this series: an allocation that crosses the soft limit on a non-fallible path can complete. A bucket interrupted as in section 6.1 can then get its key and its value after all, and the table is consistent by the time the error is raised.
The advisory for CVE-2026-109618 describes the same design in its own words: the jump on out-of-memory is replaced by a delayed error raised at the next safe point in the VM, with an immediate exit if memory use reaches a second, higher threshold. That second threshold is the hard limit. Crossing it does not unwind anything: zend_mm_hard_limit_error() prints a message and ends the worker process with _exit().
We say "can" deliberately. Connecting "this branch exists" to "the crash stopped" is reasoning from source, not an experiment. We did not observe the moment the queued error is raised relative to multipart parsing. Part 3 explains what the experiment does show.
6.5 The multipart parser
The code that was running when our trigger ran out of memory is the multipart parser, main/rfc1867.c. It changed more than the session extension did.
In 8.4.27 the parser checks for a pending out-of-memory error after each step that can allocate, 20 times in all, and leaves through its own cleanup labels when it finds one:
/* main/rfc1867.c (8.4.27) - shape of the change */
if (ZEND_HAS_OOM()) {
goto fileupload_done;
}The helpers that register a parsed variable now return whether an error is pending, and two buffer reallocations are marked fallible. The 8.4.26 version of the file has no such check anywhere.
This fits the allocator change. If the error is queued instead of thrown, something has to notice it. The parser now does, and it stops through its normal exit instead of being cut off in the middle of registering a variable.
6.6 Session safeguards during multipart parsing
The session extension gains a flag, PS(in_rfc1867_update), and routes its serializers through two helpers that switch on "incomplete classes only" while the upload-progress callback runs:
/* ext/session/session.c (8.4.27), lines 970 to 984, abbreviated */
static void php_session_unserialize_init(php_unserialize_data_t *var_hash)
{
PHP_VAR_UNSERIALIZE_INIT(*var_hash);
if (PS(in_rfc1867_update)) {
php_var_unserialize_set_incomplete_classes_only(*var_hash, true);
}
}The serializing side has a matching helper. The intent is stated in the source comment above them: the upload-progress handler decodes and re-encodes the session during request startup, before extension startup, when no user code may run. Objects found in session data are kept as __PHP_Incomplete_Class, with no autoloading and no __wakeup().
Two smaller changes sit next to it. Session initialization now stops with an error when it cannot create a session id, where it used to fall back to creating one. And the upload-progress code resets the session globals early, with a comment saying that it runs before extension startup.
A caution on wording. The source comment also says the objects are written back unchanged. We did not test that. The statement we can support is narrower: during the upload-progress update, the serializers are told not to create application objects.
6.7 Request startup and shutdown
main/main.c changed least, and its changes read like a list of things that must not be left behind:
- a new ini setting,
memory_hard_limit_percent, for the hard limit from section 6.4; - error reporting returns early while an out-of-memory error is pending;
- output buffers are not discarded while an output handler is still running;
- the delayed-error state is cleared at request shutdown;
- each of PHP's request arrays is marked undefined after it is destroyed, and
PG(modules_activated)is set back to 0 at the end of shutdown. Both carry the same comment: "May be accessed before php_request_startup()".
That comment is the second advisory in one line: state that a later request might touch before its own startup has run.
One thing did not change. The conditional from section 6.1 is still there, now at line 1967: extension shutdown is still skipped when extension startup did not finish. The patch did not remove that branch. It changed what can be left lying around when the branch is taken.
6.8 What the patch does and does not establish
These mechanisms explain the design of the fix: a queued out-of-memory error, hash operations that can report failure, a parser that checks, a session extension that will not create objects early, and shutdown code that resets more state.
They do not tell us which one prevented the crash in our lab. The difference we report in Part 3 is real. The causal line from "this line changed" to "the crash stopped" remains a hypothesis supported by reading, not by measurement.
That distinction is worth keeping straight. A changelog can make you feel you have explained a crash when you have only explained a patch.
Which brings us to the question reading cannot answer. The patch gives us several plausible defenses. The vulnerable build is one tag behind. Does the difference appear when you run them, and can it be attributed to anything specific?
What Part 3 tests
Part 3 runs the experiment this part set up: five builds, twenty trials each, a GDB backtrace of the crash, and the two application-level chains that a PHP upgrade does not fix. It spends as much time on what the results do not establish, such as where the corruption is created and which request causes it, as on what they do. It also covers a counting error in our own tooling that halved our first result.
References
- php-src: 8.4.26 compared with 8.4.27 - the full comparison between the two release tags.
- PHP 8.4.27 release notes - the NEWS file shipped with the tag, which ties both advisories to both CVE numbers.
- GHSA-w8hw-w9fx-j79w and GHSA-6wmf-4rv5-p3rp - the two PHP advisories.
- PHP security advisory, 21 October 2026 - the official announcement of the fixes.
- Original PHP2Shell research by Calif - the research this investigation follows.
- Our source-review notes - the working notes behind this part, in the PHP2SHELL repository.
Code excerpts are abbreviated and should be checked against the exact tag you are studying. The commit hashes above let you pin both ends.
Keep reading
- PHP2Shell Part 1: Internals and SessionsThe request lifecycle, sessions, hash tables and out-of-memory handling that this part reads in the source.
- PHP2Shell Part 3: Reproduction and ResultsThe experiment this part sets up, across five builds, and what the results do not establish.
- Session management and logoutSessions from the defender's side, and what sound session handling looks like in an application.