[PATCH 1/2] uadk_engine/hpre: fix memory leaks in sm2

From: lizhi <lizhi206@huawei.com> fix memory leak and avoid possible double-free risk in sm2. Signed-off-by: lizhi <lizhi206@huawei.com> --- src/uadk_sm2.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index 170d320..7737292 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -407,9 +407,12 @@ static int sign_bin_to_ber(EC_KEY *ec, struct wd_dtb *r, struct wd_dtb *s, if (sltmp < 0) { fprintf(stderr, "failed to i2d_ECDSA_SIG\n"); ret = -EINVAL; - goto free_s; + /* bs and br set to e_sig, use unified interface to prevent double release. */ + goto free_sig; } + *siglen = (size_t)sltmp; + ECDSA_SIG_free(e_sig); return 0; free_s: @@ -417,7 +420,6 @@ free_s: free_r: BN_clear_free(br); free_sig: - ECDSA_SIG_set0(e_sig, NULL, NULL); ECDSA_SIG_free(e_sig); return ret; -- 2.33.0

From: Zhiqi Song <songzhiqi1@huawei.com> There is a process-level global 'cipher' corresponding to each supported nid in uadk_engine, and the initial value is NULL, uadk_e_create_cipher() will create corresponding cipher in a process. As the uadk_e_cipher() will be called multi times in multi-thread sence, we add a jugement of 'cipher' before. It is supposed to create a cipher if it is NULL. However, in the case of multi-thread sence, there may be thread contention, multiple threads may create multiple cipher objects related to the same nid, as the creating of a cipher object is slow. And the memory of the cipher object for the specified nid are released only once in a process, so there will be memory leaks. A specified nid needs to have only one cipher object in a process, so add mutex lock in creating cipher process to avoid allocating redundant cipher object. Signed-off-by: Zhiqi Song <songzhiqi1@huawei.com> --- src/uadk_cipher_adapter.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/uadk_cipher_adapter.c b/src/uadk_cipher_adapter.c index b4a7a0e..3146968 100644 --- a/src/uadk_cipher_adapter.c +++ b/src/uadk_cipher_adapter.c @@ -20,6 +20,7 @@ #define HW_SEC_V3 3 static int g_platform; +static pthread_mutex_t create_cipher_mutex = PTHREAD_MUTEX_INITIALIZER; static int cipher_hw_v2_nids[] = { NID_aes_128_cbc, @@ -164,9 +165,10 @@ int uadk_e_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int n for (i = 0; i < num_cc; i++) { if (nid == c_info[i].nid) { + pthread_mutex_lock(&create_cipher_mutex); if (c_info[i].cipher == NULL) uadk_e_create_ciphers(i); - + (void)pthread_mutex_unlock(&create_cipher_mutex); *cipher = c_info[i].cipher; return 1; } -- 2.33.0
participants (1)
-
Qi Tao