Hello,

I am currently trying to fix some memory leaks I introduced into my
application, and am left with some leaks (according to valgrind) that
seem to relate to RSA_public_encrypt. However, I cannot detect any
problem with not freeing memory I explicitly allocated.

Hence the question: Do I miss something using RSA_public_encrypt?
I wrote a wrapper for RSA_public_encrypt, which breaks down to this,
ignoring some sanity checking:

unsigned char *RSA_encrypt(
  PublicKeyRSA *public_key,
  unsigned char* plaintext,
  int *plaintext_length)
{
    unsigned char *ciphertext = malloc(RSA_size(public_key));
    int result;

    result = RSA_public_encrypt(
         *plaintext_length,
         plaintext,
         ciphertext,
         public_key,
         RSA_PADDING
    );

    return ciphertext;
}

Valgrind repeatedly reports the RSA_public_encrypt line as a source of
mempory leaks, for example:

==6359== 5,456 (1,144 direct, 4,312 indirect) bytes in 11 blocks are
definitely lost in loss record 22 of 23
==6359==    at 0x4C2B6CD: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6359==    by 0x5A5E133: CRYPTO_malloc (in
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6359==    by 0x5AA41E6: BN_MONT_CTX_new (in
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6359==    by 0x5AA45B7: BN_MONT_CTX_set_locked (in
/lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6359==    by 0x5AC751A: ??? (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==6359==    by 0x40A162: RSA_getEncryptedSymmetricKey (rsa.c:217)
...


As suggested in
http://www.mail-archive.com/openssl-dev@openssl.org/msg08797.html,
I added OpenSSL's memleak checking to my application, but I do not know
how to interpret the output:


[14:57:43]  3655 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252FBF0
[14:57:48]  5389 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025387A0
[14:57:48]  4941 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02530A10
[14:57:53]  7767 file=stack.c, line=125, thread=139918492813024,
number=32, address=02485250
[14:57:48]  5449 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02539710
[14:57:48]  4557 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=0252FA70
[14:57:43]  3435 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02526CE0
[14:57:48]  4969 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02537770
[14:57:48]  5325 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=025385F0
[14:57:43]  4074 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525B30
[14:57:53]  7769 file=lhash.c, line=193, thread=139918492813024,
number=24, address=0247D250
[14:57:43]  3533 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02526AD0
[14:57:48]  4525 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025326B0
[14:57:43]  3691 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=0252F800
[14:57:48]  5292 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025383E0
[14:57:43]  4072 file=rsa_lib.c, line=139, thread=139918492813024,
number=168, address=02525A10
[14:57:43]  4039 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=025315C0
[14:57:53]  7772 file=stack.c, line=127, thread=139918492813024,
number=32, address=02531590
[14:57:48]  4943 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252E680
[14:57:43]  3367 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02526920
[14:57:48]  5231 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02538200
[14:57:48]  5005 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02537320
[14:57:43]  4107 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02530440
[14:57:48]  4653 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02524FD0
[14:57:43]  3405 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02526770
[14:57:48]  4430 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252F4D0
[14:57:53]  7773 file=lhash.c, line=193, thread=139918492813024,
number=24, address=02536A00
[14:57:43]  3307 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02525F10
[14:57:48]  5295 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=025379D0
[14:57:43]  3404 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025266E0
[14:57:48]  4461 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=0252F440
[14:57:48]  5037 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02537170
[14:57:43]  3531 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=025256B0
[14:57:48]  5294 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02537940
[14:57:43]  3306 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525E80
[14:57:43]  3597 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252F3B0
[14:57:43]  3501 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02525620
[14:57:48]  4813 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025368E0
[14:57:43]  4077 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02531200
[14:57:43]  4109 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252FB50
[14:57:48]  4655 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252E320
[14:57:43]  3596 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252F320
[14:57:48]  5229 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02537880
[14:57:43]  3500 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525590
[14:57:43]  4076 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02531170
[14:57:48]  4623 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252E290
[14:57:43]  3690 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525D30
[14:57:48]  4622 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252E200
[14:57:48]  5197 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02537F30
[14:57:48]  5002 file=rsa_lib.c, line=139, thread=139918492813024,
number=168, address=02536F00
[14:57:48]  5196 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02537EA0
[14:57:43]  4327 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=025327F0
[14:57:48]  4911 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02536E70
[14:57:43]  3595 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02526380
[14:57:48]  4526 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02532790
[14:57:48]  4910 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02536DE0
[14:57:48]  4815 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252E850
[14:57:43]  4171 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025316D0
[14:57:43]  3885 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=025306D0
[14:57:43]  3463 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252EFF0
[14:57:48]  5161 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02538400
[14:57:43]  3884 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02530640
[14:57:48]  4749 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02526200
[14:57:43]  4267 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02531DE0
[14:57:48]  5065 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02537C90
[14:57:43]  3819 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=025251D0
[14:57:48]  4719 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252EF00
[14:57:48]  5007 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02537460
[14:57:43]  3787 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=0252FED0
[14:57:43]  4266 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02531D50
[14:57:43]  3559 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252F6A0
[14:57:48]  5388 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02537BD0
[14:57:48]  4718 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252EE70
[14:57:43]  4299 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02531520
[14:57:48]  5006 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025373D0
[14:57:48]  5293 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02536BA0
[14:57:48]  5133 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02531490
[14:57:43]  3979 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02530C30
[14:57:43]  4011 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=0252F580
[14:57:48]  5102 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025372E0
[14:57:48]  5103 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02537A80
[14:57:43]  3978 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02530BA0
[14:57:43]  3725 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252F4F0
[14:57:43]  4135 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02531B70
[14:57:48]  4777 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02536A20
[14:57:43]  4301 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02531370
[14:57:48]  5390 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025388A0
[14:57:48]  4527 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252DC60
[14:57:48]  4585 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252E400
[14:57:48]  4681 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252EB40
[14:57:43]  3403 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025265E0
[14:57:53]  7771 file=stack.c, line=125, thread=139918492813024,
number=32, address=0252EB10
[14:57:43]  4170 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252FAE0
[14:57:43]     0 file=eng_lib.c, line=68, thread=139918492813024,
number=216, address=0247BDF0
[14:57:48]  4873 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02537010
[14:57:48]  5423 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02538EC0
[14:57:43]  3402 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02526550
[14:57:43]  3847 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02530900
[14:57:43]  4205 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=025318D0
[14:57:43]  3594 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252F220
[14:57:43]  3499 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02525490
[14:57:48]  5100 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02536F50
[14:57:48]  4847 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252E9C0
[14:57:53]  7764 file=lhash.c, line=119, thread=139918492813024,
number=176, address=0248DE40
[14:57:43]  3693 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=0252F990
[14:57:43]  4173 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02531840
[14:57:43]  3498 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525400
[14:57:48]  5004 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252E930
[14:57:43]  4172 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025317B0
[14:57:43]  3692 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252F900
[14:57:48]  4845 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=0252F100
[14:57:48]  4621 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=0252E100
[14:57:43]  3943 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02530F80
[14:57:48]  5391 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02538CE0
[14:57:48]  4620 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252E070
[14:57:48]  5039 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=025375A0
[14:57:48]  5135 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02537DA0
[14:57:43]  3629 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=025262B0
[14:57:48]  4429 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02527250
[14:57:48]  4909 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02536CE0
[14:57:48]  4428 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025271C0
[14:57:48]  4908 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02536C50
[14:57:43]  3723 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02525160
[14:57:53]  7768 file=stack.c, line=127, thread=139918492813024,
number=32, address=024619B0
[14:57:43]  3883 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02530540
[14:57:48]  4814 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252DE90
[14:57:48]  4559 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=025324E0
[14:57:43]  3341 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=025258A0
[14:57:43]  3882 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=025304B0
[14:57:48]  4812 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252DE00
[14:57:48]  4524 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525040
[14:57:53]  7766 file=ex_data.c, line=308, thread=139918492813024,
number=24, address=0247CF60
[14:57:48]  4716 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252ED70
[14:57:43]  3917 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252FD70
[14:57:43]  3915 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=025267E0
[14:57:48]  4751 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=0252E510
[14:57:48]  5257 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02538920
[14:57:43]  3339 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02525FB0
[14:57:48]  4431 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02532390
[14:57:48]  5199 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=025380C0
[14:57:48]  5101 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025371E0
[14:57:48]  5353 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02539090
[14:57:48]  5198 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02538030
[14:57:43]  3437 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02526EC0
[14:57:43]  4231 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02532210
[14:57:43]  3751 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=02530210
[14:57:43]  3821 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02530180
[14:57:48]  4717 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=025300F0
[14:57:43]  4075 file=bn_lib.c, line=317, thread=139918492813024,
number=136, address=02531090
[14:57:43]  4203 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=0252F1B0
[14:57:43]  3789 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02530060
[14:57:48]  4463 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02532000
[14:57:43]  4269 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02531F70
[14:57:53]  7770 file=ex_data.c, line=308, thread=139918492813024,
number=24, address=02526360
[14:57:43]  3786 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02525330
[14:57:43]  4268 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02531EE0
[14:57:43]  3788 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=0252FFD0
[14:57:43]  4013 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02530E50
[14:57:48]  5421 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=02538B50
[14:57:43]  3981 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02530DC0
[14:57:48]  4489 file=bn_lib.c, line=317, thread=139918492813024,
number=264, address=0252DF10
[14:57:53]  7765 file=lhash.c, line=121, thread=139918492813024,
number=128, address=02538AC0
[14:57:43]  3980 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02530D30
[14:57:43]  3309 file=bn_lib.c, line=317, thread=139918492813024,
number=8, address=02526120
[14:57:43]  3627 file=bn_mont.c, line=325, thread=139918492813024,
number=104, address=025250F0
[14:57:48]  5327 file=bn_lib.c, line=317, thread=139918492813024,
number=128, address=02538A30
[14:57:48]  5290 file=rsa_lib.c, line=139, thread=139918492813024,
number=168, address=02537B80
[14:57:43]  3308 file=bn_lib.c, line=283, thread=139918492813024,
number=24, address=02526090
16384 bytes leaked in 168 chunks


Kind regards,
Roman Matzutt

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to