crypto/openssl: fix memory leaks in SM2 ops

Message ID 20230919130412.284-1-gmuthukrishn@marvell.com (mailing list archive)
State Changes Requested, archived
Delegated to: akhil goyal
Headers
Series crypto/openssl: fix memory leaks in SM2 ops |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS

Commit Message

Gowrishankar Muthukrishnan Sept. 19, 2023, 1:04 p.m. UTC
  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(-)
  

Comments

Akhil Goyal Oct. 23, 2023, 1:33 p.m. UTC | #1
> Subject: [PATCH] crypto/openssl: fix memory leaks in SM2 ops
> 
> 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(-)
Please rebase this patch. It no longer applies cleanly on top of tree.
  

Patch

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;
 }