Skip to content

Fix GH-25528: Phar::setSignatureAlgorithm(): OpenSSL signing reads a freed private key - #25529

Closed
ArtUkrainskiy wants to merge 1 commit into
php:PHP-8.4from
ArtUkrainskiy:phar-openssl-key
Closed

ArtUkrainskiy wants to merge 1 commit into
php:PHP-8.4from
ArtUkrainskiy:phar-openssl-key

Conversation

@ArtUkrainskiy

@ArtUkrainskiy ArtUkrainskiy commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes GH-25528

UPDATED 17.11: after review, the key is a zend_string reference instead of a copy.

Phar::setSignatureAlgorithm() stores a pointer into its $privateKey argument in PHAR_G(openssl_privatekey) and doesn't copy the string. Every later write to the phar signs it again with that key. If the key string has been freed by then, signing reads freed memory. A file_get_contents() result passed directly is enough:

$phar->setSignatureAlgorithm(Phar::OPENSSL, file_get_contents('private.pem'));
$phar->addFromString('b.txt', 'b'); // signs with the freed key

This keeps the key as a zend_string with its own reference and releases it when the key is replaced and in RSHUTDOWN.

The test passes the key the same way, then allocates a string of the same size so the freed block gets reused. Without the fix it fails on a normal build too, with "unable to process private key". ext/phar tests pass, also with ASan and ZTS. The merge up to 8.5 and later has a small conflict in util.c, at the phar_call_openssl_signverify() call this changes.

@LamentXU123 LamentXU123 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit.

Comment thread ext/phar/phar_object.c Outdated
if (PHAR_G(openssl_privatekey)) {
efree(PHAR_G(openssl_privatekey));
}
PHAR_G(openssl_privatekey) = key ? estrndup(key, key_len) : NULL;

@LamentXU123 LamentXU123 Nov 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use zend_string * here so we can use zend_string_copy and zend_string_release for better performance? (i.e. don't need to allocate a second buffer for privateKey)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks! It's a zend_string now, held with zend_string_copy() and released when the key is replaced and in RSHUTDOWN.

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.
@ArtUkrainskiy
ArtUkrainskiy marked this pull request as ready for review November 17, 2026 21:07

@LamentXU123 LamentXU123 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I want to wait for another people to have a look tho :) /cc @iliaal

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks right

Comment thread 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.4.28 has already shipped. Could you move this entry to a new Phar section under 8.4.29?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its fine I will do this when I merge up later.

LamentXU123 added a commit that referenced this pull request Nov 22, 2026
* PHP-8.6:
  Fix GH-25528: Retain the Phar OpenSSL signing key (#25529)
iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 22, 2026
* PHP-8.4:
  Fix phpGH-25528: Retain the Phar OpenSSL signing key (php#25529)
iliaal pushed a commit to iliaal/php-src that referenced this pull request Nov 22, 2026
* PHP-8.5:
  Fix phpGH-25528: Retain the Phar OpenSSL signing key (php#25529)
arnaud-lb added a commit to bm1549/php-src that referenced this pull request Nov 24, 2026
* PHP-8.5: (25 commits)
  [skip ci] Add APP_DEBUG=0 for Symfony Preloading test
  streams: fix stale readbuflen after stream_filter_remove() on a read filter
  Update NEWS for phpGH-20920 LibreSSL build fix
  Fix phpGH-20920: do not use OPENSSL_strndup() for peer CN matching
  Simplify php_openssl_load_all_certs_from_file()
  Fix phpGH-20920: only register PKCS7_NO_DUAL_CONTENT when it is defined
  Fix phpGH-20920: guard ENGINE_cleanup() with OPENSSL_NO_ENGINE again
  ext/soap: Fix NULL dereference on empty Body with matching SOAPAction (phpGH-25661)
  ext/dom: Fix use-after-free in getElementById() after removing an ID attribute
  ext/reflection: Don't call destructor when constructor is not public in newInstance(Args)()
  ext/phar: unset() of an entry with an open handle use-after-free.
  ext/intl: Fix IntlCalendar::setTime() build on PHP 8.5 and later
  ext/intl: Reject non-finite timestamps in IntlCalendar::setTime() (php#25668)
  [skip ci] Speed up community build
  [skip ci] Backport test-suite.yml changes from master
  Fix propagation of public(set) of readonly trait property
  Make test accept different outputs
  Fix phpGH-25528: Retain the Phar OpenSSL signing key (php#25529)
  ext/pdo_sqlite: Fix use-after-free of bound string and LOB parameters
  ext/opcache: Fix JIT ASSIGN_DIM undefined OP_DATA and dim reporting on non-array containers (phpGH-24665)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants