From:             
Operating system: Linux, OSX
PHP version:      5.3.5
Package:          OpenSSL related
Bug Type:         Bug
Bug description:stream_socket_accept leaks memory on SSL protocol using peer 
parameter

Description:
------------
I encountered a memory leak issue when creating an SSL enabled socket
server in a long lived process. When an incoming listening socket is
accepted on either the SSL or TLS protocol, the &$peer passthrough variable
in the stream_socket_accept function causes a memory leak.



NOTE: The test script below needs a valid certificate.pem file to work!!

NOTE: The test script spawns a webserver to be reached on 127.0.0.1:8888

NOTE: Testing can be done in a browser by choosing appropriate protocol
(http/https) and perform regular page refreshes



Test matrix:

SSL=true, PEER=true   -> memory leak!

SSL=false, PEER=true  -> no memory leak

SSL=true, PEER=false  -> no memory leak

SSL=false, PEER=false -> no memory leak

Test script:
---------------
define('SSL', true);

define('PEER', true);



$context = stream_context_create();

stream_context_set_option($context, 'ssl', 'local_cert',
'certificate.pem');

stream_context_set_option($context, 'ssl', 'passphrase', 'password');

stream_context_set_option($context, 'ssl', 'allow_self_signed', true);



if(SSL) {

   $server = stream_socket_server("ssl://0.0.0.0:8888", $errno, $errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);

} else {

   $server = stream_socket_server("tcp://0.0.0.0:8888", $errno, $errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN);

}



while(true) {

   if(PEER) { $client = @stream_socket_accept($server, 2, $peer); } else {
$client = @stream_socket_accept($server, 2); }

   if($client === false) continue;

   @fwrite($client, "HTTP/1.1 200 OK\r\n\r\nMemory usage:
".memory_get_usage().' bytes');

   @fclose($client);

}

Expected result:
----------------
Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Memory usage: 641032 bytes

Actual result:
--------------
Memory usage: 641368 bytes

Memory usage: 641560 bytes

Memory usage: 641752 bytes

Memory usage: 641944 bytes

Memory usage: 642232 bytes

Memory usage: 642424 bytes

Memory usage: 642616 bytes

Memory usage: 642808 bytes

Memory usage: 643000 bytes

Memory usage: 643192 bytes

Memory usage: 643384 bytes

Memory usage: 643576 bytes

-- 
Edit bug report at http://bugs.php.net/bug.php?id=54230&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=54230&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=54230&r=trysnapshot53
Try a snapshot (trunk):              
http://bugs.php.net/fix.php?id=54230&r=trysnapshottrunk
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=54230&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=54230&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=54230&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=54230&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=54230&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=54230&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=54230&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=54230&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=54230&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=54230&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=54230&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=54230&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=54230&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=54230&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=54230&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=54230&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=54230&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=54230&r=mysqlcfg

Reply via email to