Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ PHP NEWS
. Fixed bug GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking
a generator into a by-ref parameter throws). (ndossche)
. Fix OSS-Fuzz #588264417. (ndossche)
. Fixed bug GH-25700 (Use-after-free when a lazily created auto-global
replaces an existing global variable). (Roman Ondráček)
. Fixed bug GH-25706 (session_start() / session_decode() with php_serialize:
use-after-free when a value added by __unserialize() has a destructor).
(Roman Ondráček)

- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
Expand Down
19 changes: 19 additions & 0 deletions Zend/tests/gh25700.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-25700 (Use-after-free when a lazily created auto-global replaces an existing global variable)
--INI--
auto_globals_jit=1
variables_order=GPCS
--FILE--
<?php
class D {
public function __destruct() {
$GLOBALS['keep'] = $GLOBALS['_ENV'];
}
}
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
eval('$_ENV;');
var_dump($GLOBALS['keep']);
?>
--EXPECT--
array(0) {
}
22 changes: 22 additions & 0 deletions Zend/tests/gh25700_grow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
GH-25700 (destructor grows the symbol table while the auto-global is replaced)
--INI--
auto_globals_jit=1
variables_order=GPCS
--FILE--
<?php
class D {
public function __destruct() {
for ($i = 0; $i < 200; $i++) {
$GLOBALS["g$i"] = $i;
}
}
}
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
eval('$_ENV;');
var_dump($GLOBALS['_ENV'], $g199);
?>
--EXPECT--
array(0) {
}
int(199)
19 changes: 19 additions & 0 deletions Zend/tests/gh25700_nested.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-25700 (destructor triggers the auto-global creation again)
--INI--
auto_globals_jit=1
variables_order=GPCS
--FILE--
<?php
class D {
public function __destruct() {
eval('$_ENV;');
}
}
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
eval('$_ENV;');
var_dump($GLOBALS['_ENV']);
?>
--EXPECT--
array(0) {
}
18 changes: 18 additions & 0 deletions Zend/tests/gh25700_overwrite.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-25700 (destructor overwrites the lazily created auto-global)
--INI--
auto_globals_jit=1
variables_order=GPCS
--FILE--
<?php
class D {
public function __destruct() {
$GLOBALS['_ENV'] = 1;
}
}
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
eval('$_ENV;');
var_dump($GLOBALS['_ENV']);
?>
--EXPECT--
int(1)
19 changes: 19 additions & 0 deletions Zend/tests/gh25700_unset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-25700 (destructor unsets the lazily created auto-global)
--INI--
auto_globals_jit=1
variables_order=GPCS
--FILE--
<?php
class D {
public function __destruct() {
unset($GLOBALS['_ENV']);
}
}
extract(['_ENV' => ['pad' => str_repeat('A', 64), 'boom' => new D()]]);
eval('$_ENV;');
var_dump(isset($GLOBALS['_ENV']));
?>
--EXPECTF--
Warning: Undefined global variable $_ENV in %s : eval()'d code on line 1
bool(false)
38 changes: 30 additions & 8 deletions Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,15 @@ static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_s
}
}
if (ht->pDestructor) {
ht->pDestructor(data);
/* Store the new value before destroying the old one, so that
* destructors observe a consistent hash table. */
zval tmp;
ZVAL_COPY_VALUE(&tmp, data);
ZVAL_COPY_VALUE(data, pData);
ht->pDestructor(&tmp);
} else {
ZVAL_COPY_VALUE(data, pData);
}
ZVAL_COPY_VALUE(data, pData);
return data;
}
}
Expand Down Expand Up @@ -1110,9 +1116,15 @@ static zend_always_inline zval *_zend_hash_str_add_or_update_i(HashTable *ht, co
}
}
if (ht->pDestructor) {
ht->pDestructor(data);
/* Store the new value before destroying the old one, so that
* destructors observe a consistent hash table. */
zval tmp;
ZVAL_COPY_VALUE(&tmp, data);
ZVAL_COPY_VALUE(data, pData);
ht->pDestructor(&tmp);
} else {
ZVAL_COPY_VALUE(data, pData);
}
ZVAL_COPY_VALUE(data, pData);
return data;
}
}
Expand Down Expand Up @@ -1290,9 +1302,15 @@ static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht,
return NULL;
}
if (ht->pDestructor) {
ht->pDestructor(zv);
/* Store the new value before destroying the old one, so that
* destructors observe a consistent hash table. */
zval tmp;
ZVAL_COPY_VALUE(&tmp, zv);
ZVAL_COPY_VALUE(zv, pData);
ht->pDestructor(&tmp);
} else {
ZVAL_COPY_VALUE(zv, pData);
}
ZVAL_COPY_VALUE(zv, pData);
return zv;
} else { /* we have to keep the order :( */
zend_result status = zend_hash_fconvert_packed_to_hash(ht, flag & HASH_FALLIBLE);
Expand Down Expand Up @@ -1863,9 +1881,13 @@ ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const ch
return FAILURE;
} else {
if (ht->pDestructor) {
ht->pDestructor(data);
zval tmp;
ZVAL_COPY_VALUE(&tmp, data);
ZVAL_UNDEF(data);
ht->pDestructor(&tmp);
} else {
ZVAL_UNDEF(data);
}
ZVAL_UNDEF(data);
HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
}
} else {
Expand Down
36 changes: 36 additions & 0 deletions ext/session/tests/gh25706.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-25706 (session_decode() with php_serialize: use-after-free when a value added by __unserialize() has a destructor)
--EXTENSIONS--
session
--SKIPIF--
<?php include('skipif.inc'); ?>
--INI--
session.use_cookies=0
session.cache_limiter=
session.serialize_handler=php_serialize
session.save_handler=files
--FILE--
<?php
class Reentry {
public function __destruct() {
$GLOBALS['keep'] = $_SESSION;
}
}
class Boom {
public function __unserialize($data) {
$_SESSION['pad'] = str_repeat('Z', 128);
$_SESSION['re'] = new Reentry();
}
}
ob_start();
session_start();
session_decode(serialize([new Boom()]));
var_dump($GLOBALS['keep']);
session_destroy();
?>
--EXPECT--
array(1) {
[0]=>
object(Boom)#1 (0) {
}
}
52 changes: 52 additions & 0 deletions ext/session/tests/gh25706_php_decoder.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
GH-25706 (session_decode() with the php decoder: destructor of a replaced value modifies $_SESSION)
--EXTENSIONS--
session
--SKIPIF--
<?php include('skipif.inc'); ?>
--INI--
session.use_cookies=0
session.cache_limiter=
session.serialize_handler=php
session.save_handler=files
--FILE--
<?php
class UnsetOnDestruct {
public function __destruct() {
session_unset();
}
}
class ReplaceOnDestruct {
public function __destruct() {
$_SESSION = [];
}
}

ob_start();
session_start();

$_SESSION['a'] = new UnsetOnDestruct();
$_SESSION['pad'] = str_repeat('A', 64);
session_decode('a|i:1;b|i:2;c|i:3;');
var_dump($_SESSION);

$_SESSION['a'] = new ReplaceOnDestruct();
$_SESSION['pad'] = str_repeat('A', 64);
session_decode('a|i:1;b|i:2;c|i:3;');
var_dump($_SESSION);

session_destroy();
?>
--EXPECT--
array(2) {
["b"]=>
int(2)
["c"]=>
int(3)
}
array(2) {
["b"]=>
int(2)
["c"]=>
int(3)
}
12 changes: 6 additions & 6 deletions main/php_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,8 @@ static bool php_auto_globals_create_get(zend_string *name)
array_init(&PG(http_globals)[TRACK_VARS_GET]);
}

zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_GET]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_GET]);

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

zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_POST]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_POST]);

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

zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_COOKIE]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_COOKIE]);

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

zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_FILES]);

return 0; /* don't rearm */
}
Expand Down Expand Up @@ -923,8 +923,8 @@ static bool php_auto_globals_create_server(zend_string *name)
}

check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]));
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_SERVER]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_SERVER]);

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

check_http_proxy(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV]));
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);
Z_ADDREF(PG(http_globals)[TRACK_VARS_ENV]);
zend_hash_update(&EG(symbol_table), name, &PG(http_globals)[TRACK_VARS_ENV]);

return 0; /* don't rearm */
}
Expand Down
Loading