Skip to content

Commit 34811d4

Browse files
committed
Fix use-after-free when assigning to a typed property releases the object
1 parent 5f4d73a commit 34811d4

7 files changed

Lines changed: 188 additions & 54 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHP NEWS
3636
(Marti2203).
3737
. Fixed bug GH-24989 (Use-after-free in GC when an exception is thrown).
3838
(David Carlier)
39+
. Fixed use-after-free when assigning to a typed property releases the
40+
object while the value is coerced. (Alexander Danilov)
3941

4042
- CLI:
4143
. Fixed bug GH-24697 (PHP_HTTP_PARSER_STRICT build option doesn't work).
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
--TEST--
2+
Assigning to a typed property must not use the object after coercion user code released it
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public string $s = "a";
8+
public int $i = 0;
9+
}
10+
11+
class Releaser {
12+
public function __toString(): string {
13+
unset($GLOBALS['a'][0]);
14+
return "5";
15+
}
16+
}
17+
18+
function attempt(Closure $fn) {
19+
try {
20+
$fn();
21+
echo "assigned\n";
22+
} catch (Error $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
}
26+
27+
function assign_tmp(array &$a) { $a[0]->s = new Releaser; }
28+
function assign_cv(array &$a, $v) { $a[0]->s = $v; }
29+
30+
echo "--- __toString()\n";
31+
for ($k = 0; $k < 3; $k++) {
32+
$a = [new Test];
33+
attempt(function () use (&$a) { assign_tmp($a); });
34+
$a = [new Test];
35+
$v = new Releaser;
36+
attempt(function () use (&$a, $v) { assign_cv($a, $v); });
37+
}
38+
39+
echo "--- error handler\n";
40+
set_error_handler(function () {
41+
unset($GLOBALS['a'][0]);
42+
return true;
43+
});
44+
function assign_float_string(array &$a) { $a[0]->i = "1.5"; }
45+
for ($k = 0; $k < 3; $k++) {
46+
$a = [new Test];
47+
attempt(function () use (&$a) { assign_float_string($a); });
48+
}
49+
50+
echo "--- class type: the object is not coerced\n";
51+
class Foo {}
52+
class Holder {
53+
public Foo $foo;
54+
public string|Foo $u = "";
55+
}
56+
$a = [new Holder];
57+
for ($k = 0; $k < 3; $k++) {
58+
$a[0]->foo = new Foo;
59+
var_dump($a[0]->foo instanceof Foo);
60+
}
61+
62+
echo "--- union with string: the object is coerced\n";
63+
function assign_union(array &$a, $v) { $a[0]->u = $v; }
64+
for ($k = 0; $k < 3; $k++) {
65+
$a = [new Holder];
66+
attempt(function () use (&$a) { assign_union($a, new Releaser); });
67+
}
68+
69+
echo "--- object stays alive\n";
70+
$a = [new Test];
71+
$a[0]->s = 12;
72+
var_dump($a[0]->s);
73+
74+
?>
75+
--EXPECT--
76+
--- __toString()
77+
Object was released while assigning to property Test::$s
78+
Object was released while assigning to property Test::$s
79+
Object was released while assigning to property Test::$s
80+
Object was released while assigning to property Test::$s
81+
Object was released while assigning to property Test::$s
82+
Object was released while assigning to property Test::$s
83+
--- error handler
84+
Object was released while assigning to property Test::$i
85+
Object was released while assigning to property Test::$i
86+
Object was released while assigning to property Test::$i
87+
--- class type: the object is not coerced
88+
bool(true)
89+
bool(true)
90+
bool(true)
91+
--- union with string: the object is coerced
92+
Object was released while assigning to property Holder::$u
93+
Object was released while assigning to property Holder::$u
94+
Object was released while assigning to property Holder::$u
95+
--- object stays alive
96+
string(2) "12"

‎Zend/zend_execute.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ ZEND_API bool zend_never_inline zend_verify_property_type(const zend_property_in
10571057
return i_zend_verify_property_type(info, property, strict);
10581058
}
10591059

1060-
static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *info, zval *property_val, zval *value, zend_refcounted **garbage_ptr EXECUTE_DATA_DC)
1060+
static zend_never_inline zval* zend_assign_to_typed_prop(zend_object *zobj, zend_property_info *info, zval *property_val, zval *value, zend_refcounted **garbage_ptr EXECUTE_DATA_DC)
10611061
{
10621062
zval tmp;
10631063

@@ -1075,9 +1075,23 @@ static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *inf
10751075
ZVAL_DEREF(value);
10761076
ZVAL_COPY(&tmp, value);
10771077

1078-
if (UNEXPECTED(!i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES()))) {
1079-
zval_ptr_dtor(&tmp);
1080-
return &EG(uninitialized_zval);
1078+
if (UNEXPECTED(!ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE(tmp)))) {
1079+
bool protect_object = zobj
1080+
&& (Z_TYPE(tmp) != IS_OBJECT || (ZEND_TYPE_FULL_MASK(info->type) & MAY_BE_STRING));
1081+
if (protect_object) {
1082+
GC_ADDREF(zobj);
1083+
}
1084+
bool type_matched = i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES());
1085+
if (protect_object && UNEXPECTED(GC_DELREF(zobj) == 0)) {
1086+
zend_object_released_while_assigning_to_property_error(info);
1087+
zend_objects_store_del(zobj);
1088+
zval_ptr_dtor(&tmp);
1089+
return &EG(uninitialized_zval);
1090+
}
1091+
if (UNEXPECTED(!type_matched)) {
1092+
zval_ptr_dtor(&tmp);
1093+
return &EG(uninitialized_zval);
1094+
}
10811095
}
10821096

10831097
Z_PROP_FLAG_P(property_val) &= ~IS_PROP_REINITABLE;

‎Zend/zend_vm_def.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ ZEND_VM_C_LABEL(assign_obj_simple):
24612461
property_val = OBJ_PROP(zobj, prop_offset);
24622462
if (Z_TYPE_P(property_val) != IS_UNDEF) {
24632463
if (prop_info != NULL) {
2464-
value = zend_assign_to_typed_prop(prop_info, property_val, value, &garbage EXECUTE_DATA_CC);
2464+
value = zend_assign_to_typed_prop(zobj, prop_info, property_val, value, &garbage EXECUTE_DATA_CC);
24652465
ZEND_VM_C_GOTO(free_and_exit_assign_obj);
24662466
} else {
24672467
ZEND_VM_C_LABEL(fast_assign_obj):
@@ -2595,7 +2595,7 @@ ZEND_VM_HANDLER(25, ZEND_ASSIGN_STATIC_PROP, ANY, ANY, CACHE_SLOT, SPEC(OP_DATA=
25952595
value = GET_OP_DATA_ZVAL_PTR(BP_VAR_R);
25962596

25972597
if (ZEND_TYPE_IS_SET(prop_info->type)) {
2598-
value = zend_assign_to_typed_prop(prop_info, prop, value, &garbage EXECUTE_DATA_CC);
2598+
value = zend_assign_to_typed_prop(NULL, prop_info, prop, value, &garbage EXECUTE_DATA_CC);
25992599
FREE_OP_DATA();
26002600
} else {
26012601
value = zend_assign_to_variable_ex(prop, value, OP_DATA_TYPE, EX_USES_STRICT_TYPES(), &garbage);

0 commit comments

Comments
 (0)