Skip to content

Commit 6d8b72e

Browse files
committed
Fix GH-10709: UAF in recursive AST evaluation
Fixes https://oss-fuzz.com/testcase-detail/6445949468934144
1 parent adc5edd commit 6d8b72e

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
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: 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,23 @@ ZEND_API zend_result ZEND_FASTCALL zval_update_constant_ex(zval *p, zend_class_e
680680
} else {
681681
zval tmp;
682682

683+
// Increase the refcount during zend_ast_evaluate to avoid releasing the ast too early
684+
// on nested calls to zval_update_constant_ex which can happen when retriggering ast
685+
// evaluation during autoloading.
686+
zend_ast_ref *ast_ref = Z_AST_P(p);
687+
bool ast_is_refcounted = Z_REFCOUNTED_P(p);
688+
if (ast_is_refcounted) {
689+
GC_ADDREF(ast_ref);
690+
}
683691
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
692+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
693+
rc_dtor_func((zend_refcounted *)ast_ref);
694+
}
684695
return FAILURE;
685696
}
697+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
698+
rc_dtor_func((zend_refcounted *)ast_ref);
699+
}
686700
zval_ptr_dtor_nogc(p);
687701
ZVAL_COPY_VALUE(p, &tmp);
688702
}

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,9 +3035,23 @@ static zend_result ZEND_FASTCALL zval_jit_update_constant_ex(zval *p, zend_class
30353035
} else {
30363036
zval tmp;
30373037

3038+
// Increase the refcount during zend_ast_evaluate to avoid releasing the ast too early
3039+
// on nested calls to zval_update_constant_ex which can happen when retriggering ast
3040+
// evaluation during autoloading.
3041+
zend_ast_ref *ast_ref = Z_AST_P(p);
3042+
bool ast_is_refcounted = Z_REFCOUNTED_P(p);
3043+
if (ast_is_refcounted) {
3044+
GC_ADDREF(ast_ref);
3045+
}
30383046
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
3047+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
3048+
rc_dtor_func((zend_refcounted *)ast_ref);
3049+
}
30393050
return FAILURE;
30403051
}
3052+
if (ast_is_refcounted && !GC_DELREF(ast_ref)) {
3053+
rc_dtor_func((zend_refcounted *)ast_ref);
3054+
}
30413055
zval_ptr_dtor_nogc(p);
30423056
ZVAL_COPY_VALUE(p, &tmp);
30433057
}

0 commit comments

Comments
 (0)