Skip to content

Commit 19b3003

Browse files
committed
Fix uaf for nested finally with repeated return type check
Fixes OSS-Fuzz #438780145 Closes phpGH-19488
1 parent 0efecbc commit 19b3003

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PHP NEWS
1212
. Fixed bug GH-20766 (Use-after-free in FE_FREE with GC interaction). (Bob)
1313
. Fix OSS-Fuzz #471486164 (Broken by-ref assignment to uninitialized hooked
1414
backing value). (ilutov)
15+
. Fix OSS-Fuzz #438780145 (Nested finally with repeated return type check may
16+
uaf). (ilutov)
1517

1618
- Date:
1719
. Update timelib to 2022.16. (Derick)

Zend/tests/oss_fuzz_438780145.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
OSS-Fuzz #438780145: Nested finally with repeated return type check may uaf
3+
--FILE--
4+
<?php
5+
6+
function &test(): int {
7+
$x = 0;
8+
try {
9+
return $x;
10+
} finally {
11+
try {
12+
return $x;
13+
} finally {
14+
$x = "";
15+
}
16+
}
17+
}
18+
19+
test();
20+
21+
?>
22+
--EXPECTF--
23+
Fatal error: Uncaught TypeError: test(): Return value must be of type int, string returned in %s:%d
24+
Stack trace:
25+
#0 %s(%d): test()
26+
#1 {main}
27+
thrown in %s on line %d

Zend/zend_vm_def.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8537,6 +8537,10 @@ ZEND_VM_HANDLER(159, ZEND_DISCARD_EXCEPTION, ANY, ANY)
85378537
zval *return_value = EX_VAR(EX(func)->op_array.opcodes[Z_OPLINE_NUM_P(fast_call)].op2.var);
85388538

85398539
zval_ptr_dtor(return_value);
8540+
/* Clear return value in case we hit both DISCARD_EXCEPTION and
8541+
* zend_dispatch_try_catch_finally_helper, which will free the return
8542+
* value again. See OSS-Fuzz #438780145. */
8543+
ZVAL_NULL(return_value);
85408544
}
85418545

85428546
/* cleanup delayed exception */

Zend/zend_vm_execute.h

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)