内核资源泄漏BIO_do_connect

时间:2014-11-18 11:52:30

标签: c++ c memory-leaks openssl inspector

我需要帮助以下;我已经尝试过寻找答案,但我一直在问。

Inspector XE给出了以下结果:行上的内核资源泄漏

he=BIO_gethostbyname(str);

这一行是OpenSSL源代码的一部分,我无法想象这里有什么问题。

此功能在内部调用:

BIO_do_connect();

我的代码是。

bio = BIO_new_connect(...);
if (BIO_do_connect(bio) != 1) { ... }
...
if (BIO_free(bio) != 0) { ... }

连接成功,当我与线程同时调用此函数1000次时,会发生唯一的错误。

这个程序真的会导致内核资源泄漏吗?

1 个答案:

答案 0 :(得分:2)

BIO_gethostbyname不是(不能)线程安全的,因为它返回一个指向静态缓冲区的指针。实际上,在openssl的bio.h中提到了这一点,正在阅读

struct hostent *BIO_gethostbyname(const char *name);
/* We might want a thread-safe interface too:
 * struct hostent *BIO_gethostbyname_r(const char *name,
 *     struct hostent *result, void *buffer, size_t buflen);
 * or something similar (caller allocates a struct hostent,
 * pointed to by "result", and additional buffer space for the various
 * substructures; if the buffer does not suffice, NULL is returned
 * and an appropriate error code is set).
 */

...但是,到目前为止还没有实现BIO_gethostbyname_r功能,并且无论如何它都不会帮助你,如果BIO_do_connect没有使用它。

但绝望不是!这个函数调用周围有锁定代码,因此可以(可能)使其工作。有趣的是,锁定代码并不总是做任何事情。我假设您没有使用OPENSSL_NO_LOCKING编译OpenSSL,因为它不是默认的,如果他们想要开发多线程应用程序,我无法想象有人将其放入,所以我的猜测是你忘记打电话了

CRYPTO_thread_setup();
在生成使用OpenSSL的线程之前

。这一点很重要的原因是CRYPTO_thread_setup除其他外,还设置了一个函数指针,该函数指针指向一个处理实际锁定的平台相关函数。如果未调用CRYPTO_thread_setup,则此指针仍为NULL,而CRYPTO_lockBIO_gethostbyname调用周围使用的宏底部的相关函数)将无声地执行任何操作。据我所知,目前还没有任何文件可供您了解。

可以说,您对OpenSSL代码库质量的乐观态度是错误的。