作为 win32 服务运行时,OpenSSL SSL_CTX_new 崩溃
Posted
技术标签:
【中文标题】作为 win32 服务运行时,OpenSSL SSL_CTX_new 崩溃【英文标题】:OpenSSL SSL_CTX_new crash when run as win32 service 【发布时间】:2013-09-21 13:25:44 【问题描述】:我遇到了一个奇怪的 OpenSSL 问题:当我的应用程序作为 win32 服务(本地服务/网络服务或系统服务)运行时,似乎 SSL_CTX_new() 函数崩溃了。
我的代码如下所示:
int main()
SSL_library_init();
const int nLocks = CRYPTO_num_locks();
ossl_mtx_pool = new mutexT* [nLocks]; // simple wrapper class over mutexes
for(int i = 0; i < nLocks; ++i)
ossl_mtx_pool[i] = new mutexT;
CRYPTO_THREADID_set_callback(ossl_threadid_function); // returns win32 thread id
CRYPTO_set_locking_callback(ossl_locking_fun); // simple function that calls mutexT::lock/unlock)
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
// some other app initialization here...
// run win32 service thread OR just a seaparate thread
// called later on in the context of a different thread
int myclient()
myCtx *ctx = new connContextT;
const SSL_METHOD *mm = SSLv23_client_method();
if(mm == 0)
// print some error whcih doesn't show up
printf("I'm Here!\n"); //< this print shows up
ctx->sslCtx = SSL_CTX_new(mm);
// crash >HERE< before being able to print anything else
// some code to connect to server
真正奇怪的是,如果我将这个应用程序作为普通的 win32 应用程序运行(即不调用 win32 服务函数但仍在单独的线程中运行 myclient()),一切都会完美无缺。
我在 win7 上使用 mingw32 gcc 4.7.1 进行编译,并且静态链接 openssl(无 DLL)。
我们将不胜感激任何试图理解问题的帮助。 谢谢!
【问题讨论】:
经过一些调试,似乎崩溃发生在 RAND_pseudo_bytes 函数中 我对 OpenSSL 源一无所知,但我怀疑故障发生在 RAND_pseudo_bytes 之前,但它在那里崩溃(可能是 null 取消引用)。如果您可以发布堆栈回溯,那可能会有所帮助。 我在 Win32 服务中使用 OpenSSL,它对我来说效果很好,所以这一定是您的特定 OpenSSL 构建的问题。你在哪里得到它?你是自己编译的,还是使用第三方预编译的?我使用链接到 OpenSSL 网站的 Binaries 部分的 Win32 版本。 感谢您的 cmets。 @Eric:我实际上很确定崩溃在该函数内,因为跟踪只有一层深(SSL_CTX_new --> RAND_pseudo_bytes)。 @Remy:我正在使用 mingw 从源代码构建 openssl 1.0.1e。配置选项是“no-idea no-camellia no-seed no-bf no-cast no-rc2 no-rc4 no-rc5 no-md2 no-md4 no-ripemd no-mdc2 no-ec no-ecdsa no-ecdh no -shared no-asm" 【参考方案1】:经过更多调试后,我在 ossl_threadid_function 中发现了一个非常愚蠢的错误(!!!) 显然返回错误的 ID 会导致 openssl 中的堆栈损坏,这就是我未能生成任何有用的回溯的原因。
令人惊讶的是,将应用程序作为正常进程而不是服务运行并没有造成任何问题,甚至应用程序验证程序指出的运行时工具也没有。显然,这与服务实例至少多了一个线程这一事实有关......
对此感到抱歉,并感谢大家的宝贵时间。 问候。
【讨论】:
以上是关于作为 win32 服务运行时,OpenSSL SSL_CTX_new 崩溃的主要内容,如果未能解决你的问题,请参考以下文章