ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE

Fixes #6251

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/14833)
This commit is contained in:
Dr. David von Oheimb 2021-04-12 10:01:51 +02:00 committed by Dr. David von Oheimb
parent 555dd9390b
commit 5639ee79bd
2 changed files with 29 additions and 8 deletions

View File

@ -199,16 +199,16 @@ static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
}
#endif
static void ERR_STATE_free(ERR_STATE *s)
static void ERR_STATE_free(ERR_STATE *state)
{
int i;
if (s == NULL)
if (state == NULL)
return;
for (i = 0; i < ERR_NUM_ERRORS; i++) {
err_clear(s, i, 1);
err_clear(state, i, 1);
}
OPENSSL_free(s);
CRYPTO_free(state, OPENSSL_FILE, OPENSSL_LINE);
}
DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
@ -689,7 +689,9 @@ ERR_STATE *ossl_err_get_state_int(void)
if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
return NULL;
if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
/* calling CRYPTO_zalloc(.., NULL, 0) prevents mem alloc error loop */
state = CRYPTO_zalloc(sizeof(*state), NULL, 0);
if (state == NULL) {
CRYPTO_THREAD_set_local(&err_thread_local, NULL);
return NULL;
}

View File

@ -170,9 +170,15 @@ void ossl_malloc_setup_failures(void)
void *CRYPTO_malloc(size_t num, const char *file, int line)
{
void *ptr;
INCREMENT(malloc_count);
if (malloc_impl != CRYPTO_malloc)
return malloc_impl(num, file, line);
if (malloc_impl != CRYPTO_malloc) {
ptr = malloc_impl(num, file, line);
if (ptr != NULL || num == 0)
return ptr;
goto err;
}
if (num == 0)
return NULL;
@ -187,7 +193,20 @@ void *CRYPTO_malloc(size_t num, const char *file, int line)
allow_customize = 0;
}
return malloc(num);
ptr = malloc(num);
if (ptr != NULL)
return ptr;
err:
/*
* ossl_err_get_state_int() in err.c uses CRYPTO_zalloc(num, NULL, 0) for
* ERR_STATE allocation. Prevent mem alloc error loop while reporting error.
*/
if (file != NULL || line != 0) {
ERR_new();
ERR_set_debug(file, line, NULL);
ERR_set_error(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE, NULL);
}
return NULL;
}
void *CRYPTO_zalloc(size_t num, const char *file, int line)