Commit 9af71942c66 for php

commit 9af71942c6610852dd6350cf19e278628c8b5b60
Author: Artem Ukrainskiy <art.ukrainskiy@gmail.com>
Date:   Tue Nov 17 16:50:03 2026 +0000

    Fix GH-25528: Retain the Phar OpenSSL signing key (#25529)

    Phar's OpenSSL signing code held a pointer into the private key argument.
    A later write to the archive could read freed memory after that argument
    went out of scope. Hold a reference to the zend_string and release it
    when the key is replaced or the request ends.

    Closes #25529

diff --git a/NEWS b/NEWS
index 6b173f14018..3631b3bc908 100644
--- a/NEWS
+++ b/NEWS
@@ -141,6 +141,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 d15ca0b4e85..5a51b25e288 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 58a30a52b1c..4af29eaa460 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 ee9c01dbd23..227fab0d20c 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 00000000000..b9e0265d21c
--- /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 e294cfe34d2..49932a8e384 100644
--- a/ext/phar/util.c
+++ b/ext/phar/util.c
@@ -1971,7 +1971,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) {
@@ -2043,7 +2043,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);
 				}