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

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

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

产生内存泄漏:

1Debug for memory leaks 2 3Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Detected memory leaks! 4Dumping objects -> 5{140} normal block at 0x003B97A0, 12 bytes long. 6 Data: < ; > B8 96 3B 00 00 00 00 00 06 00 00 00 7{139} normal block at 0x003B9750, 16 bytes long. 8 Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9{138} normal block at 0x003B9700, 20 bytes long. 10 Data: < P ; > 00 00 00 00 50 97 3B 00 00 00 00 00 04 00 00 00 11{137} normal block at 0x003B96B8, 12 bytes long. 12 Data: < ; > 06 00 00 00 00 97 3B 00 00 00 00 00 13{136} normal block at 0x003B9638, 64 bytes long. 14 Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 15{135} normal block at 0x003B9598, 96 bytes long. 16 Data: <8 ; A A > 38 96 3B 00 C0 FD 41 00 B0 FD 41 00 08 00 00 00 17Object 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()的注释说明和代码如下:

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

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

1CONF_modules_unload(1); //for conf 2EVP_cleanup(); //For EVP 3ENGINE_cleanup(); //for engine 4CRYPTO_cleanup_all_ex_data(); //generic 5ERR_remove_state(0); //for ERR 6ERR_free_strings(); //for ERR 7 8  // thread-local cleanup 9 ERR_remove_state(0); 10 11 // thread-safe cleanup 12 ENGINE_cleanup(); 13 CONF_modules_unload(1); 14 15 // global application exit cleanup (after all SSL activity is shutdown) 16 ERR_free_strings(); 17 EVP_cleanup(); 18 CRYPTO_cleanup_all_ex_data(); 19 20 // 这个函数是网上找的,描述说内存泄漏现象是 _comp_method(不知道有没有记错)对象没释放 21 // 额外添加的函数,用以释放资源(没有调用该函数,OpenSSL 会有内存泄漏(160字节)) 22 SSL_free_comp_methods();

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )