Skip to content

Commit 9944f58

Browse files
committed
Merge branch 'PHP-8.2'
* PHP-8.2: Fix GH-10709: UAF in recursive AST evaluation
2 parents 87e3513 + 1978a7b commit 9944f58

File tree

6 files changed

+109
-1
lines changed

6 files changed

+109
-1
lines changed

Zend/tests/gh10709.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-10709: Recursive class constant evaluation
3+
--FILE--
4+
<?php
5+
6+
class B { const C = A::C . "B"; }
7+
8+
spl_autoload_register(function ($class) {
9+
class A { const C = "A"; }
10+
var_dump(B::C);
11+
});
12+
13+
try {
14+
new B();
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
?>
20+
--EXPECT--
21+
string(2) "AB"

Zend/tests/gh10709_2.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-10709: Recursive class constant evaluation
3+
--FILE--
4+
<?php
5+
6+
class B {
7+
public $prop = A::C;
8+
}
9+
10+
spl_autoload_register(function ($class) {
11+
class A { const C = "A"; }
12+
var_dump(new B());
13+
});
14+
15+
try {
16+
var_dump(new B());
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
21+
?>
22+
--EXPECT--
23+
object(B)#2 (1) {
24+
["prop"]=>
25+
string(1) "A"
26+
}
27+
object(B)#2 (1) {
28+
["prop"]=>
29+
string(1) "A"
30+
}

Zend/tests/gh10709_3.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
GH-10709: Recursive class constant evaluation with outer call failing
3+
--FILE--
4+
<?php
5+
6+
class S {
7+
public function __toString() {
8+
static $i = 0;
9+
$i++;
10+
if ($i === 1) {
11+
return 'S';
12+
} else {
13+
throw new \Exception('Thrown from S');
14+
}
15+
}
16+
}
17+
18+
const S = new S();
19+
20+
class B {
21+
public $prop = A::C . S;
22+
}
23+
24+
spl_autoload_register(function ($class) {
25+
class A { const C = "A"; }
26+
var_dump(new B());
27+
});
28+
29+
var_dump(new B());
30+
31+
?>
32+
--EXPECTF--
33+
object(B)#3 (1) {
34+
["prop"]=>
35+
string(2) "AS"
36+
}
37+
38+
Fatal error: Uncaught Exception: Thrown from S in %s:%d
39+
Stack trace:
40+
#0 %s(%d): [constant expression]()
41+
#1 %s(%d): S->__toString()
42+
#2 {main}
43+
thrown in %s on line %d

Zend/zend_execute_API.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,19 @@ ZEND_API zend_result ZEND_FASTCALL zval_update_constant_with_ctx(zval *p, zend_c
698698
zval tmp;
699699
bool short_circuited;
700700

701-
if (UNEXPECTED(zend_ast_evaluate_ex(&tmp, ast, scope, &short_circuited, ctx) != SUCCESS)) {
701+
// Increase the refcount during zend_ast_evaluate to avoid releasing the ast too early
702+
// on nested calls to zval_update_constant_ex which can happen when retriggering ast
703+
// evaluation during autoloading.
704+
zend_ast_ref *ast_ref = Z_AST_P(p);
705+
bool ast_is_refcounted = !(GC_FLAGS(ast_ref) & GC_IMMUTABLE);
706+
if (ast_is_refcounted) {
707+
GC_ADDREF(ast_ref);
708+
}
709+
zend_result result = zend_ast_evaluate_ex(&tmp, ast, scope, &short_circuited, ctx) != SUCCESS;
710+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
711+
rc_dtor_func((zend_refcounted *)ast_ref);
712+
}
713+
if (UNEXPECTED(result != SUCCESS)) {
702714
return FAILURE;
703715
}
704716
zval_ptr_dtor_nogc(p);

ext/opcache/zend_persist.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ static void zend_persist_zval(zval *z)
261261
zend_persist_ast(GC_AST(old_ref));
262262
Z_TYPE_FLAGS_P(z) = 0;
263263
GC_SET_REFCOUNT(Z_COUNTED_P(z), 1);
264+
GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE);
264265
efree(old_ref);
265266
}
266267
break;

run-tests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ function main(): void
675675
if (!$phpdbg) {
676676
$phpdbg = get_binary($php, 'phpdbg', 'sapi/phpdbg/phpdbg');
677677
}
678+
$phpdbg = null;
678679

679680
putenv("TEST_PHP_EXECUTABLE=$php");
680681
$environment['TEST_PHP_EXECUTABLE'] = $php;

0 commit comments

Comments
 (0)