c - OpenSSL 函数 EVP_PKEY_keygen 中的内存泄漏

标签 c memory-leaks openssl

我只是尝试使用以下方法生成 RSA key :

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

int main(void) {
  OpenSSL_add_all_algorithms();

  EVP_PKEY_CTX *ctx;
  EVP_PKEY *pkey = NULL;

  ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
  if (!ctx) {
      // error
  }

  if (EVP_PKEY_keygen_init(ctx) <= 0) {
      // error
  }

  if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) {
      // error
  }

  if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { // this call seems to leak
      // error
  }

  EVP_PKEY_free(pkey);
  EVP_PKEY_CTX_free(ctx);

  EVP_cleanup();
  return 0;
}

到目前为止,我不认为我做错了什么。 Valgrind 提示“在退出时使用:6 个 block 中的 416 字节”。 首先我想,我忘了释放一些东西,后来我尝试了

valgrind openssl genrsa 1024

我还得到了“退出时使用:6 个 block 中的 416 字节”。 哼?!甚至 OpenSSL 的官方二进制文件也泄露了?

Openssl's FAQ告诉:

"Brutal" (thread-unsafe) Application-global cleanup functions:

ERR_free_strings(), EVP_cleanup() and CRYPTO_cleanup_all_ex_data().

如果我执行 *CRYPTO_cleanup_all_ex_data()* 它不会泄漏。但根据 OpenSSL 文档,无论这意味着什么,它都是一种“残酷”的方法。没有关于此功能的进一步文档。

有什么办法可以正确清理吗?

我使用的是 2014 年 1 月 6 日的 OpenSSL 1.0.1f

最佳答案

OpenSSL 的常见问题解答回答了问题(感谢@opalenzuela):

In most cases the cause of an apparent memory leak is an OpenSSL internal table that is allocated when an application starts up. Since such tables do not grow in size over time they are harmless.

深入研究 OpenSSL 的源代码显示:

/* 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)();
    }

关于c - OpenSSL 函数 EVP_PKEY_keygen 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532371/

相关文章:

c++ - 将 OpenSSL 链接到动态库中

C 程序在执行 fwrite 时终止

c - IITKWPCO SPOJ 中的 WA

c - 单个 C 代码中的 printf 和 wprintf

javascript - llnode 不返回 findjsobjects

iphone - 何时释放 NSDictionary 实例及其所有值?

c - 矩阵与 **pointer 相乘给出了错误的输出

java - 分析内存时使用哪个 GC?

c++ - 如何解决支持库中发生的段错误问题?

python-2.7 - 通过 yum install openssl11 将 CentOS 7 升级到 OpenSSL 1.1.1