From 5f9d701e8a56f4b7a0ff9c1b0c24fd130427cf1f Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Thu, 10 Mar 2022 20:03:13 +0800
Subject: [PATCH 090/109] uadk: v1: fix wd create ctx memory leak

ctx's block memory should be freed if wd fail to create ctx.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 v1/wd_aead.c   | 12 ++++++++----
 v1/wd_cipher.c |  5 ++++-
 v1/wd_dh.c     |  4 +++-
 v1/wd_digest.c |  6 +++++-
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/v1/wd_aead.c b/v1/wd_aead.c
index 6d8c541..ec537b3 100644
--- a/v1/wd_aead.c
+++ b/v1/wd_aead.c
@@ -229,14 +229,13 @@ void *wcrypto_create_aead_ctx(struct wd_queue *q,
 	ctx->ckey = setup->br.alloc(setup->br.usr, MAX_CIPHER_KEY_SIZE);
 	if (!ctx->ckey) {
 		WD_ERR("fail to alloc cipher ctx key!\n");
-		free(ctx);
-		goto free_ctx_id;
+		goto free_ctx;
 	}
 	ctx->akey = setup->br.alloc(setup->br.usr, MAX_AEAD_KEY_SIZE);
 	if (!ctx->akey) {
 		WD_ERR("fail to alloc authenticate ctx key!\n");
 		setup->br.free(setup->br.usr, ctx->ckey);
-		goto free_ctx;
+		goto free_ctx_ckey;
 	}
 
 	ctx->iv_blk_size = get_iv_block_size(setup->cmode);
@@ -244,11 +243,16 @@ void *wcrypto_create_aead_ctx(struct wd_queue *q,
 		sizeof(struct wcrypto_aead_cookie), WD_CTX_MSG_NUM);
 	if (ret) {
 		WD_ERR("fail to init cookie pool!\n");
-		goto free_ctx;
+		goto free_ctx_akey;
 	}
 	init_aead_cookie(ctx, setup);
 
 	return ctx;
+
+free_ctx_akey:
+	setup->br.free(setup->br.usr, ctx->akey);
+free_ctx_ckey:
+	setup->br.free(setup->br.usr, ctx->ckey);
 free_ctx:
 	free(ctx);
 free_ctx_id:
diff --git a/v1/wd_cipher.c b/v1/wd_cipher.c
index 8bf71be..921c464 100644
--- a/v1/wd_cipher.c
+++ b/v1/wd_cipher.c
@@ -217,11 +217,14 @@ void *wcrypto_create_cipher_ctx(struct wd_queue *q,
 		sizeof(struct wcrypto_cipher_cookie), WD_CTX_MSG_NUM);
 	if (ret) {
 		WD_ERR("fail to init cookie pool!\n");
-		goto free_ctx;
+		goto free_ctx_key;
 	}
 	init_cipher_cookie(ctx, setup);
 
 	return ctx;
+
+free_ctx_key:
+	setup->br.free(setup->br.usr, ctx->key);
 free_ctx:
 	free(ctx);
 free_ctx_id:
diff --git a/v1/wd_dh.c b/v1/wd_dh.c
index f83dd91..66f1081 100644
--- a/v1/wd_dh.c
+++ b/v1/wd_dh.c
@@ -174,10 +174,12 @@ void *wcrypto_create_dh_ctx(struct wd_queue *q, struct wcrypto_dh_ctx_setup *set
 
 	ret = wcrypto_init_dh_cookie(ctx);
 	if (ret)
-		goto free_ctx;
+		goto free_ctx_gdata;
 
 	return ctx;
 
+free_ctx_gdata:
+	setup->br.free(setup->br.usr, ctx->g.data);
 free_ctx:
 	free(ctx);
 free_ctx_id:
diff --git a/v1/wd_digest.c b/v1/wd_digest.c
index 2179415..c84e71d 100644
--- a/v1/wd_digest.c
+++ b/v1/wd_digest.c
@@ -199,11 +199,15 @@ void *wcrypto_create_digest_ctx(struct wd_queue *q,
 		sizeof(struct wcrypto_digest_cookie), WD_CTX_MSG_NUM);
 	if (ret) {
 		WD_ERR("fail to init cookie pool!\n");
-		goto free_ctx;
+		goto free_ctx_key;
 	}
 	init_digest_cookie(ctx, setup);
 
 	return ctx;
+
+free_ctx_key:
+	if (setup->mode == WCRYPTO_DIGEST_HMAC)
+		setup->br.free(setup->br.usr, ctx->key);
 free_ctx:
 	free(ctx);
 free_ctx_id:
-- 
2.27.0

