openssl: fix EVP_PKEY_CTX memory leak
PATCH 1/3 openssl: fix EVP_PKEY_CTX memory leak
From: Antonio Quartulli <[email protected]...>
A context allocated with EVP_PKEY_CTX_new_id() must be ultimately free'd by Eng VP_PKEY_CTX_free(). Failing to do so will result in a memory leak.
This bug was discovered using GCC with "-fsanitize=address".
--- src/openvpn/crypto_openssl.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
src/openvpn/crypto_openssl.c +11 -7
@@ -1125,37 +1125,41 @@ bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len) { + bool ret = false;EVP_PKEY_CTX *pctx =EVP_PKEY_CTX_new_id (EVP_PKEY_TLS1_PRF ,NULL );if (!EVP_PKEY_derive_init (pctx)) { -return false; + goto out; }if (!EVP_PKEY_CTX_set_tls1_prf_md (pctx,EVP_md5_sha1 ())) { -return false; + goto out; }if (!EVP_PKEY_CTX_set1_tls1_prf_secret (pctx, secret, secret_len)) { -return false; + goto out; }if (!EVP_PKEY_CTX_add1_tls1_prf_seed (pctx, seed, seed_len)) { -return false; + goto out; }size_t out_len = output_len;if (!EVP_PKEY_derive (pctx, output, &out_len)) { -return false; + goto out; }if (out_len != output_len) { -return false; + goto out; } -return true; + ret = true; +out: +EVP_PKEY_CTX_free (pctx); +return ret; }#else /* if OPENSSL_VERSION_NUMBER >= 0x10100000L */ /*
Signature · 1 line
2.26.3
PATCH 2/3 openssl: avoid NULL pointer dereference
From: Antonio Quartulli <[email protected]...>
EVP_PKEY_CTX_new_id() may return NULL and for this reason we must check its return value and bail out in case of failure.
Failing to do so, may result in NULL pointer dereferece when we pass the returned pointer (NULL) to other functions.
--- src/openvpn/crypto_openssl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +6 -1
@@ -1125,8 +1125,13 @@ bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len) { - bool ret = false; EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL); +if (!pctx) + { +return false; + } + +bool ret = false;if (!EVP_PKEY_derive_init (pctx)) { goto out;
Signature · 1 line
2.26.3
PATCH 3/3 ssl: remove unneeded if block
In reply to Antonio QuartulliFrom: Antonio Quartulli <[email protected]...>
There is no need to check the result of a boolean function and then assign a constant value to a variable based on that check.
Directly assign the return value of the function to the variable.
--- src/openvpn/ssl.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
src/openvpn/ssl.c +2 -4
@@ -1614,10 +1614,8 @@ openvpn_PRF(const uint8_t *secret, }/* compute PRF */ -if (!ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, output_len)) - { - ret = false; - } + ret =ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, + output_len);buf_clear (&seed);free_buf (&seed);
Signature · 1 line
2.26.3
Re: [Openvpn-devel] [PATCH 3/3] ssl: remove unneeded if block
Hi,
src/openvpn/ssl.c openvpn_PRF(const uint8_t *secret, · line 1618
Quoted 7 lines · Antonio Quartulli
/* compute PRF */src/openvpn/ssl.c +2
-4-if (!ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, output_len)) - { - ret = false; - } + ret =ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, + output_len);
If touching this, I would move the "bool ret" declaration to here as well. Doesn't make sense to have a "bool ret = true" declaration 15 lines further up if the first use is "assign it a new value", then.
Thus,
+ bool ret = ssl_tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len,
+ output, output_len);
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Looks reasonable, matches coding style & OpenSSL documentation, and passes my client-side tests with OpenSSL 1.1.1
Your patch has been applied to the master branch.
commit 24e58164b845614c2176bc6b2a939856fd830c53
Author: Antonio Quartulli Date: Mon Apr 5 10:00:05 2021 +0200
openssl: fix EVP_PKEY_CTX memory leak
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/search?l=mid&[email protected]...
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [PATCH applied] Re: openssl: avoid NULL pointer dereference
In reply to Antonio QuartulliAccording to OpenSSL documentation, this can indeed return NULL, so catch it... (Note: as for 1/3, this code is only in master, so no need to backport to release/2.5)
Your patch has been applied to the master branch.
commit f3c7698957483e0ea0f14e712502d34c826c53ca
Author: Antonio Quartulli Date: Mon Apr 5 10:00:06 2021 +0200
openssl: avoid NULL pointer dereference
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/search?l=mid&[email protected]...
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Re: [Openvpn-devel] [PATCH 3/3] ssl: remove unneeded if block
In reply to Gert DoeringHi,
Quoted 18 lines · Gert Doering
Hi,
src/openvpn/ssl.c openvpn_PRF(const uint8_t *secret, · line 1618
Quoted 7 lines · Antonio Quartulli
/* compute PRF */src/openvpn/ssl.c +2
-4-if (!ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, output_len)) - { - ret = false; - } + ret =ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, + output_len);If touching this, I would move the "bool ret" declaration to here as well. Doesn't make sense to have a "bool ret = true" declaration 15 lines further up if the first use is "assign it a new value", then.
Thus,
Review
Quoted 2 lines
(quoted diff) +2
-0+ bool ret = ssl_tls1_PRF(BPTR(&seed), BLEN(&seed), secret, secret_len, + output, output_len);
Agreed. v2 will land soon.
Regards,
Signature · 1 line
Antonio Quartulli
PATCH v2 3/3 ssl: remove unneeded if block
From: Antonio Quartulli <[email protected]...>
There is no need to check the result of a boolean function and then assign a constant value to a variable based on that check.
Directly assign the return value of the function to the variable.
--- src/openvpn/ssl.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
src/openvpn/ssl.c +2 -5
@@ -1592,7 +1592,6 @@ openvpn_PRF(const uint8_t *secret, uint8_t *output, int output_len) { - bool ret = true;/* concatenate seed components */ struct buffer seed = alloc_buf(strlen(label)
@@ -1614,10 +1613,8 @@ openvpn_PRF(const uint8_t *secret, }/* compute PRF */ -if (!ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, output, output_len)) - { - ret = false; - } +bool ret =ssl_tls1_PRF (BPTR (&seed),BLEN (&seed), secret, secret_len, + output, output_len);buf_clear (&seed);free_buf (&seed);
Signature · 1 line
2.26.3
[Openvpn-devel] [PATCH applied] Re: ssl: remove unneeded if block
*Such* a pretty patch!
Your patch has been applied to the master branch.
commit 8af7c6b2d79b63ef5587ef1bc4ec81834905698e
Author: Antonio Quartulli Date: Mon Apr 5 14:28:27 2021 +0200
ssl: remove unneeded if block
Signed-off-by: Antonio Quartulli <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg22045.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering