fix memory allocation and reference counting issues

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
(Merged from https://github.com/openssl/openssl/pull/21341)
This commit is contained in:
Pauli 2023-07-02 17:19:17 +10:00
parent 52c362b3fe
commit 97beb77f31
10 changed files with 39 additions and 22 deletions

View File

@ -98,7 +98,6 @@ BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
if (method->create != NULL && !method->create(bio)) {
ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
CRYPTO_FREE_REF(&bio->references);
goto err;
}
if (method->create == NULL)

View File

@ -85,8 +85,11 @@ static DH *dh_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
return NULL;
}
if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
}
ret->libctx = libctx;
ret->meth = DH_get_default_method();

View File

@ -144,8 +144,11 @@ static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
return NULL;
}
if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
}
ret->libctx = libctx;
ret->meth = DSA_get_default_method();

View File

@ -86,6 +86,11 @@ EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
if (ret == NULL)
return NULL;
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
OPENSSL_free(ret);
return NULL;
}
ret->libctx = libctx;
if (propq != NULL) {
ret->propq = OPENSSL_strdup(propq);
@ -93,9 +98,6 @@ EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
goto err;
}
if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
ret->meth = EC_KEY_get_default_method();
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
if (engine != NULL) {
@ -133,7 +135,6 @@ EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
return ret;
err:
CRYPTO_FREE_REF(&ret->references);
EC_KEY_free(ret);
return NULL;
}

View File

@ -78,7 +78,10 @@ static void *evp_rand_new(void)
{
EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
if (rand == NULL || !CRYPTO_NEW_REF(&rand->refcnt, 1)) {
if (rand == NULL)
return NULL;
if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
OPENSSL_free(rand);
return NULL;
}

View File

@ -443,7 +443,7 @@ static OSSL_PROVIDER *provider_new(const char *name,
if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL)
return NULL;
if (!CRYPTO_NEW_REF(&prov->refcnt, 1)) {
ossl_provider_free(prov);
OPENSSL_free(prov);
return NULL;
}
#ifndef HAVE_ATOMICS

View File

@ -86,8 +86,11 @@ static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
return NULL;
}
if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
CRYPTO_THREAD_lock_free(ret->lock);
OPENSSL_free(ret);
return NULL;
}
ret->libctx = libctx;
ret->meth = RSA_get_default_method();

View File

@ -230,7 +230,8 @@ static ossl_unused ossl_inline int CRYPTO_NEW_REF(CRYPTO_REF_COUNT *refcnt, int
static ossl_unused ossl_inline void CRYPTO_FREE_REF(CRYPTO_REF_COUNT *refcnt) \
{
CRYPTO_THREAD_lock_free(refcnt->lock);
if (refcnt != NULL)
CRYPTO_THREAD_lock_free(refcnt->lock);
}
# else /* OPENSSL_THREADS */

View File

@ -3781,6 +3781,7 @@ SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
return NULL;
/* Doing this for the run once effect */
if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
goto err;
@ -3788,11 +3789,13 @@ SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
ret = OPENSSL_zalloc(sizeof(*ret));
if (ret == NULL)
goto err;
return NULL;
/* Init the reference counting before any call to SSL_CTX_free */
if (!CRYPTO_NEW_REF(&ret->references, 1))
goto err;
if (!CRYPTO_NEW_REF(&ret->references, 1)) {
OPENSSL_free(ret);
return NULL;
}
ret->lock = CRYPTO_THREAD_lock_new();
if (ret->lock == NULL) {

View File

@ -141,9 +141,8 @@ SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
SSL_SESSION *dest;
dest = OPENSSL_malloc(sizeof(*dest));
if (dest == NULL) {
goto err;
}
if (dest == NULL)
return NULL;
memcpy(dest, src, sizeof(*dest));
/*
@ -171,8 +170,10 @@ SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
dest->next = NULL;
dest->owner = NULL;
if (!CRYPTO_NEW_REF(&dest->references, 1))
goto err;
if (!CRYPTO_NEW_REF(&dest->references, 1)) {
OPENSSL_free(dest);
return NULL;
}
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) {
ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);