public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Ethan Carter Edwards <ethan@ethancedwards.com>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: misc: adutux: replace kmalloc() with kmalloc_array()
Date: Wed, 21 May 2025 12:59:10 +0200	[thread overview]
Message-ID: <2025052111-able-unfocused-cdcf@gregkh> (raw)
In-Reply-To: <20250503-adutux_kmalloc_array-v1-1-80c74c4bd3e7@ethancedwards.com>

On Sat, May 03, 2025 at 04:43:21PM -0400, Ethan Carter Edwards wrote:
> Replace kmalloc with internal multiplication with kmalloc_array to
> improve code readability and prevent potential overflows.

But this is not an array of a structure size.

> 
> Signed-off-by: Ethan Carter Edwards <ethan@ethancedwards.com>
> ---
>  drivers/usb/misc/adutux.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/misc/adutux.c b/drivers/usb/misc/adutux.c
> index ed6a19254d2ff9fead898adad0b3996822e10167..000a3ade743258f381d85397395a43c28a8481cc 100644
> --- a/drivers/usb/misc/adutux.c
> +++ b/drivers/usb/misc/adutux.c
> @@ -680,7 +680,7 @@ static int adu_probe(struct usb_interface *interface,
>  	in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
>  	out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
>  
> -	dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
> +	dev->read_buffer_primary = kmalloc_array(4, in_end_size, GFP_KERNEL);

This is a buffer and you need the size to be correct based on the
commands, right?  It's not an array of a structure, but rather a stream
of bytes.


>  	if (!dev->read_buffer_primary)
>  		goto error;
>  
> @@ -690,7 +690,7 @@ static int adu_probe(struct usb_interface *interface,
>  	memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
>  	memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
>  
> -	dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
> +	dev->read_buffer_secondary = kmalloc_array(4, in_end_size, GFP_KERNEL);

Same here.

I think the original code is just fine as there's no bug here or way it
can overflow, right?

thanks,

greg k-h

      reply	other threads:[~2025-05-21 10:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-03 20:43 [PATCH] usb: misc: adutux: replace kmalloc() with kmalloc_array() Ethan Carter Edwards
2025-05-21 10:59 ` Greg Kroah-Hartman [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2025052111-able-unfocused-cdcf@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=ethan@ethancedwards.com \
    --cc=gustavoars@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox