From b58ab52cd6aa5efd40bc994e507a7cdb937a1553 Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Mon, 30 May 2011 05:46:10 +0000 Subject: [PATCH] SSL should be thread safe (right?) but SSL_library_init function and SSL_load_error_strings are not, or they should be called only once... Remove extra ssl lock, there is already one in ssl. This fixes #7574. I have seen this or a similar bug before and it was quit reproduceable, now it seems to be fixed. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41828 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../imap/imap_lib/ServerConnection.cpp | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp index 70553ac9a9..dabc6c7fa4 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/imap_lib/ServerConnection.cpp @@ -44,6 +44,38 @@ protected: #ifdef USE_SSL +class InitSSL { +public: + InitSSL() + { + if (SSL_library_init() != 1) { + fInit = false; + return; + } + + RAND_seed(this, sizeof(InitSSL)); + /*--- Because we're an add-on loaded at an unpredictable time, all the + memory addresses and things contained in ourself are esssentially + random. */ + fInit = true; + return; + }; + + + status_t + InitCheck() + { + return fInit ? B_OK : B_ERROR; + } + +private: + bool fInit; +}; + + +static InitSSL gInitSSL; + + class SSLConnection : public SocketConnection { public: SSLConnection(); @@ -60,9 +92,6 @@ private: SSL_CTX* fCTX; SSL* fSSL; BIO* fBIO; - - // ssl seems to be not thread save - BLocker fLocker; }; #endif @@ -255,20 +284,13 @@ SSLConnection::Connect(const char* server, uint32 port) if (fSSL != NULL) Disconnect(); - BAutolock _(fLocker); + if (gInitSSL.InitCheck() != B_OK) + return B_ERROR; status_t status = SocketConnection::Connect(server, port); if (status != B_OK) return status; - if (SSL_library_init() != 1) - return B_ERROR; - SSL_load_error_strings(); - RAND_seed(this, sizeof(SSLConnection)); - /*--- Because we're an add-on loaded at an unpredictable time, all - the memory addresses and things contained in ourself are - esssentially random. */ - fCTX = SSL_CTX_new(SSLv23_method()); fSSL = SSL_new(fCTX); fBIO = BIO_new_socket(fSocket, BIO_NOCLOSE); @@ -289,7 +311,6 @@ status_t SSLConnection::Disconnect() { TRACE("SSLConnection::Disconnect()\n"); - BAutolock _(fLocker); if (fSSL) SSL_shutdown(fSSL); @@ -308,14 +329,11 @@ SSLConnection::Disconnect() status_t SSLConnection::WaitForData(bigtime_t timeout) { - fLocker.Lock(); if (!fSSL) return B_ERROR; if (SSL_pending(fSSL) > 0) { - fLocker.Unlock(); return B_OK; } - fLocker.Unlock(); return SocketConnection::WaitForData(timeout); } @@ -323,7 +341,6 @@ SSLConnection::WaitForData(bigtime_t timeout) int32 SSLConnection::Read(char* buffer, uint32 nBytes) { - BAutolock _(fLocker); if (!fSSL) return B_ERROR; return SSL_read(fSSL, buffer, nBytes); @@ -333,7 +350,6 @@ SSLConnection::Read(char* buffer, uint32 nBytes) int32 SSLConnection::Write(const char* buffer, uint32 nBytes) { - BAutolock _(fLocker); if (!fSSL) return B_ERROR; return SSL_write(fSSL, buffer, nBytes);