mkoppanen                                Sat, 22 Aug 2009 02:31:23 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=287563

Log:
Fixes a memory leak in ssl streams. The context was not properly freed

Changed paths:
    U   php/php-src/branches/PHP_5_2/ext/openssl/xp_ssl.c
    U   php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c
    U   php/php-src/trunk/ext/openssl/xp_ssl.c

Modified: php/php-src/branches/PHP_5_2/ext/openssl/xp_ssl.c
===================================================================
--- php/php-src/branches/PHP_5_2/ext/openssl/xp_ssl.c   2009-08-21 22:47:18 UTC 
(rev 287562)
+++ php/php-src/branches/PHP_5_2/ext/openssl/xp_ssl.c   2009-08-22 02:31:23 UTC 
(rev 287563)
@@ -47,6 +47,7 @@
 typedef struct _php_openssl_netstream_data_t {
        php_netstream_data_t s;
        SSL *ssl_handle;
+       SSL_CTX *ctx;
        struct timeval connect_timeout;
        int enable_on_connect;
        int is_client;
@@ -267,6 +268,10 @@
                        SSL_free(sslsock->ssl_handle);
                        sslsock->ssl_handle = NULL;
                }
+               if (sslsock->ctx) {
+                       SSL_CTX_free(sslsock->ctx);
+                       sslsock->ctx = NULL;
+               }
                if (sslsock->s.socket != SOCK_ERR) {
 #ifdef PHP_WIN32
                        /* prevent more data from coming in */
@@ -308,7 +313,6 @@
                php_stream_xport_crypto_param *cparam
                TSRMLS_DC)
 {
-       SSL_CTX *ctx;
        SSL_METHOD *method;

        if (sslsock->ssl_handle) {
@@ -357,18 +361,19 @@

        }

-       ctx = SSL_CTX_new(method);
-       if (ctx == NULL) {
+       sslsock->ctx = SSL_CTX_new(method);
+       if (sslsock->ctx == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL context");
                return -1;
        }

-       SSL_CTX_set_options(ctx, SSL_OP_ALL);
+       SSL_CTX_set_options(sslsock->ctx, SSL_OP_ALL);

-       sslsock->ssl_handle = php_SSL_new_from_context(ctx, stream TSRMLS_CC);
+       sslsock->ssl_handle = php_SSL_new_from_context(sslsock->ctx, stream 
TSRMLS_CC);
        if (sslsock->ssl_handle == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL handle");
-               SSL_CTX_free(ctx);
+               SSL_CTX_free(sslsock->ctx);
+               sslsock->ctx = NULL;
                return -1;
        }

@@ -789,6 +794,9 @@
         * connecting */
        sslsock->s.socket = -1;

+       /* Initialize context as NULL */
+       sslsock->ctx = NULL;
+
        stream = php_stream_alloc_rel(&php_openssl_socket_ops, sslsock, 
persistent_id, "r+");

        if (stream == NULL)     {

Modified: php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c   2009-08-21 22:47:18 UTC 
(rev 287562)
+++ php/php-src/branches/PHP_5_3/ext/openssl/xp_ssl.c   2009-08-22 02:31:23 UTC 
(rev 287563)
@@ -48,6 +48,7 @@
 typedef struct _php_openssl_netstream_data_t {
        php_netstream_data_t s;
        SSL *ssl_handle;
+       SSL_CTX *ctx;
        struct timeval connect_timeout;
        int enable_on_connect;
        int is_client;
@@ -254,6 +255,10 @@
                        SSL_free(sslsock->ssl_handle);
                        sslsock->ssl_handle = NULL;
                }
+               if (sslsock->ctx) {
+                       SSL_CTX_free(sslsock->ctx);
+                       sslsock->ctx = NULL;
+               }
                if (sslsock->s.socket != SOCK_ERR) {
 #ifdef PHP_WIN32
                        /* prevent more data from coming in */
@@ -295,7 +300,6 @@
                php_stream_xport_crypto_param *cparam
                TSRMLS_DC)
 {
-       SSL_CTX *ctx;
        SSL_METHOD *method;

        if (sslsock->ssl_handle) {
@@ -344,18 +348,19 @@

        }

-       ctx = SSL_CTX_new(method);
-       if (ctx == NULL) {
+       sslsock->ctx = SSL_CTX_new(method);
+       if (sslsock->ctx == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL context");
                return -1;
        }

-       SSL_CTX_set_options(ctx, SSL_OP_ALL);
+       SSL_CTX_set_options(sslsock->ctx, SSL_OP_ALL);

-       sslsock->ssl_handle = php_SSL_new_from_context(ctx, stream TSRMLS_CC);
+       sslsock->ssl_handle = php_SSL_new_from_context(sslsock->ctx, stream 
TSRMLS_CC);
        if (sslsock->ssl_handle == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL handle");
-               SSL_CTX_free(ctx);
+               SSL_CTX_free(sslsock->ctx);
+               sslsock->ctx = NULL;
                return -1;
        }

@@ -776,6 +781,9 @@
         * connecting */
        sslsock->s.socket = -1;

+       /* Initialize context as NULL */
+       sslsock->ctx = NULL;
+
        stream = php_stream_alloc_rel(&php_openssl_socket_ops, sslsock, 
persistent_id, "r+");

        if (stream == NULL)     {

Modified: php/php-src/trunk/ext/openssl/xp_ssl.c
===================================================================
--- php/php-src/trunk/ext/openssl/xp_ssl.c      2009-08-21 22:47:18 UTC (rev 
287562)
+++ php/php-src/trunk/ext/openssl/xp_ssl.c      2009-08-22 02:31:23 UTC (rev 
287563)
@@ -48,6 +48,7 @@
 typedef struct _php_openssl_netstream_data_t {
        php_netstream_data_t s;
        SSL *ssl_handle;
+       SSL_CTX *ctx;
        struct timeval connect_timeout;
        int enable_on_connect;
        int is_client;
@@ -254,6 +255,10 @@
                        SSL_free(sslsock->ssl_handle);
                        sslsock->ssl_handle = NULL;
                }
+               if (sslsock->ctx) {
+                       SSL_CTX_free(sslsock->ctx);
+                       sslsock->ctx = NULL;
+               }
                if (sslsock->s.socket != SOCK_ERR) {
 #ifdef PHP_WIN32
                        /* prevent more data from coming in */
@@ -295,7 +300,6 @@
                php_stream_xport_crypto_param *cparam
                TSRMLS_DC)
 {
-       SSL_CTX *ctx;
        SSL_METHOD *method;

        if (sslsock->ssl_handle) {
@@ -344,18 +348,19 @@

        }

-       ctx = SSL_CTX_new(method);
-       if (ctx == NULL) {
+       sslsock->ctx = SSL_CTX_new(method);
+       if (sslsock->ctx == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL context");
                return -1;
        }

-       SSL_CTX_set_options(ctx, SSL_OP_ALL);
+       SSL_CTX_set_options(sslsock->ctx, SSL_OP_ALL);

-       sslsock->ssl_handle = php_SSL_new_from_context(ctx, stream TSRMLS_CC);
+       sslsock->ssl_handle = php_SSL_new_from_context(sslsock->ctx, stream 
TSRMLS_CC);
        if (sslsock->ssl_handle == NULL) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create 
an SSL handle");
-               SSL_CTX_free(ctx);
+               SSL_CTX_free(sslsock->ctx);
+               sslsock->ctx = NULL;
                return -1;
        }

@@ -775,6 +780,9 @@
         * connecting */
        sslsock->s.socket = -1;

+       /* Initialize context as NULL */
+       sslsock->ctx = NULL;
+
        stream = php_stream_alloc_rel(&php_openssl_socket_ops, sslsock, 
persistent_id, "r+");

        if (stream == NULL)     {

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to