From 580477d52699b0fbce8ca8f9fa8ac1fe4caa9ac7 Mon Sep 17 00:00:00 2001 From: Zhiqi Song Date: Sat, 25 Nov 2023 16:13:20 +0800 Subject: [PATCH 69/82] uadk_engine: fixup resource management issues 1. Modify return value. 2. Use matching resource application and release functions. 3. Fix memory leak in abonormal scenarios. 4. Add null pointer check for ctrl param p2 of sm2 alg. Signed-off-by: Zhiqi Song --- src/uadk_async.c | 6 ++++-- src/uadk_sm2.c | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/uadk_async.c b/src/uadk_async.c index 870065d..342db2a 100644 --- a/src/uadk_async.c +++ b/src/uadk_async.c @@ -302,8 +302,10 @@ int async_wake_job(ASYNC_JOB *job) ret = ASYNC_WAIT_CTX_get_fd(waitctx, uadk_async_key, &efd, &custom); if (ret > 0) { - if (write(efd, &buf, sizeof(uint64_t)) == -1) + if (write(efd, &buf, sizeof(uint64_t)) == -1) { fprintf(stderr, "failed to write to fd: %d - error: %d\n", efd, errno); + return errno; + } } return ret; @@ -364,7 +366,7 @@ int async_module_init(void) if (pthread_mutex_init(&(poll_queue.async_task_mutex), NULL) < 0) return 0; - poll_queue.head = calloc(ASYNC_QUEUE_TASK_NUM, sizeof(struct async_poll_task)); + poll_queue.head = OPENSSL_malloc(ASYNC_QUEUE_TASK_NUM * sizeof(struct async_poll_task)); if (poll_queue.head == NULL) return 0; diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index 1db6e3a..84bda98 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -149,7 +149,7 @@ static int get_hash_type(int nid_hash) } static int compute_hash(const char *in, size_t in_len, - char *out, size_t out_len, void *usr) + char *out, size_t out_len, void *usr) { const EVP_MD *digest = (const EVP_MD *)usr; EVP_MD_CTX *hash = EVP_MD_CTX_new(); @@ -377,7 +377,7 @@ static int sign_bin_to_ber(EC_KEY *ec, struct wd_dtb *r, struct wd_dtb *s, e_sig = ECDSA_SIG_new(); if (!e_sig) { fprintf(stderr, "failed to ECDSA_SIG_new\n"); - return -EINVAL; + return -ENOMEM; } br = BN_bin2bn((void *)r->data, r->dsize, NULL); @@ -1200,6 +1200,7 @@ static int sm2_init(EVP_PKEY_CTX *ctx) ret = uadk_e_ecc_get_support_state(SM2_SUPPORT); if (!ret) { fprintf(stderr, "sm2 is not supported\n"); + free(smctx); return 0; } @@ -1284,6 +1285,8 @@ static int sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) } goto set_data; case EVP_PKEY_CTRL_GET_MD: + if (!p2) + return 0; *(const EVP_MD **)p2 = smctx->ctx.md; return 1; case EVP_PKEY_CTRL_SET1_ID: -- 2.25.1