【问题标题】:RSA_new and RSA_generate_key_ex make memory leakageRSA_new 和 RSA_generate_key_ex 导致内存泄漏
【发布时间】:2017-05-31 05:38:51
【问题描述】:

我正在开发一个需要 RSA 密钥来加密某些用户数据的应用程序。我使用openssl,一切正常。但是,该应用程序不断警告 RSA_new 和 RSA_generate_key_ex 的内存泄漏(我认为这不应该是因为我释放了我创建的所有属性)。

这是我生成 RSA 密钥的代码:

BIGNUM e;
BN_init(&e);
BN_set_word(&e, 17);
RSA *rsa = RSA_new(); // Direct leak of 191 bytes in 1 object (RSA_new->RSA_new_method->...)
RAS_generate_key_ex(rsa, 1024, &e, NULL); // Indirect leak of 279 bytes in 1 object (RAS_generate_key_ex->rsa_builtin_keygen...)
EVP_PKEY pkey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, rsa);
RSA_free(rsa);
BN_free(&e);
MINE_COPY_KEY(pkey); // I copy the pkey to other location at here //
EVP_PKEY_free(pkey);

我以为我分配的所有内容(e、pkey、rsa)都已由“RSA_free、EVP_PKEY_free 和 BN_free”释放,但它仍然抱怨我的 Linux x64 机器中的内存泄漏

【问题讨论】:

标签: c encryption memory-leaks openssl rsa


【解决方案1】:

我尝试了你的程序(在修正了诸如RAS_generate_key_ex -> RSA_generate_key_exEVP_PKEY pkey = EVP_PKEY_new() -> EVP_PKEY* pkey = EVP_PKEY_new() 之类的拼写错误之后)。

我现在有这个来源:

#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>

int main() {
  BIGNUM e;
  BN_init(&e);
  BN_set_word(&e, 17);
  RSA *rsa = RSA_new();
  RSA_generate_key_ex(rsa, 1024, &e, NULL);
  EVP_PKEY* pkey = EVP_PKEY_new();
  EVP_PKEY_set1_RSA(pkey, rsa);
  RSA_free(rsa);
  BN_free(&e);
  //MINE_COPY_KEY(pkey); // I copy the pkey to other location at here //
  EVP_PKEY_free(pkey);
  return 0;
}

Valgrind 说:

==18061== 
==18061== LEAK SUMMARY:
==18061==    definitely lost: 0 bytes in 0 blocks
==18061==    indirectly lost: 0 bytes in 0 blocks
==18061==      possibly lost: 0 bytes in 0 blocks
==18061==    still reachable: 220 bytes in 6 blocks
==18061==         suppressed: 0 bytes in 0 blocks
==18061== Reachable blocks (those to which a pointer was found) are not shown.
==18061== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==18061== 
==18061== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==18061== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

在程序末尾添加CRYPTO_cleanup_all_ex_data(); 让 Valgring 很高兴 :-)

==18212== HEAP SUMMARY:
==18212==     in use at exit: 0 bytes in 0 blocks
==18212==   total heap usage: 457 allocs, 457 frees, 31,748 bytes allocated
==18212== 
==18212== All heap blocks were freed -- no leaks are possible
==18212== 
==18212== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==18212== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

见:https://stackoverflow.com/a/21533000/6267288

【讨论】:

  • 嘿,你是我的英雄!我发布的问题是由 ASN 检测到的,它是由其他问题引起的。但是你的帖子帮助我停止了恼人的 Valgrind 警告!
猜你喜欢
  • 2016-06-05
  • 2015-07-06
  • 2014-06-07
  • 2013-11-20
  • 2011-10-28
  • 2016-01-18
  • 2012-12-13
  • 1970-01-01
  • 2011-01-08
相关资源
最近更新 更多