Skip to content

sysvshm: Fix overflow after a writer is killed - #25741

Open
dunglas wants to merge 2 commits into
php:PHP-8.4from
dunglas:fix/sysvshm-kill-safety
Open

dunglas wants to merge 2 commits into
php:PHP-8.4from
dunglas:fix/sysvshm-kill-safety

Conversation

@dunglas

@dunglas dunglas commented Nov 25, 2026

Copy link
Copy Markdown
Member

While I was working on the removal of Zend Signals (#25627), @bukka found an atomicity problem with interned strings. I looked for similar patterns in the code base. shm_put_var() advanced the end of the data before shrinking the free space, and removing a variable grew the free space before moving the end back. A process killed between the two stores, by SIGKILL or the OOM killer, left a segment whose free space overstated what was left. The segment outlives the process, so later shm_put_var() calls wrote past its end.

This is independent of Zend Signals: sysvshm never blocked interruptions, and SIGKILL can't be deferred.

This PR orders the stores so that the free space never exceeds what is left. It also stops the lookup at the first chunk whose size or length doesn't fit in the used area, because a writer killed during a removal leaves stale bytes after the moved variables, and the lookup trusted the sizes it read there.

Reproduced on PHP-8.4 with gdb by sending SIGKILL between the two stores; the new test covers the lookup with a chunk corrupted through shmop.

shm_put_var() advanced the end of the data before shrinking the free
space, and removing a variable grew the free space before moving the
end back. A process killed between the two stores, by SIGKILL or the
OOM killer, left a segment whose free space overstated what was left.
The segment outlives the process, so later shm_put_var() calls wrote
past its end.

Order the stores so that the free space never exceeds what is left. A
kill in the middle of a removal can still leave the moved variables
inconsistent.

Found while working on the removal of Zend Signals in php#25627.
A writer killed while removing a variable leaves bytes that look like
chunks: the stale tail after the moved chunks, or a half moved chunk.
The lookup trusted their sizes, so reading or replacing such a chunk
read past the segment or moved its end out of bounds. Stop the lookup
at the first chunk whose size or length does not fit the used area.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant