From 90b9d5c12c3886d6a4ca65857e9f9543435fadb7 Mon Sep 17 00:00:00 2001 From: jsteube Date: Sun, 20 Nov 2016 23:15:54 +0100 Subject: [PATCH] Fix some potential resource leaks in case of an error --- src/debugfile.c | 10 ++++++--- src/dictstat.c | 2 ++ src/hashes.c | 3 ++- src/hwmon.c | 8 +++++++ src/loopback.c | 10 ++++++--- src/mpsp.c | 21 ++++++++++++++--- src/opencl.c | 15 ++++++++----- src/outfile.c | 10 ++++++--- src/potfile.c | 10 ++++++--- src/stdout.c | 60 ++++++++++++++++++++++++++++++++----------------- 10 files changed, 108 insertions(+), 41 deletions(-) diff --git a/src/debugfile.c b/src/debugfile.c index e7a8cf03a..8c10f8190 100644 --- a/src/debugfile.c +++ b/src/debugfile.c @@ -105,21 +105,25 @@ int debugfile_init (hashcat_ctx_t *hashcat_ctx) if (debugfile_ctx->filename) { - debugfile_ctx->fp = fopen (debugfile_ctx->filename, "ab"); + FILE *fp = fopen (debugfile_ctx->filename, "ab"); - if (debugfile_ctx->fp == NULL) + if (fp == NULL) { event_log_error (hashcat_ctx, "Could not open debug-file for writing"); return -1; } - if (lock_file (debugfile_ctx->fp) == -1) + if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", debugfile_ctx->filename, strerror (errno)); return -1; } + + debugfile_ctx->fp = fp; } else { diff --git a/src/dictstat.c b/src/dictstat.c index f4fecd547..4c347f6a3 100644 --- a/src/dictstat.c +++ b/src/dictstat.c @@ -128,6 +128,8 @@ int dictstat_write (hashcat_ctx_t *hashcat_ctx) if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", dictstat_ctx->filename, strerror (errno)); return -1; diff --git a/src/hashes.c b/src/hashes.c index 878703da3..0ffc5d9d1 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -147,12 +147,13 @@ int save_hash (hashcat_ctx_t *hashcat_ctx) if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", new_hashfile, strerror (errno)); return -1; } - u8 *out_buf = (u8 *) hcmalloc (HCBUFSIZ_LARGE); for (u32 salt_pos = 0; salt_pos < hashes->salts_cnt; salt_pos++) diff --git a/src/hwmon.c b/src/hwmon.c index a708932b2..803429671 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -120,6 +120,8 @@ static int hm_SYSFS_get_fan_speed_current (hashcat_ctx_t *hashcat_ctx, const int if (fscanf (fd_cur, "%d", &pwm1_cur) != 1) { + fclose (fd_cur); + event_log_error (hashcat_ctx, "%s: unexpected data", path_cur); return -1; @@ -140,6 +142,8 @@ static int hm_SYSFS_get_fan_speed_current (hashcat_ctx_t *hashcat_ctx, const int if (fscanf (fd_max, "%d", &pwm1_max) != 1) { + fclose (fd_max); + event_log_error (hashcat_ctx, "%s: unexpected data", path_max); return -1; @@ -223,6 +227,8 @@ static int hm_SYSFS_set_fan_speed_target (hashcat_ctx_t *hashcat_ctx, const int if (fscanf (fd_max, "%d", &pwm1_max) != 1) { + fclose (fd_max); + event_log_error (hashcat_ctx, "%s: unexpected data", path_max); return -1; @@ -283,6 +289,8 @@ static int hm_SYSFS_get_temperature_current (hashcat_ctx_t *hashcat_ctx, const i if (fscanf (fd, "%d", &temperature) != 1) { + fclose (fd); + event_log_error (hashcat_ctx, "%s: unexpected data", path); return -1; diff --git a/src/loopback.c b/src/loopback.c index 6a17d4f28..22c87f1f6 100644 --- a/src/loopback.c +++ b/src/loopback.c @@ -103,22 +103,26 @@ int loopback_write_open (hashcat_ctx_t *hashcat_ctx) snprintf (loopback_ctx->filename, HCBUFSIZ_TINY - 1, "%s/%s.%d_%u", induct_ctx->root_directory, LOOPBACK_FILE, (int) now, random_num); - loopback_ctx->fp = fopen (loopback_ctx->filename, "ab"); + FILE *fp = fopen (loopback_ctx->filename, "ab"); - if (loopback_ctx->fp == NULL) + if (fp == NULL) { event_log_error (hashcat_ctx, "%s: %s", loopback_ctx->filename, strerror (errno)); return -1; } - if (lock_file (loopback_ctx->fp) == -1) + if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", loopback_ctx->filename, strerror (errno)); return -1; } + loopback_ctx->fp = fp; + loopback_ctx->unused = true; return 0; diff --git a/src/mpsp.c b/src/mpsp.c index 9c037966c..f8650a685 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -1226,7 +1226,12 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) const int rc = mask_append (hashcat_ctx, line_buf); - if (rc == -1) return -1; + if (rc == -1) + { + fclose (mask_fp); + + return -1; + } } hcfree (line_buf); @@ -1303,7 +1308,12 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) const int rc = mask_append (hashcat_ctx, line_buf); - if (rc == -1) return -1; + if (rc == -1) + { + fclose (mask_fp); + + return -1; + } } hcfree (line_buf); @@ -1361,7 +1371,12 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) const int rc = mask_append (hashcat_ctx, line_buf); - if (rc == -1) return -1; + if (rc == -1) + { + fclose (mask_fp); + + return -1; + } } hcfree (line_buf); diff --git a/src/opencl.c b/src/opencl.c index 20cbb7332..58f57c8f6 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -338,15 +338,20 @@ static int write_kernel_binary (hashcat_ctx_t *hashcat_ctx, char *kernel_file, c return -1; } - const int rc = lock_file (fp); + if (lock_file (fp) == -1) + { + fclose (fp); - if (rc == -1) return -1; + event_log_error (hashcat_ctx, "%s: %s", kernel_file, strerror (errno)); + + return -1; + } fwrite (binary, sizeof (char), binary_size, fp); - fflush (fp); - fclose (fp); - unlock_file (fp); + fflush (fp); + + fclose (fp); } return 0; diff --git a/src/outfile.c b/src/outfile.c index d74b812a1..17917effb 100644 --- a/src/outfile.c +++ b/src/outfile.c @@ -306,22 +306,26 @@ int outfile_write_open (hashcat_ctx_t *hashcat_ctx) if (outfile_ctx->filename == NULL) return 0; - outfile_ctx->fp = fopen (outfile_ctx->filename, "ab"); + FILE *fp = fopen (outfile_ctx->filename, "ab"); - if (outfile_ctx->fp == NULL) + if (fp == NULL) { event_log_error (hashcat_ctx, "%s: %s", outfile_ctx->filename, strerror (errno)); return -1; } - if (lock_file (outfile_ctx->fp) == -1) + if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", outfile_ctx->filename, strerror (errno)); return -1; } + outfile_ctx->fp = fp; + return 0; } diff --git a/src/potfile.c b/src/potfile.c index 172de7111..c7b03f8d2 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -175,22 +175,26 @@ int potfile_write_open (hashcat_ctx_t *hashcat_ctx) if (potfile_ctx->enabled == false) return 0; - potfile_ctx->fp = fopen (potfile_ctx->filename, "ab"); + FILE *fp = fopen (potfile_ctx->filename, "ab"); - if (potfile_ctx->fp == NULL) + if (fp == NULL) { event_log_error (hashcat_ctx, "%s: %s", potfile_ctx->filename, strerror (errno)); return -1; } - if (lock_file (potfile_ctx->fp) == -1) + if (lock_file (fp) == -1) { + fclose (fp); + event_log_error (hashcat_ctx, "%s: %s", potfile_ctx->filename, strerror (errno)); return -1; } + potfile_ctx->fp = fp; + return 0; } diff --git a/src/stdout.c b/src/stdout.c index 78471f55d..1dc060fe3 100644 --- a/src/stdout.c +++ b/src/stdout.c @@ -61,24 +61,29 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, out.fp = stdout; - // i think this section can be optimized now that we have outfile_ctx - char *filename = outfile_ctx->filename; - if (filename != NULL) + if (filename) { - if ((out.fp = fopen (filename, "ab")) != NULL) - { - const int rc = lock_file (out.fp); + FILE *fp = fopen (filename, "ab"); - if (rc == -1) return -1; - } - else + if (fp == NULL) { event_log_error (hashcat_ctx, "%s: %s", filename, strerror (errno)); - out.fp = stdout; + return -1; } + + if (lock_file (fp) == -1) + { + fclose (fp); + + event_log_error (hashcat_ctx, "%s: %s", filename, strerror (errno)); + + return -1; + } + + out.fp = fp; } out.len = 0; @@ -99,7 +104,12 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, { const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw); - if (rc == -1) return -1; + if (rc == -1) + { + if (filename) fclose (out.fp); + + return -1; + } const u32 pos = device_param->innerloop_pos; @@ -128,7 +138,12 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, { const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw); - if (rc == -1) return -1; + if (rc == -1) + { + if (filename) fclose (out.fp); + + return -1; + } for (u32 il_pos = 0; il_pos < il_cnt; il_pos++) { @@ -193,7 +208,12 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, { const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw); - if (rc == -1) return -1; + if (rc == -1) + { + if (filename) fclose (out.fp); + + return -1; + } for (u32 il_pos = 0; il_pos < il_cnt; il_pos++) { @@ -225,7 +245,12 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, { const int rc = gidd_to_pw_t (hashcat_ctx, device_param, gidvid, &pw); - if (rc == -1) return -1; + if (rc == -1) + { + if (filename) fclose (out.fp); + + return -1; + } for (u32 il_pos = 0; il_pos < il_cnt; il_pos++) { @@ -254,12 +279,7 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, out_flush (&out); - if (out.fp != stdout) - { - unlock_file (out.fp); - - fclose (out.fp); - } + if (filename) fclose (out.fp); return 0; }