DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] crypto/openssl: fix memory leaks in SM2 ops
@ 2023-09-19 13:04 Gowrishankar Muthukrishnan
  2023-10-23 13:33 ` Akhil Goyal
  2023-11-02  8:38 ` [PATCH v2] crypto/openssl: fix memory leaks in asym ops Gowrishankar Muthukrishnan
  0 siblings, 2 replies; 13+ messages in thread
From: Gowrishankar Muthukrishnan @ 2023-09-19 13:04 UTC (permalink / raw)
  To: dev; +Cc: anoobj, Akhil Goyal, Kai Ji, Gowrishankar Muthukrishnan

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


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-11-13  6:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-19 13:04 [PATCH] crypto/openssl: fix memory leaks in SM2 ops Gowrishankar Muthukrishnan
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

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).