From 1edb649c1f3205988ad57ec29bf335c81c40e99c Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Mon, 9 Aug 2021 18:48:20 +0300 Subject: [PATCH 01/11] Remove duplicate conditions --- src/backend.c | 115 +++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 63 deletions(-) diff --git a/src/backend.c b/src/backend.c index 3f1a91362..4f7eb7b10 100644 --- a/src/backend.c +++ b/src/backend.c @@ -161,8 +161,6 @@ static int backend_ctx_find_alias_devices (hashcat_ctx_t *hashcat_ctx) static bool is_same_device_type (const hc_device_param_t *src, const hc_device_param_t *dst) { - if (strcmp (src->device_name, dst->device_name) != 0) return false; - if (src->is_cuda != dst->is_cuda) return false; if (src->is_hip != dst->is_hip) return false; if (src->is_opencl != dst->is_opencl) return false; @@ -5281,30 +5279,27 @@ int run_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, con { CUfunction cuda_function = NULL; - if (device_param->is_cuda == true) + switch (kern_run) { - switch (kern_run) - { - case KERN_RUN_1: cuda_function = device_param->cuda_function1; break; - case KERN_RUN_12: cuda_function = device_param->cuda_function12; break; - case KERN_RUN_2P: cuda_function = device_param->cuda_function2p; break; - case KERN_RUN_2: cuda_function = device_param->cuda_function2; break; - case KERN_RUN_2E: cuda_function = device_param->cuda_function2e; break; - case KERN_RUN_23: cuda_function = device_param->cuda_function23; break; - case KERN_RUN_3: cuda_function = device_param->cuda_function3; break; - case KERN_RUN_4: cuda_function = device_param->cuda_function4; break; - case KERN_RUN_INIT2: cuda_function = device_param->cuda_function_init2; break; - case KERN_RUN_LOOP2P: cuda_function = device_param->cuda_function_loop2p; break; - case KERN_RUN_LOOP2: cuda_function = device_param->cuda_function_loop2; break; - case KERN_RUN_AUX1: cuda_function = device_param->cuda_function_aux1; break; - case KERN_RUN_AUX2: cuda_function = device_param->cuda_function_aux2; break; - case KERN_RUN_AUX3: cuda_function = device_param->cuda_function_aux3; break; - case KERN_RUN_AUX4: cuda_function = device_param->cuda_function_aux4; break; - } - - if (hc_cuFuncSetAttribute (hashcat_ctx, cuda_function, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, dynamic_shared_mem) == -1) return -1; + case KERN_RUN_1: cuda_function = device_param->cuda_function1; break; + case KERN_RUN_12: cuda_function = device_param->cuda_function12; break; + case KERN_RUN_2P: cuda_function = device_param->cuda_function2p; break; + case KERN_RUN_2: cuda_function = device_param->cuda_function2; break; + case KERN_RUN_2E: cuda_function = device_param->cuda_function2e; break; + case KERN_RUN_23: cuda_function = device_param->cuda_function23; break; + case KERN_RUN_3: cuda_function = device_param->cuda_function3; break; + case KERN_RUN_4: cuda_function = device_param->cuda_function4; break; + case KERN_RUN_INIT2: cuda_function = device_param->cuda_function_init2; break; + case KERN_RUN_LOOP2P: cuda_function = device_param->cuda_function_loop2p; break; + case KERN_RUN_LOOP2: cuda_function = device_param->cuda_function_loop2; break; + case KERN_RUN_AUX1: cuda_function = device_param->cuda_function_aux1; break; + case KERN_RUN_AUX2: cuda_function = device_param->cuda_function_aux2; break; + case KERN_RUN_AUX3: cuda_function = device_param->cuda_function_aux3; break; + case KERN_RUN_AUX4: cuda_function = device_param->cuda_function_aux4; break; } + if (hc_cuFuncSetAttribute (hashcat_ctx, cuda_function, CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, dynamic_shared_mem) == -1) return -1; + if (kernel_threads == 0) kernel_threads = 1; num_elements = CEILDIV (num_elements, kernel_threads); @@ -5380,30 +5375,27 @@ int run_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, con { hipFunction_t hip_function = NULL; - if (device_param->is_hip == true) + switch (kern_run) { - switch (kern_run) - { - case KERN_RUN_1: hip_function = device_param->hip_function1; break; - case KERN_RUN_12: hip_function = device_param->hip_function12; break; - case KERN_RUN_2P: hip_function = device_param->hip_function2p; break; - case KERN_RUN_2: hip_function = device_param->hip_function2; break; - case KERN_RUN_2E: hip_function = device_param->hip_function2e; break; - case KERN_RUN_23: hip_function = device_param->hip_function23; break; - case KERN_RUN_3: hip_function = device_param->hip_function3; break; - case KERN_RUN_4: hip_function = device_param->hip_function4; break; - case KERN_RUN_INIT2: hip_function = device_param->hip_function_init2; break; - case KERN_RUN_LOOP2P: hip_function = device_param->hip_function_loop2p; break; - case KERN_RUN_LOOP2: hip_function = device_param->hip_function_loop2; break; - case KERN_RUN_AUX1: hip_function = device_param->hip_function_aux1; break; - case KERN_RUN_AUX2: hip_function = device_param->hip_function_aux2; break; - case KERN_RUN_AUX3: hip_function = device_param->hip_function_aux3; break; - case KERN_RUN_AUX4: hip_function = device_param->hip_function_aux4; break; - } - - //if (hc_hipFuncSetAttribute (hashcat_ctx, hip_function, HIP_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, dynamic_shared_mem) == -1) return -1; + case KERN_RUN_1: hip_function = device_param->hip_function1; break; + case KERN_RUN_12: hip_function = device_param->hip_function12; break; + case KERN_RUN_2P: hip_function = device_param->hip_function2p; break; + case KERN_RUN_2: hip_function = device_param->hip_function2; break; + case KERN_RUN_2E: hip_function = device_param->hip_function2e; break; + case KERN_RUN_23: hip_function = device_param->hip_function23; break; + case KERN_RUN_3: hip_function = device_param->hip_function3; break; + case KERN_RUN_4: hip_function = device_param->hip_function4; break; + case KERN_RUN_INIT2: hip_function = device_param->hip_function_init2; break; + case KERN_RUN_LOOP2P: hip_function = device_param->hip_function_loop2p; break; + case KERN_RUN_LOOP2: hip_function = device_param->hip_function_loop2; break; + case KERN_RUN_AUX1: hip_function = device_param->hip_function_aux1; break; + case KERN_RUN_AUX2: hip_function = device_param->hip_function_aux2; break; + case KERN_RUN_AUX3: hip_function = device_param->hip_function_aux3; break; + case KERN_RUN_AUX4: hip_function = device_param->hip_function_aux4; break; } + //if (hc_hipFuncSetAttribute (hashcat_ctx, hip_function, HIP_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, dynamic_shared_mem) == -1) return -1; + if (kernel_threads == 0) kernel_threads = 1; num_elements = CEILDIV (num_elements, kernel_threads); @@ -5477,26 +5469,23 @@ int run_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, con { cl_kernel opencl_kernel = NULL; - if (device_param->is_opencl == true) + switch (kern_run) { - switch (kern_run) - { - case KERN_RUN_1: opencl_kernel = device_param->opencl_kernel1; break; - case KERN_RUN_12: opencl_kernel = device_param->opencl_kernel12; break; - case KERN_RUN_2P: opencl_kernel = device_param->opencl_kernel2p; break; - case KERN_RUN_2: opencl_kernel = device_param->opencl_kernel2; break; - case KERN_RUN_2E: opencl_kernel = device_param->opencl_kernel2e; break; - case KERN_RUN_23: opencl_kernel = device_param->opencl_kernel23; break; - case KERN_RUN_3: opencl_kernel = device_param->opencl_kernel3; break; - case KERN_RUN_4: opencl_kernel = device_param->opencl_kernel4; break; - case KERN_RUN_INIT2: opencl_kernel = device_param->opencl_kernel_init2; break; - case KERN_RUN_LOOP2P: opencl_kernel = device_param->opencl_kernel_loop2p; break; - case KERN_RUN_LOOP2: opencl_kernel = device_param->opencl_kernel_loop2; break; - case KERN_RUN_AUX1: opencl_kernel = device_param->opencl_kernel_aux1; break; - case KERN_RUN_AUX2: opencl_kernel = device_param->opencl_kernel_aux2; break; - case KERN_RUN_AUX3: opencl_kernel = device_param->opencl_kernel_aux3; break; - case KERN_RUN_AUX4: opencl_kernel = device_param->opencl_kernel_aux4; break; - } + case KERN_RUN_1: opencl_kernel = device_param->opencl_kernel1; break; + case KERN_RUN_12: opencl_kernel = device_param->opencl_kernel12; break; + case KERN_RUN_2P: opencl_kernel = device_param->opencl_kernel2p; break; + case KERN_RUN_2: opencl_kernel = device_param->opencl_kernel2; break; + case KERN_RUN_2E: opencl_kernel = device_param->opencl_kernel2e; break; + case KERN_RUN_23: opencl_kernel = device_param->opencl_kernel23; break; + case KERN_RUN_3: opencl_kernel = device_param->opencl_kernel3; break; + case KERN_RUN_4: opencl_kernel = device_param->opencl_kernel4; break; + case KERN_RUN_INIT2: opencl_kernel = device_param->opencl_kernel_init2; break; + case KERN_RUN_LOOP2P: opencl_kernel = device_param->opencl_kernel_loop2p; break; + case KERN_RUN_LOOP2: opencl_kernel = device_param->opencl_kernel_loop2; break; + case KERN_RUN_AUX1: opencl_kernel = device_param->opencl_kernel_aux1; break; + case KERN_RUN_AUX2: opencl_kernel = device_param->opencl_kernel_aux2; break; + case KERN_RUN_AUX3: opencl_kernel = device_param->opencl_kernel_aux3; break; + case KERN_RUN_AUX4: opencl_kernel = device_param->opencl_kernel_aux4; break; } for (u32 i = 0; i <= 23; i++) From 3b11b51113f05f32d0614cf006e91f406728e4a2 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Mon, 9 Aug 2021 18:58:04 +0300 Subject: [PATCH 02/11] Remove unnecessary hc_cuEventSynchronize() call --- src/backend.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/backend.c b/src/backend.c index 4f7eb7b10..37776f8bd 100644 --- a/src/backend.c +++ b/src/backend.c @@ -5348,8 +5348,6 @@ int run_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, con if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event2) == -1) return -1; - if (hc_cuEventSynchronize (hashcat_ctx, device_param->cuda_event1) == -1) return -1; - float exec_ms; if (hc_cuEventElapsedTime (hashcat_ctx, &exec_ms, device_param->cuda_event1, device_param->cuda_event2) == -1) return -1; From bcb4a8f391a636439b0f4304f69ed9e2eb9593dc Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Tue, 10 Aug 2021 01:49:55 +0300 Subject: [PATCH 03/11] Add clFlush() to backend_session_update_mp() and backend_session_update_mp_rl() --- src/backend.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend.c b/src/backend.c index 37776f8bd..21e73c05b 100644 --- a/src/backend.c +++ b/src/backend.c @@ -15649,6 +15649,8 @@ int backend_session_update_mp (hashcat_ctx_t *hashcat_ctx) if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_root_css_buf, CL_FALSE, 0, device_param->size_root_css, mask_ctx->root_css_buf, 0, NULL, NULL) == -1) return -1; if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_markov_css_buf, CL_FALSE, 0, device_param->size_markov_css, mask_ctx->markov_css_buf, 0, NULL, NULL) == -1) return -1; + + if (hc_clFlush (hashcat_ctx, device_param->opencl_command_queue) == -1) return -1; } } @@ -15720,6 +15722,8 @@ int backend_session_update_mp_rl (hashcat_ctx_t *hashcat_ctx, const u32 css_cnt_ if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_root_css_buf, CL_FALSE, 0, device_param->size_root_css, mask_ctx->root_css_buf, 0, NULL, NULL) == -1) return -1; if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_markov_css_buf, CL_FALSE, 0, device_param->size_markov_css, mask_ctx->markov_css_buf, 0, NULL, NULL) == -1) return -1; + + if (hc_clFlush (hashcat_ctx, device_param->opencl_command_queue) == -1) return -1; } } From ce0b4f571af1753389628408b21aca3e485ac851 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 11 Aug 2021 01:30:07 +0300 Subject: [PATCH 04/11] Fix potfile_destroy() leaks --- src/potfile.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/potfile.c b/src/potfile.c index 18a2883b5..c6c127fc4 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -128,9 +128,8 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx) if (user_options->potfile_path == NULL) { - potfile_ctx->fp.pfp = NULL; - hc_asprintf (&potfile_ctx->filename, "%s/hashcat.potfile", folder_config->profile_dir); + potfile_ctx->fp.pfp = NULL; } else { @@ -173,15 +172,13 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx) void potfile_destroy (hashcat_ctx_t *hashcat_ctx) { - hashconfig_t *hashconfig = hashcat_ctx->hashconfig; potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; if (potfile_ctx->enabled == false) return; - if (hashconfig->potfile_disable == true) return; - - hcfree (potfile_ctx->out_buf); hcfree (potfile_ctx->tmp_buf); + hcfree (potfile_ctx->out_buf); + hcfree (potfile_ctx->filename); memset (potfile_ctx, 0, sizeof (potfile_ctx_t)); } From 9c7b9253e74abf570c20da4d71e7db8bc84f6198 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 11 Aug 2021 02:17:21 +0300 Subject: [PATCH 05/11] Fix potfile not closed if opened --- src/potfile.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/potfile.c b/src/potfile.c index c6c127fc4..978247afa 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -185,10 +185,13 @@ void potfile_destroy (hashcat_ctx_t *hashcat_ctx) int potfile_read_open (hashcat_ctx_t *hashcat_ctx) { - potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; + const hashconfig_t *hashconfig = hashcat_ctx->hashconfig; + potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; if (potfile_ctx->enabled == false) return 0; + if (hashconfig->potfile_disable == true) return 0; + if (hc_fopen (&potfile_ctx->fp, potfile_ctx->filename, "rb") == false) { event_log_error (hashcat_ctx, "%s: %s", potfile_ctx->filename, strerror (errno)); @@ -201,8 +204,8 @@ int potfile_read_open (hashcat_ctx_t *hashcat_ctx) void potfile_read_close (hashcat_ctx_t *hashcat_ctx) { - hashconfig_t *hashconfig = hashcat_ctx->hashconfig; - potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; + const hashconfig_t *hashconfig = hashcat_ctx->hashconfig; + potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; if (potfile_ctx->enabled == false) return; @@ -215,10 +218,13 @@ void potfile_read_close (hashcat_ctx_t *hashcat_ctx) int potfile_write_open (hashcat_ctx_t *hashcat_ctx) { - potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; + const hashconfig_t *hashconfig = hashcat_ctx->hashconfig; + potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; if (potfile_ctx->enabled == false) return 0; + if (hashconfig->potfile_disable == true) return 0; + if (hc_fopen (&potfile_ctx->fp, potfile_ctx->filename, "ab") == false) { event_log_error (hashcat_ctx, "%s: %s", potfile_ctx->filename, strerror (errno)); @@ -231,8 +237,8 @@ int potfile_write_open (hashcat_ctx_t *hashcat_ctx) void potfile_write_close (hashcat_ctx_t *hashcat_ctx) { - hashconfig_t *hashconfig = hashcat_ctx->hashconfig; - potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; + const hashconfig_t *hashconfig = hashcat_ctx->hashconfig; + potfile_ctx_t *potfile_ctx = hashcat_ctx->potfile_ctx; if (potfile_ctx->enabled == false) return; From 6cf553410be09cfb7cefd6d2dfa6547d4faae638 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 11 Aug 2021 14:06:48 +0300 Subject: [PATCH 06/11] Avoid (possible) undefined behaviour in hc_fclose(), and don't assume plain file --- src/filehandling.c | 2 +- src/potfile.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/filehandling.c b/src/filehandling.c index 9edd1cb35..a858a82bf 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -596,7 +596,7 @@ void hc_fclose (HCFILE *fp) unzClose (fp->ufp); } - else + else if (fp->pfp) { fclose (fp->pfp); } diff --git a/src/potfile.c b/src/potfile.c index 978247afa..fe8448066 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -211,8 +211,6 @@ void potfile_read_close (hashcat_ctx_t *hashcat_ctx) if (hashconfig->potfile_disable == true) return; - if (potfile_ctx->fp.pfp == NULL) return; - hc_fclose (&potfile_ctx->fp); } From 36c209c4219e7f8c8565e514c026e3e4e2664a6c Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 18 Aug 2021 16:46:17 +0300 Subject: [PATCH 07/11] Check NULL pointer before using file APIs --- src/filehandling.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/filehandling.c b/src/filehandling.c index a858a82bf..b8f50eadf 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -80,7 +80,7 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) // compressed files with BOM will be undetected! - if ((fp->is_gzip == false) && (fp->is_zip == false)) + if (fp->is_gzip == false && fp->is_zip == false) { fp->bom_size = hc_string_bom_size (check); } @@ -209,7 +209,7 @@ size_t hc_fread (void *ptr, size_t size, size_t nmemb, HCFILE *fp) n = unzReadCurrentFile (fp->ufp, ptr, s); } - else + else if (fp->pfp) { #if defined (_WIN) @@ -270,7 +270,7 @@ size_t hc_fwrite (const void *ptr, size_t size, size_t nmemb, HCFILE *fp) else if (fp->is_zip) { } - else + else if (fp->pfp) { #if defined (_WIN) @@ -349,7 +349,7 @@ int hc_fseek (HCFILE *fp, off_t offset, int whence) // r = unzSetOffset (fp->ufp, offset); */ } - else + else if (fp->pfp) { r = fseeko (fp->pfp, offset, whence); } @@ -369,7 +369,7 @@ void hc_rewind (HCFILE *fp) { unzGoToFirstFile (fp->ufp); } - else + else if (fp->pfp) { rewind (fp->pfp); } @@ -389,7 +389,7 @@ off_t hc_ftell (HCFILE *fp) { n = unztell (fp->ufp); } - else + else if (fp->pfp) { n = ftello (fp->pfp); } @@ -410,7 +410,7 @@ int hc_fputc (int c, HCFILE *fp) else if (fp->is_zip) { } - else + else if (fp->pfp) { r = fputc (c, fp->pfp); } @@ -434,7 +434,7 @@ int hc_fgetc (HCFILE *fp) if (unzReadCurrentFile (fp->ufp, &c, 1) == 1) r = (int) c; } - else + else if (fp->pfp) { r = fgetc (fp->pfp); } @@ -456,7 +456,7 @@ char *hc_fgets (char *buf, int len, HCFILE *fp) { if (unzReadCurrentFile (fp->ufp, buf, len) > 0) r = buf; } - else + else if (fp->pfp) { r = fgets (buf, len, fp->pfp); } @@ -477,7 +477,7 @@ int hc_vfprintf (HCFILE *fp, const char *format, va_list ap) else if (fp->is_zip) { } - else + else if (fp->pfp) { r = vfprintf (fp->pfp, format, ap); } @@ -502,7 +502,7 @@ int hc_fprintf (HCFILE *fp, const char *format, ...) else if (fp->is_zip) { } - else + else if (fp->pfp) { r = vfprintf (fp->pfp, format, ap); } @@ -516,23 +516,17 @@ int hc_fscanf (HCFILE *fp, const char *format, void *ptr) { if (fp == NULL) return -1; - char *buf = (char *) hcmalloc (HCBUFSIZ_TINY); - - if (buf == NULL) return -1; + char buf[HCBUFSIZ_TINY]; char *b = hc_fgets (buf, HCBUFSIZ_TINY - 1, fp); if (b == NULL) { - hcfree (buf); - return -1; } sscanf (b, format, ptr); - hcfree (buf); - return 1; } @@ -557,7 +551,7 @@ int hc_feof (HCFILE *fp) { r = unzeof (fp->ufp); } - else + else if (fp->pfp) { r = feof (fp->pfp); } @@ -576,7 +570,7 @@ void hc_fflush (HCFILE *fp) else if (fp->is_zip) { } - else + else if (fp->pfp) { fflush (fp->pfp); } @@ -664,7 +658,7 @@ size_t fgetl (HCFILE *fp, char *line_buf, const size_t line_sz) line_buf[line_len] = 0; - return (line_len); + return line_len; } u64 count_lines (HCFILE *fp) @@ -681,9 +675,7 @@ u64 count_lines (HCFILE *fp) if (nread < 1) continue; - size_t i; - - for (i = 0; i < nread; i++) + for (size_t i = 0; i < nread; i++) { if (prev == '\n') cnt++; From 089a4847dbc823bb746575b131f328b4117bde3b Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 18 Aug 2021 17:18:31 +0300 Subject: [PATCH 08/11] Check NULL pointer before using gzip/zip APIs --- src/filehandling.c | 52 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/filehandling.c b/src/filehandling.c index b8f50eadf..8ba626aff 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -199,11 +199,11 @@ size_t hc_fread (void *ptr, size_t size, size_t nmemb, HCFILE *fp) if (fp == NULL) return n; - if (fp->is_gzip) + if (fp->gfp) { n = gzfread (ptr, size, nmemb, fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { unsigned s = size * nmemb; @@ -263,11 +263,11 @@ size_t hc_fwrite (const void *ptr, size_t size, size_t nmemb, HCFILE *fp) if (fp == NULL) return n; - if (fp->is_gzip) + if (fp->gfp) { n = gzfwrite (ptr, size, nmemb, fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { } else if (fp->pfp) @@ -324,11 +324,11 @@ int hc_fseek (HCFILE *fp, off_t offset, int whence) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzseek (fp->gfp, offset, whence); } - else if (fp->is_zip) + else if (fp->ufp) { /* // untested and not used in wordlist engine @@ -361,11 +361,11 @@ void hc_rewind (HCFILE *fp) { if (fp == NULL) return; - if (fp->is_gzip) + if (fp->gfp) { gzrewind (fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { unzGoToFirstFile (fp->ufp); } @@ -381,11 +381,11 @@ off_t hc_ftell (HCFILE *fp) if (fp == NULL) return -1; - if (fp->is_gzip) + if (fp->gfp) { n = (off_t) gztell (fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { n = unztell (fp->ufp); } @@ -403,11 +403,11 @@ int hc_fputc (int c, HCFILE *fp) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzputc (fp->gfp, c); } - else if (fp->is_zip) + else if (fp->ufp) { } else if (fp->pfp) @@ -424,11 +424,11 @@ int hc_fgetc (HCFILE *fp) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzgetc (fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { unsigned char c = 0; @@ -448,11 +448,11 @@ char *hc_fgets (char *buf, int len, HCFILE *fp) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzgets (fp->gfp, buf, len); } - else if (fp->is_zip) + else if (fp->ufp) { if (unzReadCurrentFile (fp->ufp, buf, len) > 0) r = buf; } @@ -470,11 +470,11 @@ int hc_vfprintf (HCFILE *fp, const char *format, va_list ap) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzvprintf (fp->gfp, format, ap); } - else if (fp->is_zip) + else if (fp->ufp) { } else if (fp->pfp) @@ -495,11 +495,11 @@ int hc_fprintf (HCFILE *fp, const char *format, ...) va_start (ap, format); - if (fp->is_gzip) + if (fp->gfp) { r = gzvprintf (fp->gfp, format, ap); } - else if (fp->is_zip) + else if (fp->ufp) { } else if (fp->pfp) @@ -543,11 +543,11 @@ int hc_feof (HCFILE *fp) if (fp == NULL) return r; - if (fp->is_gzip) + if (fp->gfp) { r = gzeof (fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { r = unzeof (fp->ufp); } @@ -563,11 +563,11 @@ void hc_fflush (HCFILE *fp) { if (fp == NULL) return; - if (fp->is_gzip) + if (fp->gfp) { gzflush (fp->gfp, Z_SYNC_FLUSH); } - else if (fp->is_zip) + else if (fp->ufp) { } else if (fp->pfp) @@ -580,11 +580,11 @@ void hc_fclose (HCFILE *fp) { if (fp == NULL) return; - if (fp->is_gzip) + if (fp->gfp) { gzclose (fp->gfp); } - else if (fp->is_zip) + else if (fp->ufp) { unzCloseCurrentFile (fp->ufp); From f3cd32bffdff271a6b9f9f88d2841e5e3235b15c Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 18 Aug 2021 17:46:39 +0300 Subject: [PATCH 09/11] Avoid memory copy in rebuild_pws_compressed_append() and assign instead --- include/backend.h | 2 -- src/backend.c | 13 ++++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/backend.h b/include/backend.h index de74cb511..393b31440 100644 --- a/include/backend.h +++ b/include/backend.h @@ -174,8 +174,6 @@ int gidd_to_pw_t (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, c int choose_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, const u32 highest_pw_len, const u64 pws_pos, const u64 pws_cnt, const u32 fast_iteration, const u32 salt_pos); -void rebuild_pws_compressed_append (hc_device_param_t *device_param, const u64 pws_cnt, const u8 chr); - int run_cuda_kernel_atinit (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, CUdeviceptr buf, const u64 num); int run_cuda_kernel_utf8toutf16le (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, CUdeviceptr buf, const u64 num); int run_cuda_kernel_memset (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, CUdeviceptr buf, const u64 offset, const u8 value, const u64 size); diff --git a/src/backend.c b/src/backend.c index 21e73c05b..5aa355658 100644 --- a/src/backend.c +++ b/src/backend.c @@ -41,6 +41,9 @@ static double TARGET_MSEC_PROFILE[4] = { 2, 12, 96, 480 }; HC_ALIGN(16) static const u32 bzeros[4] = { 0, 0, 0, 0 }; +/* forward declarations */ +static void rebuild_pws_compressed_append (hc_device_param_t *device_param, const u64 pws_cnt, const u8 chr); + static bool is_same_device (const hc_device_param_t *src, const hc_device_param_t *dst) { // First check by PCI address @@ -4827,7 +4830,7 @@ int choose_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, return 0; } -void rebuild_pws_compressed_append (hc_device_param_t *device_param, const u64 pws_cnt, const u8 chr) +static void rebuild_pws_compressed_append (hc_device_param_t *device_param, const u64 pws_cnt, const u8 chr) { // this function is used if we have to modify the compressed pws buffer in order to // append some data to each password candidate @@ -4871,11 +4874,11 @@ void rebuild_pws_compressed_append (hc_device_param_t *device_param, const u64 p pw_idx_dst_next->off = pw_idx_dst->off + pw_idx_dst->cnt; } - memcpy (device_param->pws_comp, tmp_pws_comp, device_param->size_pws_comp); - memcpy (device_param->pws_idx, tmp_pws_idx, device_param->size_pws_idx); + hcfree (device_param->pws_comp); + hcfree (device_param->pws_idx); - hcfree (tmp_pws_comp); - hcfree (tmp_pws_idx); + device_param->pws_comp = tmp_pws_comp; + device_param->pws_idx = tmp_pws_idx; } int run_cuda_kernel_atinit (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, CUdeviceptr buf, const u64 num) From fed32a58c76c78b1345c23a799b7592e07f8c719 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 18 Aug 2021 19:28:00 +0300 Subject: [PATCH 10/11] Always cleanup and initialize all parts of HC_FILE structure --- include/types.h | 2 - src/debugfile.c | 12 ++++-- src/filehandling.c | 104 +++++++++++++++++++++++++-------------------- src/stdout.c | 12 ++++-- 4 files changed, 77 insertions(+), 53 deletions(-) diff --git a/include/types.h b/include/types.h index 7dbc41503..cda30cefb 100644 --- a/include/types.h +++ b/include/types.h @@ -1073,8 +1073,6 @@ typedef struct hc_fp gzFile gfp; // gzip fp unzFile ufp; // zip fp - bool is_gzip; - bool is_zip; int bom_size; const char *mode; diff --git a/src/debugfile.c b/src/debugfile.c index aaaed1822..b23fad791 100644 --- a/src/debugfile.c +++ b/src/debugfile.c @@ -133,9 +133,15 @@ int debugfile_init (hashcat_ctx_t *hashcat_ctx) } else { - debugfile_ctx->fp.is_gzip = false; - debugfile_ctx->fp.pfp = stdout; - debugfile_ctx->fp.fd = fileno (stdout); + HCFILE *fp = &debugfile_ctx->fp; + + fp->fd = fileno (stdout); + fp->pfp = stdout; + fp->gfp = NULL; + fp->ufp = NULL; + fp->bom_size = 0; + fp->path = NULL; + fp->mode = NULL; } return 0; diff --git a/src/filehandling.c b/src/filehandling.c index 8ba626aff..f23c13024 100644 --- a/src/filehandling.c +++ b/src/filehandling.c @@ -23,7 +23,16 @@ int _wopen (const char *path, int oflag, ...) bool hc_fopen (HCFILE *fp, const char *path, const char *mode) { - if (path == NULL || mode == NULL) return false; + if (fp == NULL || path == NULL || mode == NULL) return false; + + /* cleanup */ + fp->fd = -1; + fp->pfp = NULL; + fp->gfp = NULL; + fp->ufp = NULL; + fp->bom_size = 0; + fp->path = NULL; + fp->mode = NULL; int oflag = -1; @@ -60,13 +69,11 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) return false; } - fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; - fp->bom_size = 0; - unsigned char check[8] = { 0 }; + bool is_gzip = false; + bool is_zip = false; + int fd_tmp = open (path, O_RDONLY); if (fd_tmp != -1) @@ -75,12 +82,12 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) if (read (fd_tmp, check, sizeof (check)) > 0) { - if (check[0] == 0x1f && check[1] == 0x8b && check[2] == 0x08) fp->is_gzip = true; - if (check[0] == 0x50 && check[1] == 0x4b && check[2] == 0x03 && check[3] == 0x04) fp->is_zip = true; + if (check[0] == 0x1f && check[1] == 0x8b && check[2] == 0x08) is_gzip = true; + if (check[0] == 0x50 && check[1] == 0x4b && check[2] == 0x03 && check[3] == 0x04) is_zip = true; // compressed files with BOM will be undetected! - if (fp->is_gzip == false && fp->is_zip == false) + if (is_gzip == false && is_zip == false) { fp->bom_size = hc_string_bom_size (check); } @@ -89,40 +96,43 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) close (fd_tmp); } - if (fmode == -1) + if (is_zip == false) { - fp->fd = open (path, oflag); + if (fmode == -1) + { + fp->fd = open (path, oflag); + } + else + { + fp->fd = open (path, oflag, fmode); + } + + if (fp->fd == -1) return false; + + if (is_gzip) + { + if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; + } + else + { + if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; + + if (fp->bom_size) + { + // atm just skip bom + + const int nread = fread (check, sizeof (char), fp->bom_size, fp->pfp); + + if (nread != fp->bom_size) return false; + } + } } else - { - fp->fd = open (path, oflag, fmode); - } - - if (fp->fd == -1 && fp->is_zip == false) return false; - - if (fp->is_gzip) - { - if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false; - } - else if (fp->is_zip) { if ((fp->ufp = unzOpen64 (path)) == NULL) return false; if (unzOpenCurrentFile (fp->ufp) != UNZ_OK) return false; } - else - { - if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; - - if (fp->bom_size) - { - // atm just skip bom - - const int nread = fread (check, sizeof (char), fp->bom_size, fp->pfp); - - if (nread != fp->bom_size) return false; - } - } fp->path = path; fp->mode = mode; @@ -132,7 +142,16 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode) bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) { - if (path == NULL || mode == NULL) return false; + if (fp == NULL || path == NULL || mode == NULL) return false; + + /* cleanup */ + fp->fd = -1; + fp->pfp = NULL; + fp->gfp = NULL; + fp->ufp = NULL; + fp->bom_size = 0; + fp->path = NULL; + fp->mode = NULL; int oflag = -1; @@ -169,11 +188,6 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) return false; } - fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; - fp->bom_size = 0; - if (fmode == -1) { fp->fd = open (path, oflag); @@ -183,9 +197,9 @@ bool hc_fopen_raw (HCFILE *fp, const char *path, const char *mode) fp->fd = open (path, oflag, fmode); } - if (fp->fd == -1 && fp->is_zip == false) return false; + if (fp->fd == -1) return false; - if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; + if ((fp->pfp = fdopen (fp->fd, mode)) == NULL) return false; fp->path = path; fp->mode = mode; @@ -597,8 +611,8 @@ void hc_fclose (HCFILE *fp) fp->fd = -1; fp->pfp = NULL; - fp->is_gzip = false; - fp->is_zip = false; + fp->gfp = NULL; + fp->ufp = NULL; fp->path = NULL; fp->mode = NULL; diff --git a/src/stdout.c b/src/stdout.c index 1fd7542bc..1d52881cd 100644 --- a/src/stdout.c +++ b/src/stdout.c @@ -83,9 +83,15 @@ int process_stdout (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, } else { - out.fp.is_gzip = false; - out.fp.pfp = stdout; - out.fp.fd = fileno (stdout); + HCFILE *fp = &out.fp; + + fp->fd = fileno (stdout); + fp->pfp = stdout; + fp->gfp = NULL; + fp->ufp = NULL; + fp->bom_size = 0; + fp->path = NULL; + fp->mode = NULL; } out.len = 0; From 6b4786de8412e10c9f559c8479fc456cc928843d Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Wed, 18 Aug 2021 20:19:51 +0300 Subject: [PATCH 11/11] Make blocking clEnqueueWriteBuffer() non-blocking --- src/backend.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend.c b/src/backend.c index 5aa355658..2717b0a2a 100644 --- a/src/backend.c +++ b/src/backend.c @@ -6519,7 +6519,7 @@ int run_cracker (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param, co if (device_param->is_opencl == true) { - if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_combs_c, CL_TRUE, 0, innerloop_left * sizeof (pw_t), device_param->combs_buf, 0, NULL, NULL) == -1) return -1; + if (hc_clEnqueueWriteBuffer (hashcat_ctx, device_param->opencl_command_queue, device_param->opencl_d_combs_c, CL_FALSE, 0, innerloop_left * sizeof (pw_t), device_param->combs_buf, 0, NULL, NULL) == -1) return -1; } } else if (user_options->attack_mode == ATTACK_MODE_HYBRID1)