linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
@ 2023-03-30  1:54 Gustavo A. R. Silva
  2023-03-30  7:01 ` Tzung-Bi Shih
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2023-03-30  1:54 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: chrome-platform, linux-kernel, Gustavo A. R. Silva, linux-hardening

GCC-13 (and Clang) does not like having a partially allocated object,
since it cannot reason about it for bounds checking.

Notice that the compiler is legitimately complaining about accessing
an object (params, in this case) for which not enough memory was
allocated.

The object is of size 20 bytes:

struct ec_params_vbnvcontext {
	uint32_t                   op;                   /*     0     4 */
	uint8_t                    block[16];            /*     4    16 */

	/* size: 20, cachelines: 1, members: 2 */
	/* last cacheline: 20 bytes */
};

but only 16 bytes are allocated:

sizeof(struct ec_response_vbnvcontext) == 16

In this case, as only enough space for the op field is allocated,
we can use an object of type uint32_t instead of a whole
struct ec_params_vbnvcontext (for which not enough memory is
allocated).

Fix the following warning seen under GCC 13:
drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
   36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
      |               ^~
In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
In function ‘kmalloc’,
    inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
  580 |                 return kmalloc_trace(
      |                        ^~~~~~~~~~~~~~
  581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  582 |                                 flags, size);
      |                                 ~~~~~~~~~~~~

Link: https://github.com/KSPP/linux/issues/278
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/platform/chrome/cros_ec_vbc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_vbc.c b/drivers/platform/chrome/cros_ec_vbc.c
index c859c862d7ac..b5a584f5469a 100644
--- a/drivers/platform/chrome/cros_ec_vbc.c
+++ b/drivers/platform/chrome/cros_ec_vbc.c
@@ -20,10 +20,14 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
 	struct device *dev = kobj_to_dev(kobj);
 	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
 	struct cros_ec_device *ecdev = ec->ec_dev;
-	struct ec_params_vbnvcontext *params;
 	struct cros_ec_command *msg;
+	/*
+	 * This should be a pointer to the same type as op field in
+	 * struct ec_params_vbnvcontext.
+	 */
+	uint32_t *params_op;
 	int err;
-	const size_t para_sz = sizeof(params->op);
+	const size_t para_sz = sizeof(*params_op);
 	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
 	const size_t payload = max(para_sz, resp_sz);
 
@@ -32,8 +36,8 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
 		return -ENOMEM;
 
 	/* NB: we only kmalloc()ated enough space for the op field */
-	params = (struct ec_params_vbnvcontext *)msg->data;
-	params->op = EC_VBNV_CONTEXT_OP_READ;
+	params_op = (uint32_t *)msg->data;
+	*params_op = EC_VBNV_CONTEXT_OP_READ;
 
 	msg->version = EC_VER_VBNV_CONTEXT;
 	msg->command = EC_CMD_VBNV_CONTEXT;
-- 
2.34.1


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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  1:54 [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Gustavo A. R. Silva
@ 2023-03-30  7:01 ` Tzung-Bi Shih
  2023-03-30  7:11   ` Greg KH
  2023-03-30 20:44   ` Gustavo A. R. Silva
  2023-12-14 16:35 ` Kees Cook
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 7+ messages in thread
From: Tzung-Bi Shih @ 2023-03-30  7:01 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Benson Leung, Guenter Roeck, chrome-platform, linux-kernel,
	linux-hardening

On Wed, Mar 29, 2023 at 07:54:02PM -0600, Gustavo A. R. Silva wrote:
> In this case, as only enough space for the op field is allocated,
> we can use an object of type uint32_t instead of a whole
> struct ec_params_vbnvcontext (for which not enough memory is
> allocated).

It doesn't make sense to me.  See comments below.

> Fix the following warning seen under GCC 13:
> drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
> drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
>    36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
>       |               ^~
> In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
> In function ‘kmalloc’,
>     inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
> ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
>   580 |                 return kmalloc_trace(
>       |                        ^~~~~~~~~~~~~~
>   581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
>       |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   582 |                                 flags, size);
>       |                                 ~~~~~~~~~~~~

Please trim the commit message a bit and try to wrap at 75 columns as
[1] suggested.

[1]: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format

> @@ -20,10 +20,14 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
>  	struct cros_ec_device *ecdev = ec->ec_dev;
> -	struct ec_params_vbnvcontext *params;
>  	struct cros_ec_command *msg;
> +	/*
> +	 * This should be a pointer to the same type as op field in
> +	 * struct ec_params_vbnvcontext.
> +	 */
> +	uint32_t *params_op;
>  	int err;
> -	const size_t para_sz = sizeof(params->op);
> +	const size_t para_sz = sizeof(*params_op);
>  	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
>  	const size_t payload = max(para_sz, resp_sz);
>  
> @@ -32,8 +36,8 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
>  		return -ENOMEM;
>  
>  	/* NB: we only kmalloc()ated enough space for the op field */
> -	params = (struct ec_params_vbnvcontext *)msg->data;
> -	params->op = EC_VBNV_CONTEXT_OP_READ;
> +	params_op = (uint32_t *)msg->data;
> +	*params_op = EC_VBNV_CONTEXT_OP_READ;

I don't see a good reason to partially allocate memory here.  Perhaps, just
let `para_sz = sizeof(struct ec_params_vbnvcontext)`?  If it also makes
sense to you, please remove the comment "NB: we only..." as well.

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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  7:01 ` Tzung-Bi Shih
@ 2023-03-30  7:11   ` Greg KH
  2023-03-30 20:44   ` Gustavo A. R. Silva
  1 sibling, 0 replies; 7+ messages in thread
From: Greg KH @ 2023-03-30  7:11 UTC (permalink / raw)
  To: Tzung-Bi Shih
  Cc: Gustavo A. R. Silva, Benson Leung, Guenter Roeck,
	chrome-platform, linux-kernel, linux-hardening

On Thu, Mar 30, 2023 at 03:01:23PM +0800, Tzung-Bi Shih wrote:
> On Wed, Mar 29, 2023 at 07:54:02PM -0600, Gustavo A. R. Silva wrote:
> > In this case, as only enough space for the op field is allocated,
> > we can use an object of type uint32_t instead of a whole
> > struct ec_params_vbnvcontext (for which not enough memory is
> > allocated).
> 
> It doesn't make sense to me.  See comments below.
> 
> > Fix the following warning seen under GCC 13:
> > drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
> > drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
> >    36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
> >       |               ^~
> > In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
> > In function ‘kmalloc’,
> >     inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
> > ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
> >   580 |                 return kmalloc_trace(
> >       |                        ^~~~~~~~~~~~~~
> >   581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
> >       |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >   582 |                                 flags, size);
> >       |                                 ~~~~~~~~~~~~
> 
> Please trim the commit message a bit and try to wrap at 75 columns as
> [1] suggested.
> 
> [1]: https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format

For outputs from tools like this, going over 75 columns is fine, no need
to ever line-wrap stuff like this, that would just make it unreadable.

thanks,

greg k-h

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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  7:01 ` Tzung-Bi Shih
  2023-03-30  7:11   ` Greg KH
@ 2023-03-30 20:44   ` Gustavo A. R. Silva
  1 sibling, 0 replies; 7+ messages in thread
From: Gustavo A. R. Silva @ 2023-03-30 20:44 UTC (permalink / raw)
  To: Tzung-Bi Shih, Gustavo A. R. Silva
  Cc: Benson Leung, Guenter Roeck, chrome-platform, linux-kernel,
	linux-hardening


>>   	/* NB: we only kmalloc()ated enough space for the op field */
>> -	params = (struct ec_params_vbnvcontext *)msg->data;
>> -	params->op = EC_VBNV_CONTEXT_OP_READ;
>> +	params_op = (uint32_t *)msg->data;
>> +	*params_op = EC_VBNV_CONTEXT_OP_READ;
> 
> I don't see a good reason to partially allocate memory here.  Perhaps, just
> let `para_sz = sizeof(struct ec_params_vbnvcontext)`?  If it also makes
> sense to you, please remove the comment "NB: we only..." as well.

It looks funny to me, too. However, I think that's material for a different
patch.

What I want to get fixed here is the -Warray-bounds warning, while not messing
too much with the original implementation. :)

Thanks
--
Gustavo

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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  1:54 [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Gustavo A. R. Silva
  2023-03-30  7:01 ` Tzung-Bi Shih
@ 2023-12-14 16:35 ` Kees Cook
  2023-12-14 16:37 ` Kees Cook
  2023-12-15  8:17 ` Tzung-Bi Shih
  3 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-12-14 16:35 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Benson Leung, Guenter Roeck, chrome-platform, linux-kernel,
	linux-hardening

On Wed, Mar 29, 2023 at 07:54:02PM -0600, Gustavo A. R. Silva wrote:
> GCC-13 (and Clang) does not like having a partially allocated object,
> since it cannot reason about it for bounds checking.
> 
> Notice that the compiler is legitimately complaining about accessing
> an object (params, in this case) for which not enough memory was
> allocated.
> 
> The object is of size 20 bytes:
> 
> struct ec_params_vbnvcontext {
> 	uint32_t                   op;                   /*     0     4 */
> 	uint8_t                    block[16];            /*     4    16 */
> 
> 	/* size: 20, cachelines: 1, members: 2 */
> 	/* last cacheline: 20 bytes */
> };
> 
> but only 16 bytes are allocated:
> 
> sizeof(struct ec_response_vbnvcontext) == 16
> 
> In this case, as only enough space for the op field is allocated,
> we can use an object of type uint32_t instead of a whole
> struct ec_params_vbnvcontext (for which not enough memory is
> allocated).
> 
> Fix the following warning seen under GCC 13:
> drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
> drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
>    36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
>       |               ^~
> In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
> In function ‘kmalloc’,
>     inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
> ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
>   580 |                 return kmalloc_trace(
>       |                        ^~~~~~~~~~~~~~
>   581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
>       |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   582 |                                 flags, size);
>       |                                 ~~~~~~~~~~~~
> 
> Link: https://github.com/KSPP/linux/issues/278
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

This patch seems to have gotten lost? Looking at the conversation, I
think it should land as-is rather than changing the allocation size.

I can pick this up via my tree if that helps...

-Kees

> ---
>  drivers/platform/chrome/cros_ec_vbc.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/platform/chrome/cros_ec_vbc.c b/drivers/platform/chrome/cros_ec_vbc.c
> index c859c862d7ac..b5a584f5469a 100644
> --- a/drivers/platform/chrome/cros_ec_vbc.c
> +++ b/drivers/platform/chrome/cros_ec_vbc.c
> @@ -20,10 +20,14 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
>  	struct device *dev = kobj_to_dev(kobj);
>  	struct cros_ec_dev *ec = to_cros_ec_dev(dev);
>  	struct cros_ec_device *ecdev = ec->ec_dev;
> -	struct ec_params_vbnvcontext *params;
>  	struct cros_ec_command *msg;
> +	/*
> +	 * This should be a pointer to the same type as op field in
> +	 * struct ec_params_vbnvcontext.
> +	 */
> +	uint32_t *params_op;
>  	int err;
> -	const size_t para_sz = sizeof(params->op);
> +	const size_t para_sz = sizeof(*params_op);
>  	const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
>  	const size_t payload = max(para_sz, resp_sz);
>  
> @@ -32,8 +36,8 @@ static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
>  		return -ENOMEM;
>  
>  	/* NB: we only kmalloc()ated enough space for the op field */
> -	params = (struct ec_params_vbnvcontext *)msg->data;
> -	params->op = EC_VBNV_CONTEXT_OP_READ;
> +	params_op = (uint32_t *)msg->data;
> +	*params_op = EC_VBNV_CONTEXT_OP_READ;
>  
>  	msg->version = EC_VER_VBNV_CONTEXT;
>  	msg->command = EC_CMD_VBNV_CONTEXT;
> -- 
> 2.34.1
> 

-- 
Kees Cook

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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  1:54 [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Gustavo A. R. Silva
  2023-03-30  7:01 ` Tzung-Bi Shih
  2023-12-14 16:35 ` Kees Cook
@ 2023-12-14 16:37 ` Kees Cook
  2023-12-15  8:17 ` Tzung-Bi Shih
  3 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-12-14 16:37 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Benson Leung, Guenter Roeck, chrome-platform, linux-kernel,
	linux-hardening

On Wed, Mar 29, 2023 at 07:54:02PM -0600, Gustavo A. R. Silva wrote:
> GCC-13 (and Clang) does not like having a partially allocated object,
> since it cannot reason about it for bounds checking.
> 
> Notice that the compiler is legitimately complaining about accessing
> an object (params, in this case) for which not enough memory was
> allocated.
> 
> The object is of size 20 bytes:
> 
> struct ec_params_vbnvcontext {
> 	uint32_t                   op;                   /*     0     4 */
> 	uint8_t                    block[16];            /*     4    16 */
> 
> 	/* size: 20, cachelines: 1, members: 2 */
> 	/* last cacheline: 20 bytes */
> };
> 
> but only 16 bytes are allocated:
> 
> sizeof(struct ec_response_vbnvcontext) == 16
> 
> In this case, as only enough space for the op field is allocated,
> we can use an object of type uint32_t instead of a whole
> struct ec_params_vbnvcontext (for which not enough memory is
> allocated).
> 
> Fix the following warning seen under GCC 13:
> drivers/platform/chrome/cros_ec_vbc.c: In function ‘vboot_context_read’:
> drivers/platform/chrome/cros_ec_vbc.c:36:15: warning: array subscript ‘struct ec_params_vbnvcontext[1]’ is partly outside array bounds of ‘unsigned char[36]’ [-Warray-bounds=]
>    36 |         params->op = EC_VBNV_CONTEXT_OP_READ;
>       |               ^~
> In file included from drivers/platform/chrome/cros_ec_vbc.c:12:
> In function ‘kmalloc’,
>     inlined from ‘vboot_context_read’ at drivers/platform/chrome/cros_ec_vbc.c:30:8:
> ./include/linux/slab.h:580:24: note: at offset 20 into object of size 36 allocated by ‘kmalloc_trace’
>   580 |                 return kmalloc_trace(
>       |                        ^~~~~~~~~~~~~~
>   581 |                                 kmalloc_caches[kmalloc_type(flags)][index],
>       |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   582 |                                 flags, size);
>       |                                 ~~~~~~~~~~~~
> 
> Link: https://github.com/KSPP/linux/issues/278
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

FWIW, I think this is the right change that disrupts the code the least.

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

-Kees

-- 
Kees Cook

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

* Re: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings
  2023-03-30  1:54 [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Gustavo A. R. Silva
                   ` (2 preceding siblings ...)
  2023-12-14 16:37 ` Kees Cook
@ 2023-12-15  8:17 ` Tzung-Bi Shih
  3 siblings, 0 replies; 7+ messages in thread
From: Tzung-Bi Shih @ 2023-12-15  8:17 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck, Gustavo A. R. Silva
  Cc: chrome-platform, linux-kernel, linux-hardening, keescook

On Wed, 29 Mar 2023 19:54:02 -0600, Gustavo A. R. Silva wrote:
> GCC-13 (and Clang) does not like having a partially allocated object,
> since it cannot reason about it for bounds checking.
> 
> Notice that the compiler is legitimately complaining about accessing
> an object (params, in this case) for which not enough memory was
> allocated.
> 
> [...]

Applied, thanks!

[1/1] platform/chrome: Fix -Warray-bounds warnings
      commit: 59a9ccf19ee03179faf047822bbec76cac7467a4

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

end of thread, other threads:[~2023-12-15  8:17 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30  1:54 [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Gustavo A. R. Silva
2023-03-30  7:01 ` Tzung-Bi Shih
2023-03-30  7:11   ` Greg KH
2023-03-30 20:44   ` Gustavo A. R. Silva
2023-12-14 16:35 ` Kees Cook
2023-12-14 16:37 ` Kees Cook
2023-12-15  8:17 ` Tzung-Bi Shih

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).