Skip to contentSkip to conversation

usb: core: devio: validate device and interface before buffer allocation

PATCH In review André Moreira 2 participants 3 messages Jun 24 – Jul 10 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: core: devio: validate device and interface before buffer allocation

The proc_ioctl() function currently allocates a buffer using kmalloc() before checking the USB device state and resolving the interface number. If either validation fails, the function must free the buffer and return an error.

Move these checks to the top of the function to fail early. This avoids unnecessary memory allocation and deallocation on error paths, removes the nested 'else' structure, and eliminates redundant kfree() calls, making the code cleaner and easier to maintain.

---
 drivers/usb/core/devio.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/usb/core/devio.c +8 -5
index e191934623c73..8329d1c7d1b27 100644
@@ -2329,6 +2329,13 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
 	if (!connected(ps))
 		return -ENODEV;
 
+	if (ps->dev->state != USB_STATE_CONFIGURED)
+		return -EHOSTUNREACH;
+
+	intf = usb_ifnum_to_if(ps->dev, ctl->ifno);
+	if (!intf)
+		return -EINVAL;
+
 	/* alloc buffer */
 	size = _IOC_SIZE(ctl->ioctl_code);
 	if (size > 0) {
@@ -2345,11 +2352,7 @@ static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
 		}
 	}
 
-	if (ps->dev->state != USB_STATE_CONFIGURED)
-		retval = -EHOSTUNREACH;
-	else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
-		retval = -EINVAL;
-	else switch (ctl->ioctl_code) {
+	switch (ctl->ioctl_code) {
 
 	/* disconnect kernel driver from interface */
 	case USBDEVFS_DISCONNECT:
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · André Moreira

The proc_ioctl() function currently allocates a buffer using kmalloc() before checking the USB device state and resolving the interface number. If either validation fails, the function must free the buffer and return an error.

Move these checks to the top of the function to fail early. This avoids unnecessary memory allocation and deallocation on error paths, removes the nested 'else' structure, and eliminates redundant kfree() calls, making the code cleaner and easier to maintain.

Nice find and cleanup, thanks!

greg k-h

#2 lore raw

Thanks, Greg! Glad to help.

Em sex., 10 de jul. de 2026 às 10:14, Greg Kroah-Hartman <[email protected]> escreveu:

Quoted 15 lines · André Moreira
Quoted 9 lines

The proc_ioctl() function currently allocates a buffer using kmalloc() before checking the USB device state and resolving the interface number. If either validation fails, the function must free the buffer and return an error.

Move these checks to the top of the function to fail early. This avoids unnecessary memory allocation and deallocation on error paths, removes the nested 'else' structure, and eliminates redundant kfree() calls, making the code cleaner and easier to maintain.

Nice find and cleanup, thanks!

greg k-h