From c8025681d15bdfdee9cc00796b68f02f1cf85d67 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Sat, 23 Jul 2022 16:50:30 +0800
Subject: [PATCH 163/183] uadk: driver init should check ctx num

If 'i' is >= field 'ctx_num', then qp is still 0,
it will results in a dereference of a null pointer
when hisi_sec_driver_adapter use qp's member.

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 drv/hisi_comp.c | 5 +++++
 drv/hisi_hpre.c | 5 +++++
 drv/hisi_sec.c  | 5 +++++
 3 files changed, 15 insertions(+)

diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c
index 69785e9..c940c3f 100644
--- a/drv/hisi_comp.c
+++ b/drv/hisi_comp.c
@@ -780,6 +780,11 @@ static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv)
 	handle_t h_ctx;
 	int i;
 
+	if (!config->ctx_num) {
+		WD_ERR("invalid: zip init config ctx num is 0!\n");
+		return -WD_EINVAL;
+	}
+
 	memcpy(&zip_ctx->config, config, sizeof(struct wd_ctx_config_internal));
 	/* allocate qp for each context */
 	for (i = 0; i < config->ctx_num; i++) {
diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c
index d503d84..4c6e532 100644
--- a/drv/hisi_hpre.c
+++ b/drv/hisi_hpre.c
@@ -447,6 +447,11 @@ static int hpre_init(struct wd_ctx_config_internal *config, void *priv, const ch
 	handle_t h_ctx, h_qp;
 	int i, j;
 
+	if (!config->ctx_num) {
+		WD_ERR("invalid: hpre init config ctx num is 0!\n");
+		return -WD_EINVAL;
+	}
+
 	memcpy(&hpre_ctx->config, config, sizeof(*config));
 
 	/* allocate qp for each context */
diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c
index 5865ffb..858dbb6 100644
--- a/drv/hisi_sec.c
+++ b/drv/hisi_sec.c
@@ -2341,6 +2341,11 @@ int hisi_sec_init(struct wd_ctx_config_internal *config, void *priv)
 	handle_t h_ctx;
 	int i, j;
 
+	if (!config->ctx_num) {
+		WD_ERR("invalid: sec init config ctx num is 0!\n");
+		return -WD_EINVAL;
+	}
+
 	qm_priv.sqe_size = sizeof(struct hisi_sec_sqe);
 	/* allocate qp for each context */
 	for (i = 0; i < config->ctx_num; i++) {
-- 
2.27.0

