From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 974414327C; Fri, 3 Nov 2023 16:15:31 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8571040273; Fri, 3 Nov 2023 16:15:31 +0100 (CET) Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by mails.dpdk.org (Postfix) with ESMTP id 883B24014F for ; Fri, 3 Nov 2023 16:15:29 +0100 (CET) Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3A3BVU4V026693; Fri, 3 Nov 2023 08:15:28 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=kilIdJUsigN66xVdlorYzH2Gyfh9Y3fNo6OzScdBqQ8=; b=ECo1WdT4Pe7l+W4oWduqXaS6g8HyBmSq5vKzXGbGx8pUx3FBVrISn82sSYmdF3UtXmmu KUaD+brhqZKjo1vZHKXb+xc2+qqD3WCTOZ7OBMoX/zmUgzpjCCt2e9RmnWWXdWPP2gnz 913hCnZUXDYgA2EhUYP9QYD+Rt7Wr5tBboU3ZCeNdAw6+d81Ip6dKfpHXHRjq6+hUgB7 ta7KohYhpv+urC34mJiLwV498kA8r6kEyy08Fn/HMYOJH3G3i6id6dL6Dpy8rjAd09Q/ XKwSPYgbkYD3mtzzkti8lnYbdgWbD6dDj94bqrcZ78aVQgubfI+1e/JT4MUidzoUitIm AQ== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3u4upkhekm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 03 Nov 2023 08:15:28 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 3 Nov 2023 08:15:27 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Fri, 3 Nov 2023 08:15:26 -0700 Received: from BG-LT91401.marvell.com (BG-LT91401.marvell.com [10.28.168.34]) by maili.marvell.com (Postfix) with ESMTP id 107AE3F70B2; Fri, 3 Nov 2023 08:15:23 -0700 (PDT) From: Gowrishankar Muthukrishnan To: CC: , Akhil Goyal , Kai Ji , Ciara Power , "Gowrishankar Muthukrishnan" Subject: [PATCH v3] crypto/openssl: fix memory leaks in asym ops Date: Fri, 3 Nov 2023 20:45:18 +0530 Message-ID: <66bfdd3e22abae4586bf47d076be4b290548cc72.1699024267.git.gmuthukrishn@marvell.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <83522013646bcd96b2420b3f69b74255981b3a20.1698913776.git.gmuthukrishn@marvell.com> References: <83522013646bcd96b2420b3f69b74255981b3a20.1698913776.git.gmuthukrishn@marvell.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-ORIG-GUID: _QMp74MKZgABD59Q99XO7n3tE-sjIIdn X-Proofpoint-GUID: _QMp74MKZgABD59Q99XO7n3tE-sjIIdn X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-03_14,2023-11-02_03,2023-05-22_02 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Fix memory leaks in Asymmetric ops, as reported by valgrind. Signed-off-by: Gowrishankar Muthukrishnan --- v3: - changes as suggested in v2. --- drivers/crypto/openssl/rte_openssl_pmd.c | 30 +++++++++++++------- drivers/crypto/openssl/rte_openssl_pmd_ops.c | 16 +++++++---- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/drivers/crypto/openssl/rte_openssl_pmd.c b/drivers/crypto/openssl/rte_openssl_pmd.c index c234882417..6fb827b600 100644 --- a/drivers/crypto/openssl/rte_openssl_pmd.c +++ b/drivers/crypto/openssl/rte_openssl_pmd.c @@ -1897,6 +1897,7 @@ process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop, size_t outlen; unsigned char *dsa_sign_data; const unsigned char *dsa_sign_data_p; + int ret = -1; cop->status = RTE_CRYPTO_OP_STATUS_ERROR; params = OSSL_PARAM_BLD_to_param(param_bld); @@ -1950,9 +1951,9 @@ process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop, cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS; } + ret = 0; DSA_SIG_free(sign); OPENSSL_free(dsa_sign_data); - return 0; err_dsa_sign: if (params) @@ -1961,7 +1962,9 @@ process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop, EVP_PKEY_CTX_free(key_ctx); if (dsa_ctx) EVP_PKEY_CTX_free(dsa_ctx); - return -1; + + EVP_PKEY_free(pkey); + return ret; } /* process dsa verify operation */ @@ -2034,6 +2037,7 @@ process_openssl_dsa_verify_op_evp(struct rte_crypto_op *cop, ret = 0; } + OPENSSL_free(dsa_sig); err_dsa_verify: if (sign) DSA_SIG_free(sign); @@ -2044,6 +2048,9 @@ process_openssl_dsa_verify_op_evp(struct rte_crypto_op *cop, if (dsa_ctx) EVP_PKEY_CTX_free(dsa_ctx); + BN_free(pub_key); + EVP_PKEY_free(pkey); + return ret; } #else @@ -2674,6 +2681,9 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, EVP_PKEY_CTX *kctx = NULL, *sctx = NULL, *cctx = NULL; struct rte_crypto_asym_op *op = cop->asym; OSSL_PARAM *params = sess->u.sm2.params; + EVP_MD_CTX *md_ctx = NULL; + ECDSA_SIG *ec_sign = NULL; + EVP_MD *check_md = NULL; EVP_PKEY *pkey = NULL; int ret = -1; @@ -2739,10 +2749,7 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, { unsigned char signbuf[128] = {0}; const unsigned char *signptr; - EVP_MD_CTX *md_ctx = NULL; const BIGNUM *r, *s; - ECDSA_SIG *ec_sign; - EVP_MD *check_md; size_t signlen; kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL); @@ -2800,11 +2807,8 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, break; case RTE_CRYPTO_ASYM_OP_VERIFY: { - unsigned char signbuf[128] = {0}; + unsigned char signbuf[128] = {0}, *signbuf_new = NULL; BIGNUM *r = NULL, *s = NULL; - EVP_MD_CTX *md_ctx = NULL; - ECDSA_SIG *ec_sign; - EVP_MD *check_md; size_t signlen; kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL); @@ -2857,11 +2861,12 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, r = NULL; s = NULL; - signlen = i2d_ECDSA_SIG(ec_sign, (unsigned char **)&signbuf); + signbuf_new = signbuf; + signlen = i2d_ECDSA_SIG(ec_sign, (unsigned char **)&signbuf_new); if (signlen <= 0) goto err_sm2; - if (!EVP_DigestVerifyFinal(md_ctx, signbuf, signlen)) + if (!EVP_DigestVerifyFinal(md_ctx, signbuf_new, signlen)) goto err_sm2; BN_free(r); @@ -2880,6 +2885,9 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, ret = 0; cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS; err_sm2: + EVP_MD_free(check_md); + EVP_MD_CTX_free(md_ctx); + if (kctx) EVP_PKEY_CTX_free(kctx); diff --git a/drivers/crypto/openssl/rte_openssl_pmd_ops.c b/drivers/crypto/openssl/rte_openssl_pmd_ops.c index 2862c294a9..bef7671424 100644 --- a/drivers/crypto/openssl/rte_openssl_pmd_ops.c +++ b/drivers/crypto/openssl/rte_openssl_pmd_ops.c @@ -958,9 +958,11 @@ static int openssl_set_asym_session_parameters( rsa_ctx = EVP_PKEY_CTX_new(pkey, NULL); asym_session->xfrm_type = RTE_CRYPTO_ASYM_XFORM_RSA; asym_session->u.r.ctx = rsa_ctx; + EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(key_ctx); + OSSL_PARAM_BLD_free(param_bld); OSSL_PARAM_free(params); - break; + ret = 0; #else RSA *rsa = RSA_new(); if (rsa == NULL) @@ -1030,7 +1032,7 @@ static int openssl_set_asym_session_parameters( } asym_session->u.r.rsa = rsa; asym_session->xfrm_type = RTE_CRYPTO_ASYM_XFORM_RSA; - break; + ret = 0; #endif err_rsa: BN_clear_free(n); @@ -1042,7 +1044,7 @@ static int openssl_set_asym_session_parameters( BN_clear_free(dmq1); BN_clear_free(iqmp); - return -1; + return ret; } case RTE_CRYPTO_ASYM_XFORM_MODEX: { @@ -1184,8 +1186,7 @@ static int openssl_set_asym_session_parameters( #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) BIGNUM *p = NULL, *g = NULL; BIGNUM *q = NULL, *priv_key = NULL; - BIGNUM *pub_key = BN_new(); - BN_zero(pub_key); + BIGNUM *pub_key = NULL; OSSL_PARAM_BLD *param_bld = NULL; p = BN_bin2bn((const unsigned char *) @@ -1363,6 +1364,7 @@ static int openssl_set_asym_session_parameters( asym_session->u.sm2.params = params; OSSL_PARAM_BLD_free(param_bld); + BN_free(pkey_bn); asym_session->xfrm_type = RTE_CRYPTO_ASYM_XFORM_SM2; break; @@ -1373,6 +1375,7 @@ static int openssl_set_asym_session_parameters( if (asym_session->u.sm2.params) OSSL_PARAM_free(asym_session->u.sm2.params); + BN_free(pkey_bn); return -1; #else OPENSSL_LOG(WARNING, "SM2 unsupported in current OpenSSL Version"); @@ -1452,6 +1455,8 @@ static void openssl_reset_asym_session(struct openssl_asym_session *sess) break; case RTE_CRYPTO_ASYM_XFORM_DH: #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) + OSSL_PARAM_BLD_free(sess->u.dh.param_bld); + OSSL_PARAM_BLD_free(sess->u.dh.param_bld_peer); sess->u.dh.param_bld = NULL; sess->u.dh.param_bld_peer = NULL; #else @@ -1461,6 +1466,7 @@ static void openssl_reset_asym_session(struct openssl_asym_session *sess) break; case RTE_CRYPTO_ASYM_XFORM_DSA: #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) + OSSL_PARAM_BLD_free(sess->u.s.param_bld); sess->u.s.param_bld = NULL; #else if (sess->u.s.dsa) -- 2.25.1