From bfe83ec13812dc8e5c1566c2483d4dd53cb276d5 Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Sat, 10 Jul 2021 08:43:15 +0200 Subject: [PATCH] Added temperature watchdog for CPU on linux using sysfs --- docs/changes.txt | 8 +-- include/ext_sysfs_cpu.h | 37 +++++++++++ include/types.h | 4 ++ src/Makefile | 2 +- src/backend.c | 7 +++ src/ext_sysfs_cpu.c | 134 ++++++++++++++++++++++++++++++++++++++++ src/hwmon.c | 87 +++++++++++++++++++++++++- 7 files changed, 270 insertions(+), 9 deletions(-) create mode 100644 include/ext_sysfs_cpu.h create mode 100644 src/ext_sysfs_cpu.c diff --git a/docs/changes.txt b/docs/changes.txt index 709ca8706..51648b8ff 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -18,6 +18,8 @@ ## - Added option --markov-inverse to inverse markov statistics, with the idea of reversing the order of the password candidates +- Added temperature watchdog and fanspeed readings for CPU and GPU on macOS using iokit +- Added temperature watchdog for CPU on linux using sysfs ## ## Bugs @@ -32,12 +34,6 @@ - Fixed missing option to automatically disable kernel cache in -m 25600 and -m 25800 - Fixed out-of-boundary write in slow candidates mode in combinator attack -## -## Features -## - -- Added temperature watchdog and fanspeed readings for CPU and GPU on macOS using iokit - ## ## Improvements ## diff --git a/include/ext_sysfs_cpu.h b/include/ext_sysfs_cpu.h new file mode 100644 index 000000000..06d0f744d --- /dev/null +++ b/include/ext_sysfs_cpu.h @@ -0,0 +1,37 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#ifndef _EXT_SYSFS_CPU_H +#define _EXT_SYSFS_CPU_H + +#include + +static const char SYSFS_HWMON[] = "/sys/class/hwmon"; + +static const char SENSOR_CORETEMP[] = "coretemp"; +static const char SENSOR_K10TEMP[] = "k10temp"; +static const char SENSOR_K8TEMP[] = "k8temp"; +static const char SENSOR_ACPITZ[] = "acpitz"; + +typedef int HM_ADAPTER_SYSFS_CPU; + +typedef void *SYSFS_CPU_LIB; + +typedef struct hm_sysfs_cpu_lib +{ + // currently not using libudev, because it can only read values, not set them, so using /sys instead + + SYSFS_CPU_LIB lib; + +} hm_sysfs_cpu_lib_t; + +typedef hm_sysfs_cpu_lib_t SYSFS_CPU_PTR; + +bool sysfs_cpu_init (void *hashcat_ctx); +void sysfs_cpu_close (void *hashcat_ctx); +char *hm_SYSFS_CPU_get_syspath_hwmon (); +int hm_SYSFS_CPU_get_temperature_current (void *hashcat_ctx, int *val); + +#endif // _EXT_SYSFS_CPU_H diff --git a/include/types.h b/include/types.h index 0e5cc625d..d5119d3a0 100644 --- a/include/types.h +++ b/include/types.h @@ -1599,6 +1599,7 @@ typedef struct backend_ctx bool need_nvml; bool need_nvapi; bool need_sysfs_amdgpu; + bool need_sysfs_cpu; bool need_iokit; int comptime; @@ -1643,6 +1644,7 @@ typedef enum kernel_workload #include "ext_nvapi.h" #include "ext_nvml.h" #include "ext_sysfs_amdgpu.h" +#include "ext_sysfs_cpu.h" #include "ext_iokit.h" typedef struct hm_attrs @@ -1651,6 +1653,7 @@ typedef struct hm_attrs HM_ADAPTER_NVML nvml; HM_ADAPTER_NVAPI nvapi; HM_ADAPTER_SYSFS_AMDGPU sysfs_amdgpu; + HM_ADAPTER_SYSFS_CPU sysfs_cpu; HM_ADAPTER_IOKIT iokit; int od_version; @@ -1676,6 +1679,7 @@ typedef struct hwmon_ctx void *hm_nvml; void *hm_nvapi; void *hm_sysfs_amdgpu; + void *hm_sysfs_cpu; void *hm_iokit; hm_attrs_t *hm_device; diff --git a/src/Makefile b/src/Makefile index bc5db37f1..d7d8d2915 100644 --- a/src/Makefile +++ b/src/Makefile @@ -360,7 +360,7 @@ EMU_OBJS_ALL += emu_inc_rp emu_inc_rp_optimized EMU_OBJS_ALL += emu_inc_hash_md4 emu_inc_hash_md5 emu_inc_hash_ripemd160 emu_inc_hash_sha1 emu_inc_hash_sha256 emu_inc_hash_sha384 emu_inc_hash_sha512 emu_inc_hash_streebog256 emu_inc_hash_streebog512 emu_inc_ecc_secp256k1 EMU_OBJS_ALL += emu_inc_cipher_aes emu_inc_cipher_camellia emu_inc_cipher_des emu_inc_cipher_kuznyechik emu_inc_cipher_serpent emu_inc_cipher_twofish -OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_nvrtc ext_OpenCL ext_sysfs_amdgpu ext_iokit ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) +OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_nvrtc ext_OpenCL ext_sysfs_amdgpu ext_sysfs_cpu ext_iokit ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) ifeq ($(ENABLE_BRAIN),1) OBJS_ALL += brain diff --git a/src/backend.c b/src/backend.c index ac0ba4f2d..20ec98a0e 100644 --- a/src/backend.c +++ b/src/backend.c @@ -5684,6 +5684,7 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) bool need_nvml = false; bool need_nvapi = false; bool need_sysfs_amdgpu = false; + bool need_sysfs_cpu = false; bool need_iokit = false; int backend_devices_idx = 0; @@ -6727,6 +6728,10 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) } } #endif + + #if defined (__linux__) + need_sysfs_cpu = true; + #endif } if (device_param->opencl_device_type & CL_DEVICE_TYPE_GPU) @@ -7403,6 +7408,7 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) backend_ctx->need_nvml = need_nvml; backend_ctx->need_nvapi = need_nvapi; backend_ctx->need_sysfs_amdgpu = need_sysfs_amdgpu; + backend_ctx->need_sysfs_cpu = need_sysfs_cpu; backend_ctx->need_iokit = need_iokit; backend_ctx->comptime = comptime; @@ -7450,6 +7456,7 @@ void backend_ctx_devices_destroy (hashcat_ctx_t *hashcat_ctx) backend_ctx->need_nvml = false; backend_ctx->need_nvapi = false; backend_ctx->need_sysfs_amdgpu = false; + backend_ctx->need_sysfs_cpu = false; backend_ctx->need_iokit = false; } diff --git a/src/ext_sysfs_cpu.c b/src/ext_sysfs_cpu.c new file mode 100644 index 000000000..c03507ca8 --- /dev/null +++ b/src/ext_sysfs_cpu.c @@ -0,0 +1,134 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include "common.h" +#include "types.h" +#include "memory.h" +#include "shared.h" +#include "event.h" +#include "folder.h" +#include "ext_sysfs_cpu.h" + +bool sysfs_cpu_init (void *hashcat_ctx) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + SYSFS_CPU_PTR *sysfs_cpu = (SYSFS_CPU_PTR *) hwmon_ctx->hm_sysfs_cpu; + + memset (sysfs_cpu, 0, sizeof (SYSFS_CPU_PTR)); + + char *path; + + hc_asprintf (&path, "%s/hwmon0", SYSFS_HWMON); + + const bool r = hc_path_read (path); + + hcfree (path); + + return r; +} + +void sysfs_cpu_close (void *hashcat_ctx) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + SYSFS_CPU_PTR *sysfs_cpu = (SYSFS_CPU_PTR *) hwmon_ctx->hm_sysfs_cpu; + + if (sysfs_cpu) + { + hcfree (sysfs_cpu); + } +} + +char *hm_SYSFS_CPU_get_syspath_hwmon () +{ + char *found[4]; + + found[0] = NULL; + found[1] = NULL; + found[2] = NULL; + found[3] = NULL; + + // 16 ok? + + for (int i = 0; i < 16; i++) + { + char *path = NULL; + + hc_asprintf (&path, "%s/hwmon%d/name", SYSFS_HWMON, i); + + HCFILE fp; + + if (hc_fopen_raw (&fp, path, "rb") == false) continue; + + char buf[16]; + + const size_t line_len = fgetl (&fp, buf, sizeof (buf)); + + if (line_len) + { + if (strcmp (buf, SENSOR_CORETEMP) == 0) hc_asprintf (&found[0], "%s/hwmon%d", SYSFS_HWMON, i); + if (strcmp (buf, SENSOR_K10TEMP) == 0) hc_asprintf (&found[1], "%s/hwmon%d", SYSFS_HWMON, i); + if (strcmp (buf, SENSOR_K8TEMP) == 0) hc_asprintf (&found[2], "%s/hwmon%d", SYSFS_HWMON, i); + if (strcmp (buf, SENSOR_ACPITZ) == 0) hc_asprintf (&found[3], "%s/hwmon%d", SYSFS_HWMON, i); + } + + hc_fclose (&fp); + + hcfree (path); + } + + if (found[0]) return found[0]; + if (found[1]) return found[1]; + if (found[2]) return found[2]; + if (found[3]) return found[3]; + + return NULL; +} + +int hm_SYSFS_CPU_get_temperature_current (void *hashcat_ctx, int *val) +{ + char *syspath = hm_SYSFS_CPU_get_syspath_hwmon (); + + if (syspath == NULL) return -1; + + char *path = NULL; + + hc_asprintf (&path, "%s/temp1_input", syspath); + + hcfree (syspath); + + HCFILE fp; + + if (hc_fopen_raw (&fp, path, "r") == false) + { + event_log_error (hashcat_ctx, "%s: %s", path, strerror (errno)); + + hcfree (path); + + return -1; + } + + int temperature = 0; + + if (hc_fscanf (&fp, "%d", &temperature) != 1) + { + hc_fclose (&fp); + + event_log_error (hashcat_ctx, "%s: unexpected data.", path); + + hcfree (path); + + return -1; + } + + hc_fclose (&fp); + + *val = temperature / 1000; + + hcfree (path); + + return 0; +} diff --git a/src/hwmon.c b/src/hwmon.c index 6c7862b2e..463e8c654 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -269,6 +269,20 @@ int hm_get_temperature_with_devices_idx (hashcat_ctx_t *hashcat_ctx, const int b } } #endif + + if (hwmon_ctx->hm_sysfs_cpu) + { + int temperature = 0; + + if (hm_SYSFS_CPU_get_temperature_current (hashcat_ctx, &temperature) == -1) + { + hwmon_ctx->hm_device[backend_device_idx].temperature_get_supported = false; + + return -1; + } + + return temperature; + } } if (backend_ctx->devices_param[backend_device_idx].opencl_device_type & CL_DEVICE_TYPE_GPU) @@ -1066,6 +1080,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hm_attrs_t *hm_adapters_nvapi = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); hm_attrs_t *hm_adapters_nvml = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); hm_attrs_t *hm_adapters_sysfs_amdgpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); + hm_attrs_t *hm_adapters_sysfs_cpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); hm_attrs_t *hm_adapters_iokit = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); #define FREE_ADAPTERS \ @@ -1074,6 +1089,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hcfree (hm_adapters_nvapi); \ hcfree (hm_adapters_nvml); \ hcfree (hm_adapters_sysfs_amdgpu); \ + hcfree (hm_adapters_sysfs_cpu); \ hcfree (hm_adapters_iokit); \ } while (0) @@ -1134,6 +1150,18 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) } } + if (backend_ctx->need_sysfs_cpu == true) + { + hwmon_ctx->hm_sysfs_cpu = (SYSFS_CPU_PTR *) hcmalloc (sizeof (SYSFS_CPU_PTR)); + + if (sysfs_cpu_init (hashcat_ctx) == false) + { + hcfree (hwmon_ctx->hm_sysfs_cpu); + + hwmon_ctx->hm_sysfs_cpu = NULL; + } + } + #if defined(__APPLE__) if (backend_ctx->need_iokit == true) { @@ -1425,6 +1453,42 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) } } + if (hwmon_ctx->hm_sysfs_cpu) + { + if (true) + { + for (int backend_devices_idx = 0; backend_devices_idx < backend_ctx->backend_devices_cnt; backend_devices_idx++) + { + hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx]; + + if (device_param->skipped == true) continue; + + if (device_param->is_cuda == true) + { + // nothing to do + } + + if (device_param->is_opencl == true) + { + const u32 device_id = device_param->device_id; + + if ((device_param->opencl_device_type & CL_DEVICE_TYPE_CPU) == 0) continue; + + if (hwmon_ctx->hm_sysfs_cpu) + { + hm_adapters_sysfs_cpu[device_id].buslanes_get_supported = false; + hm_adapters_sysfs_cpu[device_id].corespeed_get_supported = false; + hm_adapters_sysfs_cpu[device_id].fanspeed_get_supported = false; + hm_adapters_sysfs_cpu[device_id].fanpolicy_get_supported = false; + hm_adapters_sysfs_cpu[device_id].memoryspeed_get_supported = false; + hm_adapters_sysfs_cpu[device_id].temperature_get_supported = true; + hm_adapters_sysfs_cpu[device_id].utilization_get_supported = false; + } + } + } + } + } + #if defined(__APPLE__) if (backend_ctx->need_iokit == true) { @@ -1440,7 +1504,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) #endif - if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs_amdgpu == NULL && hwmon_ctx->hm_iokit == NULL) + if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs_amdgpu == NULL && hwmon_ctx->hm_sysfs_cpu == NULL && hwmon_ctx->hm_iokit == NULL) { FREE_ADAPTERS; @@ -1473,6 +1537,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hwmon_ctx->hm_device[backend_devices_idx].adl = 0; hwmon_ctx->hm_device[backend_devices_idx].sysfs_amdgpu = 0; + hwmon_ctx->hm_device[backend_devices_idx].sysfs_cpu = 0; hwmon_ctx->hm_device[backend_devices_idx].iokit = 0; hwmon_ctx->hm_device[backend_devices_idx].nvapi = 0; hwmon_ctx->hm_device[backend_devices_idx].nvml = 0; @@ -1535,8 +1600,21 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) } } #endif - } + if (hwmon_ctx->hm_sysfs_cpu) + { + hwmon_ctx->hm_device[backend_devices_idx].buslanes_get_supported |= hm_adapters_sysfs_cpu[device_id].buslanes_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].corespeed_get_supported |= hm_adapters_sysfs_cpu[device_id].corespeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].fanspeed_get_supported |= hm_adapters_sysfs_cpu[device_id].fanspeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].fanpolicy_get_supported |= hm_adapters_sysfs_cpu[device_id].fanpolicy_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].memoryspeed_get_supported |= hm_adapters_sysfs_cpu[device_id].memoryspeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].temperature_get_supported |= hm_adapters_sysfs_cpu[device_id].temperature_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].threshold_shutdown_get_supported |= hm_adapters_sysfs_cpu[device_id].threshold_shutdown_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].threshold_slowdown_get_supported |= hm_adapters_sysfs_cpu[device_id].threshold_slowdown_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].throttle_get_supported |= hm_adapters_sysfs_cpu[device_id].throttle_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].utilization_get_supported |= hm_adapters_sysfs_cpu[device_id].utilization_get_supported; + } + } if (device_param->opencl_device_type & CL_DEVICE_TYPE_GPU) { @@ -1686,6 +1764,11 @@ void hwmon_ctx_destroy (hashcat_ctx_t *hashcat_ctx) sysfs_amdgpu_close (hashcat_ctx); } + if (hwmon_ctx->hm_sysfs_cpu) + { + sysfs_cpu_close (hashcat_ctx); + } + #if defined (__APPLE__) if (hwmon_ctx->hm_iokit) {