[PATCH v5 3/3] usb: gadget: gadgetfs: avoid writing through the const setup pointer

From: Jing Lin via B4 Relay

Date: Mon Nov 09 2026 - 00:56:06 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. Use a
local copy of the request for the clamped length instead. Userspace
still observes the truncated wLength in the GADGETFS_SETUP event, and
the queued request length is unchanged.

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

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index 4635a603e..23a36d556 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1510,15 +1510,16 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
struct usb_request *req = dev->req;
int value = -EOPNOTSUPP;
struct usb_gadgetfs_event *event;
+ struct usb_ctrlrequest ctrl_buf;
u16 w_value = le16_to_cpu(ctrl->wValue);
u16 w_length = le16_to_cpu(ctrl->wLength);

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);
+ /* Report the length we can actually handle to userspace. */
+ ctrl_buf = *ctrl;
+ ctrl_buf.wLength = cpu_to_le16(RBUF_SIZE);
+ ctrl = &ctrl_buf;
w_length = RBUF_SIZE;
} else {
return value;

--
Git-155)