From: Wenkai Lin linwenkai6@hisilicon.com
SM4 CE driver set priv size to 0, allocating memory is not allowed, so set a proper priv size and add size check.
Signed-off-by: Wenkai Lin linwenkai6@hisilicon.com --- drv/isa_ce_sm4.c | 5 ++++- drv/isa_ce_sm4.h | 5 +++++ wd_util.c | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/drv/isa_ce_sm4.c b/drv/isa_ce_sm4.c index fbbb2b7..7c6d983 100644 --- a/drv/isa_ce_sm4.c +++ b/drv/isa_ce_sm4.c @@ -34,8 +34,11 @@ static int isa_ce_init(void *conf, void *priv) { struct wd_ctx_config_internal *config = conf; + struct sm4_ce_drv_ctx *sctx = priv;
config->epoll_en = 0; + memcpy(&sctx->config, config, sizeof(struct wd_ctx_config_internal)); + return 0; }
@@ -423,7 +426,7 @@ static int cipher_recv(handle_t ctx, void *msg) .alg_name = (ce_alg_name),\ .calc_type = UADK_ALG_CE_INSTR,\ .priority = 200,\ - .priv_size = 0,\ + .priv_size = sizeof(struct sm4_ce_drv_ctx),\ .op_type_num = 1,\ .fallback = 0,\ .init = isa_ce_init,\ diff --git a/drv/isa_ce_sm4.h b/drv/isa_ce_sm4.h index 9ae027d..a80224f 100644 --- a/drv/isa_ce_sm4.h +++ b/drv/isa_ce_sm4.h @@ -6,6 +6,7 @@
#pragma once #include <stdint.h> +#include "wd_alg_common.h"
#ifdef __cplusplus extern "C" { @@ -17,6 +18,10 @@ struct SM4_KEY { __u32 rk[SM4_KEY_SCHEDULE]; };
+struct sm4_ce_drv_ctx { + struct wd_ctx_config_internal config; +}; +
void sm4_v8_set_encrypt_key(const unsigned char *userKey, struct SM4_KEY *key); void sm4_v8_set_decrypt_key(const unsigned char *userKey, struct SM4_KEY *key); diff --git a/wd_util.c b/wd_util.c index fb58167..dc627d8 100644 --- a/wd_util.c +++ b/wd_util.c @@ -1935,6 +1935,11 @@ int wd_alg_init_driver(struct wd_ctx_config_internal *config, { int ret;
+ if (!driver->priv_size) { + WD_ERR("invalid: driver priv ctx size is zero!\n"); + return -WD_EINVAL; + } + if (!driver->init) { driver->fallback = 0; WD_ERR("driver have no init interface.\n");