Skip to content

Commit a21065e

Browse files
committed
Use-after-free in extract() with EXTR_REFS
Fixes GH-18209 Closes GH-18211
1 parent 13d51f8 commit a21065e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PHP NEWS
99
- Standard:
1010
. Fixed bug GH-18145 (php8ts crashes in php_clear_stat_cache()).
1111
(Jakub Zelenka)
12+
. Fixed bug GH-18209 (Use-after-free in extract() with EXTR_REFS). (ilutov)
1213

1314
10 Apr 2025, PHP 8.3.20
1415

Diff for: ext/standard/array.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,10 @@ static zend_long php_extract_ref_overwrite(zend_array *arr, zend_array *symbol_t
18631863
} else {
18641864
ZVAL_MAKE_REF_EX(entry, 2);
18651865
}
1866-
zval_ptr_dtor(orig_var);
1866+
zval garbage;
1867+
ZVAL_COPY_VALUE(&garbage, orig_var);
18671868
ZVAL_REF(orig_var, Z_REF_P(entry));
1869+
zval_ptr_dtor(&garbage);
18681870
} else {
18691871
if (Z_ISREF_P(entry)) {
18701872
Z_ADDREF_P(entry);

Diff for: ext/standard/tests/gh18209.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-18209: Use-after-free in extract() with EXTR_REFS
3+
--CREDITS--
4+
Noam Rathaus (nrathaus)
5+
--FILE--
6+
<?php
7+
8+
class C {
9+
public function __destruct() {
10+
var_dump($GLOBALS['b']);
11+
$GLOBALS['b'] = 43;
12+
}
13+
}
14+
15+
$b = new C;
16+
$array = ['b' => 42];
17+
extract($array, EXTR_REFS);
18+
var_dump($b);
19+
20+
?>
21+
--EXPECT--
22+
int(42)
23+
int(43)

0 commit comments

Comments
 (0)