Skip to content

Commit 3f334aa

Browse files
committed
Fix memory management bugs in SignatureVerifier_OpenSSL
Rarely executed error paths in SignatureVerifier_OpenSSL contained several memory management bugs. To trigger these bugs requires triggering OpenSSL to fail to allocate memory in BN_new() or RSA_new().
1 parent f073d83 commit 3f334aa

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/SignatureVerifier_OpenSSL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ SignatureVerifier_OpenSSL::SignatureVerifier_OpenSSL(basic_Error & e)
8585
#if OPENSSL_VERSION_NUMBER < 0x10100000L
8686
if (this->rsa != NULL) {
8787
this->rsa->n = BN_new();
88-
if (this->rsa->n == NULL) { RSA_free(this->rsa); }
88+
if (this->rsa->n == NULL) { RSA_free(this->rsa); return; }
8989

9090
this->rsa->e = BN_new();
91-
if (this->rsa->e == NULL) { RSA_free(this->rsa); }
91+
if (this->rsa->e == NULL) { RSA_free(this->rsa); return; }
9292
}
9393
#else
9494
if (this->rsa != NULL) {
@@ -104,7 +104,7 @@ SignatureVerifier_OpenSSL::SignatureVerifier_OpenSSL(basic_Error & e)
104104
if (e != NULL) { BN_free(e); }
105105
} else {
106106
int result = RSA_set0_key(this->rsa, n, e, NULL);
107-
if (result != 1) { RSA_free(this->rsa); }
107+
if (result != 1) { BN_free(n); BN_free(e); RSA_free(this->rsa); this->rsa = NULL; }
108108
}
109109
}
110110

@@ -191,7 +191,7 @@ SignatureVerifier_OpenSSL::set_modulus_base64_(basic_Error & e, std::string cons
191191
}
192192

193193
int result = RSA_set0_key(this->rsa, n, exp, NULL);
194-
if (result != 1) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_SET0_KEY_FAILED); return; }
194+
if (result != 1) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_SET0_KEY_FAILED); BN_free(n); BN_free(exp); return; }
195195
#endif
196196
}
197197

@@ -228,7 +228,7 @@ SignatureVerifier_OpenSSL::set_exponent_base64_(basic_Error & e, std::string con
228228
}
229229

230230
int result = RSA_set0_key(this->rsa, n, exp, NULL);
231-
if (result != 1) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_SET0_KEY_FAILED); return; }
231+
if (result != 1) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_SET0_KEY_FAILED); BN_free(n); BN_free(exp); return; }
232232
#endif
233233
}
234234

0 commit comments

Comments
 (0)