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 4FEE6425FF; Tue, 19 Sep 2023 15:04:25 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DABD640278; Tue, 19 Sep 2023 15:04:24 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by mails.dpdk.org (Postfix) with ESMTP id CBBC340276 for ; Tue, 19 Sep 2023 15:04:22 +0200 (CEST) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38J6mDKT022848; Tue, 19 Sep 2023 06:04:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=aoHVvbQtz0Iii9Rfkrte2eM6AzX1+LYy+H169p7t8bs=; b=NkRDV8aCcA7HTj8VpGTncvg0Waw5HDf4DTVLQQE+fHmK5nPBoZgsgxkvnuRq6GQz7SE5 YCHG1KYh1//uksXGMmTDiFtjGYhBrIa7KPtR+JrE/dZoUfpijzMdLAMoF4EWwQfbAB1v g6kUZ+Vv3fNtcMffnoCaOk9K++3RxgZC+iFUs71k0FBSz1OJvYy+p6/Iroj47xICX1TP Wllh0Xn+BSqpTXZos1ImdA7CJloMZH/EJ04b7YSzPjdgXLaI/W4DJe/xZsIBIFs+TYQV y3kHUxvrNqgiTRHdqD70skWOCTZy0fWzHF39EzUeT4G5M03oI42JGb3JRSkx1da3CkUx 4g== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3t5bvkrefd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Tue, 19 Sep 2023 06:04:21 -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; Tue, 19 Sep 2023 06:04:19 -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; Tue, 19 Sep 2023 06:04:19 -0700 Received: from BG-LT91401.marvell.com (unknown [10.193.74.240]) by maili.marvell.com (Postfix) with ESMTP id 40CA43F70A6; Tue, 19 Sep 2023 06:04:15 -0700 (PDT) From: Gowrishankar Muthukrishnan To: CC: , Akhil Goyal , Kai Ji , Gowrishankar Muthukrishnan Subject: [PATCH] crypto/openssl: fix memory leaks in SM2 ops Date: Tue, 19 Sep 2023 18:34:12 +0530 Message-ID: <20230919130412.284-1-gmuthukrishn@marvell.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-GUID: iUzHFUYdOXXYwg14GdEpubDQsNOFPZCm X-Proofpoint-ORIG-GUID: iUzHFUYdOXXYwg14GdEpubDQsNOFPZCm X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-19_06,2023-09-19_01,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 SM2 ops, as reported by valgrind. Signed-off-by: Gowrishankar Muthukrishnan --- drivers/crypto/openssl/rte_openssl_pmd.c | 45 ++++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/drivers/crypto/openssl/rte_openssl_pmd.c b/drivers/crypto/openssl/rte_openssl_pmd.c index 5e8624cebe..c69889d522 100644 --- a/drivers/crypto/openssl/rte_openssl_pmd.c +++ b/drivers/crypto/openssl/rte_openssl_pmd.c @@ -2674,10 +2674,13 @@ 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_BLD *param_bld = NULL; + ECDSA_SIG *ec_sign = NULL; + EVP_MD_CTX *md_ctx = NULL; OSSL_PARAM *params = NULL; + EVP_MD *check_md = NULL; EVP_PKEY *pkey = NULL; BIGNUM *pkey_bn = NULL; - uint8_t pubkey[64]; + uint8_t pubkey[65]; size_t len = 0; int ret = -1; @@ -2787,10 +2790,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); @@ -2842,17 +2842,12 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, op->sm2.s.length = BN_num_bytes(s); BN_bn2bin(r, op->sm2.r.data); BN_bn2bin(s, op->sm2.s.data); - - ECDSA_SIG_free(ec_sign); } break; case RTE_CRYPTO_ASYM_OP_VERIFY: { - unsigned char signbuf[128] = {0}; BIGNUM *r = NULL, *s = NULL; - EVP_MD_CTX *md_ctx = NULL; - ECDSA_SIG *ec_sign; - EVP_MD *check_md; + unsigned char *signbuf; size_t signlen; kctx = EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL); @@ -2902,19 +2897,16 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, goto err_sm2; } - r = NULL; - s = NULL; - - signlen = i2d_ECDSA_SIG(ec_sign, (unsigned char **)&signbuf); - if (signlen <= 0) + signlen = i2d_ECDSA_SIG(ec_sign, 0); + signbuf = rte_malloc(NULL, signlen, 0); + signlen = i2d_ECDSA_SIG(ec_sign, &signbuf); + if (signlen <= 0) { + rte_free(signbuf); goto err_sm2; + } if (!EVP_DigestVerifyFinal(md_ctx, signbuf, signlen)) goto err_sm2; - - BN_free(r); - BN_free(s); - ECDSA_SIG_free(ec_sign); } break; default: @@ -2928,6 +2920,15 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, ret = 0; cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS; err_sm2: + if (ec_sign) + ECDSA_SIG_free(ec_sign); + + if (check_md) + EVP_MD_free(check_md); + + if (md_ctx) + EVP_MD_CTX_free(md_ctx); + if (kctx) EVP_PKEY_CTX_free(kctx); @@ -2943,6 +2944,12 @@ process_openssl_sm2_op_evp(struct rte_crypto_op *cop, if (param_bld) OSSL_PARAM_BLD_free(param_bld); + if (params) + OSSL_PARAM_free(params); + + if (pkey_bn) + BN_free(pkey_bn); + return ret; } -- 2.25.1