Skip to content

Commit 144d2ee

Browse files
committed
Fix phpGH-16588: UAF in Observer->serialize
Closes phpGH-16600.
1 parent e0a0e21 commit 144d2ee

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ PHP NEWS
102102
(ilutov)
103103
. Fixed bug GH-16479 (Use-after-free in SplObjectStorage::setInfo()). (ilutov)
104104
. Fixed bug GH-16478 (Use-after-free in SplFixedArray::unset()). (ilutov)
105+
. Fixed bug GH-16588 (UAF in Observer->serialize). (nielsdos)
105106

106107
- Standard:
107108
. Fixed bug GH-16293 (Failed assertion when throwing in assert() callback with

ext/spl/spl_observer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,18 @@ PHP_METHOD(SplObjectStorage, serialize)
797797
RETURN_NULL();
798798
}
799799
ZVAL_OBJ(&obj, element->obj);
800+
801+
/* Protect against modification; we need a full copy because the data may be refcounted. */
802+
zval inf_copy;
803+
ZVAL_COPY(&inf_copy, &element->inf);
804+
800805
php_var_serialize(&buf, &obj, &var_hash);
801806
smart_str_appendc(&buf, ',');
802-
php_var_serialize(&buf, &element->inf, &var_hash);
807+
php_var_serialize(&buf, &inf_copy, &var_hash);
803808
smart_str_appendc(&buf, ';');
804809
zend_hash_move_forward_ex(&intern->storage, &pos);
810+
811+
zval_ptr_dtor(&inf_copy);
805812
}
806813

807814
/* 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)