Skip to content

Commit

Permalink
Merge pull request softhsm#517 from halderen/closememleak
Browse files Browse the repository at this point in the history
Memory leak upon clearing resources or unloading
  • Loading branch information
halderen authored Feb 24, 2020
2 parents 2b9768a + 37d2f9b commit 80b96a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/lib/SoftHSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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<std::string, CK_MECHANISM_TYPE> mechanisms_table;
std::list<CK_MECHANISM_TYPE> supportedMechanisms;
CK_ULONG nrSupportedMechanisms;

/*****************************************************************************
Implementation of PKCS #11 functions
*****************************************************************************/
Expand Down
10 changes: 7 additions & 3 deletions src/lib/SoftHSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ class SoftHSM
SessionManager* sessionManager;
HandleManager* handleManager;

// A list with the supported mechanisms
std::map<std::string, CK_MECHANISM_TYPE> mechanisms_table;
std::list<CK_MECHANISM_TYPE> supportedMechanisms;
CK_ULONG nrSupportedMechanisms;

int forkID;

// Encrypt/Decrypt variants
Expand Down Expand Up @@ -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<std::string, CK_MECHANISM_TYPE> &t);

bool isMechanismPermitted(OSObject* key, CK_MECHANISM_PTR pMechanism);
void prepareSupportedMecahnisms(std::map<std::string, CK_MECHANISM_TYPE> &t);
bool detectFork(void);
};

0 comments on commit 80b96a9

Please sign in to comment.