Linux USB
 help / color / mirror / Atom feed
* [PATCH v3 0/3] usb: core: fix three use-after-free windows in the USB anchor protocol
@ 2026-11-27 13:28 Hongyu Xie
  2026-11-27 13:28 ` [PATCH v3 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Hongyu Xie @ 2026-11-27 13:28 UTC (permalink / raw)
  To: Greg KH, Alan Stern
  Cc: Oliver Neukum, oliver, Hans de Goede, linux-usb, linux-kernel

Anchors are usually embedded in driver-private structures, and
several anchor API returns -- a non-zero
usb_wait_anchor_empty_timeout(), usb_kill_anchored_urbs(),
usb_poison_anchored_urbs() and usb_scuttle_anchored_urbs() --
license the caller to free that memory.  The wakeup protocol backing
this license has three windows in which a URB giveback or unanchor
still touches the anchor after the caller has been told it is
unused:

 1. The waiter's condition check and the giveback's
    suspend_wakeups decrement plus wake_up() were not serialized,
    so the waiter could return while a giveback was still in
    flight.  Reported by syzbot repeatedly since 2019; patch 1
    references the two reports this analysis is built on.

 2. __usb_hcd_giveback_urb() captured urb->anchor at the top of
    the function but incremented suspend_wakeups only after DMA
    unmapping and the usbmon hooks.  A concurrent unanchor could
    empty the anchor with suspend_wakeups still zero, so the waiter
    returned and freed the anchor underneath the pending increment.
    No syzbot report is attributed to this interleaving; it was
    found by extending the analysis of patch 1.

 3. usb_unanchor_urb() and the giveback read urb->anchor and only
    then acquire anchor->lock; between the two the anchor can
    already have been freed.  The gap is as old as
    usb_unanchor_urb() itself and cannot be closed with a
    reference count: the first dereference of the freshly read
    pointer would have to be the increment itself.

Patch 1 completes the locking protocol for the waiter/wakeup side.
Patch 2 folds the giveback's pointer capture, the increment and the
unanchor into a single anchor->lock critical section.  Patch 3
turns the wakeup protocol into a contention-gated RCU lifetime
protocol: lockless anchor readers register in a usbcore-private
per-URB counter (urb->anchor_readers) inside RCU read-side critical
sections, urb->anchor is published with rcu_assign_pointer(),
__usb_unanchor_urb() snapshots get_state_synchronize_rcu() onto the
anchor only when another reader is concurrently in flight on that
URB, and every teardown return that lets the caller free the anchor
calls cond_synchronize_rcu() only when an unelapsed contended grace
period is pending (touching nothing on the anchor after unlocking).

Contract notes for patch 3:

 - usb_kill_anchored_urbs(), usb_poison_anchored_urbs() and
   usb_scuttle_anchored_urbs() may sleep even on an empty anchor
   (when draining an in-flight giveback pin or waiting for a
   contended unanchor's grace period): the empty-anchor retry blocks
   on the anchor's wait queue instead of busy-waiting.
   usb_scuttle_anchored_urbs() is the only sleeping-context contract
   change among the three (the others already slept transitively
   through usb_kill_urb()/usb_poison_urb()), and all three carry a
   might_sleep() at entry so an atomic-context caller is caught at
   the call site with debugging configurations enabled.  All in-tree
   callers already sleep.

 - Uncontended waits and drains on persistent or empty anchors pay
   zero RCU grace periods, and usb_wait_anchor_empty_timeout()
   preserves its remaining-jiffies return value on uncontended
   waits.  Only when two threads actually raced to unanchor a URB on
   the anchor (and that grace period has not yet elapsed) does a
   successful wait or drain wait in cond_synchronize_rcu(), in which
   case usb_wait_anchor_empty_timeout() may overrun its timeout by
   up to one grace period and clamps its non-zero return to at
   least 1.

 - The safe-to-free license is per caller: a caller that frees on
   its own successful return must first wait for any other
   in-flight teardown calls to return.  The kerneldoc of all four
   teardown returns states this proviso alongside the no-new-URBs
   one.

Why the retry loop must not busy-wait: on !PREEMPT_RCU kernels
whose scheduler tick reports no quiescent state for a spinning
process context (the v6.6 stable tree, for one; the 7.3 base adds
process context only with CONFIG_PREEMPT_COUNT), a busy retry stalls
every grace period that still awaits that CPU's quiescent state for
as long as it spins, and under real-time scheduling it can starve
the very completion path it is waiting for.

The sleeping retry keeps the re-drain behavior of the busy loop it
replaces: usb_anchor_urb() wakes the anchor's wait queue (gated by
waitqueue_active() under anchor->lock, so the per-submission hot
path stays off the wakeup machinery when no drain is sleeping), and
the wait also ends -- without declaring the anchor unused -- when a
URB is anchored after the drain, so the drain restarts and tears
the late arrival down too.

No driver changes are needed: struct urb gains a usbcore-private
reader counter (anchor_readers), and struct usb_anchor records the
conditional grace-period state (need_rcu_gp and rcu_gp_seq).

checkpatch: 0 errors, 0 warnings for all three patches.

Signed-off-by: Hongyu Xie <xiehongyu1@kylinos.cn>
---

Changes in v3 (patch 3 only; patches 1 and 2 are unchanged):

 - Gate the teardown RCU grace period on actual unanchor contention
   (urb->anchor_readers + anchor->need_rcu_gp / anchor->rcu_gp_seq +
   cond_synchronize_rcu()) instead of calling synchronize_rcu()
   unconditionally on every successful wait and drain.  Runtime I/O
   paths that wait on or drain persistent or empty anchors without
   unanchor races (such as zd1211rw register write batches, hiface
   PCM stream stops, usbtmc reads and btusb traffic stops) now pay
   zero grace periods, and usb_wait_anchor_empty_timeout() preserves
   its remaining-jiffies return value on uncontended waits.

 - Link to v2: https://lore.kernel.org/r/179577006613.23588.1120308700542393912@kylinos.cn

Changes in v2 (patches 2 and 3; patch 1 is unchanged; rebased onto
v7.4-rc3 in RESEND v2):

 - Patch 2's usb_unanchor_urb_and_suspend_wakeups() race-loser
   path unlocked each branch's locked anchor before clearing the
   local anchor pointer to NULL.

 - Replaced the kill/poison/scuttle empty-anchor busy-wait retry
   with a sleepable wait_event() on anchor->wait that re-drains
   late-anchored URBs via waitqueue_active()-gated wakeups in
   usb_anchor_urb().

 - Dropped teardown-side suspend_wakeups pins and updated kerneldoc
   and Documentation/driver-api/usb/anchors.rst to document the
   teardown lifetime contract and per-caller proviso.

Hongyu Xie (3):
      usb: core: Fix use-after-free in anchor wakeup protocol
      usb: core: Close anchor capture-to-increment race in URB giveback
      usb: core: Close residual anchor read-to-lock window with RCU

 Documentation/driver-api/usb/anchors.rst |  26 +-
 drivers/usb/core/hcd.c                   |  10 +-
 drivers/usb/core/urb.c                   | 490 ++++++++++++++++++++++++++++---
 drivers/usb/core/usb.h                   |   1 +
 include/linux/usb.h                      |  10 +-
 5 files changed, 488 insertions(+), 49 deletions(-)

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.39.5

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-27 13:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 13:28 [PATCH v3 0/3] usb: core: fix three use-after-free windows in the USB anchor protocol Hongyu Xie
2026-11-27 13:28 ` [PATCH v3 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
2026-11-27 13:57   ` Greg KH
2026-11-27 13:28 ` [PATCH v3 2/3] usb: core: Close anchor capture-to-increment race in URB giveback Hongyu Xie
2026-11-27 13:29 ` [PATCH v3 3/3] usb: core: Close residual anchor read-to-lock window with RCU Hongyu Xie
2026-11-27 13:40 ` [PATCH v3 0/3] usb: core: fix three use-after-free windows in the USB anchor protocol sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox