Re: [PATCH v3 3/4] usb: gadget: f_hid: fix race between userspace report update and GET_REPORT response

From: Neill Kapron

Date: Mon Nov 16 2026 - 17:12:04 EST


> The GET_REPORT response is served by pointing get_req->buf at the
> data[] array of a report_list entry and calling usb_ep_queue().
> The request completes asynchronously, so the controller may still
> be reading the entry's buffer after get_report_workqueue_handler()
> drops get_report_spinlock.
>
> At the same time, userspace can update that same entry via the
> GADGET_HID_WRITE_GET_REPORT ioctl: f_hidg_get_report() copies new
> report data into the entry while holding the spinlock, but the
> transfer reading the entry is not serialized against that update
> (the lock was already released by the work handler).
>
> The result is that the host can receive a GET_REPORT response that
> mixes bytes of the old and the new report (a torn write), with no
> memory-safety violation but corrupted report data on the wire.
>
> Serve the response from a dedicated buffer allocated once in
> hidg_alloc() and freed in hidg_release(): the handler copies the
> entry data into get_req_buf and zeroes the rest before queueing,
> so userspace updates to the entry can no longer race with the
> asynchronous read of the previous response, and no uninitialized
> slab memory is exposed to the host. Placing the buffer at the
> function-instance level (rather than bind/unbind) means it is only
> freed after all USB activity has ceased and all userspace file
> references are gone, so no disable/set_alt/unbind ordering
> constraints apply.
>
> This also removes the now unused get_report member from struct
> f_hidg.
>
> Fixes: a139c98f760e ("USB: gadget: f_hid: Add GET_REPORT via userspace IOCTL")
> Signed-off-by: Shunbo Shi <shishunbo@xxxxxxxxxxxxxxxxxxxx>
> ---
> drivers/usb/gadget/function/f_hid.c | 36 +++++++++++++++++++++++++----
> 1 file changed, 32 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c
> index d7ba30323414..8c2adf392389 100644
> --- a/drivers/usb/gadget/function/f_hid.c
> +++ b/drivers/usb/gadget/function/f_hid.c
> @@ -94,7 +94,7 @@ struct f_hidg {
>
> /* get report */
> struct usb_request *get_req;
> - struct usb_hidg_report get_report;
> + void *get_req_buf;
> bool get_report_returned;
> int get_report_req_report_id;
> int get_report_req_report_length;
> @@ -128,6 +128,7 @@ static void hidg_release(struct device *dev)
> kfree(entry);
> }
>
> + kfree(hidg->get_req_buf);
> kfree(hidg->report_desc);
> kfree(hidg->set_report_buf);
> kfree(hidg);
> @@ -568,6 +569,7 @@ static void get_report_workqueue_handler(struct work_struct *work)
> struct usb_request *req;
> struct report_entry *ptr;
> unsigned long flags;
> + unsigned int copied;
>
> int status = 0;
>
> @@ -587,7 +589,10 @@ static void get_report_workqueue_handler(struct work_struct *work)
> ptr = f_hidg_search_for_report(hidg, hidg->get_report_req_report_id);
> if (ptr && !ptr->report_data.userspace_req) {
> /* Report exists in list and it is to be used for immediate response */
> - req->buf = ptr->report_data.data;
> + copied = min_t(unsigned int, req->length, ptr->report_data.length);
> + memcpy(req->buf, ptr->report_data.data, copied);
> + if (copied < MAX_REPORT_LENGTH)
> + memset(req->buf + copied, 0x0, MAX_REPORT_LENGTH - copied);

Isn't this a silent UAPI change? f_hidg_get_report() already
initializes the report and req->length is already bound by
MAX_REPORT_LENGTH. Can't we just memcpy req->length bytes to keep the
existing UAPI behavior without exposing the slab memory or needing
memset?

--
Neill Kapron <nkapron@xxxxxxxxxx>