Skip to content

Commit 085f4ce

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

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,21 @@ 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+
GC_ADDREF(ast_ref);
688+
683689
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
690+
if (!GC_DELREF(ast_ref)) {
691+
rc_dtor_func((zend_refcounted *)ast_ref);
692+
}
684693
return FAILURE;
685694
}
695+
if (!GC_DELREF(ast_ref)) {
696+
rc_dtor_func((zend_refcounted *)ast_ref);
697+
}
686698
zval_ptr_dtor_nogc(p);
687699
ZVAL_COPY_VALUE(p, &tmp);
688700
}

0 commit comments

Comments
 (0)