EVP: fix reference counting for EVP_CIPHER.
authorPauli <ppzgs1@gmail.com>
Mon, 8 Feb 2021 01:38:21 +0000 (11:38 +1000)
committerPauli <ppzgs1@gmail.com>
Fri, 12 Feb 2021 02:28:55 +0000 (12:28 +1000)
Under some circumstances, the reference count for a cipher wasn't updated
properly.  This shows up best when fetches are not being queried but would be
possible if the cache flushed at a bad time.

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14126)

crypto/evp/evp_enc.c

index eb8c0faf1475902e6ab76f5b1ed5b0c7c30f3839..b804d749147e01ae4b6b1cd1ecd91533c306f279 100644 (file)
@@ -182,6 +182,14 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
 #endif
     }
 
+    if (cipher->prov != NULL) {
+        if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
+            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
+            return 0;
+        }
+        EVP_CIPHER_free(ctx->fetched_cipher);
+        ctx->fetched_cipher = (EVP_CIPHER *)cipher;
+    }
     ctx->cipher = cipher;
     if (ctx->provctx == NULL) {
         ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));