Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Unified Diff: src/platform/vboot_reference/crypto/rsa_utility.c

Issue 858008: VBoot Reference: Fix many memory leaks. (Closed)
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/platform/vboot_reference/crypto/rsa_utility.c
diff --git a/src/platform/vboot_reference/crypto/rsa_utility.c b/src/platform/vboot_reference/crypto/rsa_utility.c
index e0071c16d48b53d7e67bfd76fbd68002503b333f..9b419d03b66e81ef1e47f399c975bf3168e0bdb4 100644
--- a/src/platform/vboot_reference/crypto/rsa_utility.c
+++ b/src/platform/vboot_reference/crypto/rsa_utility.c
@@ -20,6 +20,13 @@ int RSAProcessedKeySize(int algorithm) {
return (2 * key_len + sizeof(int) + sizeof(uint32_t));
}
+RSAPublicKey* RSAPublicKeyNew(void) {
+ RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
+ key->n = NULL;
+ key->rr = NULL;
+ return key;
+}
+
void RSAPublicKeyFree(RSAPublicKey* key) {
if (key) {
Free(key->n);
@@ -29,15 +36,24 @@ void RSAPublicKeyFree(RSAPublicKey* key) {
}
RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) {
- RSAPublicKey* key = (RSAPublicKey*) Malloc(sizeof(RSAPublicKey));
+ RSAPublicKey* key = RSAPublicKeyNew();
MemcpyState st;
int key_len;
st.remaining_buf = (uint8_t*) buf;
st.remaining_len = len;
-
StatefulMemcpy(&st, &key->len, sizeof(key->len));
key_len = key->len * sizeof(uint32_t); /* key length in bytes. */
+
+ /* Sanity Check the key length. */
+ if (RSA1024NUMBYTES != key_len &&
+ RSA2048NUMBYTES != key_len &&
+ RSA4096NUMBYTES != key_len &&
+ RSA8192NUMBYTES != key_len) {
+ RSAPublicKeyFree(key);
+ return NULL;
+ }
+
key->n = (uint32_t*) Malloc(key_len);
key->rr = (uint32_t*) Malloc(key_len);
@@ -45,9 +61,7 @@ RSAPublicKey* RSAPublicKeyFromBuf(const uint8_t* buf, int len) {
StatefulMemcpy(&st, key->n, key_len);
StatefulMemcpy(&st, key->rr, key_len);
if (st.remaining_len != 0) { /* Underrun or overrun. */
- Free(key->n);
- Free(key->rr);
- Free(key);
+ RSAPublicKeyFree(key);
return NULL;
}

Powered by Google App Engine
This is Rietveld 408576698