DPDK patches and discussions
 help / color / mirror / Atom feed
From: Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>
To: <dev@dpdk.org>
Cc: <anoobj@marvell.com>, Akhil Goyal <gakhil@marvell.com>,
	Kai Ji <kai.ji@intel.com>,
	Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>
Subject: [PATCH] crypto/openssl: fix memory leaks in SM2 ops
Date: Tue, 19 Sep 2023 18:34:12 +0530	[thread overview]
Message-ID: <20230919130412.284-1-gmuthukrishn@marvell.com> (raw)

Fix memory leaks in SM2 ops, as reported by valgrind.

Signed-off-by: Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>
---
 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


             reply	other threads:[~2023-09-19 13:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-19 13:04 Gowrishankar Muthukrishnan [this message]
2023-10-23 13:33 ` Akhil Goyal
2023-11-02  8:38 ` [PATCH v2] crypto/openssl: fix memory leaks in asym ops Gowrishankar Muthukrishnan
2023-11-02 10:03   ` Gowrishankar Muthukrishnan
2023-11-02 22:47   ` Stephen Hemminger
2023-11-03 15:19     ` [EXT] " Gowrishankar Muthukrishnan
2023-11-03 10:18   ` Power, Ciara
2023-11-03 11:38   ` Power, Ciara
2023-11-03 15:15   ` [PATCH v3] " Gowrishankar Muthukrishnan
2023-11-03 15:39     ` Power, Ciara
2023-11-09 20:18     ` Akhil Goyal
2023-11-13  5:41     ` [PATCH v4] " Gowrishankar Muthukrishnan
2023-11-13  6:23       ` Akhil Goyal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230919130412.284-1-gmuthukrishn@marvell.com \
    --to=gmuthukrishn@marvell.com \
    --cc=anoobj@marvell.com \
    --cc=dev@dpdk.org \
    --cc=gakhil@marvell.com \
    --cc=kai.ji@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).