Skip to content

Commit

Permalink
ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE
Browse files Browse the repository at this point in the history
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 #14833)
  • Loading branch information
DDvO committed Aug 27, 2022
1 parent 555dd93 commit 5639ee7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 7 additions & 5 deletions crypto/err/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 22 additions & 3 deletions crypto/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 5639ee7

Please sign in to comment.