Lazy object revert init with object destructor can lead to double free
Description
Originally reported by @cnwangjihe.
The following code:
<?php
class A {
public $b;
}
class B {
public function __construct(public $a) {}
public function __destruct() {
unset($this->a->b);
}
}
$r = new ReflectionClass(A::class);
$a = $r->newLazyGhost(function (A $a) {
$a->b = new B($a);
throw new Exception();
});
$a->any;
Resulted in this output:
=================================================================
==86350==ERROR: AddressSanitizer: heap-use-after-free on address 0x6ca445bf6820 at pc 0x5b90bfecc629 bp 0x7fff60ef9820 sp 0x7fff60ef9818
READ of size 4 at 0x6ca445bf6820 thread T0
#0 0x5b90bfecc628 in zend_gc_delref /home/ilutov/Developer/php-src/Zend/zend_types.h:809
#1 0x5b90bfecee85 in zend_objects_store_del /home/ilutov/Developer/php-src/Zend/zend_objects_API.c:179
#2 0x5b90bff640be in rc_dtor_func /home/ilutov/Developer/php-src/Zend/zend_variables.c:56
#3 0x5b90bfed1701 in i_zval_ptr_dtor /home/ilutov/Developer/php-src/Zend/zend_variables.h:44
#4 0x5b90bfed33e5 in zend_object_dtor_property /home/ilutov/Developer/php-src/Zend/zend_objects.c:72
#5 0x5b90bfe9092f in zend_lazy_object_revert_init /home/ilutov/Developer/php-src/Zend/zend_lazy_objects.c:419
#6 0x5b90bfe94ecb in zend_lazy_object_init /home/ilutov/Developer/php-src/Zend/zend_lazy_objects.c:664
#7 0x5b90bfeb1e70 in zend_std_read_property /home/ilutov/Developer/php-src/Zend/zend_object_handlers.c:986
#8 0x5b90bfc57db8 in ZEND_FETCH_OBJ_R_SPEC_CV_CONST_INLINE_HANDLER /home/ilutov/Developer/php-src/Zend/zend_vm_execute.h:42312
#9 0x5b90bfcf8b15 in execute_ex /home/ilutov/Developer/php-src/Zend/zend_vm_execute.h:114704
#10 0x5b90bfcfdca2 in zend_execute /home/ilutov/Developer/php-src/Zend/zend_vm_execute.h:115646
#11 0x5b90bff8ea2f in zend_execute_script /home/ilutov/Developer/php-src/Zend/zend.c:1972
#12 0x5b90bf487e7b in php_execute_script_ex /home/ilutov/Developer/php-src/main/main.c:2655
#13 0x5b90bf488528 in php_execute_script /home/ilutov/Developer/php-src/main/main.c:2695
#14 0x5b90bff97072 in do_cli /home/ilutov/Developer/php-src/sapi/cli/php_cli.c:947
#15 0x5b90bff9adfc in main /home/ilutov/Developer/php-src/sapi/cli/php_cli.c:1370
But I expected this output instead:
What happens is roughly:
- The initializer is called.
$aand$bform a cycle.- The initializer throws, starting the reverting process.
- The new value of the property
$bis destroyed. - Being the only reference,
B::__destruct()is called. Before that, the refcount of$bis set to 1. $a->bhas not been updated yet, sounset($this->a->b);can decrement$b's refcount again.- It reaches 0 again, skipping the destructor that has been called already, and freeing
$b. - When we exit and come back to the destructor path, accessing
$btriggers a UAF.
Naive fix (can likely be restricted to just lazy objects):
diff --git a/Zend/zend_objects.c b/Zend/zend_objects.c
index 2fc264742cd..991bffc8fc4 100644
--- a/Zend/zend_objects.c
+++ b/Zend/zend_objects.c
@@ -69,7 +69,7 @@ void zend_object_dtor_property(zend_object *object, zval *p)
ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
}
}
- i_zval_ptr_dtor(p);
+ zval_ptr_safe_dtor(p);
}
}
PHP Version
PHP 8.4+
Operating System
No response
Hi, while testing lazy object teardown on 8.5.13 (46bcb661d) I reproduced this UAF at additional destruction sites and variants:
- revert_init():434 <- init():647, the original report (ghost + throw + destructor)
- make_lazy():351 (reset + destructor)
- init_proxy():546, dynamic properties cleanup of a successfully initialized proxy. No throw needed. PR #22401's deferred-garbage hunk (@@ -529) covers this site; the original report only demonstrates the ghost path.
- init_proxy():553, declared typed properties only: a factory that writes the proxy's own typed slot triggers the UAF with zero dynamic properties (requires the factory to touch the proxy).
Additional generalizations on 8.5.13, all ASAN-reproduced:
- A userland __destruct is not required: an internal destructor suffices. A suspended Fiber inside the initializer plus an unset during revert gives a heap-use-after-free WRITE of size 4 in zend_vm_execute.h (zend_dispatch_try_catch_finally_helper_SPEC): the finally's unset re-destroys the Fiber object, and its own VM frame then writes into the freed object.
- A throw is not required either: revert_init():434 is also reached via a non-NULL initializer return, via abandoning the initializing Fiber, and via exit().
The code is line-identical between 8.4.28 and 8.5.13 (zero dtor-related diff between the tags), so both release lines are affected. Minimal repro (5 lines):
class A { public $b; }
class B { public function __construct(public $a) {}
public function __destruct() { unset($this->a->b); } }
$a = (new ReflectionClass(A::class))->newLazyGhost(
function ($o) { $o->b = new B($o); throw new Exception; });
$a->b;
All four sites are covered by the deferred-garbage pattern in PR #22401, so this is one more reason to merge it. Hope this helps.
Hi, follow-up to my earlier comment: we generalized the trigger away from crafted destructors. Six representative natural cleanup patterns were tested across ghost/throw, ghost/non-NULL-return, proxy/success and reset paths on 8.5.13 (46bcb661d) -- five of six fire the UAF on all four paths (20/24 cells):
- bidirectional relation cleanup (Doctrine-like: a Customer destructor syncing
$this->order->customer = nullon the lazy Order) - set-hook side effects (a write through the hook that clears a cache slot)
- method cleanup chains (destructor calls $lazy->release(), which nulls fields -- no direct slot access needed)
- collection self-unlink (a child destructor removing itself from the parent's collection stored on the lazy object)
- finalizer closures (destructor invokes a
use($lazy)closure that clears slots)
Only a WeakMap registry that keeps a strong reference (delaying destruction) is safe. Minimal repro is plain ORM-style code:
class Customer { public ?Order $order = null;
function __construct(?Order $o = null) { $this->order = $o; }
function __destruct() { if ($this->order) $this->order->customer = null; } }
class Order { public ?Customer $customer = null; }
$order = (new ReflectionClass(Order::class))->newLazyGhost(function ($o) {
$o->customer = new Customer($o);
throw new Exception('init failed'); // or return 42; -- throw not required
});
$order->customer;
ASAN: heap-use-after-free READ in zend_gc_delref via objects_store_del <- zend_object_dtor_property <- zend_lazy_object_revert_init():434. Also confirmed the code is line-identical between 8.4.28 and 8.5.13, so both release lines are affected.
The deferred-garbage pattern in PR #22401 clears each slot before running the garbage destructors, which neutralizes all of these re-entry vectors (verified by code reading against each pattern), so this is additional support for merging. Hope this helps.