vendor: a bulk OUT completion queued before SET_INTERFACE re-arms over vendord_set_alt's receive

Open
#4,476 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
embedded-iot

Research direction

Start with vendor_device.c at vendord_set_alt and vendord_xfer_cb, then trace queued endpoint completions through usbd.c and the nrf5x implementation in dcd_nrf5x.c. The report is a code trace and has not been reproduced; first verify the event ordering and failure on nrf5x with alternate settings enabled. Done means stale OUT completions cannot deliver data from the previous setting or cause the receive to be armed twice.

Written by the indexing model from the issue text.

Description

Bug 🐞 follow-up Prio 📌

Greptile raised this in adafruit/Adafruit_TinyUSB_Arduino#615, the vendored sync of d543126c5. I checked it against
26adc208a; it is unchanged on current master.

Trigger

  • CFG_TUD_VENDOR_ALT_SETTINGS=1 with CFG_TUD_VENDOR_RX_MANUAL_XFER=0, the default.
  • SET_INTERFACE to a setting that reuses the armed bulk OUT address: the same setting again, or two settings sharing
    the endpoint.
  • An OUT completion on that endpoint queued after the SETUP event. On nrf5x this needs no host misbehaviour.
    dcd_int_handler queues EP0SETUP before OUT END in the same ISR pass (dcd_nrf5x.c:988-993), and a bulk OUT
    completes only after its EasyDMA run.

Causal path

  1. vendord_control_xfer_cb → vendord_set_alt (vendor_device.c:885-888). The stall and clear-stall (:569-570,
    :605-606) retire the old transfer in the DCD, but its completion is already queued.
  2. usbd_edpt_xfer arms a new receive (:614-615). nrf5x sets started = true (dcd_nrf5x.c:576-577).
  3. The stale completion is dispatched (usbd.c:742-776). The endpoint is still bound to the vendor driver, so nothing
    filters it; #4385 only drops completions on unbound endpoints. usbd clears BUSY and CLAIMED (:757-760).
  4. vendord_xfer_cb (vendor_device.c:956-961) delivers the old setting's data through tud_vendor_rx_cb and calls
    usbd_edpt_xfer again. The usbd BUSY check passes (usbd.c:1730), and nrf5x fails TU_ASSERT(!xfer->started)
    (dcd_nrf5x.c:557).

Consequence

  • With a debugger attached, or DHCSR.C_DEBUGEN left set, the assert runs BKPT, which escalates to a HardFault lockup.
  • Otherwise usbd_edpt_xfer returns false while the DCD keeps the receive armed, and RX resumes on the next packet.
  • In both cases one packet from the old setting reaches the app as new-setting data. rx_cb reads the buffer that the
    still-armed receive may DMA into.
  • Ports without the started check re-program an armed endpoint; not analysed.

Related

  • #4212 item 1: the same usbd mechanism, for nrf5x data_received.
  • #3962: dcd_edpt_abort_xfer.
  • #4385 and #4428.

Uncertain

  • Not reproduced; this is a code trace only, and DCDs other than nrf5x were not checked.
  • RX_MANUAL_XFER=1 can probably hit the same assert when an app thread arms the endpoint between the two events; not
    traced. The only in-tree ALT_SETTINGS example, usbtest, uses RX_MANUAL_XFER=1.
  • A fix likely needs a per-endpoint transfer generation in usbd to drop completions of retired transfers.
Dominant language
C
Stars
7.3k
Forks
1.6k
Avg merge
1d 19h
Merged PRs (30d)
67

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hathach/tinyusb

All issues in hathach/tinyusb

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.