From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380166; b=Y4SSWzbGLwnXWx+wnjH4Q8HMIjt0MvXOPhqddSiAhRWQNP4zPtaND3RQLzWK6KFeBlOk aHih761soNqafzbPPpnjmpQVwOXGDSrbxg0+A3fMTchQ65LwHl79UVC/IPVrCBfaYbwyv W9R5CzZZ3iO8+X9LUQObUb+RMkqkb4sNBCt7UfQpeV0tmjAGrvWTI4xawp3mN8AjvW4Ul Z/mKPfFpuGWgRJr4HImUKYgfxaUvjUYQ7RaJLqLG42T/Q13VUEAtJh9HBltJfIty2aICq mDBx+8AOQUCy287kV1tisqRZsguQChOnKYsk7GTyTx649rQQRZde1mocGM+7y+rU2NQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380166; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=5RuVa1op5gFNZOV7GcaiGynIJwluN0ju8x8uR7eiXxQ=; b=Fd5iWY0PzKN/AV99VbT084RavPNrYvz1nWE5MWudrD/rSGL5M0ubupedyvL5C5wGb3gb mmbgcMDzfk1UA8sBhJNfpw7L5D2Ep6sWuTfycHBHyGgOW7WsibVN18MMxrdkH44lkr+WF 3kfo7m191+JCNVTPM70gGl5gn1CZkPyqOY3yf15clRATC29ITemaXKxHp7drOStRAFzz3 zOGiYjI9sEIVUNBWvXxpCJWomUARDfvTXcWWa4XDGrinmpD9EiQNAd0JQk3itfg2beoae VRlUKsNyTVjrkP8QCJRmFLlmFq6yrnqD9v95NltCl8jZKUS6hTPxC/O6d5qC+jdcCQg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=lKgNKBxp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D00A606A336; Fri, 4 Dec 2026 10:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=lKgNKBxp Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7FF91F00934; Fri, 4 Dec 2026 10:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380166; bh=5RuVa1op5gFNZOV7GcaiGynIJwluN0ju8x8uR7eiXxQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lKgNKBxpTsA1r4V0FHCVcRbVoqs4SX7b1/kV0PJpMdUee7Zc6wUXb20Rp4lC/Mckq KzcP61ZrHCtmoCVfW2AcMyX1iWhv6HXoUOTcln2lIOMm/BcAyX+/Cjpmp79D/ZtIyv cqS96+Baer/XqN3KVb5kkhIoQIyrqd5DgT3STVXs= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Shunbo Shi , Sasha Levin Subject: [PATCH 7.3 155/742] usb: gadget: f_hid: fix race between userspace report update and GET_REPORT response Date: Fri, 4 Dec 2026 19:00:19 +0900 Message-ID: <20261204100639.234654315@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shunbo Shi [ Upstream commit 07ec204e8aecbfee425008832b18d5d720daca6d ] 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(). 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 Link: https://patch.msgid.link/20261117125051.5646-3-shishunbo@cmss.chinamobile.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/function/f_hid.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/drivers/usb/gadget/function/f_hid.c b/drivers/usb/gadget/function/f_hid.c index 670ae7959bbd7..e29e7d1da924a 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); @@ -587,7 +588,7 @@ 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; + memcpy(req->buf, ptr->report_data.data, req->length); status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); hidg->get_report_returned = true; spin_unlock_irqrestore(&hidg->get_report_spinlock, flags); @@ -622,10 +623,9 @@ static void get_report_workqueue_handler(struct work_struct *work) * Either get an updated response just serviced by userspace * or send the latest response in the list */ - req->buf = ptr->report_data.data; + memcpy(req->buf, ptr->report_data.data, req->length); } else { /* If there are no prevoiusly sent reports send empty report */ - req->buf = hidg->get_report.data; memset(req->buf, 0x0, req->length); } @@ -1161,6 +1161,15 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f) if (!hidg->get_req) return -ENOMEM; + /* + * The GET_REPORT response is served from a dedicated buffer + * (allocated in hidg_alloc()), not from the report_list + * entries: userspace may update an entry while the controller + * is still reading the previous response, which would corrupt + * the data on the wire. + */ + hidg->get_req->buf = hidg->get_req_buf; + hidg->get_req->zero = 0; hidg->get_req->complete = hidg_get_report_complete; hidg->get_req->context = hidg; @@ -1647,6 +1656,18 @@ static struct usb_function *hidg_alloc(struct usb_function_instance *fi) } hidg->use_out_ep = !opts->no_out_endpoint; + /* + * Dedicated buffer for GET_REPORT responses, allocated once for + * the whole lifetime of this function instance: hidg_release() + * is the only safe free point, as it runs after all USB + * activity has ceased and all file references are gone. + */ + hidg->get_req_buf = kmalloc(MAX_REPORT_LENGTH, GFP_KERNEL); + if (!hidg->get_req_buf) { + ret = -ENOMEM; + goto err_put_device; + } + ++opts->refcnt; mutex_unlock(&opts->lock); -- 2.53.0