Skip to content

Commit 81cafaa

Browse files
committed
Fix uouv on oom on object allocation
Initialize object.handlers to std_object_handlers in zend_object_alloc. This avoids a use-after-free for objects using custom handlers that are installed after allocation, accessing the handlers on shutdown when they haven't been set yet. Fixes phpGH-11734
1 parent b0bc057 commit 81cafaa

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Zend/tests/gh11734.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-11734: Use-of-uninitialized-value when OOM on object allocation
3+
--INI--
4+
memory_limit=2M
5+
--SKIPIF--
6+
<?php
7+
$zend_mm_enabled = getenv("USE_ZEND_ALLOC");
8+
if ($zend_mm_enabled === "0") {
9+
die("skip Zend MM disabled");
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
$objs = [];
15+
while (true) {
16+
$objs[] = new SplPriorityQueue;
17+
}
18+
?>
19+
--EXPECTF--
20+
Fatal error: Allowed memory size of %d bytes exhausted%S (tried to allocate %d bytes) in %s on line %d

Zend/zend_objects_API.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ static zend_always_inline size_t zend_object_properties_size(zend_class_entry *c
9191
static zend_always_inline void *zend_object_alloc(size_t obj_size, zend_class_entry *ce) {
9292
void *obj = emalloc(obj_size + zend_object_properties_size(ce));
9393
memset(obj, 0, obj_size - sizeof(zend_object));
94+
/* Set to std_object_handlers in case there is an OOM error before any other handlers are
95+
* installed. This avoids a use-of-uninitialized-value on shutdown. This would be more fitting in
96+
* zend_object_std_init(), but some extensions set handlers before calling
97+
* zend_object_std_init(). */
98+
((zend_object *)((uintptr_t)obj + obj_size - sizeof(zend_object)))->handlers = &std_object_handlers;
9499
return obj;
95100
}
96101

0 commit comments

Comments
 (0)