Fix error handling in CRYPTO_get_ex_new_index
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Tue, 19 Sep 2023 18:15:18 +0000 (20:15 +0200)
committerBernd Edlinger <bernd.edlinger@hotmail.de>
Thu, 21 Sep 2023 12:46:01 +0000 (14:46 +0200)
If an out of memory error happens when the index zero
is reserved in a newly created ip->meth stack object,
that reservation is not done in a second attempt, which makes
various X_set_ex_data overwrite the value of X_set_app_data.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22149)

(cherry picked from commit d4f22a915ac50570015a23ad794032c4fb9496cb)

crypto/ex_data.c

index 0268da2a109cbcf402b7915c4be1d00fdb18c6e8..f92d0505fd93ff87617a1dfc0d7d6f0a439bd062 100644 (file)
@@ -171,6 +171,8 @@ int ossl_crypto_get_ex_new_index_ex(OSSL_LIB_CTX *ctx, int class_index,
          * "app_data" routines use ex_data index zero.  See RT 3710. */
         if (ip->meth == NULL
             || !sk_EX_CALLBACK_push(ip->meth, NULL)) {
+            sk_EX_CALLBACK_free(ip->meth);
+            ip->meth = NULL;
             ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
             goto err;
         }