From 4b871646febcf001b70c82f4d1186650086e6119 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Wed, 10 Jul 2019 17:27:45 +0200 Subject: [PATCH] fix some memory leaks --- src/benchmark.c | 2 ++ src/brain.c | 36 ++++++++++++++++++++++++++++++++++++ src/dispatch.c | 7 ++++++- src/keyboard_layout.c | 4 ++-- src/main.c | 33 ++++++++++++++++++++++++++++----- src/restore.c | 2 ++ 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/benchmark.c b/src/benchmark.c index 92b0fcd44..78f7d9f08 100644 --- a/src/benchmark.c +++ b/src/benchmark.c @@ -71,6 +71,8 @@ int benchmark_next (hashcat_ctx_t *hashcat_ctx) cur = hash_mode + 1; + hcfree (modulefile); + return hash_mode; } } diff --git a/src/brain.c b/src/brain.c index 1bba33a5e..94840dc9e 100644 --- a/src/brain.c +++ b/src/brain.c @@ -2996,6 +2996,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "socket: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3006,6 +3008,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "setsockopt: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3013,6 +3017,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "setsockopt: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } #else @@ -3054,6 +3060,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s: %s\n", listen_host, gai_strerror (rc_getaddrinfo)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } } @@ -3062,6 +3070,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "bind: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3069,6 +3079,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "listen: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3078,6 +3090,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3090,11 +3104,15 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } if (brain_server_read_hash_dumps (brain_server_dbs, ".") == false) { + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3105,11 +3123,15 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } if (brain_server_read_attack_dumps (brain_server_dbs, ".") == false) { + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3119,6 +3141,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3135,6 +3159,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3163,6 +3189,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "%s\n", MSG_ENOMEM); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3185,6 +3213,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br { brain_logging (stderr, 0, "signal: %s\n", strerror (errno)); + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3258,11 +3288,15 @@ int brain_server (const char *listen_host, const int listen_port, const char *br if (brain_server_write_hash_dumps (brain_server_dbs, ".") == false) { + if (brain_password == NULL) hcfree (auth_password); + return -1; } if (brain_server_write_attack_dumps (brain_server_dbs, ".") == false) { + if (brain_password == NULL) hcfree (auth_password); + return -1; } @@ -3287,6 +3321,8 @@ int brain_server (const char *listen_host, const int listen_port, const char *br hcfree (brain_server_client_options); + if (brain_password == NULL) hcfree (auth_password); + close (server_fd); #if defined (_WIN) diff --git a/src/dispatch.c b/src/dispatch.c index 1b197e5f7..f330f690f 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -168,7 +168,12 @@ static int calc_stdin (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_par iconv_ctx = iconv_open (user_options->encoding_to, user_options->encoding_from); - if (iconv_ctx == (iconv_t) -1) return -1; + if (iconv_ctx == (iconv_t) -1) + { + hcfree (buf); + + return -1; + } iconv_tmp = (char *) hcmalloc (HCBUFSIZ_TINY); } diff --git a/src/keyboard_layout.c b/src/keyboard_layout.c index 2e6927557..d59998a9a 100644 --- a/src/keyboard_layout.c +++ b/src/keyboard_layout.c @@ -20,12 +20,12 @@ static int sort_by_src_len (const void *p1, const void *p2) bool initialize_keyboard_layout_mapping (const char *filename, keyboard_layout_mapping_t *keyboard_layout_mapping, int *keyboard_layout_mapping_cnt) { - char *line_buf = (char *) hcmalloc (HCBUFSIZ_LARGE); - HCFILE fp; if (hc_fopen (&fp, filename, "r") == false) return false; + char *line_buf = (char *) hcmalloc (HCBUFSIZ_LARGE); + int maps_cnt = 0; while (!hc_feof (&fp)) diff --git a/src/main.c b/src/main.c index d3822b449..ad68870d7 100644 --- a/src/main.c +++ b/src/main.c @@ -1062,8 +1062,12 @@ int main (int argc, char **argv) const int rc_hashcat_init = hashcat_init (hashcat_ctx, event); - if (rc_hashcat_init == -1) return -1; + if (rc_hashcat_init == -1) + { + hcfree (hashcat_ctx); + return -1; + } // install and shared folder need to be set to recognize "make install" use const char *install_folder = NULL; @@ -1081,17 +1085,32 @@ int main (int argc, char **argv) const int rc_options_init = user_options_init (hashcat_ctx); - if (rc_options_init == -1) return -1; + if (rc_options_init == -1) + { + hcfree (hashcat_ctx); + + return -1; + } // parse commandline parameters and check them const int rc_options_getopt = user_options_getopt (hashcat_ctx, argc, argv); - if (rc_options_getopt == -1) return -1; + if (rc_options_getopt == -1) + { + hcfree (hashcat_ctx); + + return -1; + } const int rc_options_sanity = user_options_sanity (hashcat_ctx); - if (rc_options_sanity == -1) return -1; + if (rc_options_sanity == -1) + { + hcfree (hashcat_ctx); + + return -1; + } // some early exits @@ -1102,6 +1121,8 @@ int main (int argc, char **argv) { const int rc = brain_server (user_options->brain_host, user_options->brain_port, user_options->brain_password, user_options->brain_session_whitelist); + hcfree (hashcat_ctx); + return rc; } #endif @@ -1110,6 +1131,8 @@ int main (int argc, char **argv) { printf ("%s\n", VERSION_TAG); + hcfree (hashcat_ctx); + return 0; } @@ -1167,7 +1190,7 @@ int main (int argc, char **argv) hashcat_destroy (hashcat_ctx); - free (hashcat_ctx); + hcfree (hashcat_ctx); return rc_final; } diff --git a/src/restore.c b/src/restore.c index 3800a361b..f5358f580 100644 --- a/src/restore.c +++ b/src/restore.c @@ -106,6 +106,8 @@ static int read_restore (hashcat_ctx_t *hashcat_ctx) hc_fclose (&fp); + hcfree (buf); + return -1; }