From: Dr. David von Oheimb Date: Mon, 12 Apr 2021 08:01:51 +0000 (+0200) Subject: ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE X-Git-Tag: openssl-3.2.0-alpha1~2156 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=5639ee79bdc9;p=thirdparty%2Fopenssl.git ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE Fixes #6251 Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale Reviewed-by: David von Oheimb (Merged from https://github.com/openssl/openssl/pull/14833) --- diff --git a/crypto/err/err.c b/crypto/err/err.c index 02fae89746a..a6c5ff61328 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -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; } diff --git a/crypto/mem.c b/crypto/mem.c index 3d67d9256a6..74bf3b892cb 100644 --- a/crypto/mem.c +++ b/crypto/mem.c @@ -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)