You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
OpenSSL memory leak without call to SSL_COMP_free_compression_methods #817
That's correct. Sorry for the confusion. I included OPENSSL_NO_COMP in the title because is directly related to the problem. Sensibly, it should be defined in the default build process, but I avoid modifying that build process as much as possible.
To give you some details on what is happening. I am wrapping cURL in a COM library (staticly linked to cURL and OpenSSL) that can be loaded/unloaded as needed. I'm not entirely sure how COM isolates the memory, but I know that every time the library loads it allocates new memory within the process. That's why we can't just load curl_global_init(CURL_GLOBAL_ALL) at the beginning of the process. It can't reference the "shared" memory from its previous loading.
Anyway, during a stress test, I found that the library was leaking an 36 bytes every time it was loaded/unloaded which added up after a few hours. That lead to the code sample in the original post.
Confirmed with Windows 7 x64 Enterprise w/curl from master (53ae370 2016-05-20) and OpenSSL/1.0.2h default config. I guess it is documented somewhere in openssl that we're supposed to call that function on cleanup? This is the first time I've heard of it.
I am wrapping cURL in a COM library (staticly linked to cURL and OpenSSL) that can be loaded/unloaded as needed. I'm not entirely sure how COM isolates the memory, but I know that every time the library loads it allocates new memory within the process. That's why we can't just load curl_global_init(CURL_GLOBAL_ALL) at the beginning of the process. It can't reference the "shared" memory from its previous loading.
Initializing libcurl from a DLL is tricky. curl_global_init and curl_global_cleanup are not thread safe and may call the initialization and cleanup routines of other libraries that are not thread safe. It's documented to not call init when any other threads are running. Also read this tldr. Also you must follow thread safety requirements.
The SSL_COMP_free_compression_methods function was added in OpenSSL in 2014: openssl/openssl@db7b5e0d768485 (unknown to me just now which versions that have it)
I'm still doing testing on it and it won't be something that I'll push into production lightly. The whole reason this came up is that OpenSSL is already loaded in memory. The library wasn't able to reference that shared memory. cURL always reported SSL errors. I switched to using CURL_GLOBAL_ALL and it resolved the problem. The other parts of the code that use OpenSSL - even after the library has been loaded/unloaded thousands of times - appear to be unaffected (so far).
It's a horrible mess.
Agreed. Plus, the updated documentation for 1.1.0 states that everything will be handled automagically, but given my predicament, I'll have to force cleanup one way or another.
As of version 1.1.0 OpenSSL will automatically allocate all resources that it needs so no explicit initialisation is required. Similarly it will also automatically deinitialise as required.
changed the title [-]Memory leak when OpenSSL is compiled without OPENSSL_NO_COMP defined[/-][+]OpenSSL memory leak without call to SSL_COMP_free_compression_methods[/+]on May 20, 2016
I'm still doing testing on it and it won't be something that I'll push into production lightly. The whole reason this came up is that OpenSSL is already loaded in memory. The library wasn't able to reference that shared memory. cURL always reported SSL errors. I switched to using CURL_GLOBAL_ALL and it resolved the problem. The other parts of the code that use OpenSSL - even after the library has been loaded/unloaded thousands of times - appear to be unaffected (so far).
If you have a static OpenSSL library in your DLL you should be OK as long as you control the code and know what is going on. The problems come in when there are OpenSSL DLLs (or shared objects in the case of Linux) that are used by the application or another library loaded by the application (ie not your library) and do their own openssl initialization or thread hook routines (usurping yours). In some cases if it's proven that it's guaranteed to be already initialized properly by other means I've advised to not call with global_all (in other words do CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL), because you don't want to init SSL and set it up if it's already initialized and working and must continue after you call curl_global_cleanup.
Now that said in your case because you have static OpenSSL that is available in your DLL that static OpenSSL can't be used by other parts of your program (except through some function publicly exposed in your DLL of course). So you must initialize your static openssl and deinit. Also side note, I think there were some problems on windows with static openssl if you use the static crt, and they recommended at one point dynamic crt only (ie dll), not sure if that still applies.
Another thing, a bit more advanced - if you didn't want static openssl and instead wanted the openssl dlls because they are easier to update when modular, and wanted to avoid such conflicts, you could change the name of the DLLs like ssleay32_for_mylib.dll for build or modify the .lib to change the name of the dll (I don't remember how to do this but it's possible) then put that changed name in the .lib so that the lib links to that DLL name when your DLL searches for the __imp functions. In other words your DLL can have its own OpenSSL DLLs. In 1.1.0 from what I've read I don't think any of that would be necessary as long as it's built with threading support.
Given the state of things, you should probably change this to a "will not fix". I just want there to be a clear record of it, one way or another.
We support and will continue to support 1.0.2 so I've put in a fix. Thanks, landed in 3caaeff.
@arekm Start a new issue please. I reviewed OpenSSL 1.0.2h and I don't see that it calls SSL_COMP_free_compression_methods internally, therefore I still think what we are doing is correct. If you ask libcurl to initialize openssl then logically it follows when libcurl cleans up it's going to clean up openssl. It's possible something in PHP is calling that function or is calling curl global cleanup concurrently.
@jay: My guess about the issue is like this: consider php which uses openssl internally, php-pgsql where libpgsql uses openssl internally, php-curl. All that three things used in one single 'php' binary. Now curl is being ended, does its cleanup and decides to free global data. Then php-openssl and php-libpgsql still try to do something openssl related where openssl needs that global data for that. Result -> crash. (but that's only my guess)
Activity
bagder commentedon May 19, 2016
So when built and used normally there is no leak.
You rebuild OpenSSL and run it again and now it leaks. Isn't that an OpenSSL problem? Isn't that an ABI break that it doesn't behave the same way?
jveazey commentedon May 19, 2016
By default, OPENSSL_NO_COMP is not defined. At least, when using nt.mak and ntdll.mak
Edit: The only thing I've changed in the default VC++ build process is the runtime library
bagder commentedon May 19, 2016
Oh, are you saying you have a memory leak with a default build of OpenSSL? I don't, on Linux at least.
jveazey commentedon May 19, 2016
That's correct. Sorry for the confusion. I included OPENSSL_NO_COMP in the title because is directly related to the problem. Sensibly, it should be defined in the default build process, but I avoid modifying that build process as much as possible.
To give you some details on what is happening. I am wrapping cURL in a COM library (staticly linked to cURL and OpenSSL) that can be loaded/unloaded as needed. I'm not entirely sure how COM isolates the memory, but I know that every time the library loads it allocates new memory within the process. That's why we can't just load
curl_global_init(CURL_GLOBAL_ALL)at the beginning of the process. It can't reference the "shared" memory from its previous loading.Anyway, during a stress test, I found that the library was leaking an 36 bytes every time it was loaded/unloaded which added up after a few hours. That lead to the code sample in the original post.
jay commentedon May 20, 2016
Confirmed with Windows 7 x64 Enterprise w/curl from master (53ae370 2016-05-20) and OpenSSL/1.0.2h default config. I guess it is documented somewhere in openssl that we're supposed to call that function on cleanup? This is the first time I've heard of it.
Initializing libcurl from a DLL is tricky.
curl_global_initandcurl_global_cleanupare not thread safe and may call the initialization and cleanup routines of other libraries that are not thread safe. It's documented to not call init when any other threads are running. Also read this tldr. Also you must follow thread safety requirements.bagder commentedon May 20, 2016
It's a horrible mess.
The
SSL_COMP_free_compression_methodsfunction was added in OpenSSL in 2014: openssl/openssl@db7b5e0d768485 (unknown to me just now which versions that have it)Docs added over a year later in 2015: openssl/openssl@c490a5512e98
In April 2016, the function is marked deprecated and a no-op: openssl/openssl@03b0e735556ed (I assume this is the 1.1.0 branch)
So, there's a version gap in there during which it can/should be used...
jveazey commentedon May 20, 2016
I'm still doing testing on it and it won't be something that I'll push into production lightly. The whole reason this came up is that OpenSSL is already loaded in memory. The library wasn't able to reference that shared memory. cURL always reported SSL errors. I switched to using
CURL_GLOBAL_ALLand it resolved the problem. The other parts of the code that use OpenSSL - even after the library has been loaded/unloaded thousands of times - appear to be unaffected (so far).Agreed. Plus, the updated documentation for 1.1.0 states that everything will be handled automagically, but given my predicament, I'll have to force cleanup one way or another.
https://www.openssl.org/docs/manmaster/ssl/OPENSSL_init_ssl.html
https://www.openssl.org/docs/manmaster/crypto/OPENSSL_init_crypto.html
Given the state of things, you should probably change this to a "will not fix". I just want there to be a clear record of it, one way or another.
[-]Memory leak when OpenSSL is compiled without OPENSSL_NO_COMP defined[/-][+]OpenSSL memory leak without call to SSL_COMP_free_compression_methods[/+]openssl: cleanup must free compression methods
jay commentedon May 20, 2016
If you have a static OpenSSL library in your DLL you should be OK as long as you control the code and know what is going on. The problems come in when there are OpenSSL DLLs (or shared objects in the case of Linux) that are used by the application or another library loaded by the application (ie not your library) and do their own openssl initialization or thread hook routines (usurping yours). In some cases if it's proven that it's guaranteed to be already initialized properly by other means I've advised to not call with global_all (in other words do
CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL), because you don't want to init SSL and set it up if it's already initialized and working and must continue after you call curl_global_cleanup.Now that said in your case because you have static OpenSSL that is available in your DLL that static OpenSSL can't be used by other parts of your program (except through some function publicly exposed in your DLL of course). So you must initialize your static openssl and deinit. Also side note, I think there were some problems on windows with static openssl if you use the static crt, and they recommended at one point dynamic crt only (ie dll), not sure if that still applies.
Another thing, a bit more advanced - if you didn't want static openssl and instead wanted the openssl dlls because they are easier to update when modular, and wanted to avoid such conflicts, you could change the name of the DLLs like ssleay32_for_mylib.dll for build or modify the .lib to change the name of the dll (I don't remember how to do this but it's possible) then put that changed name in the .lib so that the lib links to that DLL name when your DLL searches for the __imp functions. In other words your DLL can have its own OpenSSL DLLs. In 1.1.0 from what I've read I don't think any of that would be necessary as long as it's built with threading support.
We support and will continue to support 1.0.2 so I've put in a fix. Thanks, landed in 3caaeff.
arekm commentedon Jul 6, 2016
This change is causing segfault of php when curl module is loaded. Reverting commit 3caaeff and problem is gone.
Tested on php5.3.29 and 5.5.36. openssl 1.0.2h
- rel 3; revert additional SSL freeing since it will harm other opens…
jay commentedon Jul 6, 2016
@arekm Start a new issue please. I reviewed OpenSSL 1.0.2h and I don't see that it calls
SSL_COMP_free_compression_methodsinternally, therefore I still think what we are doing is correct. If you ask libcurl to initialize openssl then logically it follows when libcurl cleans up it's going to clean up openssl. It's possible something in PHP is calling that function or is calling curl global cleanup concurrently.arekm commentedon Jul 6, 2016
@jay: My guess about the issue is like this: consider php which uses openssl internally, php-pgsql where libpgsql uses openssl internally, php-curl. All that three things used in one single 'php' binary. Now curl is being ended, does its cleanup and decides to free global data. Then php-openssl and php-libpgsql still try to do something openssl related where openssl needs that global data for that. Result -> crash. (but that's only my guess)
Opening new issue.