Skip to content

Commit 5ef3ec7

Browse files
committed
ext/openssl: fix memory leaks when retrying failed TLS setup
1 parent 564e956 commit 5ef3ec7

3 files changed

Lines changed: 68 additions & 19 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ PHP NEWS
3030
. Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
3131
branches. (ilutov, Arnaud)
3232

33+
- OpenSSL:
34+
. Fixed memory leaks when stream_socket_enable_crypto() is retried after a
35+
failed TLS setup. (adapik)
36+
3337
- Reflection:
3438
. Fixed bug GH-24729 (Use-after-free in
3539
ReflectionClass::setStaticPropertyValue() when the old value's destructor
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Retrying stream_socket_enable_crypto() after a failed crypto setup does not leak
3+
--EXTENSIONS--
4+
openssl
5+
--FILE--
6+
<?php
7+
function enable_twice(bool $isClient, array $options): void
8+
{
9+
$server = stream_socket_server('tcp://127.0.0.1:0');
10+
$client = stream_socket_client('tcp://' . stream_socket_get_name($server, false),
11+
context: stream_context_create());
12+
$stream = $isClient ? $client : stream_socket_accept($server);
13+
stream_context_set_options($stream, ['ssl' => $options]);
14+
15+
$method = $isClient ? STREAM_CRYPTO_METHOD_TLS_CLIENT : STREAM_CRYPTO_METHOD_TLS_SERVER;
16+
var_dump(@stream_socket_enable_crypto($stream, true, $method));
17+
var_dump(@stream_socket_enable_crypto($stream, true, $method));
18+
}
19+
20+
echo "cafile\n";
21+
enable_twice(true, ['cafile' => __DIR__ . '/does-not-exist.pem']);
22+
23+
echo "ciphers\n";
24+
enable_twice(true, ['ciphers' => 'bogus']);
25+
26+
echo "local_cert\n";
27+
enable_twice(true, ['local_cert' => __DIR__ . '/does-not-exist.pem']);
28+
29+
echo "alpn_protocols with dh_param\n";
30+
enable_twice(false, ['alpn_protocols' => 'h2', 'dh_param' => __DIR__ . '/does-not-exist.pem']);
31+
?>
32+
--EXPECT--
33+
cafile
34+
bool(false)
35+
bool(false)
36+
ciphers
37+
bool(false)
38+
bool(false)
39+
local_cert
40+
bool(false)
41+
bool(false)
42+
alpn_protocols with dh_param
43+
bool(false)
44+
bool(false)

‎ext/openssl/xp_ssl.c‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
17791779
} else {
17801780
verify_peer = true;
17811781
if (FAILURE == php_openssl_enable_peer_verification(sslsock->ctx, stream)) {
1782-
return FAILURE;
1782+
goto fail;
17831783
}
17841784
}
17851785

@@ -1797,7 +1797,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
17971797
#endif
17981798
if (cipherlist) {
17991799
if (SSL_CTX_set_cipher_list(sslsock->ctx, cipherlist) != 1) {
1800-
return FAILURE;
1800+
goto fail;
18011801
}
18021802
}
18031803

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

18211821
if (alpn == NULL) {
18221822
php_error_docref(NULL, E_WARNING, "Failed parsing comma-separated TLS ALPN protocol string");
1823-
SSL_CTX_free(sslsock->ctx);
1824-
sslsock->ctx = NULL;
1825-
return FAILURE;
1823+
goto fail;
18261824
}
18271825
if (sslsock->is_client) {
18281826
if (SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len)) {
18291827
php_error_docref(NULL, E_WARNING, "Failed setting TLS ALPN protocols, protocol names must not be empty");
18301828
efree(alpn);
1831-
SSL_CTX_free(sslsock->ctx);
1832-
sslsock->ctx = NULL;
1833-
return FAILURE;
1829+
goto fail;
18341830
}
18351831
} else {
18361832
sslsock->alpn_ctx.data = (unsigned char *) pestrndup((const char*)alpn, alpn_len, php_stream_is_persistent(stream));
@@ -1847,7 +1843,7 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
18471843
}
18481844

18491845
if (FAILURE == php_openssl_set_local_cert(sslsock->ctx, stream)) {
1850-
return FAILURE;
1846+
goto fail;
18511847
}
18521848

18531849
SSL_CTX_set_options(sslsock->ctx, ssl_ctx_options);
@@ -1861,23 +1857,15 @@ static zend_result php_openssl_setup_crypto(php_stream *stream,
18611857
PHP_STREAM_CONTEXT(stream) &&
18621858
FAILURE == php_openssl_set_server_specific_opts(stream, sslsock->ctx)
18631859
) {
1864-
return FAILURE;
1860+
goto fail;
18651861
}
18661862

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

18691865
if (sslsock->ssl_handle == NULL
18701866
|| !SSL_set_ex_data(sslsock->ssl_handle, php_openssl_get_ssl_stream_data_index(), stream)) {
18711867
php_error_docref(NULL, E_WARNING, "SSL handle creation failure");
1872-
SSL_CTX_free(sslsock->ctx);
1873-
sslsock->ctx = NULL;
1874-
#ifdef HAVE_TLS_ALPN
1875-
if (sslsock->alpn_ctx.data) {
1876-
pefree(sslsock->alpn_ctx.data, php_stream_is_persistent(stream));
1877-
sslsock->alpn_ctx.data = NULL;
1878-
}
1879-
#endif
1880-
return FAILURE;
1868+
goto fail;
18811869
}
18821870

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

19131901
return SUCCESS;
1902+
1903+
fail:
1904+
SSL_free(sslsock->ssl_handle);
1905+
sslsock->ssl_handle = NULL;
1906+
SSL_CTX_free(sslsock->ctx);
1907+
sslsock->ctx = NULL;
1908+
#ifdef HAVE_TLS_ALPN
1909+
if (sslsock->alpn_ctx.data) {
1910+
pefree(sslsock->alpn_ctx.data, php_stream_is_persistent(stream));
1911+
sslsock->alpn_ctx.data = NULL;
1912+
}
1913+
#endif
1914+
return FAILURE;
19141915
}
19151916
/* }}} */
19161917

0 commit comments

Comments
 (0)