hcrypto: p11_module_init_once make handle static global

Coverity complains about the leakage of 'handle' when the identifier
goes out of scope.  Change handle into a static global to hold the
value instead of a stack variable.

Change-Id: I040707ac731558f7d523f128a006a80b98d45b79
This commit is contained in:
Jeffrey Altman
2016-04-17 15:08:12 -05:00
parent a08431b658
commit 4ad2f5830a

View File

@@ -81,38 +81,40 @@ struct pkcs11_md_ctx {
CK_SESSION_HANDLE hSession; CK_SESSION_HANDLE hSession;
}; };
static void *pkcs11_module_handle;
static void static void
p11_module_init_once(void *context) p11_module_init_once(void *context)
{ {
CK_RV rv; CK_RV rv;
CK_FUNCTION_LIST_PTR module; CK_FUNCTION_LIST_PTR module;
CK_RV (*C_GetFunctionList_fn)(CK_FUNCTION_LIST_PTR_PTR); CK_RV (*C_GetFunctionList_fn)(CK_FUNCTION_LIST_PTR_PTR);
void *handle = NULL;
if (!issuid()) { if (!issuid()) {
char *pkcs11ModulePath = getenv("PKCS11_MODULE_PATH"); char *pkcs11ModulePath = getenv("PKCS11_MODULE_PATH");
if (pkcs11ModulePath != NULL) { if (pkcs11ModulePath != NULL) {
handle = dlopen(pkcs11ModulePath, RTLD_LAZY | RTLD_LOCAL | pkcs11_module_handle =
RTLD_GROUP | RTLD_NODELETE); dlopen(pkcs11ModulePath,
if (handle == NULL) RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP | RTLD_NODELETE);
if (pkcs11_module_handle == NULL)
fprintf(stderr, "p11_module_init(%s): %s\n", pkcs11ModulePath, dlerror()); fprintf(stderr, "p11_module_init(%s): %s\n", pkcs11ModulePath, dlerror());
} }
} }
#ifdef PKCS11_MODULE_PATH #ifdef PKCS11_MODULE_PATH
if (handle == NULL) { if (pkcs11_module_handle == NULL) {
handle = dlopen(PKCS11_MODULE_PATH, RTLD_LAZY | RTLD_LOCAL | pkcs11_module_handle =
RTLD_GROUP | RTLD_NODELETE); dlopen(PKCS11_MODULE_PATH,
if (handle == NULL) RTLD_LAZY | RTLD_LOCAL | RTLD_GROUP | RTLD_NODELETE);
if (pkcs11_module_handle == NULL)
fprintf(stderr, "p11_module_init(%s): %s\n", PKCS11_MODULE_PATH, dlerror()); fprintf(stderr, "p11_module_init(%s): %s\n", PKCS11_MODULE_PATH, dlerror());
} }
#endif #endif
if (handle == NULL) { if (pkcs11_module_handle == NULL) {
rv = CKR_LIBRARY_LOAD_FAILED; rv = CKR_LIBRARY_LOAD_FAILED;
goto cleanup; goto cleanup;
} }
C_GetFunctionList_fn = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR)) C_GetFunctionList_fn = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))
dlsym(handle, "C_GetFunctionList"); dlsym(pkcs11_module_handle, "C_GetFunctionList");
if (C_GetFunctionList_fn == NULL) { if (C_GetFunctionList_fn == NULL) {
rv = CKR_LIBRARY_LOAD_FAILED; rv = CKR_LIBRARY_LOAD_FAILED;
goto cleanup; goto cleanup;
@@ -129,9 +131,11 @@ p11_module_init_once(void *context)
*((CK_FUNCTION_LIST_PTR_PTR)context) = module; *((CK_FUNCTION_LIST_PTR_PTR)context) = module;
cleanup: cleanup:
if (handle != NULL && p11_module == NULL) if (pkcs11_module_handle != NULL && p11_module == NULL) {
dlclose(handle); dlclose(pkcs11_module_handle);
/* else leak handle */ pkcs11_module_handle = NULL;
}
/* else leak pkcs11_module_handle */
} }
static CK_RV static CK_RV