From 04c917941780fdef54a2781c3d20cacd135a96c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Ondr=C3=A1=C4=8Dek?= Date: Thu, 26 Nov 2026 20:55:24 +0100 Subject: [PATCH] Zend: Fix GH-25700 and GH-25706 by storing new value before destroying old one in hash update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Roman Ondráček --- NEWS | 5 +++ Zend/tests/gh25700.phpt | 19 ++++++++ Zend/tests/gh25700_grow.phpt | 22 +++++++++ Zend/tests/gh25700_nested.phpt | 19 ++++++++ Zend/tests/gh25700_overwrite.phpt | 18 ++++++++ Zend/tests/gh25700_unset.phpt | 19 ++++++++ Zend/zend_hash.c | 38 ++++++++++++---- ext/session/tests/gh25706.phpt | 36 +++++++++++++++ ext/session/tests/gh25706_php_decoder.phpt | 52 ++++++++++++++++++++++ main/php_variables.c | 12 ++--- 10 files changed, 226 insertions(+), 14 deletions(-) create mode 100644 Zend/tests/gh25700.phpt create mode 100644 Zend/tests/gh25700_grow.phpt create mode 100644 Zend/tests/gh25700_nested.phpt create mode 100644 Zend/tests/gh25700_overwrite.phpt create mode 100644 Zend/tests/gh25700_unset.phpt create mode 100644 ext/session/tests/gh25706.phpt create mode 100644 ext/session/tests/gh25706_php_decoder.phpt diff --git a/NEWS b/NEWS index e42393462d83..df4f22949b1b 100644 --- a/NEWS +++ b/NEWS @@ -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) diff --git a/Zend/tests/gh25700.phpt b/Zend/tests/gh25700.phpt new file mode 100644 index 000000000000..21aefd156d0c --- /dev/null +++ b/Zend/tests/gh25700.phpt @@ -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-- + ['pad' => str_repeat('A', 64), 'boom' => new D()]]); +eval('$_ENV;'); +var_dump($GLOBALS['keep']); +?> +--EXPECT-- +array(0) { +} diff --git a/Zend/tests/gh25700_grow.phpt b/Zend/tests/gh25700_grow.phpt new file mode 100644 index 000000000000..b6ae837b2f7e --- /dev/null +++ b/Zend/tests/gh25700_grow.phpt @@ -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-- + ['pad' => str_repeat('A', 64), 'boom' => new D()]]); +eval('$_ENV;'); +var_dump($GLOBALS['_ENV'], $g199); +?> +--EXPECT-- +array(0) { +} +int(199) diff --git a/Zend/tests/gh25700_nested.phpt b/Zend/tests/gh25700_nested.phpt new file mode 100644 index 000000000000..f66655f9ca13 --- /dev/null +++ b/Zend/tests/gh25700_nested.phpt @@ -0,0 +1,19 @@ +--TEST-- +GH-25700 (destructor triggers the auto-global creation again) +--INI-- +auto_globals_jit=1 +variables_order=GPCS +--FILE-- + ['pad' => str_repeat('A', 64), 'boom' => new D()]]); +eval('$_ENV;'); +var_dump($GLOBALS['_ENV']); +?> +--EXPECT-- +array(0) { +} diff --git a/Zend/tests/gh25700_overwrite.phpt b/Zend/tests/gh25700_overwrite.phpt new file mode 100644 index 000000000000..92694c324866 --- /dev/null +++ b/Zend/tests/gh25700_overwrite.phpt @@ -0,0 +1,18 @@ +--TEST-- +GH-25700 (destructor overwrites the lazily created auto-global) +--INI-- +auto_globals_jit=1 +variables_order=GPCS +--FILE-- + ['pad' => str_repeat('A', 64), 'boom' => new D()]]); +eval('$_ENV;'); +var_dump($GLOBALS['_ENV']); +?> +--EXPECT-- +int(1) diff --git a/Zend/tests/gh25700_unset.phpt b/Zend/tests/gh25700_unset.phpt new file mode 100644 index 000000000000..a99270ea965b --- /dev/null +++ b/Zend/tests/gh25700_unset.phpt @@ -0,0 +1,19 @@ +--TEST-- +GH-25700 (destructor unsets the lazily created auto-global) +--INI-- +auto_globals_jit=1 +variables_order=GPCS +--FILE-- + ['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) diff --git a/Zend/zend_hash.c b/Zend/zend_hash.c index 3d88d179f7cc..e3b80b5cfc54 100644 --- a/Zend/zend_hash.c +++ b/Zend/zend_hash.c @@ -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; } } @@ -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; } } @@ -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); @@ -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 { diff --git a/ext/session/tests/gh25706.phpt b/ext/session/tests/gh25706.phpt new file mode 100644 index 000000000000..7a5c156c4197 --- /dev/null +++ b/ext/session/tests/gh25706.phpt @@ -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-- + +--INI-- +session.use_cookies=0 +session.cache_limiter= +session.serialize_handler=php_serialize +session.save_handler=files +--FILE-- + +--EXPECT-- +array(1) { + [0]=> + object(Boom)#1 (0) { + } +} diff --git a/ext/session/tests/gh25706_php_decoder.phpt b/ext/session/tests/gh25706_php_decoder.phpt new file mode 100644 index 000000000000..4d1787aa00aa --- /dev/null +++ b/ext/session/tests/gh25706_php_decoder.phpt @@ -0,0 +1,52 @@ +--TEST-- +GH-25706 (session_decode() with the php decoder: destructor of a replaced value modifies $_SESSION) +--EXTENSIONS-- +session +--SKIPIF-- + +--INI-- +session.use_cookies=0 +session.cache_limiter= +session.serialize_handler=php +session.save_handler=files +--FILE-- + +--EXPECT-- +array(2) { + ["b"]=> + int(2) + ["c"]=> + int(3) +} +array(2) { + ["b"]=> + int(2) + ["c"]=> + int(3) +} diff --git a/main/php_variables.c b/main/php_variables.c index 23fa3d6f5fe0..072f36d74c3b 100644 --- a/main/php_variables.c +++ b/main/php_variables.c @@ -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 */ } @@ -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 */ } @@ -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 */ } @@ -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 */ } @@ -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 @@ -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 */ }