usb: device: msc: usb_dc_stm32_isr causing memory data access violation fault #63330
Labels
area: USB
Universal Serial Bus
bug
The issue is a bug, or the PR is fixing a bug
priority: low
Low impact/importance bug
Describe the bug
When usb_dc_stm32_isr is invoked and performs a USB DC Reset (by calling msd_init()) it clears the curr_offset to zero. If this ISR is activated after memoryWrite() has been called and has updated curr_offset to a block size (thus thread_op will be set to THREAD_OP_WRITE_QUEUED), then when thread_memory_write_done() is invoked the curr_offset will be reset to 0 which will result in overflowed_len being set to a negative number. Since it is treated as a unsigned integer it results in memmove attempting to move a very large block of data resulting in a memory access violation, and thus a fault and reset.
Commit that changed this behavior was: 6d4266a
To Reproduce
Not fully sure how to consistently reproduce, but the following may work often.
in
and
in
and
in
When the breakpoint at
is reached wait about 20 seconds, before pressing play. (I have also the USB Console enabled and I am seeing the console on my computer show USB disconnected before continuing, perhaps this is what is invoking the USB ISR).
After resuming the reset should occur by hitting the breakpoint
followed by
where overflowed_len should be a very large value, resulting in a memory access violation.
Stacks at the breakpoints are:
usb_dc_stm32_isr -> HAL_PCD_IRQHandler -> PCD_EP_OutXfrComplete_init -> HAL_PCD_DataOutStageCallback -> mass_storage_bulk_out -> memoryWrite
usb_dc_stm32_isr -> HAL_PCD_IRQHandler -> HAL_PCD_ResetCallback -> forward_status_cb -> mass_storage_status_cb -> msd_init
z_thread_entry -> mass_thread_main -> thread_memory_write_done
Expected behavior
When the USB ISR is invoked, after the memoryWrite call sets up the buffer for a write of a block size or greater, but before the thread memory write done is invoked, thread memory write should not attempt to write a block size of an invalid length.
Impact
Minimal impact with the patch I added to my code base, but might not be best long term solution.
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: