[PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol
From: Hongyu Xie
Date: Wed Dec 02 2026 - 19:48:50 EST
Drivers embed struct usb_anchor in private structures and free them
once usb_wait_anchor_empty_timeout() returns non-zero (for example,
xpad_disconnect() frees xpad->irq_out_anchor).
Two races allow a waiter to return and free the anchor while an URB
giveback still touches anchor->wait:
1. usb_anchor_resume_wakeups() decrements suspend_wakeups and calls
wake_up(&anchor->wait) without holding anchor->lock. A waiter can
observe suspend_wakeups == 0 and free the anchor before wake_up()
runs.
2. usb_wait_anchor_empty_timeout() evaluates usb_anchor_check_wakeup()
without holding anchor->lock, so it can observe an empty urb_list
while __usb_unanchor_urb() has not yet finished wake_up() and
released anchor->lock.
Take anchor->lock via guard(spinlock_irqsave) in
usb_anchor_check_wakeup() and usb_anchor_resume_wakeups(). Keep
__usb_anchor_check_wakeup() for callers that already hold anchor->lock,
and annotate both it and __usb_unanchor_urb() with __must_hold() and
lockdep_assert_held().
syzbot reported this race repeatedly:
BUG: KASAN: use-after-free in register_lock_class+0xe6a/0x1120 kernel/locking/lockdep.c:1336
[...]
__wake_up_common_lock+0xb4/0x130 kernel/sched/wait.c:136
usb_anchor_resume_wakeups drivers/usb/core/urb.c:958 [inline]
usb_anchor_resume_wakeups+0xbe/0xe0 drivers/usb/core/urb.c:951
__usb_hcd_giveback_urb+0x2df/0x5c0 drivers/usb/core/hcd.c:1674
Freed by task 3889:
[...]
kfree+0x173/0x390 mm/slab.c:3786
xpad_disconnect+0x1cb/0x530 drivers/input/joystick/xpad.c:1905
Fixes: 6ec4147e7bdb ("usb-anchor: Delay usb_wait_anchor_empty_timeout wake up till completion is done")
Fixes: 51a2f077c44e ("USB: introduce usb_anchor")
Reported-by: syzbot+7f5e98b052c766f47f68@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/r/00000000000090170a0614431f47@xxxxxxxxxx
Reported-by: syzbot+59d3714b44d79d02f944@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://lore.kernel.org/r/00000000000038435e05e9f94cd4@xxxxxxxxxx
Cc: stable@xxxxxxxxxxxxxxx
Cc: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
Cc: Oliver Neukum <oliver@xxxxxxxxxx>
Cc: Hans de Goede <hansg@xxxxxxxxxx>
Signed-off-by: Hongyu Xie <xiehongyu1@xxxxxxxxxx>
---
drivers/usb/core/urb.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index ae0d61b5081f..5ecde5dc613e 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -6,6 +6,7 @@
#include <linux/module.h>
#include <linux/string.h>
#include <linux/bitops.h>
+#include <linux/cleanup.h>
#include <linux/slab.h>
#include <linux/log2.h>
#include <linux/kmsan.h>
@@ -139,19 +140,30 @@ void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor)
}
EXPORT_SYMBOL_GPL(usb_anchor_urb);
-static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
+static int __usb_anchor_check_wakeup(struct usb_anchor *anchor)
+ __must_hold(&anchor->lock)
{
+ lockdep_assert_held(&anchor->lock);
+
return atomic_read(&anchor->suspend_wakeups) == 0 &&
list_empty(&anchor->urb_list);
}
-/* Callers must hold anchor->lock */
+static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
+{
+ guard(spinlock_irqsave)(&anchor->lock);
+ return __usb_anchor_check_wakeup(anchor);
+}
+
static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
+ __must_hold(&anchor->lock)
{
+ lockdep_assert_held(&anchor->lock);
+
urb->anchor = NULL;
list_del(&urb->anchor_list);
usb_put_urb(urb);
- if (usb_anchor_check_wakeup(anchor))
+ if (__usb_anchor_check_wakeup(anchor))
wake_up(&anchor->wait);
}
@@ -835,7 +847,7 @@ void usb_kill_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irq(&anchor->lock);
cpu_relax();
@@ -874,7 +886,7 @@ void usb_poison_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irq(&anchor->lock);
cpu_relax();
@@ -930,8 +942,9 @@ void usb_anchor_resume_wakeups(struct usb_anchor *anchor)
if (!anchor)
return;
+ guard(spinlock_irqsave)(&anchor->lock);
atomic_dec(&anchor->suspend_wakeups);
- if (usb_anchor_check_wakeup(anchor))
+ if (__usb_anchor_check_wakeup(anchor))
wake_up(&anchor->wait);
}
EXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups);
@@ -1005,7 +1018,7 @@ void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
struct urb, anchor_list);
__usb_unanchor_urb(victim, anchor);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irqrestore(&anchor->lock, flags);
cpu_relax();
--
2.32.0