Skip to content

Commit ac1372d

Browse files
committed
Fix alpn_ctx leaking in openssl
1 parent 8823b68 commit ac1372d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

ext/openssl/xp_ssl.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,8 +1599,8 @@ int php_openssl_setup_crypto(php_stream *stream,
15991599
if (sslsock->is_client) {
16001600
SSL_CTX_set_alpn_protos(sslsock->ctx, alpn, alpn_len);
16011601
} else {
1602-
sslsock->alpn_ctx = (php_openssl_alpn_ctx *) emalloc(sizeof(php_openssl_alpn_ctx));
1603-
sslsock->alpn_ctx->data = (unsigned char*)estrndup((const char*)alpn, alpn_len);
1602+
sslsock->alpn_ctx = (php_openssl_alpn_ctx *) pemalloc(sizeof(php_openssl_alpn_ctx), php_stream_is_persistent(stream));
1603+
sslsock->alpn_ctx->data = (unsigned char *) pestrndup((const char*)alpn, alpn_len, php_stream_is_persistent(stream));
16041604
sslsock->alpn_ctx->len = alpn_len;
16051605
SSL_CTX_set_alpn_select_cb(sslsock->ctx, server_alpn_callback, sslsock);
16061606
}
@@ -1632,6 +1632,13 @@ int php_openssl_setup_crypto(php_stream *stream,
16321632
php_error_docref(NULL, E_WARNING, "SSL handle creation failure");
16331633
SSL_CTX_free(sslsock->ctx);
16341634
sslsock->ctx = NULL;
1635+
#ifdef HAVE_TLS_ALPN
1636+
if (sslsock->alpn_ctx) {
1637+
pefree(sslsock->alpn_ctx->data, php_stream_is_persistent(stream));
1638+
pefree(sslsock->alpn_ctx, php_stream_is_persistent(stream));
1639+
sslsock->alpn_ctx = NULL;
1640+
}
1641+
#endif
16351642
return FAILURE;
16361643
} else {
16371644
SSL_set_ex_data(sslsock->ssl_handle, php_openssl_get_ssl_stream_data_index(), stream);
@@ -2137,6 +2144,12 @@ static int php_openssl_sockop_close(php_stream *stream, int close_handle) /* {{{
21372144
SSL_CTX_free(sslsock->ctx);
21382145
sslsock->ctx = NULL;
21392146
}
2147+
#ifdef HAVE_TLS_ALPN
2148+
if (sslsock->alpn_ctx) {
2149+
pefree(sslsock->alpn_ctx->data, php_stream_is_persistent(stream));
2150+
pefree(sslsock->alpn_ctx, php_stream_is_persistent(stream));
2151+
}
2152+
#endif
21402153
#ifdef PHP_WIN32
21412154
if (sslsock->s.socket == -1)
21422155
sslsock->s.socket = SOCK_ERR;

0 commit comments

Comments
 (0)