Skip to content

Fix use after free when WeakMap is modified during field write #7671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
116 changes: 116 additions & 0 deletions Zend/tests/weakrefs/weakrefs_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
--TEST--
WeakReference overwriting existing value
--FILE--
<?php

class HasDtor {
public function __destruct() {
echo "In destruct\n";
global $w, $all;
for ($i = 0; $i < 10; $i++) {
$v = new stdClass();
$all[] = $v;
$w[$v] = $i;
}
}
}
$all = [];
$w = new WeakMap();
$o = new stdClass();

$w[$o] = new HasDtor();
$w[$o] = 123;
var_dump($w);
?>
--EXPECT--
In destruct
object(WeakMap)#1 (11) {
[0]=>
array(2) {
["key"]=>
object(stdClass)#2 (0) {
}
["value"]=>
int(123)
}
[1]=>
array(2) {
["key"]=>
object(stdClass)#4 (0) {
}
["value"]=>
int(0)
}
[2]=>
array(2) {
["key"]=>
object(stdClass)#5 (0) {
}
["value"]=>
int(1)
}
[3]=>
array(2) {
["key"]=>
object(stdClass)#6 (0) {
}
["value"]=>
int(2)
}
[4]=>
array(2) {
["key"]=>
object(stdClass)#7 (0) {
}
["value"]=>
int(3)
}
[5]=>
array(2) {
["key"]=>
object(stdClass)#8 (0) {
}
["value"]=>
int(4)
}
[6]=>
array(2) {
["key"]=>
object(stdClass)#9 (0) {
}
["value"]=>
int(5)
}
[7]=>
array(2) {
["key"]=>
object(stdClass)#10 (0) {
}
["value"]=>
int(6)
}
[8]=>
array(2) {
["key"]=>
object(stdClass)#11 (0) {
}
["value"]=>
int(7)
}
[9]=>
array(2) {
["key"]=>
object(stdClass)#12 (0) {
}
["value"]=>
int(8)
}
[10]=>
array(2) {
["key"]=>
object(stdClass)#13 (0) {
}
["value"]=>
int(9)
}
}
6 changes: 5 additions & 1 deletion Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,12 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval

zval *zv = zend_hash_index_find(&wm->ht, (zend_ulong) obj_key);
if (zv) {
zval_ptr_dtor(zv);
/* Because the destructors can have side effects such as resizing or rehashing the WeakMap storage,
* free the zval only after overwriting the original value. */
zval zv_orig;
ZVAL_COPY_VALUE(&zv_orig, zv);
ZVAL_COPY_VALUE(zv, value);
zval_ptr_dtor(&zv_orig);
return;
}

Expand Down