2

I am developing an app which needs an RSA key to encrypt certain user data. I use openssl and everything works fine. However, the app keeps alarming a memory leakage at RSA_new and RSA_generate_key_ex (which I thought it should not be because I release all the properties created by me).

Here is my code to generate the RSA key:

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);

I thought everything allocated by me (e, pkey, rsa) is already released by "RSA_free, EVP_PKEY_free, and BN_free" but it still have complain on memory leakage in my Linux x64 machine

jww
  • 97,681
  • 90
  • 411
  • 885
Yu-Chih
  • 345
  • 4
  • 13
  • Try using Valgrind to see the memory leak report. – AmeyaVS May 31 '17 at 05:40
  • 2
    Please show the leak. Also see [Library Initialization](https://wiki.openssl.org/index.php/Library_Initialization) on the OpenSSL wiki. It shows you how to startup and shutdown the library. And [Memory leak in OpenSSL?](https://stackoverflow.com/q/34794272/608639), [Memory leak in OpenSSL function EVP_PKEY_keygen](https://stackoverflow.com/q/21532371/608639), [OpenSSL::SSL_library_init() memory leak](https://stackoverflow.com/q/11759725/608639), [Memory leak in OpenSSL function EVP_EncryptFinal_ex](https://stackoverflow.com/q/18503993/608639) on Stack Overflow. – jww May 31 '17 at 06:52

1 Answers1

4

I tried your program (after fixing typos such as RAS_generate_key_ex -> RSA_generate_key_ex and EVP_PKEY pkey = EVP_PKEY_new() -> EVP_PKEY* pkey = EVP_PKEY_new()).

I had now this source:

#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 says:

==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)

Adding CRYPTO_cleanup_all_ex_data(); at the end of the program makes Valgring happy :-)

==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)

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

OlivierM
  • 2,820
  • 24
  • 41
  • Hey, you are my hero! The problem I post is detected by ASN and it is caused by other issues. But your post helps me to stop the annoying Valgrind warning!! – Yu-Chih Jun 01 '17 at 06:18