On failure to allocate memory, free the public keys list. Signed-off-by: Mimi Zohar <zohar@xxxxxxxxxxxxx> --- src/imaevm.h | 1 + src/libimaevm.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/imaevm.h b/src/imaevm.h index 18d7b0e447e1..828976e52881 100644 --- a/src/imaevm.h +++ b/src/imaevm.h @@ -249,6 +249,7 @@ uint32_t imaevm_read_keyid(const char *certfile); int sign_hash(const char *algo, const unsigned char *hash, int size, const char *keyfile, const char *keypass, unsigned char *sig); int verify_hash(const char *file, const unsigned char *hash, int size, unsigned char *sig, int siglen); int ima_verify_signature(const char *file, unsigned char *sig, int siglen, unsigned char *digest, int digestlen); +void free_public_keys(void *public_keys); void init_public_keys(const char *keyfiles); int imaevm_hash_algo_from_sig(unsigned char *sig); const char *imaevm_hash_algo_by_id(int algo); diff --git a/src/libimaevm.c b/src/libimaevm.c index 117a1a72b60c..74e9d09b1f05 100644 --- a/src/libimaevm.c +++ b/src/libimaevm.c @@ -399,11 +399,25 @@ static EVP_PKEY *find_keyid(uint32_t keyid) return 0; } +void free_public_keys(void *public_keys) +{ + struct public_key_entry *entry = public_keys, *next; + + while (entry != NULL) { + next = entry->next; + if (entry->key) + free(entry->key); + free(entry); + entry = next; + } +} + void init_public_keys(const char *keyfiles) { struct public_key_entry *entry; char *tmp_keyfiles, *keyfiles_free; char *keyfile; + int err = 0; int i = 1; tmp_keyfiles = strdup(keyfiles); @@ -417,6 +431,7 @@ void init_public_keys(const char *keyfiles) entry = malloc(sizeof(struct public_key_entry)); if (!entry) { perror("malloc"); + err = -ENOMEM; break; } @@ -433,6 +448,8 @@ void init_public_keys(const char *keyfiles) g_public_keys = entry; } free(keyfiles_free); + if (err < 0) + free_public_keys(g_public_keys); } /* -- 2.39.3