From d1ec010660905ae0b99d783737350ccc08b37b16 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 9 Oct 2012 16:27:28 +0200 Subject: [PATCH] Fix memory leaks found by valgrind. --- src/net_setup.c | 4 ++++ src/net_socket.c | 3 +++ src/openssl/cipher.c | 6 ++---- src/openssl/digest.c | 3 +-- src/protocol_key.c | 5 +++++ src/sptps.c | 11 +++++------ 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/net_setup.c b/src/net_setup.c index 5d4e915..00273cf 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -680,6 +680,8 @@ static bool setup_myself(void) { return false; } + free(cipher); + regenerate_key(); /* Check if we want to use message authentication codes... */ @@ -700,6 +702,8 @@ static bool setup_myself(void) { return false; } + free(digest); + /* Compression */ if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) { diff --git a/src/net_socket.c b/src/net_socket.c index df6f3c0..36da2d0 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -571,6 +571,9 @@ static void free_outgoing(outgoing_t *outgoing) { if(outgoing->ai) freeaddrinfo(outgoing->ai); + if(outgoing->config_tree) + exit_configuration(&outgoing->config_tree); + if(outgoing->name) free(outgoing->name); diff --git a/src/openssl/cipher.c b/src/openssl/cipher.c index 1ca15ab..abd652c 100644 --- a/src/openssl/cipher.c +++ b/src/openssl/cipher.c @@ -65,10 +65,8 @@ bool cipher_open_blowfish_ofb(cipher_t *cipher) { void cipher_close(cipher_t *cipher) { EVP_CIPHER_CTX_cleanup(&cipher->ctx); - if(cipher->counter) { - free(cipher->counter); - cipher->counter = 0; - } + free(cipher->counter); + cipher->counter = NULL; } size_t cipher_keylength(const cipher_t *cipher) { diff --git a/src/openssl/digest.c b/src/openssl/digest.c index 3159e7a..e88274e 100644 --- a/src/openssl/digest.c +++ b/src/openssl/digest.c @@ -78,8 +78,7 @@ bool digest_set_key(digest_t *digest, const void *key, size_t len) { } void digest_close(digest_t *digest) { - if(digest->key) - free(digest->key); + free(digest->key); digest->key = NULL; } diff --git a/src/protocol_key.c b/src/protocol_key.c index fb53fe1..c042c4b 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -258,6 +258,9 @@ bool send_ans_key(node_t *to) { size_t keylen = cipher_keylength(&myself->incipher); char key[keylen * 2 + 1]; + cipher_close(&to->incipher); + digest_close(&to->indigest); + cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher)); digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest)); to->incompression = myself->incompression; @@ -345,6 +348,8 @@ bool ans_key_h(connection_t *c, const char *request) { } /* Don't use key material until every check has passed. */ + cipher_close(&from->outcipher); + digest_close(&from->outdigest); from->status.validkey = false; if(compression < 0 || compression > 11) { diff --git a/src/sptps.c b/src/sptps.c index 10d6e04..ed1d952 100644 --- a/src/sptps.c +++ b/src/sptps.c @@ -627,18 +627,17 @@ bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_ // Stop a SPTPS session. bool sptps_stop(sptps_t *s) { // Clean up any resources. + cipher_close(&s->incipher); + cipher_close(&s->outcipher); + digest_close(&s->indigest); + digest_close(&s->outdigest); ecdh_free(&s->ecdh); free(s->inbuf); - s->inbuf = NULL; free(s->mykex); - s->mykex = NULL; free(s->hiskex); - s->hiskex = NULL; free(s->key); - s->key = NULL; free(s->label); - s->label = NULL; free(s->late); - s->late = NULL; + memset(s, 0, sizeof *s); return true; } -- 2.25.1