7a7b1e
From 16f994e7944a917fa81c8db11c56c594f4e78b40 Mon Sep 17 00:00:00 2001
7a7b1e
From: Alexander Bokovoy <abokovoy@redhat.com>
7a7b1e
Date: Tue, 31 Jul 2018 14:59:03 +0300
7a7b1e
Subject: [PATCH] Reset mutex callbacks to the default version when finished
7a7b1e
7a7b1e
If a PKCS11 API caller provided own mutex handling callbacks,
7a7b1e
we need to ensure they aren't used after C_Finalize is called
7a7b1e
and SoftHSM instance is recycled.
7a7b1e
7a7b1e
Inability to do so may lead to a situation where callbacks might
7a7b1e
be provided by a different dynamically loaded object which is removed
7a7b1e
after C_Finalize() call. Thus, callback pointers become invalid and
7a7b1e
calling them leads to crashes.
7a7b1e
7a7b1e
Fixes: https://github.com/opendnssec/SoftHSMv2/issues/408
7a7b1e
7a7b1e
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
7a7b1e
---
7a7b1e
 src/lib/SoftHSM.cpp | 16 ++++++++++++----
7a7b1e
 1 file changed, 12 insertions(+), 4 deletions(-)
7a7b1e
7a7b1e
diff --git a/src/lib/SoftHSM.cpp b/src/lib/SoftHSM.cpp
7a7b1e
index ee94d3f..e4cc044 100644
7a7b1e
--- a/src/lib/SoftHSM.cpp
7a7b1e
+++ b/src/lib/SoftHSM.cpp
7a7b1e
@@ -314,6 +314,15 @@ static CK_ATTRIBUTE bsAttribute(CK_ATTRIBUTE_TYPE type, const ByteString &value)
7a7b1e
 /*****************************************************************************
7a7b1e
  Implementation of SoftHSM class specific functions
7a7b1e
  *****************************************************************************/
7a7b1e
+static void resetMutexFactoryCallbacks()
7a7b1e
+{
7a7b1e
+	// Reset MutexFactory callbacks to our versions
7a7b1e
+	MutexFactory::i()->setCreateMutex(OSCreateMutex);
7a7b1e
+	MutexFactory::i()->setDestroyMutex(OSDestroyMutex);
7a7b1e
+	MutexFactory::i()->setLockMutex(OSLockMutex);
7a7b1e
+	MutexFactory::i()->setUnlockMutex(OSUnlockMutex);
7a7b1e
+}
7a7b1e
+
7a7b1e
 
7a7b1e
 // Return the one-and-only instance
7a7b1e
 SoftHSM* SoftHSM::i()
7a7b1e
@@ -342,6 +351,7 @@ SoftHSM::SoftHSM()
7a7b1e
 	slotManager = NULL;
7a7b1e
 	sessionManager = NULL;
7a7b1e
 	handleManager = NULL;
7a7b1e
+	resetMutexFactoryCallbacks();
7a7b1e
 }
7a7b1e
 
7a7b1e
 // Destructor
7a7b1e
@@ -352,6 +362,7 @@ SoftHSM::~SoftHSM()
7a7b1e
 	if (slotManager != NULL) delete slotManager;
7a7b1e
 	if (objectStore != NULL) delete objectStore;
7a7b1e
 	if (sessionObjectStore != NULL) delete sessionObjectStore;
7a7b1e
+	resetMutexFactoryCallbacks();
7a7b1e
 }
7a7b1e
 
7a7b1e
 /*****************************************************************************
7a7b1e
@@ -402,10 +413,7 @@ CK_RV SoftHSM::C_Initialize(CK_VOID_PTR pInitArgs)
7a7b1e
 			if (args->flags & CKF_OS_LOCKING_OK)
7a7b1e
 			{
7a7b1e
 				// Use our own mutex functions.
7a7b1e
-				MutexFactory::i()->setCreateMutex(OSCreateMutex);
7a7b1e
-				MutexFactory::i()->setDestroyMutex(OSDestroyMutex);
7a7b1e
-				MutexFactory::i()->setLockMutex(OSLockMutex);
7a7b1e
-				MutexFactory::i()->setUnlockMutex(OSUnlockMutex);
7a7b1e
+				resetMutexFactoryCallbacks();
7a7b1e
 				MutexFactory::i()->enable();
7a7b1e
 			}
7a7b1e
 			else
7a7b1e
-- 
7a7b1e
2.17.1
7a7b1e