]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
ERR: Make CRYPTO_malloc() and friends report ERR_R_MALLOC_FAILURE
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Mon, 12 Apr 2021 08:01:51 +0000 (10:01 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Sat, 27 Aug 2022 07:40:09 +0000 (09:40 +0200)
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)

crypto/err/err.c
crypto/mem.c

index 02fae89746a7e7e19503ab23aa8091c51566a50b..a6c5ff613283ca2fb1c0cd4cee7b95298f3a9de8 100644 (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;
         }
index 3d67d9256a64d5c58a2547a5509503810010d715..74bf3b892cb6059c022700527235070f9518260a 100644 (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)