From f15516b66eb92d0c9a90532eb23c96e93a14ee10 Mon Sep 17 00:00:00 2001
From: Junchong Pan <panjunchong@hisilicon.com>
Date: Wed, 2 Mar 2022 07:25:49 +0000
Subject: [PATCH 078/109] uadk: modify param verification

When config->ctxs[0] is NULL, take ctxs[0].ctx
may cause a crash, so check config->ctxs[0] when
performing init param check.

Signed-off-by: Junchong Pan <panjunchong@hisilicon.com>
Signed-off-by: Zhiqi Song <songzhiqi1@huawei.com>
---
 v1/wd_sgl.c | 4 ++--
 wd_aead.c   | 2 +-
 wd_cipher.c | 2 +-
 wd_comp.c   | 5 ++---
 wd_dh.c     | 2 +-
 wd_digest.c | 2 +-
 wd_ecc.c    | 2 +-
 7 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/v1/wd_sgl.c b/v1/wd_sgl.c
index 97edbf4..9995d69 100644
--- a/v1/wd_sgl.c
+++ b/v1/wd_sgl.c
@@ -516,8 +516,8 @@ void wd_free_sgl(void *pool, struct wd_sgl *sgl)
 	struct wd_sgl *next;
 	int i;
 
-	if (unlikely(!p || !sgl)) {
-		WD_ERR("pool or sgl is null!\n");
+	if (unlikely(!p || !sgl || !p->sgl_pool)) {
+		WD_ERR("pool or sgl or p->sgl_pool is null!\n");
 		return;
 	}
 
diff --git a/wd_aead.c b/wd_aead.c
index d8581db..a78f152 100644
--- a/wd_aead.c
+++ b/wd_aead.c
@@ -392,7 +392,7 @@ static int aead_param_check(struct wd_aead_sess *sess,
 
 static int aead_init_check(struct wd_ctx_config *config, struct wd_sched *sched)
 {
-	if (!config || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("wd aead config or sched is NULL!\n");
 		return -WD_EINVAL;
 	}
diff --git a/wd_cipher.c b/wd_cipher.c
index 0b29fb5..8daac0f 100644
--- a/wd_cipher.c
+++ b/wd_cipher.c
@@ -157,7 +157,7 @@ static int cipher_key_len_check(struct wd_cipher_sess *sess, __u32 length)
 static int cipher_init_check(struct wd_ctx_config *config,
 			     struct wd_sched *sched)
 {
-	if (!config || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("wd cipher config or sched is NULL!\n");
 		return -WD_EINVAL;
 	}
diff --git a/wd_comp.c b/wd_comp.c
index 722666d..886e6fc 100644
--- a/wd_comp.c
+++ b/wd_comp.c
@@ -87,7 +87,7 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched)
 	void *priv;
 	int ret;
 
-	if (!config || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("invalid params, config or sched is NULL!\n");
 		return -WD_EINVAL;
 	}
@@ -223,8 +223,7 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count)
 		req = &msg->req;
 		req->src_len = msg->in_cons;
 		req->dst_len = msg->produced;
-		if (req->cb)
-			req->cb(req, req->cb_param);
+		req->cb(req, req->cb_param);
 
 		/* free msg cache to msg_pool */
 		wd_put_msg_to_pool(&wd_comp_setting.pool, idx, resp_msg.tag);
diff --git a/wd_dh.c b/wd_dh.c
index b361d5d..841a126 100644
--- a/wd_dh.c
+++ b/wd_dh.c
@@ -80,7 +80,7 @@ void wd_dh_set_driver(struct wd_dh_driver *drv)
 
 static int param_check(struct wd_ctx_config *config, struct wd_sched *sched)
 {
-	if (!config || !config->ctxs[0].ctx || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("config or sched NULL\n");
 		return -WD_EINVAL;
 	}
diff --git a/wd_digest.c b/wd_digest.c
index 1c05851..21c3876 100644
--- a/wd_digest.c
+++ b/wd_digest.c
@@ -154,7 +154,7 @@ void wd_digest_free_sess(handle_t h_sess)
 
 static int digest_init_check(struct wd_ctx_config *config, struct wd_sched *sched)
 {
-	if (!config || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("failed to check input param!\n");
 		return -WD_EINVAL;
 	}
diff --git a/wd_ecc.c b/wd_ecc.c
index 89566ea..3232e67 100644
--- a/wd_ecc.c
+++ b/wd_ecc.c
@@ -134,7 +134,7 @@ void wd_ecc_set_driver(struct wd_ecc_driver *drv)
 
 static int init_param_check(struct wd_ctx_config *config, struct wd_sched *sched)
 {
-	if (!config || !config->ctxs[0].ctx || !sched) {
+	if (!config || !config->ctxs || !config->ctxs[0].ctx || !sched) {
 		WD_ERR("config or sched NULL\n");
 		return -WD_EINVAL;
 	}
-- 
2.27.0

