Message ID | ZCTrutoN+9TiJM8u@work |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp823440vqo; Wed, 29 Mar 2023 19:28:30 -0700 (PDT) X-Google-Smtp-Source: AKy350YvgrgZNFwIz1yAG+0XEPu8ZEg2Qr/zjsUsEbBeAXRrWj9WDp6/UM3/ufYQhSpq3EUeXlxD X-Received: by 2002:a17:902:d2cb:b0:1a1:db10:7ba3 with SMTP id n11-20020a170902d2cb00b001a1db107ba3mr29766191plc.2.1680143310354; Wed, 29 Mar 2023 19:28:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680143310; cv=none; d=google.com; s=arc-20160816; b=MP2iVR5eusGsXa6UixJcGYaRL443lhNbHzU/SP/l9ERcSV/qMCj0U7yw0/ei3Xno98 vJzTlF9TGg49xkJOhKE84+BsQIwAbhmf/eBp3wtcSGfQtImeY0o7G81SE/jSuhP44yiW my+UeqjJY8U8oYa+HL48oSKgZQqElf8LceJLrUttWtvyeoi94IKnmyIqcOGTtlbq5/Qw gXDTR3OQ6pUpHuO3EqUm9c1FJ+/tCKq0TCiGOvu/7HOxMnh5GtLJiDYkFJNGoyUVx9i6 8jt4dk1izfAFqOKvI0lZOg8sctGO4bPztCO+hIvCvhZfjWENNxJj8Blun29xE6bBGS8+ oV5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=39X9lKa7I2RUmx39sRDCO7wFAwEncZWhnH45wDAcBzY=; b=H3cGTQwNktKlZFUITgzKhDYyJM2jd9PEY955ELhxyL4ZF9Ac6yJQIuVXIRR6xyVYR3 1KjbTLQn1na6Ek3BqpvS11NpdM+TzBWFaqWRJRtLpqhn9gf10TERVKBu8Iz8bNK5wfvy oYvovEMJcUomrjMoNAyHyjrOhynomijzbuyd18G0fcXBLjkXSgdn8v8WYZ++RL6XLj6J FG2CuwY3551CZuf1UbQt0/8zFtC5MVjskkTt9Uc0VTv3WQh5Hw3hlGk+H4T2Fx6x3EKh jllqgNVH2NUXVo+l+dxvQH7lnBtDH5W8+g+sLbLEBwxYsI3G/GmzYJR995hTgOZgrmKz YB6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vBpMJ4k4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j3-20020a170903028300b001a20c983d05si15561751plr.235.2023.03.29.19.28.16; Wed, 29 Mar 2023 19:28:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vBpMJ4k4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229704AbjC3Bxf (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Wed, 29 Mar 2023 21:53:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229586AbjC3Bxd (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Mar 2023 21:53:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 393B09F; Wed, 29 Mar 2023 18:53:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id C6159B82581; Thu, 30 Mar 2023 01:53:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EA94C433EF; Thu, 30 Mar 2023 01:53:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680141209; bh=3XmANaFzBt3a+2sASRW3pZzJKWGwytosBj+PU3cNreY=; h=Date:From:To:Cc:Subject:From; b=vBpMJ4k4UCgp74JR3KKcFqw9mZDJn651IODDcgfoSH2hVylUD6bbLyLUTq23qYkLS o718vQ/9nOb+TcuEbi9fRfq26mS3I1l8Z5ztvcZJetA8asvxeqbQ+qJGbgT9tWcX4N zOuz1BZxX6+xqHdJpJscAfZjhyPBXEK0nflyuNgUqCfCpo9l/5m19fgp/VzMb8RiUA VeR5nzuJsAw9J6o3vIKtboOWRVq4HU3tVnGA0YL4lP7TKC3mkyqkd/WT+pio3FTNd3 sU5dKfZAN7XjBTZCirUCBQ+bRcn23bRSQ7ZoDVD688i2NaMZ/rSAM1XReEQPlDNqMe YaV7a3Ur/53vg== Date: Wed, 29 Mar 2023 19:54:02 -0600 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: Benson Leung <bleung@chromium.org>, Guenter Roeck <groeck@chromium.org> Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, linux-hardening@vger.kernel.org Subject: [PATCH][next] platform/chrome: Fix -Warray-bounds warnings Message-ID: <ZCTrutoN+9TiJM8u@work> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761757951670364837?= X-GMAIL-MSGID: =?utf-8?q?1761757951670364837?= |
Series |
[next] platform/chrome: Fix -Warray-bounds warnings
|
|
Commit Message
Gustavo A. R. Silva
March 30, 2023, 1:54 a.m. UTC
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(-)
Comments
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.
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
>> /* 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
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 >
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
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
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;