[04/20] crypto/qat: fix a memory leak when set encrypt key fail

Message ID tencent_28091DB36FDC914F91B8180F1EC1D3918807@qq.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series fix memory leaks in error handling |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Weiguo Li Feb. 22, 2022, 6:18 p.m. UTC
  We allocated memory for 'in', we don't free it when AES_set_encrypt_key()
fails and it will lead to memory leak.
We can move set_encrypt_key() ahead of the memory allocation to fix it.

Fixes: 1703e94ac5ce ("qat: add driver for QuickAssist devices")

Signed-off-by: Weiguo Li <liwg06@foxmail.com>
---
 drivers/crypto/qat/qat_sym_session.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
  

Comments

David Marchand June 24, 2022, 8:49 p.m. UTC | #1
On Tue, Feb 22, 2022 at 7:19 PM Weiguo Li <liwg06@foxmail.com> wrote:
>
> We allocated memory for 'in', we don't free it when AES_set_encrypt_key()
> fails and it will lead to memory leak.
> We can move set_encrypt_key() ahead of the memory allocation to fix it.

This move seems to fix the leak indeed.
But this change does not follow the pattern used in the rest of this
file and I don't feel confident enough to accept it without the driver
maintainer opinion.
  

Patch

diff --git a/drivers/crypto/qat/qat_sym_session.c b/drivers/crypto/qat/qat_sym_session.c
index 8ca475ca8b..3dc13942cb 100644
--- a/drivers/crypto/qat/qat_sym_session.c
+++ b/drivers/crypto/qat/qat_sym_session.c
@@ -1400,18 +1400,17 @@  static int qat_sym_do_precomputes(enum icp_qat_hw_auth_algo hash_alg,
 		memset(p_state_buf, 0, ICP_QAT_HW_GALOIS_H_SZ +
 				ICP_QAT_HW_GALOIS_LEN_A_SZ +
 				ICP_QAT_HW_GALOIS_E_CTR0_SZ);
+		if (AES_set_encrypt_key(auth_key, auth_keylen << 3,
+			&enc_key) != 0) {
+			return -EFAULT;
+		}
 		in = rte_zmalloc("working mem for key",
 				ICP_QAT_HW_GALOIS_H_SZ, 16);
 		if (in == NULL) {
 			QAT_LOG(ERR, "Failed to alloc memory");
 			return -ENOMEM;
 		}
-
 		memset(in, 0, ICP_QAT_HW_GALOIS_H_SZ);
-		if (AES_set_encrypt_key(auth_key, auth_keylen << 3,
-			&enc_key) != 0) {
-			return -EFAULT;
-		}
 		AES_encrypt(in, out, &enc_key);
 		*p_state_len = ICP_QAT_HW_GALOIS_H_SZ +
 				ICP_QAT_HW_GALOIS_LEN_A_SZ +