From 1afb7e20db8876b22b7c774d0e2e01f61f5d29a2 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 5 Jan 2017 13:49:07 -0700 Subject: [PATCH] fix for freeing copy of mpi in the case of not using fastmath --- src/ssl.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ssl.c b/src/ssl.c index 4d12a13bd..8dc27190d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -16502,6 +16502,10 @@ void wolfSSL_RSA_free(WOLFSSL_RSA* rsa) * declared and then not used. */ #if !defined(NO_ASN) || !defined(NO_DSA) || defined(HAVE_ECC) || \ (!defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)) +/* when calling SetIndividualExternal, mpi should be cleared by caller if no + * longer used. ie mp_clear(mpi). This is to free data when fastmath is + * disabled since a copy of mpi is made by this function and placed into bn. + */ static int SetIndividualExternal(WOLFSSL_BIGNUM** bn, mp_int* mpi) { WOLFSSL_MSG("Entering SetIndividualExternal"); @@ -16570,18 +16574,23 @@ WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai, return NULL; } - /* SetIndividualExternal mallocs bn in the case that bn is NULL */ + /* SetIndividualExternal mallocs bn in the case that bn is NULL + * mp_clear needs called because mpi is copied and causes memory leak with + * --disable-fastmath */ if (bn == NULL) { if (SetIndividualExternal(&bn, &mpi) != SSL_SUCCESS) { wolfSSL_BN_free(bn); + mp_clear(&mpi); return NULL; } } else { if (SetIndividualExternal(&bn, &mpi) != SSL_SUCCESS) { + mp_clear(&mpi); return NULL; } } + mp_clear(&mpi); return bn; }