From c1eed2840c9c96c6f4641ec69894c991bd91f5ec Mon Sep 17 00:00:00 2001 From: Artem Ukrainskiy Date: Tue, 17 Nov 2026 16:50:03 +0000 Subject: [PATCH] ext/phar: Hold a reference to the setSignatureAlgorithm() private key PHAR_G(openssl_privatekey) pointed into the key argument. Once that string was freed, the next flush of an OpenSSL-signed phar read freed memory to sign it, in the same request or in a later one. Keep the key as a zend_string with its own reference, and release it when it is replaced and at the end of the request. --- NEWS | 2 ++ ext/phar/phar.c | 5 +++ ext/phar/phar_internal.h | 3 +- ext/phar/phar_object.c | 12 ++++--- ...har_setsignaturealgo_openssl_key_copy.phpt | 33 +++++++++++++++++++ ext/phar/util.c | 5 +-- 6 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 ext/phar/tests/phar_setsignaturealgo_openssl_key_copy.phpt diff --git a/NEWS b/NEWS index 5591f321bd0e..431e38ac3479 100644 --- a/NEWS +++ b/NEWS @@ -431,6 +431,8 @@ PHP NEWS mounted entries). (Lazizbek Ergashev) . Fixed bug GH-24531 (Crash when modifying a zip-based phar whose file was renamed). (Ilia Alshanetsky) + . Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a + freed private key). (Artem Ukrainskiy) - Phpdbg: . Fix wrong command names. (ndossche, Weilin Du) diff --git a/ext/phar/phar.c b/ext/phar/phar.c index d15ca0b4e85e..5a51b25e2882 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -3506,6 +3506,11 @@ PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */ PHAR_G(cwd_init) = 0; } + if (PHAR_G(openssl_privatekey)) { + zend_string_release(PHAR_G(openssl_privatekey)); + PHAR_G(openssl_privatekey) = NULL; + } + PHAR_G(request_done) = 1; return SUCCESS; } diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index 58a30a52b1c1..4af29eaa4604 100644 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -150,8 +150,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phar) char* cwd; uint32_t cwd_len; bool cwd_init; - char *openssl_privatekey; - uint32_t openssl_privatekey_len; + zend_string *openssl_privatekey; /* phar_get_archive cache */ char* last_phar_name; uint32_t last_phar_name_len; diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index ee9c01dbd239..227fab0d20c3 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -3051,10 +3051,10 @@ PHP_METHOD(Phar, setDefaultStub) PHP_METHOD(Phar, setSignatureAlgorithm) { zend_long algo; - char *error, *key = NULL; - size_t key_len = 0; + char *error; + zend_string *key = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s!", &algo, &key, &key_len) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|S!", &algo, &key) != SUCCESS) { RETURN_THROWS(); } @@ -3080,8 +3080,10 @@ PHP_METHOD(Phar, setSignatureAlgorithm) } phar_obj->archive->sig_flags = (uint32_t)algo; phar_obj->archive->is_modified = 1; - PHAR_G(openssl_privatekey) = key; - PHAR_G(openssl_privatekey_len) = key_len; + if (PHAR_G(openssl_privatekey)) { + zend_string_release(PHAR_G(openssl_privatekey)); + } + PHAR_G(openssl_privatekey) = key ? zend_string_copy(key) : NULL; phar_flush(phar_obj->archive, &error); if (error) { diff --git a/ext/phar/tests/phar_setsignaturealgo_openssl_key_copy.phpt b/ext/phar/tests/phar_setsignaturealgo_openssl_key_copy.phpt new file mode 100644 index 000000000000..b9e0265d21c6 --- /dev/null +++ b/ext/phar/tests/phar_setsignaturealgo_openssl_key_copy.phpt @@ -0,0 +1,33 @@ +--TEST-- +Phar::setSignatureAlgorithm() with Phar::OPENSSL keeps its own copy of the private key +--EXTENSIONS-- +phar +openssl +--SKIPIF-- + +--INI-- +phar.require_hash=0 +phar.readonly=0 +--FILE-- +setSignatureAlgorithm(Phar::OPENSSL, file_get_contents($keyfile)); +// Reuse the freed memory +$other = str_repeat('x', filesize($keyfile)); +// Signs the phar again +$p['file2.txt'] = 'hello'; +var_dump($p->getSignature()['hash_type']); +?> +--CLEAN-- + +--EXPECT-- +string(7) "OpenSSL" diff --git a/ext/phar/util.c b/ext/phar/util.c index f4de9922f899..19c4fd500002 100644 --- a/ext/phar/util.c +++ b/ext/phar/util.c @@ -1954,7 +1954,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char mdtype = EVP_sha1(); } - in = BIO_new_mem_buf(PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len)); + in = PHAR_G(openssl_privatekey) ? BIO_new_mem_buf(ZSTR_VAL(PHAR_G(openssl_privatekey)), (int) ZSTR_LEN(PHAR_G(openssl_privatekey))) : NULL; if (in == NULL) { if (error) { @@ -2026,7 +2026,8 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char siglen = 0; php_stream_seek(fp, 0, SEEK_END); - if (FAILURE == phar_call_openssl_signverify(1, fp, php_stream_tell(fp), PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len), (char **)&sigbuf, &siglen, phar->sig_flags)) { + zend_string *privatekey = PHAR_G(openssl_privatekey); + if (FAILURE == phar_call_openssl_signverify(1, fp, php_stream_tell(fp), privatekey ? ZSTR_VAL(privatekey) : NULL, privatekey ? ZSTR_LEN(privatekey) : 0, (char **)&sigbuf, &siglen, phar->sig_flags)) { if (error) { spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname); }