[php-src] Issue #15938: Side effects after zend_fetch_property_address() / get_property_ptr_ptr()

From: Date: Tue, 17 Sep 2024 12:54:44 +0000
Subject: [php-src] Issue #15938: Side effects after zend_fetch_property_address() / get_property_ptr_ptr()
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-247815@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/15938 Author: arnaud-lb ### Description In some cases it is possible to modify the object or release it after acquiring the address of a property with zend_fetch_property_address() / get_property_ptr_ptr(), leading to corruptions. Here we remove the b key from the zend_object.properties hashtable, and then write a zval to the slot. This leaks the zval: ``` php <?php #[AllowDynamicProperties] class C { public $a; } $obj = new C(); $obj->b = str_repeat('a', 10); $obj->b .= new class { function __toString() { global $obj; unset($obj->b); return str_repeat('c', 10); } }; ``` Here the zend_object.properties ht is resized, so the address points to a freed block: ``` php <?php #[AllowDynamicProperties] class C { public $a; } $obj = new C(); $obj->b = ''; $obj->b .= new class { function __toString() { global $obj; for ($i = 0; $i < 8; $i++) { $obj->{$i} = 0; } return 'str'; } }; ``` Here the object is released, so the address points to a freed block: ``` php <?php class C { public $a; } $obj = new C(); $obj->a = ''; $obj->a .= new class { function __toString() { global $obj; $obj = null; return 'str'; } }; ``` Using ReflectionClass::resetAsLazy*() in __toString() in these cases will also cause similar issues. This issue exists with ASSIGN_OBJ_OP, ASSIGN_OBJ_REF, maybe ASSIGN_(PRE|POST)_(INC|DEC). I think we are aware of similar issues, but I'm not sure we were aware of these cases particularly. The 3rd case may be resolved with a addref()/delref() of the object like we do in https://github.com/php/php-src/blob/21196ca9f6279ad860dd63ca703df3087347a8cb/Zend/zend_object_handlers.c#L1002 But the two others are trickier. If conversion of the value is the only way to trigger a side effect after acquisition of the address, maybe we can ensure this occurs before that. However we don't always know the required conversion before fetching the property. Or can we prevent any operation on the object, with a flag or by setting zend_object.handlers to error-only handlers temporarily? ### PHP Version master ### Operating System _No response_

« previous php.bugs (#247815) next »