OpenSSL中关于RSA_new和RSA_free的内存泄漏

本文介绍了解决OpenSSL中RSA加解密操作产生的内存泄漏问题的方法。通过调用CRYPTO_cleanup_all_ex_data()函数,可以在程序退出时清理残留数据,避免内存泄漏。此外还提供了其他模块清理建议。

在使用OpenSSL的RSA加解密的时候,发现RSA_new()初始化和RSA_free()释放RSA结构体后依然会有内存泄漏。网上Baidu、Google之,发现这个相关信息很少(至少中文搜索结果是这样,不知是研究这个的人太少还是这个太基础了。。。),最后终于在某个E文论坛上找到了解决办法。在这里总结了一下,供大家参考。我的OpenSSL版本是0.9.8l。(by 月落上弦)

具体如下:
RSA * rsa = RSA_new();
RSA_free( rsa );

产生内存泄漏:

Debug for memory leaks 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Detected memory leaks!
Dumping objects ->
{140} normal block at 0x003B97A0, 12 bytes long.
 Data: <  ;         > B8 96 3B 00 00 00 00 00 06 00 00 00 
{139} normal block at 0x003B9750, 16 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{138} normal block at 0x003B9700, 20 bytes long.
 Data: <    P ;         > 00 00 00 00 50 97 3B 00 00 00 00 00 04 00 00 00 
{137} normal block at 0x003B96B8, 12 bytes long.
 Data: <      ;     > 06 00 00 00 00 97 3B 00 00 00 00 00 
{136} normal block at 0x003B9638, 64 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{135} normal block at 0x003B9598, 96 bytes long.
 Data: <8 ;   A   A     > 38 96 3B 00 C0 FD 41 00 B0 FD 41 00 08 00 00 00 
Object dump complete.
View Code

解决方法很简单:

调用OpenSSL的crypto库,在退出前需要调用API "CRYPTO_cleanup_all_ex_data",清除管理CRYPTO_EX_DATA的全局hash表中的数据,避免内存泄漏。如下:

RSA * rsa = RSA_new();
RSA_free( rsa );
CRYPTO_cleanup_all_ex_data(); 

这样就没有内存泄漏了。

 

需要注意的是,CRYPTO_cleanup_all_ex_data()不能在potential race-conditions条件在调用(不太懂这个术语,我理解的意思是当函数外部存在RSA结构体的时候,在函数内部执行CRYPTO_cleanup_all_ex_data()将导致函数外的RSA结构体也被清理掉),因此最好在程序结束的时候才调用。

关于CRYPTO_cleanup_all_ex_data()的注释说明和代码如下:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/* Release all "ex_data" state to prevent memory leaks. This can't be made
 * thread-safe without overhauling a lot of stuff, and shouldn't really be
 * called under potential race-conditions anyway (it's for program shutdown
 * after all). */
void CRYPTO_cleanup_all_ex_data(void)
    {
    IMPL_CHECK
    EX_IMPL(cleanup)();
    }

同样,其他相应的模块也需要在使用后清理:

CONF_modules_unload(1);        //for conf
EVP_cleanup();                 //For EVP
ENGINE_cleanup();              //for engine
CRYPTO_cleanup_all_ex_data();  //generic 
ERR_remove_state(0);           //for ERR
ERR_free_strings();            //for ERR

 

 

  // thread-local cleanup
    ERR_remove_state(0);
     
    // thread-safe cleanup
    ENGINE_cleanup();
    CONF_modules_unload(1);
 
    // global application exit cleanup (after all SSL activity is shutdown)
    ERR_free_strings();
    EVP_cleanup();
    CRYPTO_cleanup_all_ex_data();
  
        // 这个函数是网上找的,描述说内存泄漏现象是 _comp_method(不知道有没有记错)对象没释放
        // 额外添加的函数,用以释放资源(没有调用该函数,OpenSSL 会有内存泄漏(160字节))
    SSL_free_comp_methods();

 

  http://www.cnblogs.com/moonset7/archive/2009/12/30/1635770.html 月落上弦

转载于:https://www.cnblogs.com/coolYuan/p/8297693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值