Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions ext/sysvshm/sysvshm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/* }}} */
Expand All @@ -437,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;
}
Expand All @@ -454,19 +460,21 @@ 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);

chunk_ptr = (sysvshm_chunk *) ((char *) ptr + 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;
}
/* }}} */
Expand Down
35 changes: 35 additions & 0 deletions ext/sysvshm/tests/shm_corrupted_chunk.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Chunks with out of bounds sizes are ignored
--EXTENSIONS--
sysvshm
shmop
--FILE--
<?php
$key = 0x53484D33;

$s = shm_attach($key, 1024);
shm_put_var($s, 1, 'a');

$raw = shmop_open($key, 'w', 0, 0);
$next = 8 + 4 * PHP_INT_SIZE + 2 * PHP_INT_SIZE;
shmop_write($raw, pack(PHP_INT_SIZE === 8 ? 'q' : 'l', 0x10000), $next);

var_dump(shm_has_var($s, 1));
var_dump(shm_remove_var($s, 1));
var_dump(shm_put_var($s, 2, 'b'));

shm_remove($s);
?>
--EXPECTF--
bool(false)

Warning: shm_remove_var(): Variable key 1 doesn't exist in %s on line %d
bool(false)
bool(true)
--CLEAN--
<?php
$raw = @shmop_open(0x53484D33, 'w', 0, 0);
if ($raw) {
shmop_delete($raw);
}
?>
Loading