Skip to content

Commit 7202fe1

Browse files
committed
Fix GH-10709: UAF in recursive AST evaluation
Fixes https://oss-fuzz.com/testcase-detail/6445949468934144 Closes GH-10718
1 parent 22c9e7e commit 7202fe1

File tree

7 files changed

+121
-2
lines changed

7 files changed

+121
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PHP NEWS
55
- Core:
66
. Added optional support for max_execution_time in ZTS/Linux builds
77
(Kévin Dunglas)
8+
. Fixed use-after-free in recursive AST evaluation. (ilutov)
89

910
- FTP:
1011
. Propagate success status of ftp_close(). (nielsdos)

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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): S->__toString()
41+
#1 {main}
42+
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
@@ -685,7 +685,19 @@ ZEND_API zend_result ZEND_FASTCALL zval_update_constant_ex(zval *p, zend_class_e
685685
} else {
686686
zval tmp;
687687

688-
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
688+
// Increase the refcount during zend_ast_evaluate to avoid releasing the ast too early
689+
// on nested calls to zval_update_constant_ex which can happen when retriggering ast
690+
// evaluation during autoloading.
691+
zend_ast_ref *ast_ref = Z_AST_P(p);
692+
bool ast_is_refcounted = !(GC_FLAGS(ast_ref) & GC_IMMUTABLE);
693+
if (ast_is_refcounted) {
694+
GC_ADDREF(ast_ref);
695+
}
696+
zend_result result = zend_ast_evaluate(&tmp, ast, scope);
697+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
698+
rc_dtor_func((zend_refcounted *)ast_ref);
699+
}
700+
if (UNEXPECTED(result != SUCCESS)) {
689701
return FAILURE;
690702
}
691703
zval_ptr_dtor_nogc(p);

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,19 @@ static zend_result ZEND_FASTCALL zval_jit_update_constant_ex(zval *p, zend_class
30693069
} else {
30703070
zval tmp;
30713071

3072-
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
3072+
// Increase the refcount during zend_ast_evaluate to avoid releasing the ast too early
3073+
// on nested calls to zval_update_constant_ex which can happen when retriggering ast
3074+
// evaluation during autoloading.
3075+
zend_ast_ref *ast_ref = Z_AST_P(p);
3076+
bool ast_is_refcounted = !(GC_FLAGS(ast_ref) & GC_IMMUTABLE);
3077+
if (ast_is_refcounted) {
3078+
GC_ADDREF(ast_ref);
3079+
}
3080+
zend_result result = zend_ast_evaluate(&tmp, ast, scope);
3081+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
3082+
rc_dtor_func((zend_refcounted *)ast_ref);
3083+
}
3084+
if (UNEXPECTED(result != SUCCESS)) {
30733085
return FAILURE;
30743086
}
30753087
zval_ptr_dtor_nogc(p);

ext/opcache/zend_persist.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static void zend_persist_zval(zval *z)
248248
zend_persist_ast(GC_AST(old_ref));
249249
Z_TYPE_FLAGS_P(z) = 0;
250250
GC_SET_REFCOUNT(Z_COUNTED_P(z), 1);
251+
GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE);
251252
efree(old_ref);
252253
}
253254
break;

0 commit comments

Comments
 (0)