* [PATCH] usb: core: devio: validate device and interface before buffer allocation
@ 2026-06-24 2:35 André Moreira
2026-07-10 13:14 ` Greg Kroah-Hartman
0 siblings, 1 reply; 3+ messages in thread
From: André Moreira @ 2026-06-24 2:35 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel, 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.
Signed-off-by: André Moreira <andrem.33333@gmail.com>
---
drivers/usb/core/devio.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index e191934623c73..8329d1c7d1b27 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:
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] usb: core: devio: validate device and interface before buffer allocation
2026-06-24 2:35 [PATCH] usb: core: devio: validate device and interface before buffer allocation André Moreira
@ 2026-07-10 13:14 ` Greg Kroah-Hartman
2026-07-10 20:29 ` Andre Moreira
0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-07-10 13:14 UTC (permalink / raw)
To: André Moreira; +Cc: linux-usb, linux-kernel
On Tue, Jun 23, 2026 at 11:35:31PM -0300, André Moreira wrote:
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] usb: core: devio: validate device and interface before buffer allocation
2026-07-10 13:14 ` Greg Kroah-Hartman
@ 2026-07-10 20:29 ` Andre Moreira
0 siblings, 0 replies; 3+ messages in thread
From: Andre Moreira @ 2026-07-10 20:29 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel
Thanks, Greg! Glad to help.
Em sex., 10 de jul. de 2026 às 10:14, Greg Kroah-Hartman
<gregkh@linuxfoundation.org> escreveu:
>
> On Tue, Jun 23, 2026 at 11:35:31PM -0300, André Moreira wrote:
> > 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-10 20:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-24 2:35 [PATCH] usb: core: devio: validate device and interface before buffer allocation André Moreira
2026-07-10 13:14 ` Greg Kroah-Hartman
2026-07-10 20:29 ` Andre Moreira
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox