From: Hui Peng <benquike@gmail.com>
To: gregkh@linuxfoundation.org, Chris.Wulff@biamp.com, david.sands@biamp.com
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/2] usb: gadget: f_hid: don't call copy_from_user() under get_report_spinlock
Date: Sat, 19 Sep 2026 11:00:49 +0000 [thread overview]
Message-ID: <20260919110050.3764064-1-benquike@gmail.com> (raw)
In-Reply-To: <2026091905-humbling-swooned-c371@gregkh>
f_hidg_get_report() already copies the incoming struct usb_hidg_report
from userspace into the freshly allocated 'entry' before acquiring
hidg->get_report_spinlock.
When a report with the same report_id is already present in
hidg->report_list, the update path copies from userspace a second time,
this time directly into ptr->report_data while the spinlock is held with
interrupts disabled:
spin_lock_irqsave(&hidg->get_report_spinlock, flags);
ptr = f_hidg_search_for_report(hidg, report_id);
if (ptr) {
if (copy_from_user(&ptr->report_data, buffer,
sizeof(struct usb_hidg_report))) {
copy_from_user() may fault and sleep, so this is a sleeping function
called from atomic context, reported by CONFIG_DEBUG_ATOMIC_SLEEP as
"BUG: sleeping function called from invalid context". Userspace can
reach it at will by issuing GADGET_HID_WRITE_GET_REPORT twice with the
same report_id on /dev/hidgN.
The second copy is also redundant: the same user buffer has already been
copied into entry->report_data a few lines above, and report_id was
derived from that copy. Assign from the already copied data instead,
which removes the fault from the critical section and makes the update
atomic with respect to the list lookup.
Fixes: a139c98f760e ("USB: gadget: f_hid: Add GET_REPORT via userspace IOCTL")
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
v2: Add the Fixes: tag Greg asked for.
f_hidg_get_report(), the spinlock and the copy_from_user() call under
it were all added together by a139c98f760e ("USB: gadget: f_hid: Add
GET_REPORT via userspace IOCTL"), first released in v6.12, so that is
where this starts. git blame on the removed lines agrees.
Found by inspection while investigating the use-after-free fixed in
patch 2/2, and build tested only; I do not have HID gadget hardware,
but the path is reachable from /dev/hidgN with dummy_hcd.
drivers/usb/gadget/function/f_hid.c | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
--- a/drivers/usb/gadget/function/f_hid.c
+++ b/drivers/usb/gadget/function/f_hid.c
@@ -668,13 +668,7 @@ static int f_hidg_get_report(struct file
if (ptr) {
/* Report already exists in list - update it */
- if (copy_from_user(&ptr->report_data, buffer,
- sizeof(struct usb_hidg_report))) {
- spin_unlock_irqrestore(&hidg->get_report_spinlock, flags);
- ERROR(cdev, "copy_from_user error\n");
- kfree(entry);
- return -EINVAL;
- }
+ ptr->report_data = entry->report_data;
kfree(entry);
} else {
/* Report does not exist in list - add it */
--
2.43.0
next prev parent reply other threads:[~2026-09-19 11:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-19 9:07 [PATCH " Hui Peng
2026-09-19 9:07 ` [PATCH 2/2] usb: gadget: f_hid: fix use-after-free of hidg->func.config after unbind Hui Peng
2026-09-19 10:28 ` Greg Kroah-Hartman
2026-09-19 10:28 ` [PATCH 1/2] usb: gadget: f_hid: don't call copy_from_user() under get_report_spinlock Greg Kroah-Hartman
2026-09-19 11:00 ` Hui Peng [this message]
2026-09-19 11:00 ` [PATCH v2 2/2] usb: gadget: f_hid: fix use-after-free of hidg->func.config after unbind Hui Peng
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=20260919110050.3764064-1-benquike@gmail.com \
--to=benquike@gmail.com \
--cc=Chris.Wulff@biamp.com \
--cc=david.sands@biamp.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@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
all inboxes | Powered by JetHome®