openssl内存泄漏:是我还是bug?

时间:2018-11-08 09:58:34

标签: c++ memory-leaks openssl

尝试使用openssl(1.0.2p)从证书中解析信息,不能使其无泄漏。代码:

std::ifstream fst("2048b-rsa-example-cert.der", std::ios::binary);
std::vector<std::uint8_t> certificate((std::istreambuf_iterator<char>(fst)),
                      std::istreambuf_iterator<char>() );

const std::uint8_t* data = certificate.data();
X509 *info = d2i_X509(nullptr, &data, certificate.size());
X509_free(info);

证书示例:http://fm4dd.com/openssl/certexamples.htm

最大的泄漏回溯:

==20846==    at 0x4C2EEAF: malloc (vg_replace_malloc.c:299)
==20846==    by 0x52380E7: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51BDF2F: lh_new (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x5239484: ex_data_check (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x5239544: def_get_class (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x5239FBA: int_new_ex_data (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51DE8D3: x509_cb (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51E28F8: asn1_item_ex_combine_new (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51E55E8: asn1_item_ex_d2i (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51E6224: ASN1_item_ex_d2i (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x51E627A: ASN1_item_d2i (in /usr/lib64/libcrypto.so.1.0.0)
==20846==    by 0x109199: main (ssl.cpp:48)

平台:Linux

OpenSSL 1.0.2p  14 Aug 2018
built on: reproducible build, date unspecified
platform: linux-x86_64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx)
compiler: x86_64-pc-linux-gnu-gcc -I. -I.. -I../include  -fPIC -DOPENSSL_PIC -DZLIB -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DL_ENDIAN -Wall -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM -O2 -pipe -march=native -fno-strict-aliasing -Wa,--noexecstack
OPENSSLDIR: "/etc/ssl"

1 个答案:

答案 0 :(得分:0)

OpenSSL Library Initialization Wiki提供了可能需要调用以正确清理库的功能列表。据我所知,回溯中提到的字节/块没有泄漏,但是它们在退出时仍在使用中-尽管您没有提供所有输出。

对于此代码段,看起来就像添加

CRYPTO_cleanup_all_ex_data();
最后,

可以解决问题。使用带有标志valgrind的{​​{1}}进行运行,显示“退出时使用”的数量从6个块的416字节减少到0个块的0字节。