Skip to content

Commit f2faa48

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix GH-25528: Retain the Phar OpenSSL signing key (#25529)
2 parents 0ccbf27 + 9af7194 commit f2faa48

6 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ PHP NEWS
131131
. Fixed bug GH-25588 (Use-after-free of a string parameter omitted on a later
132132
execute() or rebound while fetching). (Ilia Alshanetsky)
133133

134+
- Phar:
135+
. Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a
136+
freed private key). (Artem Ukrainskiy)
137+
134138
- POSIX:
135139
. Fixed bug GH-25530 (posix_get_last_error() returns an error from a
136140
previous request). (Artem Ukrainskiy)

‎ext/phar/phar.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,11 @@ PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */
35143514
PHAR_G(cwd_init) = 0;
35153515
}
35163516

3517+
if (PHAR_G(openssl_privatekey)) {
3518+
zend_string_release(PHAR_G(openssl_privatekey));
3519+
PHAR_G(openssl_privatekey) = NULL;
3520+
}
3521+
35173522
PHAR_G(request_done) = 1;
35183523
return SUCCESS;
35193524
}

‎ext/phar/phar_internal.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phar)
150150
char* cwd;
151151
uint32_t cwd_len;
152152
bool cwd_init;
153-
char *openssl_privatekey;
154-
uint32_t openssl_privatekey_len;
153+
zend_string *openssl_privatekey;
155154
/* phar_get_archive cache */
156155
char* last_phar_name;
157156
uint32_t last_phar_name_len;

‎ext/phar/phar_object.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,10 +3051,10 @@ PHP_METHOD(Phar, setDefaultStub)
30513051
PHP_METHOD(Phar, setSignatureAlgorithm)
30523052
{
30533053
zend_long algo;
3054-
char *error, *key = NULL;
3055-
size_t key_len = 0;
3054+
char *error;
3055+
zend_string *key = NULL;
30563056

3057-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s!", &algo, &key, &key_len) != SUCCESS) {
3057+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|S!", &algo, &key) != SUCCESS) {
30583058
RETURN_THROWS();
30593059
}
30603060

@@ -3080,8 +3080,10 @@ PHP_METHOD(Phar, setSignatureAlgorithm)
30803080
}
30813081
phar_obj->archive->sig_flags = (uint32_t)algo;
30823082
phar_obj->archive->is_modified = 1;
3083-
PHAR_G(openssl_privatekey) = key;
3084-
PHAR_G(openssl_privatekey_len) = key_len;
3083+
if (PHAR_G(openssl_privatekey)) {
3084+
zend_string_release(PHAR_G(openssl_privatekey));
3085+
}
3086+
PHAR_G(openssl_privatekey) = key ? zend_string_copy(key) : NULL;
30853087

30863088
phar_flush(phar_obj->archive, &error);
30873089
if (error) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Phar::setSignatureAlgorithm() with Phar::OPENSSL keeps its own copy of the private key
3+
--EXTENSIONS--
4+
phar
5+
openssl
6+
--SKIPIF--
7+
<?php
8+
if (!in_array("OpenSSL", Phar::getSupportedSignatures())) die("skip openssl support required");
9+
?>
10+
--INI--
11+
phar.require_hash=0
12+
phar.readonly=0
13+
--FILE--
14+
<?php
15+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
16+
$keyfile = __DIR__ . '/files/private.pem';
17+
18+
$p = new Phar($fname);
19+
$p['file1.txt'] = 'hi';
20+
// The key string is freed when the call returns
21+
$p->setSignatureAlgorithm(Phar::OPENSSL, file_get_contents($keyfile));
22+
// Reuse the freed memory
23+
$other = str_repeat('x', filesize($keyfile));
24+
// Signs the phar again
25+
$p['file2.txt'] = 'hello';
26+
var_dump($p->getSignature()['hash_type']);
27+
?>
28+
--CLEAN--
29+
<?php
30+
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
31+
?>
32+
--EXPECT--
33+
string(7) "OpenSSL"

‎ext/phar/util.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char
19401940
mdtype = EVP_sha1();
19411941
}
19421942

1943-
in = BIO_new_mem_buf(PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len));
1943+
in = PHAR_G(openssl_privatekey) ? BIO_new_mem_buf(ZSTR_VAL(PHAR_G(openssl_privatekey)), (int) ZSTR_LEN(PHAR_G(openssl_privatekey))) : NULL;
19441944

19451945
if (in == NULL) {
19461946
if (error) {
@@ -2012,7 +2012,8 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char
20122012
siglen = 0;
20132013
php_stream_seek(fp, 0, SEEK_END);
20142014

2015-
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)) {
2015+
zend_string *privatekey = PHAR_G(openssl_privatekey);
2016+
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)) {
20162017
if (error) {
20172018
spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname);
20182019
}

0 commit comments

Comments
 (0)