public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user
@ 2025-09-19 11:56 Thorsten Blum
  2025-10-08 11:05 ` Greg Kroah-Hartman
  2025-10-08 21:23 ` Oliver Neukum
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Blum @ 2025-09-19 11:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Alan Stern, Rex Nie, Jann Horn
  Cc: Thorsten Blum, linux-usb, linux-kernel

Replace kmalloc() followed by copy_from_user() with memdup_user() to
simplify and improve proc_do_submiturb(). Replace the hard-coded 8 bytes
with the size of 'struct usb_ctrlrequest'.

Return early if an error occurs, and avoid manually setting 'ret' and
using 'goto error'.

No functional changes intended.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 drivers/usb/core/devio.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
index f6ce6e26e0d4..3bc54a5c59ff 100644
--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -1670,13 +1670,9 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb
 		/* min 8 byte setup packet */
 		if (uurb->buffer_length < 8)
 			return -EINVAL;
-		dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
-		if (!dr)
-			return -ENOMEM;
-		if (copy_from_user(dr, uurb->buffer, 8)) {
-			ret = -EFAULT;
-			goto error;
-		}
+		dr = memdup_user(uurb->buffer, sizeof(struct usb_ctrlrequest));
+		if (IS_ERR(dr))
+			return PTR_ERR(dr);
 		if (uurb->buffer_length < (le16_to_cpu(dr->wLength) + 8)) {
 			ret = -EINVAL;
 			goto error;
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user
  2025-09-19 11:56 [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user Thorsten Blum
@ 2025-10-08 11:05 ` Greg Kroah-Hartman
  2025-10-08 21:23 ` Oliver Neukum
  1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2025-10-08 11:05 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: Alan Stern, Rex Nie, Jann Horn, linux-usb, linux-kernel

On Fri, Sep 19, 2025 at 01:56:50PM +0200, Thorsten Blum wrote:
> Replace kmalloc() followed by copy_from_user() with memdup_user() to
> simplify and improve proc_do_submiturb(). Replace the hard-coded 8 bytes
> with the size of 'struct usb_ctrlrequest'.
> 
> Return early if an error occurs, and avoid manually setting 'ret' and
> using 'goto error'.
> 
> No functional changes intended.


Same comments as the other patch that you submitted doing the same thing
for a different part of the same function.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user
  2025-09-19 11:56 [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user Thorsten Blum
  2025-10-08 11:05 ` Greg Kroah-Hartman
@ 2025-10-08 21:23 ` Oliver Neukum
  1 sibling, 0 replies; 3+ messages in thread
From: Oliver Neukum @ 2025-10-08 21:23 UTC (permalink / raw)
  To: Thorsten Blum, Greg Kroah-Hartman, Alan Stern, Rex Nie, Jann Horn
  Cc: linux-usb, linux-kernel

Hi,

On 19.09.25 13:56, Thorsten Blum wrote:
> Replace kmalloc() followed by copy_from_user() with memdup_user() to
> simplify and improve proc_do_submiturb(). Replace the hard-coded 8 bytes
> with the size of 'struct usb_ctrlrequest'.
> 
> Return early if an error occurs, and avoid manually setting 'ret' and
> using 'goto error'.
> 
> No functional changes intended.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>   drivers/usb/core/devio.c | 10 +++-------
>   1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c
> index f6ce6e26e0d4..3bc54a5c59ff 100644
> --- a/drivers/usb/core/devio.c
> +++ b/drivers/usb/core/devio.c
> @@ -1670,13 +1670,9 @@ static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb
>   		/* min 8 byte setup packet */
>   		if (uurb->buffer_length < 8)
>   			return -EINVAL;
> -		dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
> -		if (!dr)
> -			return -ENOMEM;
> -		if (copy_from_user(dr, uurb->buffer, 8)) {
> -			ret = -EFAULT;
> -			goto error;
> -		}
> +		dr = memdup_user(uurb->buffer, sizeof(struct usb_ctrlrequest));

You cannot do this. User space cannot and must not know or care how long
struct usb_ctrlrequest is. It is a data structure internal to the kernel.
For the purpose of this API we copy 8 bytes. That is set in stone.
If the kernel's data structure ever changes length, we will have
to define a new ioctl.

You have to leave the literal 8.

	Regards
		Oliver


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-08 21:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-19 11:56 [PATCH] USB: core: replace kmalloc + copy_from_user with memdup_user Thorsten Blum
2025-10-08 11:05 ` Greg Kroah-Hartman
2025-10-08 21:23 ` Oliver Neukum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox