[GIT-PULLS] [php-src] PR #22959: ext/sysvshm: don't orphan the segment shm_attach() just created

[email protected] (iliaal) Thu, 30 Jul 2026 23:45:27 +0000
Newsgroups php.git-pulls
Message-ID <[email protected]>
Pull Request: https://github.com/php/php-src/pull/22959
Author: iliaal

`shm_attach()` probes with `shmget($key, 0, 0)` and, when that fails, creates the segment with `IPC_CREAT | IPC_EXCL` before attaching. A failing `shmat()` 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:

```php
$key = ftok(__FILE__, 't');
var_dump(shm_attach($key, 1024, 0));     // false: mode 0000 denies the owner's own attach
var_dump(shm_attach($key, 1024, 0600));  // false, and stays false on every later run
```

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.