1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-23 23:19:09 +00:00

Fix some potential resource leaks in case of an error

This commit is contained in:
jsteube 2016-11-20 23:15:54 +01:00
parent ea4fd1de50
commit 90b9d5c12c
10 changed files with 108 additions and 41 deletions

View File

@ -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
{

View File

@ -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;

View File

@ -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++)

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}