Linux USB
 help / color / mirror / Atom feed
* [PATCH] HID: usbhid: drop pending output reports in usbhid_stop()
@ 2026-11-27  8:45 Yannick Müller
  2026-11-27  8:56 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Yannick Müller @ 2026-11-27  8:45 UTC (permalink / raw)
  To: Jiri Kosina, Benjamin Tissoires
  Cc: linux-input, linux-usb, Yannick Müller

Each entry of the queue (usbhid->out[]) points to the
struct hid_report it was built from. The queue lives in struct
usbhid_device, which exists as long as usbhid is bound to the USB
interface. The reports only live as long as one HID driver binding:
hid_device_remove() frees them with hid_close_report() right after the
driver has stopped the device.

usbhid_stop() empties the control queue but not the output queue, and
usbhid_start() does not reset it. Output reports that are still queued
when the HID driver is unbound, for example because the device has not
completed its interrupt-OUT transfers, therefore survive a rebind. The
first output report after the next usbhid_start() restarts the queue at
the old tail. hid_submit_out() then reads the stale report which
at this point has already been freed resulting in a use-after-free.

Drain the output queue in usbhid_stop() along with the control queue.
Make hid_irq_out() leave outtail alone when the queue is already empty,
so that the completion of the URB killed right afterwards cannot move
outtail past outhead.

Fixes: a7197c2e4120 ("HID: Handle driver-specific device descriptor in core")
Cc: stable@vger.kernel.org
Signed-off-by: Yannick Müller <mail@ynnk.dev>
---
 drivers/hid/usbhid/hid-core.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/usbhid/hid-core.c b/drivers/hid/usbhid/hid-core.c
index d7cf2b56e117..af92192fca05 100644
--- a/drivers/hid/usbhid/hid-core.c
+++ b/drivers/hid/usbhid/hid-core.c
@@ -452,7 +452,7 @@ static void hid_irq_out(struct urb *urb)
 
 	if (unplug) {
 		usbhid->outtail = usbhid->outhead;
-	} else {
+	} else if (usbhid->outhead != usbhid->outtail) {
 		usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
 
 		if (usbhid->outhead != usbhid->outtail &&
@@ -1260,6 +1260,12 @@ static void usbhid_stop(struct hid_device *hid)
 		usbhid->ctrltail = (usbhid->ctrltail + 1) &
 			(HID_CONTROL_FIFO_SIZE - 1);
 	}
+	while (usbhid->outtail != usbhid->outhead) {
+		kfree(usbhid->out[usbhid->outtail].raw_report);
+		usbhid->out[usbhid->outtail].raw_report = NULL;
+		usbhid->outtail = (usbhid->outtail + 1) &
+			(HID_OUTPUT_FIFO_SIZE - 1);
+	}
 	spin_unlock_irq(&usbhid->lock);
 
 	usb_kill_urb(usbhid->urbin);

base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
-- 
2.47.3


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

* Re: [PATCH] HID: usbhid: drop pending output reports in usbhid_stop()
  2026-11-27  8:45 [PATCH] HID: usbhid: drop pending output reports in usbhid_stop() Yannick Müller
@ 2026-11-27  8:56 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-27  8:56 UTC (permalink / raw)
  To: Yannick Müller; +Cc: linux-usb

> Each entry of the queue (usbhid->out[]) points to the
> struct hid_report it was built from. The queue lives in struct
> usbhid_device, which exists as long as usbhid is bound to the USB
> interface. The reports only live as long as one HID driver binding:
> hid_device_remove() frees them with hid_close_report() right after the
> driver has stopped the device.
> 
> usbhid_stop() empties the control queue but not the output queue, and
> usbhid_start() does not reset it. Output reports that are still queued
> when the HID driver is unbound, for example because the device has not
> completed its interrupt-OUT transfers, therefore survive a rebind. The
> first output report after the next usbhid_start() restarts the queue at
> the old tail. hid_submit_out() then reads the stale report which
> at this point has already been freed resulting in a use-after-free.
> 
> Drain the output queue in usbhid_stop() along with the control queue.
> Make hid_irq_out() leave outtail alone when the queue is already empty,
> so that the completion of the URB killed right afterwards cannot move
> outtail past outhead.
> 
> Fixes: a7197c2e4120 ("HID: Handle driver-specific device descriptor in core")
> Cc: stable@vger.kernel.org
> Signed-off-by: Yannick Müller <mail@ynnk.dev>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127084612.222563-1-mail@ynnk.dev?part=1

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27  8:45 [PATCH] HID: usbhid: drop pending output reports in usbhid_stop() Yannick Müller
2026-11-27  8:56 ` sashiko-bot

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