[php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Sun, 22 Nov 2026 16:16:19 +0000
Subject: [php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144035@lists.php.net to get a copy of this message
Author: Weilin Du (LamentXU123) Date: 2026-11-23T00:09:40+08:00 Commit: https://github.com/php/php-src/commit/f2faa48faee81e67f507e98ae108b291bc501d41 Raw diff: https://github.com/php/php-src/commit/f2faa48faee81e67f507e98ae108b291bc501d41.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: Fix GH-25528: Retain the Phar OpenSSL signing key (#25529) Changed paths: A ext/phar/tests/phar_setsignaturealgo_openssl_key_copy.phpt M NEWS M ext/phar/phar.c M ext/phar/phar_internal.h M ext/phar/phar_object.c M ext/phar/util.c Diff: diff --git a/NEWS b/NEWS index 667d1c520144..c02edbf7d6c7 100644 --- a/NEWS +++ b/NEWS @@ -131,6 +131,10 @@ PHP NEWS . Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later execute() or rebound while fetching). (Ilia Alshanetsky) +- Phar: + . Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a + freed private key). (Artem Ukrainskiy) + - POSIX: . Fixed bug GH-25530 (posix_get_last_error() returns an error from a previous request). (Artem Ukrainskiy) diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 9aa3f8d73bf1..d85a2c2d2dfd 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -3514,6 +3514,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 11301726b831..2b243705b6d0 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 5ce6f39a5777..c6770789de49 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-- +<?php +if (!in_array("OpenSSL", Phar::getSupportedSignatures())) die("skip openssl support required"); +?> +--INI-- +phar.require_hash=0 +phar.readonly=0 +--FILE-- +<?php +$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar'; +$keyfile = __DIR__ . '/files/private.pem'; + +$p = new Phar($fname); +$p['file1.txt'] = 'hi'; +// The key string is freed when the call returns +$p->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-- +<?php +unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar'); +?> +--EXPECT-- +string(7) "OpenSSL" diff --git a/ext/phar/util.c b/ext/phar/util.c index a959735ceae1..7f94ac6286a4 100644 --- a/ext/phar/util.c +++ b/ext/phar/util.c @@ -1940,7 +1940,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) { @@ -2012,7 +2012,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(true, 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(true, 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); }

« previous php.cvs (#144035) next »