Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ PHP NEWS
. Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
branches. (ilutov, Arnaud)

- OpenSSL:
. Fixed memory leaks when stream_socket_enable_crypto() is retried after a
failed TLS setup. (adapik)

- Reflection:
. Fixed bug GH-24729 (Use-after-free in
ReflectionClass::setStaticPropertyValue() when the old value's destructor
Expand Down
44 changes: 44 additions & 0 deletions ext/openssl/tests/stream_crypto_setup_failure_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Retrying stream_socket_enable_crypto() after a failed crypto setup does not leak
--EXTENSIONS--
openssl
--FILE--
<?php
function enable_twice(bool $isClient, array $options): void
{
$server = stream_socket_server('tcp://127.0.0.1:0');
$client = stream_socket_client('tcp://' . stream_socket_get_name($server, false),
context: stream_context_create());
$stream = $isClient ? $client : stream_socket_accept($server);
stream_context_set_options($stream, ['ssl' => $options]);

$method = $isClient ? STREAM_CRYPTO_METHOD_TLS_CLIENT : STREAM_CRYPTO_METHOD_TLS_SERVER;
var_dump(@stream_socket_enable_crypto($stream, true, $method));
var_dump(@stream_socket_enable_crypto($stream, true, $method));
}

echo "cafile\n";
enable_twice(true, ['cafile' => __DIR__ . '/does-not-exist.pem']);

echo "ciphers\n";
enable_twice(true, ['ciphers' => 'bogus']);

echo "local_cert\n";
enable_twice(true, ['local_cert' => __DIR__ . '/does-not-exist.pem']);

echo "alpn_protocols with dh_param\n";
enable_twice(false, ['alpn_protocols' => 'h2', 'dh_param' => __DIR__ . '/does-not-exist.pem']);
?>
--EXPECT--
cafile
bool(false)
bool(false)
ciphers
bool(false)
bool(false)
local_cert
bool(false)
bool(false)
alpn_protocols with dh_param
bool(false)
bool(false)
39 changes: 20 additions & 19 deletions ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
} else {
verify_peer = true;
if (FAILURE == php_openssl_enable_peer_verification(sslsock->ctx, stream)) {
return FAILURE;
goto fail;
}
}

Expand All @@ -1797,7 +1797,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
#endif
if (cipherlist) {
if (SSL_CTX_set_cipher_list(sslsock->ctx, cipherlist) != 1) {
return FAILURE;
goto fail;
}
}

Expand All @@ -1820,17 +1820,13 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,

if (alpn == NULL) {
php_error_docref(NULL, E_WARNING, "Failed parsing comma-separated TLS ALPN protocol string");
SSL_CTX_free(sslsock->ctx);
sslsock->ctx = NULL;
return FAILURE;
goto fail;
}
if (sslsock->is_client) {
if (SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len)) {
php_error_docref(NULL, E_WARNING, "Failed setting TLS ALPN protocols, protocol names must not be empty");
efree(alpn);
SSL_CTX_free(sslsock->ctx);
sslsock->ctx = NULL;
return FAILURE;
goto fail;
}
} else {
sslsock->alpn_ctx.data = (unsigned char *) pestrndup((const char*)alpn, alpn_len, php_stream_is_persistent(stream));
Expand All @@ -1847,7 +1843,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
}

if (FAILURE == php_openssl_set_local_cert(sslsock->ctx, stream)) {
return FAILURE;
goto fail;
}

SSL_CTX_set_options(sslsock->ctx, ssl_ctx_options);
Expand All @@ -1861,23 +1857,15 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
PHP_STREAM_CONTEXT(stream) &&
FAILURE == php_openssl_set_server_specific_opts(stream, sslsock->ctx)
) {
return FAILURE;
goto fail;
}

sslsock->ssl_handle = SSL_new(sslsock->ctx);

if (sslsock->ssl_handle == NULL
|| !SSL_set_ex_data(sslsock->ssl_handle, php_openssl_get_ssl_stream_data_index(), stream)) {
php_error_docref(NULL, E_WARNING, "SSL handle creation failure");
SSL_CTX_free(sslsock->ctx);
sslsock->ctx = NULL;
#ifdef HAVE_TLS_ALPN
if (sslsock->alpn_ctx.data) {
pefree(sslsock->alpn_ctx.data, php_stream_is_persistent(stream));
sslsock->alpn_ctx.data = NULL;
}
#endif
return FAILURE;
goto fail;
}

if (!SSL_set_fd(sslsock->ssl_handle, sslsock->s.socket)) {
Expand Down Expand Up @@ -1911,6 +1899,19 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
}

return SUCCESS;

fail:
SSL_free(sslsock->ssl_handle);
sslsock->ssl_handle = NULL;
SSL_CTX_free(sslsock->ctx);
sslsock->ctx = NULL;
#ifdef HAVE_TLS_ALPN
if (sslsock->alpn_ctx.data) {
pefree(sslsock->alpn_ctx.data, php_stream_is_persistent(stream));
sslsock->alpn_ctx.data = NULL;
}
#endif
return FAILURE;
}
/* }}} */

Expand Down
Loading