Skip to content

Commit a1d863b

Browse files
committed
Use-after-free in extract() with EXTR_REFS
Fixes GH-18209
1 parent c0b441f commit a1d863b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

ext/standard/array.c

Lines changed: 3 additions & 1 deletion
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);

ext/standard/tests/gh18209.phpt

Lines changed: 23 additions & 0 deletions
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)