Linux Input/HID development
 help / color / mirror / Atom feed
From: "Yannick Müller" <mail@ynnk.dev>
To: Jiri Kosina <jikos@kernel.org>, Benjamin Tissoires <bentiss@kernel.org>
Cc: linux-input@vger.kernel.org, linux-usb@vger.kernel.org,
	"Yannick Müller" <mail@ynnk.dev>
Subject: [PATCH] HID: usbhid: drop pending output reports in usbhid_stop()
Date: Fri, 27 Nov 2026 09:45:10 +0100	[thread overview]
Message-ID: <20261127084612.222563-1-mail@ynnk.dev> (raw)

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


                 reply	other threads:[~2026-11-27  8:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127084612.222563-1-mail@ynnk.dev \
    --to=mail@ynnk.dev \
    --cc=bentiss@kernel.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox