Skip to content

Commit 1740d94

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-16588: UAF in Observer->serialize
2 parents 922bd2f + 3599fd0 commit 1740d94

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

ext/spl/spl_observer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,18 @@ PHP_METHOD(SplObjectStorage, serialize)
833833
RETURN_NULL();
834834
}
835835
ZVAL_OBJ(&obj, element->obj);
836+
837+
/* Protect against modification; we need a full copy because the data may be refcounted. */
838+
zval inf_copy;
839+
ZVAL_COPY(&inf_copy, &element->inf);
840+
836841
php_var_serialize(&buf, &obj, &var_hash);
837842
smart_str_appendc(&buf, ',');
838-
php_var_serialize(&buf, &element->inf, &var_hash);
843+
php_var_serialize(&buf, &inf_copy, &var_hash);
839844
smart_str_appendc(&buf, ';');
840845
zend_hash_move_forward_ex(&intern->storage, &pos);
846+
847+
zval_ptr_dtor(&inf_copy);
841848
}
842849

843850
/* members */

ext/spl/tests/gh16588.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-16588 (UAF in Observer->serialize)
3+
--CREDITS--
4+
chibinz
5+
--FILE--
6+
<?php
7+
8+
class C {
9+
function __serialize(): array {
10+
global $store;
11+
$store->removeAll($store);
12+
return [];
13+
}
14+
}
15+
16+
$store = new SplObjectStorage;
17+
$store[new C] = new stdClass;
18+
var_dump($store->serialize());
19+
20+
?>
21+
--EXPECT--
22+
string(47) "x:i:1;O:1:"C":0:{},O:8:"stdClass":0:{};m:a:0:{}"

0 commit comments

Comments
 (0)