summaryrefslogtreecommitdiff
path: root/drivers/usb/core
diff options
context:
space:
mode:
authorAndré Moreira <andrem.33333@gmail.com>2026-06-23 23:35:31 -0300
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2026-07-10 15:14:33 +0200
commit83f0355cf48e4e69bf6d1b9776650ce2275f8ae8 (patch)
tree635f0143ce898eca090737eda439c4492a1fd264 /drivers/usb/core
parent612551bf7cf7113b8bbd5e8c1eea86d9427c2f95 (diff)
downloadlinux-next-83f0355cf48e4e69bf6d1b9776650ce2275f8ae8.tar.gz
linux-next-83f0355cf48e4e69bf6d1b9776650ce2275f8ae8.zip
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. Signed-off-by: André Moreira <andrem.33333@gmail.com> Link: https://patch.msgid.link/20260624023532.63009-1-andrem.33333@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/usb/core')
-rw-r--r--drivers/usb/core/devio.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index e191934623c7..8329d1c7d1b2 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -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: