linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create()
@ 2022-01-21 22:22 Gustavo A. R. Silva
  2022-01-24 20:19 ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Gustavo A. R. Silva @ 2022-01-21 22:22 UTC (permalink / raw)
  To: Viresh Kumar, Johan Hovold, Alex Elder, Greg Kroah-Hartman
  Cc: greybus-dev, linux-staging, linux-kernel, Gustavo A. R. Silva,
	linux-hardening

Make use of the struct_size() helper 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/staging/greybus/i2c.c:111:24: 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/staging/greybus/i2c.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/staging/greybus/i2c.c b/drivers/staging/greybus/i2c.c
index de2f6516da09..9dfc6791c20e 100644
--- a/drivers/staging/greybus/i2c.c
+++ b/drivers/staging/greybus/i2c.c
@@ -108,9 +108,7 @@ gb_i2c_operation_create(struct gb_connection *connection,
 		else
 			data_out_size += (u32)msg->len;
 
-	request_size = sizeof(*request);
-	request_size += msg_count * sizeof(*op);
-	request_size += data_out_size;
+	request_size = struct_size(request, ops, msg_count) + data_out_size;
 
 	/* Response consists only of incoming data */
 	operation = gb_operation_create(connection, GB_I2C_TYPE_TRANSFER,
-- 
2.27.0


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

* Re: [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create()
  2022-01-21 22:22 [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create() Gustavo A. R. Silva
@ 2022-01-24 20:19 ` Kees Cook
  2022-01-24 20:42   ` Gustavo A. R. Silva
  2022-01-26 10:54   ` Dan Carpenter
  0 siblings, 2 replies; 5+ messages in thread
From: Kees Cook @ 2022-01-24 20:19 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Viresh Kumar, Johan Hovold, Alex Elder, Greg Kroah-Hartman,
	greybus-dev, linux-staging, linux-kernel, linux-hardening

On Fri, Jan 21, 2022 at 04:22:50PM -0600, Gustavo A. R. Silva wrote:
> Make use of the struct_size() helper 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/staging/greybus/i2c.c:111:24: 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/staging/greybus/i2c.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/greybus/i2c.c b/drivers/staging/greybus/i2c.c
> index de2f6516da09..9dfc6791c20e 100644
> --- a/drivers/staging/greybus/i2c.c
> +++ b/drivers/staging/greybus/i2c.c
> @@ -108,9 +108,7 @@ gb_i2c_operation_create(struct gb_connection *connection,
>  		else
>  			data_out_size += (u32)msg->len;
>  
> -	request_size = sizeof(*request);
> -	request_size += msg_count * sizeof(*op);
> -	request_size += data_out_size;
> +	request_size = struct_size(request, ops, msg_count) + data_out_size;

This could still overflow if struct_size() returns SIZE_MAX. Perhaps:

	if (check_add_overflow(struct_size(request, ops, msg_count),
			       data_out_size, &request_size))
		request_size = SIZE_MAX;

I should brush off the saturating arithmetic helpers series:
https://lore.kernel.org/all/20210920180853.1825195-1-keescook@chromium.org/

>  
>  	/* Response consists only of incoming data */
>  	operation = gb_operation_create(connection, GB_I2C_TYPE_TRANSFER,
> -- 
> 2.27.0
> 

-- 
Kees Cook

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

* Re: [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create()
  2022-01-24 20:19 ` Kees Cook
@ 2022-01-24 20:42   ` Gustavo A. R. Silva
  2022-01-26 10:54   ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2022-01-24 20:42 UTC (permalink / raw)
  To: Kees Cook
  Cc: Viresh Kumar, Johan Hovold, Alex Elder, Greg Kroah-Hartman,
	greybus-dev, linux-staging, linux-kernel, linux-hardening

On Mon, Jan 24, 2022 at 12:19:03PM -0800, Kees Cook wrote:
> On Fri, Jan 21, 2022 at 04:22:50PM -0600, Gustavo A. R. Silva wrote:
> > Make use of the struct_size() helper 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/staging/greybus/i2c.c:111:24: 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/staging/greybus/i2c.c | 4 +---
> >  1 file changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/drivers/staging/greybus/i2c.c b/drivers/staging/greybus/i2c.c
> > index de2f6516da09..9dfc6791c20e 100644
> > --- a/drivers/staging/greybus/i2c.c
> > +++ b/drivers/staging/greybus/i2c.c
> > @@ -108,9 +108,7 @@ gb_i2c_operation_create(struct gb_connection *connection,
> >  		else
> >  			data_out_size += (u32)msg->len;
> >  
> > -	request_size = sizeof(*request);
> > -	request_size += msg_count * sizeof(*op);
> > -	request_size += data_out_size;
> > +	request_size = struct_size(request, ops, msg_count) + data_out_size;
> 
> This could still overflow if struct_size() returns SIZE_MAX. Perhaps:

uggh... I got too excited with all the new cases reported. 
> 
> 	if (check_add_overflow(struct_size(request, ops, msg_count),
> 			       data_out_size, &request_size))
> 		request_size = SIZE_MAX;

Yep; I'll respin and include this change, thanks!
> 
> I should brush off the saturating arithmetic helpers series:
> https://lore.kernel.org/all/20210920180853.1825195-1-keescook@chromium.org/

Yeah; those helpers are very much needed in many places we need to audit...

--
Gustavo

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

* Re: [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create()
  2022-01-24 20:19 ` Kees Cook
  2022-01-24 20:42   ` Gustavo A. R. Silva
@ 2022-01-26 10:54   ` Dan Carpenter
  2022-01-27 10:05     ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2022-01-26 10:54 UTC (permalink / raw)
  To: Kees Cook
  Cc: Gustavo A. R. Silva, Viresh Kumar, Johan Hovold, Alex Elder,
	Greg Kroah-Hartman, greybus-dev, linux-staging, linux-kernel,
	linux-hardening

On Mon, Jan 24, 2022 at 12:19:03PM -0800, Kees Cook wrote:
> This could still overflow if struct_size() returns SIZE_MAX. Perhaps:
> 
> 	if (check_add_overflow(struct_size(request, ops, msg_count),
> 			       data_out_size, &request_size))
> 		request_size = SIZE_MAX;
> 
> I should brush off the saturating arithmetic helpers series:
> https://lore.kernel.org/all/20210920180853.1825195-1-keescook@chromium.org/

Yes, please!  Those seem like a million times easier to use.

regards,
dan carpenter

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

* Re: [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create()
  2022-01-26 10:54   ` Dan Carpenter
@ 2022-01-27 10:05     ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2022-01-27 10:05 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Gustavo A. R. Silva, Viresh Kumar, Johan Hovold, Alex Elder,
	Greg Kroah-Hartman, greybus-dev, linux-staging, linux-kernel,
	linux-hardening

On Wed, Jan 26, 2022 at 01:54:04PM +0300, Dan Carpenter wrote:
> On Mon, Jan 24, 2022 at 12:19:03PM -0800, Kees Cook wrote:
> > This could still overflow if struct_size() returns SIZE_MAX. Perhaps:
> > 
> > 	if (check_add_overflow(struct_size(request, ops, msg_count),
> > 			       data_out_size, &request_size))
> > 		request_size = SIZE_MAX;
> > 
> > I should brush off the saturating arithmetic helpers series:
> > https://lore.kernel.org/all/20210920180853.1825195-1-keescook@chromium.org/
> 
> Yes, please!  Those seem like a million times easier to use.

Here they are! :) Please review:

https://lore.kernel.org/lkml/20220124232342.3113350-1-keescook@chromium.org/

Thanks!

-- 
Kees Cook

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

end of thread, other threads:[~2022-01-27 10:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 22:22 [PATCH][next] staging: greybus: i2c: Use struct_size() helper in gb_i2c_operation_create() Gustavo A. R. Silva
2022-01-24 20:19 ` Kees Cook
2022-01-24 20:42   ` Gustavo A. R. Silva
2022-01-26 10:54   ` Dan Carpenter
2022-01-27 10:05     ` 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).