Skip to contentSkip to conversation

crypto/openssl: fix asym memory leaks

PATCH Superseded Ciara Power 3 participants 5 messages Nov 3, 2023 – Nov 13, 2023 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto/openssl: fix asym memory leaks

Numerous memory leaks were detected by ASAN
in the OpenSSL PMD asymmetric code path.

These are now fixed to free all variables allocated by OpenSSL functions such as BN_bin2bn and OSSL_PARAM_BLD_new.

Some need to exist until the op is processed,
for example the BIGNUMs associated with DSA.
The pointers for these are added to the private asym session so they can be accessed later when calling free.

Fixes 4c7ae22f1f83 ("crypto/openssl: update DSA routine with 3.0 EVP API")
Fixes c794b40c9258 ("crypto/openssl: update DH routine with 3.0 EVP API")
Fixes 3b7d638fb11f ("crypto/openssl: support asymmetric SM2")
Fixes ac42813a0a7c ("crypto/openssl: add DH and DSA asym operations")
Fixes d7bd42f6db19 ("crypto/openssl: update RSA routine with 3.0 EVP API")

---
Depends-on: patch-133837 ("crypto/openssl: fix memory leaks in asym ops")

---
 drivers/crypto/openssl/openssl_pmd_private.h |  6 ++
 drivers/crypto/openssl/rte_openssl_pmd.c     | 42 +++-------
 drivers/crypto/openssl/rte_openssl_pmd_ops.c | 81 +++++++++++---------
 3 files changed, 62 insertions(+), 67 deletions(-)
drivers/crypto/openssl/openssl_pmd_private.h +6 -0
index 1edb669dfd..334912d335 100644
@@ -190,6 +190,8 @@ struct openssl_asym_session {
 		struct dh {
 			DH *dh_key;
 			uint32_t key_op;
+			BIGNUM *p;
+			BIGNUM *g;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
 			OSSL_PARAM_BLD *param_bld_peer;
@@ -199,6 +201,10 @@ struct openssl_asym_session {
 			DSA *dsa;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
+			BIGNUM *p;
+			BIGNUM *g;
+			BIGNUM *q;
+			BIGNUM *priv_key;
 #endif
 		} s;
 		struct {
drivers/crypto/openssl/rte_openssl_pmd.c +12 -30

68 lines not on this page · raw message

drivers/crypto/openssl/rte_openssl_pmd_ops.c +44 -37

173 lines not on this page · raw message

Signature · 1 line
2.34.1
Acked-by Kai Ji on v1 1/1
#1 lore raw

________________________________
From: Power, Ciara <[email protected]>
Sent: 03 November 2023 15:45
To: [email protected] <[email protected]>
Cc: Power, Ciara <[email protected]>; Ji, Kai <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: [PATCH] crypto/openssl: fix asym memory leaks

Numerous memory leaks were detected by ASAN
in the OpenSSL PMD asymmetric code path.

These are now fixed to free all variables allocated by OpenSSL functions such as BN_bin2bn and OSSL_PARAM_BLD_new.

Some need to exist until the op is processed,
for example the BIGNUMs associated with DSA.
The pointers for these are added to the private asym session so they can be accessed later when calling free.

Fixes 4c7ae22f1f83 ("crypto/openssl: update DSA routine with 3.0 EVP API")
Fixes c794b40c9258 ("crypto/openssl: update DH routine with 3.0 EVP API")
Fixes 3b7d638fb11f ("crypto/openssl: support asymmetric SM2")
Fixes ac42813a0a7c ("crypto/openssl: add DH and DSA asym operations")
Fixes d7bd42f6db19 ("crypto/openssl: update RSA routine with 3.0 EVP API")

---
Depends-on: patch-133837 ("crypto/openssl: fix memory leaks in asym ops")
---
2.34.1

#2 lore raw
In reply to Ciara Power
Quoted 23 lines

----------------------------------------------------------------------
Numerous memory leaks were detected by ASAN
in the OpenSSL PMD asymmetric code path.

These are now fixed to free all variables allocated by OpenSSL functions such as BN_bin2bn and OSSL_PARAM_BLD_new.

Some need to exist until the op is processed,
for example the BIGNUMs associated with DSA.
The pointers for these are added to the private asym session so they can be accessed later when calling free.

Fixes 4c7ae22f1f83 ("crypto/openssl: update DSA routine with 3.0 EVP API")
Fixes c794b40c9258 ("crypto/openssl: update DH routine with 3.0 EVP API")
Fixes 3b7d638fb11f ("crypto/openssl: support asymmetric SM2")
Fixes ac42813a0a7c ("crypto/openssl: add DH and DSA asym operations")
Fixes d7bd42f6db19 ("crypto/openssl: update RSA routine with 3.0 EVP API")
Quoted 1 line

Please rebase on top of crypto tree.

#3 lore raw

PATCH v2 1/1 crypto/openssl: fix asym memory leaks

In reply to Ciara Power

Numerous memory leaks were detected by ASAN
in the OpenSSL PMD asymmetric code path.

These are now fixed to free all variables allocated by OpenSSL functions such as BN_bin2bn and OSSL_PARAM_BLD_new.

Some need to exist until the op is processed,
for example the BIGNUMs associated with DSA.
The pointers for these are added to the private asym session so they can be accessed later when calling free.

Some cases need to be treated differently if OpenSSL < 3.0. It has slightly different handling of memory, as functions such as RSA_set0_key() take over memory management of values, so the caller should not free the values.

Fixes 4c7ae22f1f83 ("crypto/openssl: update DSA routine with 3.0 EVP API")
Fixes c794b40c9258 ("crypto/openssl: update DH routine with 3.0 EVP API")
Fixes 3b7d638fb11f ("crypto/openssl: support asymmetric SM2")
Fixes ac42813a0a7c ("crypto/openssl: add DH and DSA asym operations")
Fixes d7bd42f6db19 ("crypto/openssl: update RSA routine with 3.0 EVP API")
Fixes ad149f93093e ("crypto/openssl: fix memory leaks in asym ops")

---
v2: Added a few more fixes for OpenSSL < 3.0 cases.

---
 drivers/crypto/openssl/openssl_pmd_private.h |  6 ++
 drivers/crypto/openssl/rte_openssl_pmd.c     |  1 +
 drivers/crypto/openssl/rte_openssl_pmd_ops.c | 98 +++++++++++++-------
 3 files changed, 73 insertions(+), 32 deletions(-)
drivers/crypto/openssl/openssl_pmd_private.h +6 -0
index 1edb669dfd..334912d335 100644
@@ -190,6 +190,8 @@ struct openssl_asym_session {
 		struct dh {
 			DH *dh_key;
 			uint32_t key_op;
+			BIGNUM *p;
+			BIGNUM *g;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
 			OSSL_PARAM_BLD *param_bld_peer;
@@ -199,6 +201,10 @@ struct openssl_asym_session {
 			DSA *dsa;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
+			BIGNUM *p;
+			BIGNUM *g;
+			BIGNUM *q;
+			BIGNUM *priv_key;
 #endif
 		} s;
 		struct {
drivers/crypto/openssl/rte_openssl_pmd.c +1 -0
index 9d463520ff..e8cb09defc 100644
@@ -1960,6 +1960,7 @@ process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop,
 		OSSL_PARAM_free(params);
 	EVP_PKEY_CTX_free(key_ctx);
 	EVP_PKEY_CTX_free(dsa_ctx);
+	EVP_PKEY_free(pkey);
 	return ret;
 }
 
drivers/crypto/openssl/rte_openssl_pmd_ops.c +66 -32

204 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

PATCH v3 1/1 crypto/openssl: fix asym memory leaks

In reply to Ciara Power

Numerous memory leaks were detected by ASAN
in the OpenSSL PMD asymmetric code path.

These are now fixed to free all variables allocated by OpenSSL functions such as BN_bin2bn and OSSL_PARAM_BLD_new.

Some need to exist until the op is processed,
for example the BIGNUMs associated with DSA.
The pointers for these are added to the private asym session so they can be accessed later when calling free.

Some cases need to be treated differently if OpenSSL < 3.0. It has slightly different handling of memory, as functions such as RSA_set0_key() take over memory management of values, so the caller should not free the values.

Fixes 4c7ae22f1f83 ("crypto/openssl: update DSA routine with 3.0 EVP API")
Fixes c794b40c9258 ("crypto/openssl: update DH routine with 3.0 EVP API")
Fixes 3b7d638fb11f ("crypto/openssl: support asymmetric SM2")
Fixes ac42813a0a7c ("crypto/openssl: add DH and DSA asym operations")
Fixes d7bd42f6db19 ("crypto/openssl: update RSA routine with 3.0 EVP API")
Fixes ad149f93093e ("crypto/openssl: fix memory leaks in asym ops")

---
v2: Added a few more fixes for OpenSSL < 3.0 cases.
v3: Fixed long line.

---
 drivers/crypto/openssl/openssl_pmd_private.h |  6 ++
 drivers/crypto/openssl/rte_openssl_pmd.c     |  1 +
 drivers/crypto/openssl/rte_openssl_pmd_ops.c | 99 +++++++++++++-------
 3 files changed, 74 insertions(+), 32 deletions(-)
drivers/crypto/openssl/openssl_pmd_private.h +6 -0
index 1edb669dfd..334912d335 100644
@@ -190,6 +190,8 @@ struct openssl_asym_session {
 		struct dh {
 			DH *dh_key;
 			uint32_t key_op;
+			BIGNUM *p;
+			BIGNUM *g;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
 			OSSL_PARAM_BLD *param_bld_peer;
@@ -199,6 +201,10 @@ struct openssl_asym_session {
 			DSA *dsa;
 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L)
 			OSSL_PARAM_BLD * param_bld;
+			BIGNUM *p;
+			BIGNUM *g;
+			BIGNUM *q;
+			BIGNUM *priv_key;
 #endif
 		} s;
 		struct {
drivers/crypto/openssl/rte_openssl_pmd.c +1 -0
index 9d463520ff..e8cb09defc 100644
@@ -1960,6 +1960,7 @@ process_openssl_dsa_sign_op_evp(struct rte_crypto_op *cop,
 		OSSL_PARAM_free(params);
 	EVP_PKEY_CTX_free(key_ctx);
 	EVP_PKEY_CTX_free(dsa_ctx);
+	EVP_PKEY_free(pkey);
 	return ret;
 }
 
drivers/crypto/openssl/rte_openssl_pmd_ops.c +67 -32

205 lines not on this page · raw message

Signature · 1 line
2.25.1