linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] usb: gadget: f_fs: Use struct_size() and flex_array_size() helpers
@ 2022-01-20 22:29 Gustavo A. R. Silva
  2022-01-20 23:26 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2022-01-20 22:29 UTC (permalink / raw)
  To: Felipe Balbi, Greg Kroah-Hartman
  Cc: linux-usb, linux-kernel, Gustavo A. R. Silva, linux-hardening

Make use of the struct_size() and flex_array_size() helpers instead of
an open-coded version, in order to avoid any potential type mistakes
or integer overflows that, in the worst scenario, could lead to heap
overflows.

Also, address the following sparse warnings:
drivers/usb/gadget/function/f_fs.c:922:23: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/usb/gadget/function/f_fs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 25ad1e97a458..7461d27e9604 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -919,12 +919,12 @@ static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
 		data_len, ret);
 
 	data_len -= ret;
-	buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
+	buf = kmalloc(struct_size(buf, storage, data_len), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
 	buf->length = data_len;
 	buf->data = buf->storage;
-	memcpy(buf->storage, data + ret, data_len);
+	memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len));
 
 	/*
 	 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
-- 
2.27.0


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

* Re: [PATCH][next] usb: gadget: f_fs: Use struct_size() and flex_array_size() helpers
  2022-01-20 22:29 [PATCH][next] usb: gadget: f_fs: Use struct_size() and flex_array_size() helpers Gustavo A. R. Silva
@ 2022-01-20 23:26 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2022-01-20 23:26 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Felipe Balbi, Greg Kroah-Hartman, linux-usb, linux-kernel,
	linux-hardening

On Thu, Jan 20, 2022 at 04:29:33PM -0600, Gustavo A. R. Silva wrote:
> Make use of the struct_size() and flex_array_size() helpers instead of
> an open-coded version, in order to avoid any potential type mistakes
> or integer overflows that, in the worst scenario, could lead to heap
> overflows.
> 
> Also, address the following sparse warnings:
> drivers/usb/gadget/function/f_fs.c:922:23: warning: using sizeof on a flexible structure
> 
> Link: https://github.com/KSPP/linux/issues/174
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/usb/gadget/function/f_fs.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index 25ad1e97a458..7461d27e9604 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -919,12 +919,12 @@ static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
>  		data_len, ret);
>  
>  	data_len -= ret;
> -	buf = kmalloc(sizeof(*buf) + data_len, GFP_KERNEL);
> +	buf = kmalloc(struct_size(buf, storage, data_len), GFP_KERNEL);
>  	if (!buf)
>  		return -ENOMEM;
>  	buf->length = data_len;
>  	buf->data = buf->storage;
> -	memcpy(buf->storage, data + ret, data_len);
> +	memcpy(buf->storage, data + ret, flex_array_size(buf, storage, data_len));

Looks good to me. This is a place where I think we'll be back later to
fix up the alloc/deserialize-from-memory pattern with a future helper,
but in the meantime, yes, let's get this covered by struct_size().

Reviewed-by: Kees Cook <keescook@chromium.org>

>  
>  	/*
>  	 * At this point read_buffer is NULL or READ_BUFFER_DROP (if
> -- 
> 2.27.0
> 

-- 
Kees Cook

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

end of thread, other threads:[~2022-01-20 23:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-20 22:29 [PATCH][next] usb: gadget: f_fs: Use struct_size() and flex_array_size() helpers Gustavo A. R. Silva
2022-01-20 23:26 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).