From 4f10c04f1aaafc3753ad3eee3265b03ca6387341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 24 Nov 2026 08:36:51 +0100 Subject: [PATCH 1/2] sysvshm: Fix overflow after a writer is killed 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 https://github.com/php/php-src/pull/25627. --- ext/sysvshm/sysvshm.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/sysvshm/sysvshm.c b/ext/sysvshm/sysvshm.c index 9c68c09147d4..669593b08eb6 100644 --- a/ext/sysvshm/sysvshm.c +++ b/ext/sysvshm/sysvshm.c @@ -416,8 +416,10 @@ static int php_put_shm_data(sysvshm_chunk_head *ptr, zend_long key, const char * shm_var->length = len; shm_var->next = total_size; memcpy(&(shm_var->mem), data, len); - ptr->end += total_size; + /* Never let free overstate the space left, the segment outlives a killed writer */ ptr->free -= total_size; + ZEND_ATOMIC_FENCE_RELEASE(); + ptr->end += total_size; return 0; } /* }}} */ @@ -454,7 +456,7 @@ static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key) static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos) { sysvshm_chunk *chunk_ptr, *next_chunk_ptr; - zend_long memcpy_len; + zend_long memcpy_len, chunk_size; ZEND_ASSERT(ptr); @@ -462,11 +464,13 @@ static int php_remove_shm_data(sysvshm_chunk_head *ptr, zend_long shm_varpos) next_chunk_ptr = (sysvshm_chunk *) ((char *) ptr + shm_varpos + chunk_ptr->next); memcpy_len = ptr->end-shm_varpos - chunk_ptr->next; - ptr->free += chunk_ptr->next; - ptr->end -= chunk_ptr->next; + chunk_size = chunk_ptr->next; if (memcpy_len > 0) { memmove(chunk_ptr, next_chunk_ptr, memcpy_len); } + ptr->end -= chunk_size; + ZEND_ATOMIC_FENCE_RELEASE(); + ptr->free += chunk_size; return 0; } /* }}} */ From 2d9734dd9e2d4ba410b1d8417b0406394a814376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 25 Nov 2026 15:52:22 +0100 Subject: [PATCH 2/2] sysvshm: Validate chunk bounds on lookup 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. --- ext/sysvshm/sysvshm.c | 12 +++++--- ext/sysvshm/tests/shm_corrupted_chunk.phpt | 35 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 ext/sysvshm/tests/shm_corrupted_chunk.phpt diff --git a/ext/sysvshm/sysvshm.c b/ext/sysvshm/sysvshm.c index 669593b08eb6..32fe8eb5ea63 100644 --- a/ext/sysvshm/sysvshm.c +++ b/ext/sysvshm/sysvshm.c @@ -439,14 +439,18 @@ static zend_long php_check_shm_data(sysvshm_chunk_head *ptr, zend_long key) return -1; } shm_var = (sysvshm_chunk*) ((char *) ptr + pos); + /* A writer killed while removing a variable leaves garbage after the moved chunks */ + if (ptr->end - pos < (zend_long) sizeof(sysvshm_chunk) + || shm_var->next < (zend_long) sizeof(sysvshm_chunk) + || shm_var->next > ptr->end - pos + || shm_var->length < 0 + || shm_var->length > shm_var->next - (zend_long) XtOffsetOf(sysvshm_chunk, mem)) { + return -1; + } if (shm_var->key == key) { return pos; } pos += shm_var->next; - - if (shm_var->next <= 0 || pos < ptr->start) { - return -1; - } } return -1; } diff --git a/ext/sysvshm/tests/shm_corrupted_chunk.phpt b/ext/sysvshm/tests/shm_corrupted_chunk.phpt new file mode 100644 index 000000000000..2e03dcbe00a8 --- /dev/null +++ b/ext/sysvshm/tests/shm_corrupted_chunk.phpt @@ -0,0 +1,35 @@ +--TEST-- +Chunks with out of bounds sizes are ignored +--EXTENSIONS-- +sysvshm +shmop +--FILE-- + +--EXPECTF-- +bool(false) + +Warning: shm_remove_var(): Variable key 1 doesn't exist in %s on line %d +bool(false) +bool(true) +--CLEAN-- +