hcrypto: w32crypto crypt provider handle leak

_hc_CryptProvider() returns a global handle to a Win32 Crypt Provider.
If the global handle is NULL, then a handle is allocated.  Unfortunately,
due to a coding mistake the global handle variable, g_cryptprovider, was
never set and a new handle was allocated with each call.

Refactor the function to ensure that the global handle is the value
that is returned.  Use NULL instead of 0 for pointer assignment.

Change-Id: If1ef3aa19cbd1d51860370db24c086e86922ff0d
This commit is contained in:
Jeffrey Altman
2014-12-24 15:43:03 -05:00
parent 0e93a04c01
commit b3f29170a3

View File

@@ -43,16 +43,16 @@
#include "randi.h" #include "randi.h"
volatile static HCRYPTPROV g_cryptprovider = 0; volatile static HCRYPTPROV g_cryptprovider = NULL;
static HCRYPTPROV static HCRYPTPROV
_hc_CryptProvider(void) _hc_CryptProvider(void)
{ {
BOOL rv; BOOL rv;
HCRYPTPROV cryptprovider = 0; HCRYPTPROV cryptprovider = NULL;
if (g_cryptprovider != 0) if (g_cryptprovider != NULL)
return g_cryptprovider; goto out;
rv = CryptAcquireContext(&cryptprovider, NULL, rv = CryptAcquireContext(&cryptprovider, NULL,
MS_ENHANCED_PROV, PROV_RSA_FULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
@@ -82,15 +82,15 @@ _hc_CryptProvider(void)
CRYPT_VERIFYCONTEXT); CRYPT_VERIFYCONTEXT);
} }
if (rv && if (rv == 0 &&
InterlockedCompareExchangePointer((PVOID *) &g_cryptprovider, InterlockedCompareExchangePointer((PVOID *) &g_cryptprovider,
(PVOID) cryptprovider, 0) != 0) { (PVOID) cryptprovider, NULL) != 0) {
CryptReleaseContext(cryptprovider, 0); CryptReleaseContext(cryptprovider, 0);
cryptprovider = g_cryptprovider;
} }
return cryptprovider; out:
return g_cryptprovider;
} }
/* /*