Skip to content

Commit a69991e

Browse files
Additional allocation hardening (#137)
* Based on additional fixes made when porting previous fix to 1.9 branch (#135) * Check for failure in CRYPTO_THREAD_lock_new * Reduce scope for unexpected behavior in RSA keygen
1 parent 8a6a595 commit a69991e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

KeysInUse/keysinuse.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ static void keysinuse_init_internal()
241241

242242
lh_keysinuse_ctx_imp_lock = CRYPTO_THREAD_lock_new();
243243
lh_keysinuse_ctx_imp = lh_SCOSSL_KEYSINUSE_CTX_IMP_new(scossl_keysinuse_ctx_hash, scossl_keysinuse_ctx_cmp);
244+
if (lh_keysinuse_ctx_imp_lock == NULL || lh_keysinuse_ctx_imp == NULL)
245+
{
246+
goto cleanup;
247+
}
244248

245249
#ifndef KEYSINUSE_LOG_SYSLOG
246250
if (!keysinuse_init_logging())

ScosslCommon/src/scossl_helpers.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ void SCOSSL_set_trace_level(int trace_level, int ossl_ERR_level)
198198

199199
void SCOSSL_set_trace_log_filename(const char *filename)
200200
{
201+
if( _loggingLock == NULL )
202+
{
203+
return;
204+
}
205+
201206
if( _traceLogFilename )
202207
{
203208
OPENSSL_free(_traceLogFilename);
@@ -239,6 +244,11 @@ static void _scossl_log_bytes_valist(
239244
char errStringBuf[SCOSSL_TRACELOG_PARA_LENGTH];
240245
char paraBuf[SCOSSL_TRACELOG_PARA_LENGTH];
241246
char *trace_level_prefix = "";
247+
248+
if( _loggingLock == NULL )
249+
{
250+
return;
251+
}
242252

243253
if( SYMCRYPT_MAX(_traceLogLevel, _osslERRLogLevel) < trace_level )
244254
{

SymCryptProvider/src/keymgmt/p_scossl_rsa_keymgmt.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ static SCOSSL_PROV_RSA_KEY_CTX *p_scossl_rsa_keygen(_In_ SCOSSL_RSA_KEYGEN_CTX *
401401
PUINT64 pPubExp64;
402402
UINT32 genFlags = SYMCRYPT_FLAG_RSAKEY_SIGN | SYMCRYPT_FLAG_RSAKEY_ENCRYPT;
403403

404-
keyCtx = OPENSSL_malloc(sizeof(SCOSSL_PROV_RSA_KEY_CTX));
404+
keyCtx = OPENSSL_zalloc(sizeof(SCOSSL_PROV_RSA_KEY_CTX));
405405
if (keyCtx == NULL)
406406
{
407407
goto cleanup;
@@ -434,7 +434,6 @@ static SCOSSL_PROV_RSA_KEY_CTX *p_scossl_rsa_keygen(_In_ SCOSSL_RSA_KEYGEN_CTX *
434434
goto cleanup;
435435
}
436436

437-
keyCtx->initialized = TRUE;
438437
keyCtx->keyType = genCtx->keyType;
439438
keyCtx->pssRestrictions = genCtx->pssRestrictions;
440439
genCtx->pssRestrictions = NULL;
@@ -443,6 +442,8 @@ static SCOSSL_PROV_RSA_KEY_CTX *p_scossl_rsa_keygen(_In_ SCOSSL_RSA_KEYGEN_CTX *
443442
keyCtx->keysinuseCtx = NULL;
444443
#endif
445444

445+
keyCtx->initialized = TRUE;
446+
446447
cleanup:
447448
if (keyCtx != NULL && !keyCtx->initialized)
448449
{

0 commit comments

Comments
 (0)