fix(core): give USB EP0 receptions a full-packet buffer
What changed, and why it matters
This commit fixes a USB memory-safety bug in Trezor hardware wallets. When the device expected no data during the final 'status' handshake of a USB control transfer, it told the USB hardware to write incoming bytes to address zero (NULL) or into an 8-byte command buffer. The USB hardware can actually deliver a full 64-byte packet regardless of what length was requested, so a malicious or misbehaving USB host could make the device write memory past the intended location or through the NULL pointer, causing a crash or potentially worse. The fix gives every such reception a dedicated 64-byte buffer inside the device handle.
Treat this as a security fix and include it in the next firmware release. Users should update Trezor firmware, bootloader, and prodtest to a version containing this commit. Hardware-wallet vendors relying on the same STM32 USB stack should audit similar EP0 OUT buffer handling. No CVE or advisory is present in the supplied materials, so one may be assigned if the project chooses to publish a security advisory.
Security signals we found
NULL-pointer write on USB EP0 OUT reception
Buffer overflow on USB EP0 OUT reception due to hardware accepting full packet despite armed length
Memory corruption vector reachable from USB host without authentication
Fix affects bootloader and prodtest as well as core firmware
Changelog frames issue as a crash fix for unexpected USB status-stage data
Evidence from the diff
The STM32 USB OTG core accepts a full USB packet on EP0 OUT even when the armed receive length is shorter. The stack previously armed status-stage and premature-end-of-IN-transfer receptions with a NULL buffer, and the VCP class armed class-request receptions into an 8-byte cmd_buffer. A host sending an unexpected full packet could therefore cause a write through NULL or a buffer overflow. The patch removes the per-class cmd_buffer, adds a packet-sized ep0_buf[USB_MAX_EP0_SIZE] to USBD_HandleTypeDef, and routes all EP0 OUT receptions through it. It also changes VCP class requests that expect a response to stall (USBD_CtlError) instead of echoing from the small buffer.
Changed components
core/embed/io/usb/stm32/usbd_core.ccore/embed/io/usb/stm32/usbd_ioreq.ccore/embed/io/usb/stm32/usb_class_vcp.ccore/embed/io/usb/stm32/usbd_def.hTrezor Core firmware USB device stackTrezor bootloader USB stackTrezor prodtest USB stackInspect captured patch +13 / −9
### core/.changelog.d/8166.fixed
@@ -0,0 +1 @@
+Fix a crash when a USB host sends unexpected data in the status stage of a control transfer.
### core/embed/io/usb/stm32/usb_class_vcp.c
@@ -154,7 +154,6 @@ typedef struct {
uint16_t max_packet_len;
uint8_t ep_in_is_idle; // Set to 1 after IN endpoint gets idle
bool recv_pending;
- uint8_t cmd_buffer[USB_CDC_MAX_CMD_PACKET_LEN];
} usb_vcp_state_t;
_Static_assert(sizeof(usb_vcp_state_t) <= USBD_CLASS_STATE_MAX_SIZE);
@@ -373,8 +372,6 @@ static uint8_t usb_vcp_class_deinit(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
static uint8_t usb_vcp_class_setup(USBD_HandleTypeDef *dev,
USBD_SetupReqTypedef *req) {
- usb_vcp_state_t *state = (usb_vcp_state_t *)dev->pUserData;
-
static const usb_cdc_line_coding_t line_coding = {
.dwDTERate = 115200,
.bCharFormat = USB_CDC_1_STOP_BITS,
@@ -391,12 +388,11 @@ static uint8_t usb_vcp_class_setup(USBD_HandleTypeDef *dev,
USBD_CtlSendData(dev, UNCONST(&line_coding),
MIN_8bits(req->wLength, sizeof(line_coding)));
} else {
- USBD_CtlSendData(dev, state->cmd_buffer,
- MIN_8bits(req->wLength, USB_CDC_MAX_CMD_PACKET_LEN));
+ USBD_CtlError(dev, req);
}
} else { // USB_REQ_DIR_H2D
if (req->wLength > 0) {
- USBD_CtlPrepareRx(dev, state->cmd_buffer,
+ USBD_CtlPrepareRx(dev, dev->ep0_buf,
MIN_8bits(req->wLength, USB_CDC_MAX_CMD_PACKET_LEN));
}
}
### core/embed/io/usb/stm32/usbd_core.c
@@ -378,7 +378,7 @@ USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev ,uint8_t epnum,
/* Prepare endpoint for premature end of transfer */
USBD_LL_PrepareReceive (pdev,
0,
- NULL,
+ pdev->ep0_buf,
0);
}
else
@@ -394,7 +394,7 @@ USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev ,uint8_t epnum,
/* Prepare endpoint for premature end of transfer */
USBD_LL_PrepareReceive (pdev,
0,
- NULL,
+ pdev->ep0_buf,
0);
}
else
### core/embed/io/usb/stm32/usbd_def.h
@@ -251,6 +251,11 @@ typedef struct _USBD_HandleTypeDef
void *pClassData;
void *pUserData;
void *pData;
+ /* EP0 OUT reception buffer: class request data, and the packets that
+ carry no data for the stack (status stage, premature end of an IN
+ transfer). The OTG core accepts a full packet on EP0 however short the
+ armed length, so the buffer must hold one. */
+ uint8_t ep0_buf[USB_MAX_EP0_SIZE];
} USBD_HandleTypeDef;
/**
### core/embed/io/usb/stm32/usbd_ioreq.c
@@ -204,7 +204,7 @@ USBD_StatusTypeDef USBD_CtlReceiveStatus (USBD_HandleTypeDef *pdev)
/* Start the transfer */
USBD_LL_PrepareReceive ( pdev,
0,
- NULL,
+ pdev->ep0_buf,
0);
return USBD_OK;
### core/embed/projects/bootloader/.changelog.d/8166.fixed
@@ -0,0 +1 @@
+Fix a crash when a USB host sends unexpected data in the status stage of a control transfer.
### core/embed/projects/prodtest/.changelog.d/8166.fixed
@@ -0,0 +1 @@
+Fix a crash when a USB host sends unexpected data in the status stage of a control transfer.Why this scored 77/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.