[PATCH v10 6/6] usb: gadget: gadgetfs: avoid writing through the const setup pointer

From: Jing Lin via B4 Relay

Date: Thu Nov 26 2026 - 07:45:19 EST


From: Jing Lin <kayano050408@xxxxxxxxx>

gadgetfs_setup() receives the setup packet as const, but clamps an
oversized control-IN request by casting away the constness and writing
the truncated length back into the caller's wLength field.

The setup() callback has no business modifying the setup packet, and
ctrl->wLength is only used afterwards when initializing event->u.setup.
Store the clamped w_length into event->u.setup.wLength directly instead.

Suggested-by: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Jing Lin <kayano050408@xxxxxxxxx>
---
drivers/usb/gadget/legacy/inode.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index ed26cef30..e79a4e401 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1668,10 +1668,6 @@ gadgetfs_do_setup(struct dev_data *dev, const struct usb_ctrlrequest *ctrl,

if (w_length > RBUF_SIZE) {
if (ctrl->bRequestType & USB_DIR_IN) {
- /* Cast away the const, we are going to overwrite on purpose. */
- __le16 *temp = (__le16 *)&ctrl->wLength;
-
- *temp = cpu_to_le16(RBUF_SIZE);
w_length = RBUF_SIZE;
} else {
return value;
@@ -1828,6 +1824,8 @@ gadgetfs_do_setup(struct dev_data *dev, const struct usb_ctrlrequest *ctrl,
/* state changes when reader collects event */
event = next_event (dev, GADGETFS_SETUP);
event->u.setup = *ctrl;
+ /* Report the length we can actually handle */
+ event->u.setup.wLength = cpu_to_le16(w_length);
ep0_readable (dev);
/*
* Return USB_GADGET_DELAYED_STATUS as a workaround to

--
Git-155)