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.
This commit is contained in:
debian 2026-11-29 13:05:49 -03:00
parent 4accca793f
commit a561ac6090

View file

@ -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;
@ -295,18 +325,20 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
* io_buffer cannot land at insecure_memory_base. * io_buffer cannot land at insecure_memory_base.
* *
* Wait 1 second to ensure all spray buffers are freed by the * 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 * On a native host there is no evidence that the same wait is
* that motivated it: the vhci layer adds latency and the buffers * needed, and no measurement has been taken either way. One
* are freed late. On a native host the reference implementation * hypothesis is that the idle second is a window in which the
* goes straight from the spray to the overwrite, and the idle * device can leave dfuIDLE on its own, which would surface as
* second is a window in which the device can leave dfuIDLE on its * NO_DEVICE (-4) at stage 4; that is unconfirmed. What this change
* own. When that happens the next control transfer returns * claims is narrower: do not keep a delay on transports that never
* NO_DEVICE (-4) and the sequence fails at stage 4. * asked for it, so a native run behaves like the bus it runs on.
*/ */
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");