ext/sysvshm: don't orphan the segment shm_attach() just created - #22959
Closed
iliaal wants to merge 1 commit into
Closed
ext/sysvshm: don't orphan the segment shm_attach() just created#22959iliaal wants to merge 1 commit into
iliaal wants to merge 1 commit into
Conversation
When the key does not exist yet, shm_attach() creates the segment with IPC_CREAT | IPC_EXCL and then attaches to it. If shmat() fails the segment stays behind with no PHP handle able to remove it, and the existence probe finds it on every later call, so a permission mask that denies the caller wedges the key until ipcrm. Remove the segment on that failure; IPC_EXCL makes the ownership flag exact, so one we opened rather than created is never touched. Closes phpGH-22959
iliaal
force-pushed
the
fix/sysvshm-orphan-segment
branch
from
July 30, 2026 23:45
2426687 to
4854c86
Compare
devnexen
approved these changes
Jul 31, 2026
devnexen
added a commit
to devnexen/php-src
that referenced
this pull request
Aug 1, 2026
shm_attach() wrote the requested size into the header of a segment it did not create, so a foreign segment framed as larger than it is let shm_put_var() write past the mapping. Take the size from shmctl() IPC_STAT instead, and reject a segment too small to hold the header. Noticed while reviewing phpGH-22959.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
shm_attach()probes withshmget($key, 0, 0)and, when that fails, creates the segment withIPC_CREAT | IPC_EXCLbefore attaching. A failingshmat()then returns false and drops the id, so the segment it created stays in the kernel with nothing on the PHP side able to remove it. The next call probes successfully, finds the orphan and fails on the same shmat(), so the key stays wedged until someone runs ipcrm:IPC_EXCL is what makes the ownership flag safe here. A successful probe means the segment predates the call, so only the create branch sets it, and the removal can't destroy a segment we merely opened. ext/opcache/shared_alloc_shm.c does the same IPC_RMID on shmat() failure without an ownership check, since it only ever creates its own.
shmop_open() has the same defect on three exits after a create and cannot take this guard as-is: its "c" mode passes IPC_CREAT without IPC_EXCL, so one shmget() can't tell a create from an open. That needs its own change.