From 37d2f9b698b729a1438c4cc1c40677acbffe464b Mon Sep 17 00:00:00 2001 From: halderen Date: Thu, 6 Feb 2020 14:43:01 +0100 Subject: [PATCH] Memory leak while freeing up resources. This leak will only become evident when destroying SoftHSM, like in unloading the shared libary of when using the close-on-fork. Not even when performing C_Finalize or closing the application as the C++ library will clean up the latter. --- src/lib/SoftHSM.cpp | 17 +++++++++++------ src/lib/SoftHSM.h | 10 +++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib/SoftHSM.cpp b/src/lib/SoftHSM.cpp index f08af235f..48dc3eba9 100644 --- a/src/lib/SoftHSM.cpp +++ b/src/lib/SoftHSM.cpp @@ -344,10 +344,17 @@ SoftHSM* SoftHSM::i() if (!instance.get()) { instance.reset(new SoftHSM()); - } else if(instance->detectFork()) + } + else if(instance->detectFork()) { if (Configuration::i()->getBool("library.reset_on_fork", false)) { + /* It is important to first clear the singleton + * instance, and then fill it again, so make sure + * the old instance is first destroyed as some + * static structures are erased in the destructor. + */ + instance.reset(NULL); instance.reset(new SoftHSM()); } } @@ -393,14 +400,12 @@ SoftHSM::~SoftHSM() if (sessionObjectStore != NULL) delete sessionObjectStore; sessionObjectStore = NULL; + mechanisms_table.clear(); + supportedMechanisms.clear(); + resetMutexFactoryCallbacks(); } -// A list with the supported mechanisms -std::map mechanisms_table; -std::list supportedMechanisms; -CK_ULONG nrSupportedMechanisms; - /***************************************************************************** Implementation of PKCS #11 functions *****************************************************************************/ diff --git a/src/lib/SoftHSM.h b/src/lib/SoftHSM.h index beb603d8e..39d36b7f2 100644 --- a/src/lib/SoftHSM.h +++ b/src/lib/SoftHSM.h @@ -193,6 +193,11 @@ class SoftHSM SessionManager* sessionManager; HandleManager* handleManager; + // A list with the supported mechanisms + std::map mechanisms_table; + std::list supportedMechanisms; + CK_ULONG nrSupportedMechanisms; + int forkID; // Encrypt/Decrypt variants @@ -478,9 +483,8 @@ class SoftHSM CK_RV MechParamCheckRSAPKCSOAEP(CK_MECHANISM_PTR pMechanism); - static bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism); - static void prepareSupportedMecahnisms(std::map &t); - + bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism); + void prepareSupportedMecahnisms(std::map &t); bool detectFork(void); };