Fix a mem leak in CMS
authorMatt Caswell <matt@openssl.org>
Tue, 1 May 2018 08:29:17 +0000 (09:29 +0100)
committerMatt Caswell <matt@openssl.org>
Tue, 8 May 2018 07:43:39 +0000 (08:43 +0100)
The function CMS_RecipientInfo_set0_pkey() is a "set0" and therefore
memory management passes to OpenSSL. If the same function is called again
then we should ensure that any previous value that was set is freed first
before we set it again.

Fixes #5052

Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/6142)

crypto/cms/cms_env.c
crypto/cms/cms_smime.c

index 6ca3be71adcf5e342fc851a864d414e4c0773a26..7c2d420b54bbc1d8f0227d4e5eead2c8df5ee240 100644 (file)
@@ -282,6 +282,7 @@ int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
         CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
         return 0;
     }
+    EVP_PKEY_free(ri->d.ktri->pkey);
     ri->d.ktri->pkey = pkey;
     return 1;
 }
index 7e7b6e5d4f79644c9c0b56887a6c29cae53748c8..76883bfb9bb0d895eb5136a92f943c05e8b78081 100644 (file)
@@ -631,6 +631,7 @@ int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
          * all.
          */
         else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
+            EVP_PKEY_up_ref(pk);
             CMS_RecipientInfo_set0_pkey(ri, pk);
             r = CMS_RecipientInfo_decrypt(cms, ri);
             CMS_RecipientInfo_set0_pkey(ri, NULL);