From 40324cdcc010b78b03e990ca27444b56910c07d9 Mon Sep 17 00:00:00 2001 From: David Hedbor Date: Tue, 18 Jul 2017 06:57:58 -0700 Subject: [PATCH] Fix OpenSSLInitialized thread safety (#1739) (#1740) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix OpenSSLInitialized thread safety (#1739) The init/uninit methods can be called from multiple threads, and thus need synchronization with a mutex. * Renamed mutex variable and use ScopedLock. * Change reference count variable to be an integer, since it’s protected by a mutex and no longer needs to be atomic. --- Crypto/include/Poco/Crypto/OpenSSLInitializer.h | 3 ++- Crypto/src/OpenSSLInitializer.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Crypto/include/Poco/Crypto/OpenSSLInitializer.h b/Crypto/include/Poco/Crypto/OpenSSLInitializer.h index b616f08fa4..f4d1d6a415 100644 --- a/Crypto/include/Poco/Crypto/OpenSSLInitializer.h +++ b/Crypto/include/Poco/Crypto/OpenSSLInitializer.h @@ -84,8 +84,9 @@ class Crypto_API OpenSSLInitializer static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line); private: + static Poco::FastMutex _mutex; static Poco::FastMutex* _mutexes; - static Poco::AtomicCounter _rc; + static int _rc; static bool _disableSSLInitialization; }; diff --git a/Crypto/src/OpenSSLInitializer.cpp b/Crypto/src/OpenSSLInitializer.cpp index 3808341723..6a74ba8127 100644 --- a/Crypto/src/OpenSSLInitializer.cpp +++ b/Crypto/src/OpenSSLInitializer.cpp @@ -33,9 +33,9 @@ using Poco::Thread; namespace Poco { namespace Crypto { - +Poco::FastMutex OpenSSLInitializer::_mutex; Poco::FastMutex* OpenSSLInitializer::_mutexes(0); -Poco::AtomicCounter OpenSSLInitializer::_rc; +int OpenSSLInitializer::_rc(0); bool OpenSSLInitializer::_disableSSLInitialization = false; OpenSSLInitializer::OpenSSLInitializer() @@ -59,6 +59,7 @@ OpenSSLInitializer::~OpenSSLInitializer() void OpenSSLInitializer::initialize() { + FastMutex::ScopedLock lock(_mutex); if (++_rc == 1) { #if OPENSSL_VERSION_NUMBER >= 0x0907000L @@ -104,6 +105,7 @@ void OpenSSLInitializer::initialize() void OpenSSLInitializer::uninitialize() { + FastMutex::ScopedLock lock(_mutex); if (--_rc == 0) { if(_mutexes != NULL) {