Skip to content

Commit 4f9208c

Browse files
committed
Zend: Fix GH-25700 and GH-25706 by storing new value before destroying old one in hash update
Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
1 parent 04d4653 commit 4f9208c

10 files changed

Lines changed: 226 additions & 14 deletions

File tree

‎NEWS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ PHP NEWS
5151
an output-buffer callback). (David Carlier)
5252
. Fixed leak when exception is thrown from a destructor during GC.
5353
(realFlowControl)
54+
. Fixed bug GH-25700 (Use-after-free when a lazily created auto-global
55+
replaces an existing global variable). (Roman Ondráček)
56+
. Fixed bug GH-25706 (session_start() / session_decode() with php_serialize:
57+
use-after-free when a value added by __unserialize() has a destructor).
58+
(Roman Ondráček)
5459

5560
- Curl:
5661
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)

‎Zend/tests/gh25700.phpt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-25700 (Use-after-free when a lazily created auto-global replaces an existing global variable)
3+
--INI--
4+
auto_globals_jit=1
5+
variables_order=GPCS
6+
--FILE--
7+
<?php
8+
class D {
9+
public function __destruct() {
10+
$GLOBALS['keep'] = $GLOBALS['_ENV'];
11+
}
12+
}
13+
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
14+
eval('$_ENV;');
15+
var_dump($GLOBALS['keep']);
16+
?>
17+
--EXPECT--
18+
array(0) {
19+
}

‎Zend/tests/gh25700_grow.phpt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-25700 (destructor grows the symbol table while the auto-global is replaced)
3+
--INI--
4+
auto_globals_jit=1
5+
variables_order=GPCS
6+
--FILE--
7+
<?php
8+
class D {
9+
public function __destruct() {
10+
for ($i = 0; $i < 200; $i++) {
11+
$GLOBALS["g$i"] = $i;
12+
}
13+
}
14+
}
15+
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
16+
eval('$_ENV;');
17+
var_dump($GLOBALS['_ENV'], $g199);
18+
?>
19+
--EXPECT--
20+
array(0) {
21+
}
22+
int(199)

‎Zend/tests/gh25700_nested.phpt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-25700 (destructor triggers the auto-global creation again)
3+
--INI--
4+
auto_globals_jit=1
5+
variables_order=GPCS
6+
--FILE--
7+
<?php
8+
class D {
9+
public function __destruct() {
10+
eval('$_ENV;');
11+
}
12+
}
13+
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
14+
eval('$_ENV;');
15+
var_dump($GLOBALS['_ENV']);
16+
?>
17+
--EXPECT--
18+
array(0) {
19+
}

‎Zend/tests/gh25700_overwrite.phpt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-25700 (destructor overwrites the lazily created auto-global)
3+
--INI--
4+
auto_globals_jit=1
5+
variables_order=GPCS
6+
--FILE--
7+
<?php
8+
class D {
9+
public function __destruct() {
10+
$GLOBALS['_ENV'] = 1;
11+
}
12+
}
13+
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
14+
eval('$_ENV;');
15+
var_dump($GLOBALS['_ENV']);
16+
?>
17+
--EXPECT--
18+
int(1)

‎Zend/tests/gh25700_unset.phpt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-25700 (destructor unsets the lazily created auto-global)
3+
--INI--
4+
auto_globals_jit=1
5+
variables_order=GPCS
6+
--FILE--
7+
<?php
8+
class D {
9+
public function __destruct() {
10+
unset($GLOBALS['_ENV']);
11+
}
12+
}
13+
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
14+
eval('$_ENV;');
15+
var_dump(isset($GLOBALS['_ENV']));
16+
?>
17+
--EXPECTF--
18+
Warning: Undefined global variable $_ENV in %s : eval()'d code on line 1
19+
bool(false)

‎Zend/zend_hash.c‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,15 @@ static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_s
10311031
}
10321032
}
10331033
if (ht->pDestructor) {
1034-
ht->pDestructor(data);
1034+
/* Store the new value before destroying the old one, so that
1035+
* destructors observe a consistent hash table. */
1036+
zval tmp;
1037+
ZVAL_COPY_VALUE(&tmp, data);
1038+
ZVAL_COPY_VALUE(data, pData);
1039+
ht->pDestructor(&tmp);
1040+
} else {
1041+
ZVAL_COPY_VALUE(data, pData);
10351042
}
1036-
ZVAL_COPY_VALUE(data, pData);
10371043
return data;
10381044
}
10391045
}
@@ -1110,9 +1116,15 @@ static zend_always_inline zval *_zend_hash_str_add_or_update_i(HashTable *ht, co
11101116
}
11111117
}
11121118
if (ht->pDestructor) {
1113-
ht->pDestructor(data);
1119+
/* Store the new value before destroying the old one, so that
1120+
* destructors observe a consistent hash table. */
1121+
zval tmp;
1122+
ZVAL_COPY_VALUE(&tmp, data);
1123+
ZVAL_COPY_VALUE(data, pData);
1124+
ht->pDestructor(&tmp);
1125+
} else {
1126+
ZVAL_COPY_VALUE(data, pData);
11141127
}
1115-
ZVAL_COPY_VALUE(data, pData);
11161128
return data;
11171129
}
11181130
}
@@ -1290,9 +1302,15 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
12901302
return NULL;
12911303
}
12921304
if (ht->pDestructor) {
1293-
ht->pDestructor(zv);
1305+
/* Store the new value before destroying the old one, so that
1306+
* destructors observe a consistent hash table. */
1307+
zval tmp;
1308+
ZVAL_COPY_VALUE(&tmp, zv);
1309+
ZVAL_COPY_VALUE(zv, pData);
1310+
ht->pDestructor(&tmp);
1311+
} else {
1312+
ZVAL_COPY_VALUE(zv, pData);
12941313
}
1295-
ZVAL_COPY_VALUE(zv, pData);
12961314
return zv;
12971315
} else { /* we have to keep the order :( */
12981316
zend_result status = zend_hash_fconvert_packed_to_hash(ht, flag & HASH_FALLIBLE);
@@ -1863,9 +1881,13 @@ ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const ch
18631881
return FAILURE;
18641882
} else {
18651883
if (ht->pDestructor) {
1866-
ht->pDestructor(data);
1884+
zval tmp;
1885+
ZVAL_COPY_VALUE(&tmp, data);
1886+
ZVAL_UNDEF(data);
1887+
ht->pDestructor(&tmp);
1888+
} else {
1889+
ZVAL_UNDEF(data);
18671890
}
1868-
ZVAL_UNDEF(data);
18691891
HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
18701892
}
18711893
} else {

‎ext/session/tests/gh25706.phpt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-25706 (session_decode() with php_serialize: use-after-free when a value added by __unserialize() has a destructor)
3+
--EXTENSIONS--
4+
session
5+
--SKIPIF--
6+
<?php include('skipif.inc'); ?>
7+
--INI--
8+
session.use_cookies=0
9+
session.cache_limiter=
10+
session.serialize_handler=php_serialize
11+
session.save_handler=files
12+
--FILE--
13+
<?php
14+
class Reentry {
15+
public function __destruct() {
16+
$GLOBALS['keep'] = $_SESSION;
17+
}
18+
}
19+
class Boom {
20+
public function __unserialize($data) {
21+
$_SESSION['pad'] = str_repeat('Z', 128);
22+
$_SESSION['re'] = new Reentry();
23+
}
24+
}
25+
ob_start();
26+
session_start();
27+
session_decode(serialize([new Boom()]));
28+
var_dump($GLOBALS['keep']);
29+
session_destroy();
30+
?>
31+
--EXPECT--
32+
array(1) {
33+
[0]=>
34+
object(Boom)#1 (0) {
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
GH-25706 (session_decode() with the php decoder: destructor of a replaced value modifies $_SESSION)
3+
--EXTENSIONS--
4+
session
5+
--SKIPIF--
6+
<?php include('skipif.inc'); ?>
7+
--INI--
8+
session.use_cookies=0
9+
session.cache_limiter=
10+
session.serialize_handler=php
11+
session.save_handler=files
12+
--FILE--
13+
<?php
14+
class UnsetOnDestruct {
15+
public function __destruct() {
16+
session_unset();
17+
}
18+
}
19+
class ReplaceOnDestruct {
20+
public function __destruct() {
21+
$_SESSION = [];
22+
}
23+
}
24+
25+
ob_start();
26+
session_start();
27+
28+
$_SESSION['a'] = new UnsetOnDestruct();
29+
$_SESSION['pad'] = str_repeat('A', 64);
30+
session_decode('a|i:1;b|i:2;c|i:3;');
31+
var_dump($_SESSION);
32+
33+
$_SESSION['a'] = new ReplaceOnDestruct();
34+
$_SESSION['pad'] = str_repeat('A', 64);
35+
session_decode('a|i:1;b|i:2;c|i:3;');
36+
var_dump($_SESSION);
37+
38+
session_destroy();
39+
?>
40+
--EXPECT--
41+
array(2) {
42+
["b"]=>
43+
int(2)
44+
["c"]=>
45+
int(3)
46+
}
47+
array(2) {
48+
["b"]=>
49+
int(2)
50+
["c"]=>
51+
int(3)
52+
}

‎main/php_variables.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ static bool php_auto_globals_create_get(zend_string *name)
828828
array_init(&PG(http_globals)[TRACK_VARS_GET]);
829829
}
830830

831-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
832831
Z_ADDREF(PG(http_globals)[TRACK_VARS_GET]);
832+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
833833

834834
return 0; /* don't rearm */
835835
}
@@ -847,8 +847,8 @@ static bool php_auto_globals_create_post(zend_string *name)
847847
array_init(&PG(http_globals)[TRACK_VARS_POST]);
848848
}
849849

850-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
851850
Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
851+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
852852

853853
return 0; /* don't rearm */
854854
}
@@ -862,8 +862,8 @@ static bool php_auto_globals_create_cookie(zend_string *name)
862862
array_init(&PG(http_globals)[TRACK_VARS_COOKIE]);
863863
}
864864

865-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
866865
Z_ADDREF(PG(http_globals)[TRACK_VARS_COOKIE]);
866+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
867867

868868
return 0; /* don't rearm */
869869
}
@@ -874,8 +874,8 @@ static bool php_auto_globals_create_files(zend_string *name)
874874
array_init(&PG(http_globals)[TRACK_VARS_FILES]);
875875
}
876876

877-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
878877
Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
878+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
879879

880880
return 0; /* don't rearm */
881881
}
@@ -923,8 +923,8 @@ static bool php_auto_globals_create_server(zend_string *name)
923923
}
924924

925925
check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
926-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
927926
Z_ADDREF(PG(http_globals)[TRACK_VARS_SERVER]);
927+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
928928

929929
/* TODO: TRACK_VARS_SERVER is modified in a number of places (e.g. phar) past this point,
930930
* where rc>1 due to the $_SERVER global. Ideally this shouldn't happen, but for now we
@@ -944,8 +944,8 @@ static bool php_auto_globals_create_env(zend_string *name)
944944
}
945945

946946
check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV]));
947-
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
948947
Z_ADDREF(PG(http_globals)[TRACK_VARS_ENV]);
948+
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
949949

950950
return 0; /* don't rearm */
951951
}

0 commit comments

Comments
 (0)