Message ID | 184d96f95d6261b1a91704eb68adbd0a2e1c2cc2.1627646101.git.gustavoars@kernel.org (mailing list archive) |
---|---|
State | Changes Requested, archived |
Delegated to: | Sakari Ailus |
Headers |
Received: from vger.kernel.org ([23.128.96.18]) by www.linuxtv.org with esmtp (Exim 4.92) (envelope-from <linux-media-owner@vger.kernel.org>) id 1m9RGp-00BSna-Rl; Fri, 30 Jul 2021 12:05:44 +0000 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238709AbhG3MFp (ORCPT <rfc822;mkrufky@linuxtv.org> + 1 other); Fri, 30 Jul 2021 08:05:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:45638 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230263AbhG3MFp (ORCPT <rfc822;linux-media@vger.kernel.org>); Fri, 30 Jul 2021 08:05:45 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 955DD60F01; Fri, 30 Jul 2021 12:05:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1627646740; bh=/YlnU4V3aPDSJabKbYYPbeEQmm/CqFf1w/7AEVfRlFY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OCnEFos0dJ2x7MMIS264xEhMeVRksdxI281u3TeuppryAu0qZNns6/HavEw2ZmnSM JKkgPHFZGnTN0pMJha1YMZKpaCQ6kNcLwhd0uTn0r14upyD8smdxAuxOelTDFSBbUI D3L7yIQqQerdQWBtOrecQCWzI23Bc5+u4/9/vUrYaHRl33+7MrP28hT799aiFzCfCB n6hIc6yJAvd1uGh5uXRVws93pBX0j9yk0A9xsTCxvnF3XDlsi+rbEXwAV4oWEqMlQH B2JSK7L96ho5qjc54RA/7zmg3vO56GEIFmcuJLQqzcyu4lDY4ufIYsYzixcs4x8e5e 82sjCEO1CaubA== Date: Fri, 30 Jul 2021 07:08:13 -0500 From: "Gustavo A. R. Silva" <gustavoars@kernel.org> To: linux-kernel@vger.kernel.org Cc: Yong Zhi <yong.zhi@intel.com>, Sakari Ailus <sakari.ailus@linux.intel.com>, Bingbu Cao <bingbu.cao@intel.com>, Tianshu Qiu <tian.shu.qiu@intel.com>, Mauro Carvalho Chehab <mchehab@kernel.org>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, linux-media@vger.kernel.org, linux-staging@lists.linux.dev, linux-hardening@vger.kernel.org, "Gustavo A. R. Silva" <gustavoars@kernel.org>, Dan Carpenter <dan.carpenter@oracle.com> Subject: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison Message-ID: <184d96f95d6261b1a91704eb68adbd0a2e1c2cc2.1627646101.git.gustavoars@kernel.org> References: <cover.1627646101.git.gustavoars@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <cover.1627646101.git.gustavoars@kernel.org> Precedence: bulk List-ID: <linux-media.vger.kernel.org> X-Mailing-List: linux-media@vger.kernel.org X-LSpam-Score: -3.1 (---) X-LSpam-Report: No, score=-3.1 required=5.0 tests=BAYES_00=-1.9,DKIMWL_WL_HIGH=0.001,DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,DKIM_VALID_EF=-0.1,MAILING_LIST_MULTI=-1,RCVD_IN_DNSWL_NONE=-0.0001 autolearn=ham autolearn_force=no |
Series |
Fix size comparison bug and use flexible array
|
|
Commit Message
Gustavo A. R. Silva
July 30, 2021, 12:08 p.m. UTC
There is a wrong comparison of the total size of the loaded firmware
css->fw->size with the size of a pointer to struct imgu_fw_header.
Fix this by using the right operand 'struct imgu_fw_header' for
sizeof, instead of 'struct imgu_fw_header *' and turn binary_header
into a flexible-array member. Also, adjust the relational operator
to be '<=' instead of '<', as it seems that the intention of the
comparison is to determine if the loaded firmware contains any
'struct imgu_fw_info' items in the binary_header[] array than merely
the file_header (struct imgu_fw_bi_file_h).
The replacement of the one-element array with a flexible-array member
also help with the ongoing efforts to globally enable -Warray-bounds
and get us closer to being able to tighten the FORTIFY_SOURCE routines
on memcpy().
Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/109
Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")
Cc: stable@vger.kernel.org
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
It'd be just great if someone that knows this code better can confirm
these changes are correct. In particular the adjustment of the
relational operator. Thanks!
Changes in v2:
- Use flexible array and adjust relational operator, accordingly.
- Update changelog text.
drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +-
drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
Comments
Hi Gustavo, I missed you already had sent v2... On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: > There is a wrong comparison of the total size of the loaded firmware > css->fw->size with the size of a pointer to struct imgu_fw_header. > > Fix this by using the right operand 'struct imgu_fw_header' for > sizeof, instead of 'struct imgu_fw_header *' and turn binary_header > into a flexible-array member. Also, adjust the relational operator > to be '<=' instead of '<', as it seems that the intention of the > comparison is to determine if the loaded firmware contains any > 'struct imgu_fw_info' items in the binary_header[] array than merely > the file_header (struct imgu_fw_bi_file_h). > > The replacement of the one-element array with a flexible-array member > also help with the ongoing efforts to globally enable -Warray-bounds > and get us closer to being able to tighten the FORTIFY_SOURCE routines > on memcpy(). > > Link: https://github.com/KSPP/linux/issues/79 > Link: https://github.com/KSPP/linux/issues/109 > Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") > Cc: stable@vger.kernel.org > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > --- > > It'd be just great if someone that knows this code better can confirm > these changes are correct. In particular the adjustment of the > relational operator. Thanks! > > Changes in v2: > - Use flexible array and adjust relational operator, accordingly. The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed. > - Update changelog text. > > drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +- > drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.c b/drivers/staging/media/ipu3/ipu3-css-fw.c > index 45aff76198e2..630cb5186b48 100644 > --- a/drivers/staging/media/ipu3/ipu3-css-fw.c > +++ b/drivers/staging/media/ipu3/ipu3-css-fw.c > @@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css) > /* Check and display fw header info */ > > css->fwp = (struct imgu_fw_header *)css->fw->data; > - if (css->fw->size < sizeof(struct imgu_fw_header *) || > + if (css->fw->size <= sizeof(struct imgu_fw_header) || > css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) > goto bad_fw; > if (sizeof(struct imgu_fw_bi_file_h) + > diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.h b/drivers/staging/media/ipu3/ipu3-css-fw.h > index 3c078f15a295..c0bc57fd678a 100644 > --- a/drivers/staging/media/ipu3/ipu3-css-fw.h > +++ b/drivers/staging/media/ipu3/ipu3-css-fw.h > @@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h { > > struct imgu_fw_header { > struct imgu_fw_bi_file_h file_header; > - struct imgu_fw_info binary_header[1]; /* binary_nr items */ > + struct imgu_fw_info binary_header[]; /* binary_nr items */ > }; > > /******************* Firmware functions *******************/
Hi Sakari, On 8/2/21 01:05, Sakari Ailus wrote: > Hi Gustavo, > > I missed you already had sent v2... > > On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: >> There is a wrong comparison of the total size of the loaded firmware >> css->fw->size with the size of a pointer to struct imgu_fw_header. >> >> Fix this by using the right operand 'struct imgu_fw_header' for >> sizeof, instead of 'struct imgu_fw_header *' and turn binary_header >> into a flexible-array member. Also, adjust the relational operator >> to be '<=' instead of '<', as it seems that the intention of the >> comparison is to determine if the loaded firmware contains any >> 'struct imgu_fw_info' items in the binary_header[] array than merely >> the file_header (struct imgu_fw_bi_file_h). >> >> The replacement of the one-element array with a flexible-array member >> also help with the ongoing efforts to globally enable -Warray-bounds >> and get us closer to being able to tighten the FORTIFY_SOURCE routines >> on memcpy(). >> >> Link: https://github.com/KSPP/linux/issues/79 >> Link: https://github.com/KSPP/linux/issues/109 >> Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") >> Cc: stable@vger.kernel.org >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> >> --- >> >> It'd be just great if someone that knows this code better can confirm >> these changes are correct. In particular the adjustment of the >> relational operator. Thanks! >> >> Changes in v2: >> - Use flexible array and adjust relational operator, accordingly. > > The operator was just correct. The check is just there to see the firmware > is at least as large as the struct as which it is being accessed. I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right? The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right? Notice that generally speaking flexible-array members don't occupy space in the containing structure: $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o struct imgu_fw_header { struct imgu_fw_bi_file_h file_header; /* 0 72 */ /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ struct imgu_fw_info binary_header[] __attribute__((__aligned__(8))); /* 72 0 */ /* size: 72, cachelines: 2, members: 2 */ /* forced alignments: 1 */ /* last cacheline: 8 bytes */ } __attribute__((__aligned__(8))); $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o struct imgu_fw_header { struct imgu_fw_bi_file_h file_header; /* 0 72 */ /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ struct imgu_fw_info binary_header[1] __attribute__((__aligned__(8))); /* 72 1200 */ /* size: 1272, cachelines: 20, members: 2 */ /* forced alignments: 1 */ /* last cacheline: 56 bytes */ } __attribute__((__aligned__(8))); So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<=' Can you please confirm if this v2 is now correct? if so, it'd be great to have your Reviewed-by tag. :) Thanks! -- Gustavo > >> - Update changelog text. >> >> drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +- >> drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +- >> 2 files changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.c b/drivers/staging/media/ipu3/ipu3-css-fw.c >> index 45aff76198e2..630cb5186b48 100644 >> --- a/drivers/staging/media/ipu3/ipu3-css-fw.c >> +++ b/drivers/staging/media/ipu3/ipu3-css-fw.c >> @@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css) >> /* Check and display fw header info */ >> >> css->fwp = (struct imgu_fw_header *)css->fw->data; >> - if (css->fw->size < sizeof(struct imgu_fw_header *) || >> + if (css->fw->size <= sizeof(struct imgu_fw_header) || >> css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) >> goto bad_fw; >> if (sizeof(struct imgu_fw_bi_file_h) + >> diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.h b/drivers/staging/media/ipu3/ipu3-css-fw.h >> index 3c078f15a295..c0bc57fd678a 100644 >> --- a/drivers/staging/media/ipu3/ipu3-css-fw.h >> +++ b/drivers/staging/media/ipu3/ipu3-css-fw.h >> @@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h { >> >> struct imgu_fw_header { >> struct imgu_fw_bi_file_h file_header; >> - struct imgu_fw_info binary_header[1]; /* binary_nr items */ >> + struct imgu_fw_info binary_header[]; /* binary_nr items */ >> }; >> >> /******************* Firmware functions *******************/ >
I missed that you change < sizeof() to <= sizeof()... Sakari is right that it should be < sizeof(). <= doesn't work at all ever. If you wanted a higher limit then the next limit woule be: if (css->fw->size < struct_size(css->fwp, binary_header, 1)) regards, dan carpenter
Hi Gustavo, Apologies for the delay. On Mon, Aug 02, 2021 at 08:46:20AM -0500, Gustavo A. R. Silva wrote: > Hi Sakari, > > On 8/2/21 01:05, Sakari Ailus wrote: > > Hi Gustavo, > > > > I missed you already had sent v2... > > > > On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: > >> There is a wrong comparison of the total size of the loaded firmware > >> css->fw->size with the size of a pointer to struct imgu_fw_header. > >> > >> Fix this by using the right operand 'struct imgu_fw_header' for > >> sizeof, instead of 'struct imgu_fw_header *' and turn binary_header > >> into a flexible-array member. Also, adjust the relational operator > >> to be '<=' instead of '<', as it seems that the intention of the > >> comparison is to determine if the loaded firmware contains any > >> 'struct imgu_fw_info' items in the binary_header[] array than merely > >> the file_header (struct imgu_fw_bi_file_h). > >> > >> The replacement of the one-element array with a flexible-array member > >> also help with the ongoing efforts to globally enable -Warray-bounds > >> and get us closer to being able to tighten the FORTIFY_SOURCE routines > >> on memcpy(). > >> > >> Link: https://github.com/KSPP/linux/issues/79 > >> Link: https://github.com/KSPP/linux/issues/109 > >> Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") > >> Cc: stable@vger.kernel.org > >> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > >> --- > >> > >> It'd be just great if someone that knows this code better can confirm > >> these changes are correct. In particular the adjustment of the > >> relational operator. Thanks! > >> > >> Changes in v2: > >> - Use flexible array and adjust relational operator, accordingly. > > > > The operator was just correct. The check is just there to see the firmware > > is at least as large as the struct as which it is being accessed. > > I'm a bit confused, so based on your reply to v1 of this series, this patch > is now correct, right? > > The operator in v1 _was_ correct as long as the one-element array wasn't > transformed into a flexible array, right? > > Notice that generally speaking flexible-array members don't occupy space in the > containing structure: > > $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o > struct imgu_fw_header { > struct imgu_fw_bi_file_h file_header; /* 0 72 */ > /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ > struct imgu_fw_info binary_header[] __attribute__((__aligned__(8))); /* 72 0 */ > > /* size: 72, cachelines: 2, members: 2 */ > /* forced alignments: 1 */ > /* last cacheline: 8 bytes */ > } __attribute__((__aligned__(8))); > > $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o > struct imgu_fw_header { > struct imgu_fw_bi_file_h file_header; /* 0 72 */ > /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ > struct imgu_fw_info binary_header[1] __attribute__((__aligned__(8))); /* 72 1200 */ > > /* size: 1272, cachelines: 20, members: 2 */ > /* forced alignments: 1 */ > /* last cacheline: 56 bytes */ > } __attribute__((__aligned__(8))); > > So, now that the flexible array transformation is included in the same patch as the > bugfix, the operator is changed from '<' to '<=' '<' is correct since you only need as much data as the struct you're about to access is large, not a byte more than that. As Dan noted. I think you could add a check for binary_nr is at least one.
Hi Sakari, Please, see my comments below... On 8/10/21 10:18, Sakari Ailus wrote: > Hi Gustavo, > > Apologies for the delay. > > On Mon, Aug 02, 2021 at 08:46:20AM -0500, Gustavo A. R. Silva wrote: >> Hi Sakari, >> >> On 8/2/21 01:05, Sakari Ailus wrote: >>> Hi Gustavo, >>> >>> I missed you already had sent v2... >>> >>> On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: >>>> There is a wrong comparison of the total size of the loaded firmware >>>> css->fw->size with the size of a pointer to struct imgu_fw_header. >>>> >>>> Fix this by using the right operand 'struct imgu_fw_header' for >>>> sizeof, instead of 'struct imgu_fw_header *' and turn binary_header >>>> into a flexible-array member. Also, adjust the relational operator >>>> to be '<=' instead of '<', as it seems that the intention of the >>>> comparison is to determine if the loaded firmware contains any >>>> 'struct imgu_fw_info' items in the binary_header[] array than merely >>>> the file_header (struct imgu_fw_bi_file_h). >>>> >>>> The replacement of the one-element array with a flexible-array member >>>> also help with the ongoing efforts to globally enable -Warray-bounds >>>> and get us closer to being able to tighten the FORTIFY_SOURCE routines >>>> on memcpy(). >>>> >>>> Link: https://github.com/KSPP/linux/issues/79 >>>> Link: https://github.com/KSPP/linux/issues/109 >>>> Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") >>>> Cc: stable@vger.kernel.org >>>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> >>>> --- >>>> >>>> It'd be just great if someone that knows this code better can confirm >>>> these changes are correct. In particular the adjustment of the >>>> relational operator. Thanks! >>>> >>>> Changes in v2: >>>> - Use flexible array and adjust relational operator, accordingly. >>> >>> The operator was just correct. The check is just there to see the firmware >>> is at least as large as the struct as which it is being accessed. >> >> I'm a bit confused, so based on your reply to v1 of this series, this patch >> is now correct, right? >> >> The operator in v1 _was_ correct as long as the one-element array wasn't >> transformed into a flexible array, right? >> >> Notice that generally speaking flexible-array members don't occupy space in the >> containing structure: >> >> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o >> struct imgu_fw_header { >> struct imgu_fw_bi_file_h file_header; /* 0 72 */ >> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ >> struct imgu_fw_info binary_header[] __attribute__((__aligned__(8))); /* 72 0 */ >> >> /* size: 72, cachelines: 2, members: 2 */ >> /* forced alignments: 1 */ >> /* last cacheline: 8 bytes */ >> } __attribute__((__aligned__(8))); >> >> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o >> struct imgu_fw_header { >> struct imgu_fw_bi_file_h file_header; /* 0 72 */ >> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ >> struct imgu_fw_info binary_header[1] __attribute__((__aligned__(8))); /* 72 1200 */ >> >> /* size: 1272, cachelines: 20, members: 2 */ >> /* forced alignments: 1 */ >> /* last cacheline: 56 bytes */ >> } __attribute__((__aligned__(8))); >> >> So, now that the flexible array transformation is included in the same patch as the >> bugfix, the operator is changed from '<' to '<=' > > '<' is correct since you only need as much data as the struct you're about > to access is large, not a byte more than that. As Dan noted. > > I think you could add a check for binary_nr is at least one. If we need to check that binary_nr is at least one, then this would be the right change: css->fwp = (struct imgu_fw_header *)css->fw->data; - if (css->fw->size < sizeof(struct imgu_fw_header *) || + if (css->fw->size < struct_size(css->fwp, binary_header, 1) || css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) goto bad_fw; -- Gustavo
On Tue, Aug 10, 2021 at 11:26:14AM -0500, Gustavo A. R. Silva wrote: > Hi Sakari, > > Please, see my comments below... > > On 8/10/21 10:18, Sakari Ailus wrote: > > Hi Gustavo, > > > > Apologies for the delay. > > > > On Mon, Aug 02, 2021 at 08:46:20AM -0500, Gustavo A. R. Silva wrote: > >> Hi Sakari, > >> > >> On 8/2/21 01:05, Sakari Ailus wrote: > >>> Hi Gustavo, > >>> > >>> I missed you already had sent v2... > >>> > >>> On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: > >>>> There is a wrong comparison of the total size of the loaded firmware > >>>> css->fw->size with the size of a pointer to struct imgu_fw_header. > >>>> > >>>> Fix this by using the right operand 'struct imgu_fw_header' for > >>>> sizeof, instead of 'struct imgu_fw_header *' and turn binary_header > >>>> into a flexible-array member. Also, adjust the relational operator > >>>> to be '<=' instead of '<', as it seems that the intention of the > >>>> comparison is to determine if the loaded firmware contains any > >>>> 'struct imgu_fw_info' items in the binary_header[] array than merely > >>>> the file_header (struct imgu_fw_bi_file_h). > >>>> > >>>> The replacement of the one-element array with a flexible-array member > >>>> also help with the ongoing efforts to globally enable -Warray-bounds > >>>> and get us closer to being able to tighten the FORTIFY_SOURCE routines > >>>> on memcpy(). > >>>> > >>>> Link: https://github.com/KSPP/linux/issues/79 > >>>> Link: https://github.com/KSPP/linux/issues/109 > >>>> Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") > >>>> Cc: stable@vger.kernel.org > >>>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> > >>>> --- > >>>> > >>>> It'd be just great if someone that knows this code better can confirm > >>>> these changes are correct. In particular the adjustment of the > >>>> relational operator. Thanks! > >>>> > >>>> Changes in v2: > >>>> - Use flexible array and adjust relational operator, accordingly. > >>> > >>> The operator was just correct. The check is just there to see the firmware > >>> is at least as large as the struct as which it is being accessed. > >> > >> I'm a bit confused, so based on your reply to v1 of this series, this patch > >> is now correct, right? > >> > >> The operator in v1 _was_ correct as long as the one-element array wasn't > >> transformed into a flexible array, right? > >> > >> Notice that generally speaking flexible-array members don't occupy space in the > >> containing structure: > >> > >> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o > >> struct imgu_fw_header { > >> struct imgu_fw_bi_file_h file_header; /* 0 72 */ > >> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ > >> struct imgu_fw_info binary_header[] __attribute__((__aligned__(8))); /* 72 0 */ > >> > >> /* size: 72, cachelines: 2, members: 2 */ > >> /* forced alignments: 1 */ > >> /* last cacheline: 8 bytes */ > >> } __attribute__((__aligned__(8))); > >> > >> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o > >> struct imgu_fw_header { > >> struct imgu_fw_bi_file_h file_header; /* 0 72 */ > >> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ > >> struct imgu_fw_info binary_header[1] __attribute__((__aligned__(8))); /* 72 1200 */ > >> > >> /* size: 1272, cachelines: 20, members: 2 */ > >> /* forced alignments: 1 */ > >> /* last cacheline: 56 bytes */ > >> } __attribute__((__aligned__(8))); > >> > >> So, now that the flexible array transformation is included in the same patch as the > >> bugfix, the operator is changed from '<' to '<=' > > > > '<' is correct since you only need as much data as the struct you're about > > to access is large, not a byte more than that. As Dan noted. > > > > I think you could add a check for binary_nr is at least one. > > If we need to check that binary_nr is at least one, then this would be the right > change: > > css->fwp = (struct imgu_fw_header *)css->fw->data; > - if (css->fw->size < sizeof(struct imgu_fw_header *) || > + if (css->fw->size < struct_size(css->fwp, binary_header, 1) || > css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) > goto bad_fw; There's already a check the space required for the array of binary_nr is there. But not the number itself.
On 8/10/21 11:30, Sakari Ailus wrote: > On Tue, Aug 10, 2021 at 11:26:14AM -0500, Gustavo A. R. Silva wrote: >> Hi Sakari, >> >> Please, see my comments below... >> >> On 8/10/21 10:18, Sakari Ailus wrote: >>> Hi Gustavo, >>> >>> Apologies for the delay. >>> >>> On Mon, Aug 02, 2021 at 08:46:20AM -0500, Gustavo A. R. Silva wrote: >>>> Hi Sakari, >>>> >>>> On 8/2/21 01:05, Sakari Ailus wrote: >>>>> Hi Gustavo, >>>>> >>>>> I missed you already had sent v2... >>>>> >>>>> On Fri, Jul 30, 2021 at 07:08:13AM -0500, Gustavo A. R. Silva wrote: >>>>>> There is a wrong comparison of the total size of the loaded firmware >>>>>> css->fw->size with the size of a pointer to struct imgu_fw_header. >>>>>> >>>>>> Fix this by using the right operand 'struct imgu_fw_header' for >>>>>> sizeof, instead of 'struct imgu_fw_header *' and turn binary_header >>>>>> into a flexible-array member. Also, adjust the relational operator >>>>>> to be '<=' instead of '<', as it seems that the intention of the >>>>>> comparison is to determine if the loaded firmware contains any >>>>>> 'struct imgu_fw_info' items in the binary_header[] array than merely >>>>>> the file_header (struct imgu_fw_bi_file_h). >>>>>> >>>>>> The replacement of the one-element array with a flexible-array member >>>>>> also help with the ongoing efforts to globally enable -Warray-bounds >>>>>> and get us closer to being able to tighten the FORTIFY_SOURCE routines >>>>>> on memcpy(). >>>>>> >>>>>> Link: https://github.com/KSPP/linux/issues/79 >>>>>> Link: https://github.com/KSPP/linux/issues/109 >>>>>> Fixes: 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management") >>>>>> Cc: stable@vger.kernel.org >>>>>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> >>>>>> --- >>>>>> >>>>>> It'd be just great if someone that knows this code better can confirm >>>>>> these changes are correct. In particular the adjustment of the >>>>>> relational operator. Thanks! >>>>>> >>>>>> Changes in v2: >>>>>> - Use flexible array and adjust relational operator, accordingly. >>>>> >>>>> The operator was just correct. The check is just there to see the firmware >>>>> is at least as large as the struct as which it is being accessed. >>>> >>>> I'm a bit confused, so based on your reply to v1 of this series, this patch >>>> is now correct, right? >>>> >>>> The operator in v1 _was_ correct as long as the one-element array wasn't >>>> transformed into a flexible array, right? >>>> >>>> Notice that generally speaking flexible-array members don't occupy space in the >>>> containing structure: >>>> >>>> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o >>>> struct imgu_fw_header { >>>> struct imgu_fw_bi_file_h file_header; /* 0 72 */ >>>> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ >>>> struct imgu_fw_info binary_header[] __attribute__((__aligned__(8))); /* 72 0 */ >>>> >>>> /* size: 72, cachelines: 2, members: 2 */ >>>> /* forced alignments: 1 */ >>>> /* last cacheline: 8 bytes */ >>>> } __attribute__((__aligned__(8))); >>>> >>>> $ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o >>>> struct imgu_fw_header { >>>> struct imgu_fw_bi_file_h file_header; /* 0 72 */ >>>> /* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */ >>>> struct imgu_fw_info binary_header[1] __attribute__((__aligned__(8))); /* 72 1200 */ >>>> >>>> /* size: 1272, cachelines: 20, members: 2 */ >>>> /* forced alignments: 1 */ >>>> /* last cacheline: 56 bytes */ >>>> } __attribute__((__aligned__(8))); >>>> >>>> So, now that the flexible array transformation is included in the same patch as the >>>> bugfix, the operator is changed from '<' to '<=' >>> >>> '<' is correct since you only need as much data as the struct you're about >>> to access is large, not a byte more than that. As Dan noted. >>> >>> I think you could add a check for binary_nr is at least one. >> >> If we need to check that binary_nr is at least one, then this would be the right >> change: >> >> css->fwp = (struct imgu_fw_header *)css->fw->data; >> - if (css->fw->size < sizeof(struct imgu_fw_header *) || >> + if (css->fw->size < struct_size(css->fwp, binary_header, 1) || >> css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) >> goto bad_fw; > > There's already a check the space required for the array of binary_nr is > there. But not the number itself. Yep; and that is for the upper limit. The whole fix would be this: diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.c b/drivers/staging/media/ipu3/ipu3-css-fw.c index 45aff76198e2..8830f42f2b12 100644 --- a/drivers/staging/media/ipu3/ipu3-css-fw.c +++ b/drivers/staging/media/ipu3/ipu3-css-fw.c @@ -124,12 +124,11 @@ int imgu_css_fw_init(struct imgu_css *css) /* Check and display fw header info */ css->fwp = (struct imgu_fw_header *)css->fw->data; - if (css->fw->size < sizeof(struct imgu_fw_header *) || + if (css->fw->size < struct_size(css->fwp, binary_header, 1) || css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) goto bad_fw; - if (sizeof(struct imgu_fw_bi_file_h) + - css->fwp->file_header.binary_nr * sizeof(struct imgu_fw_info) > - css->fw->size) + if (struct_size((struct imgu_fw_header *)0, binary_header, + css->fwp->file_header.binary_nr) > css->fw->size) goto bad_fw; dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n", diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.h b/drivers/staging/media/ipu3/ipu3-css-fw.h index 3c078f15a295..c0bc57fd678a 100644 --- a/drivers/staging/media/ipu3/ipu3-css-fw.h +++ b/drivers/staging/media/ipu3/ipu3-css-fw.h @@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h { struct imgu_fw_header { struct imgu_fw_bi_file_h file_header; - struct imgu_fw_info binary_header[1]; /* binary_nr items */ + struct imgu_fw_info binary_header[]; /* binary_nr items */ }; /******************* Firmware functions *******************/ Notice that "css->fw->size < struct_size(css->fwp, binary_header, 1)" with binary_header declared as a flexible-array member is equivalent to "css->fw->size < sizeof(struct imgu_fw_header)" with binary_header declared as a one-element array. -- Gustavo
diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.c b/drivers/staging/media/ipu3/ipu3-css-fw.c index 45aff76198e2..630cb5186b48 100644 --- a/drivers/staging/media/ipu3/ipu3-css-fw.c +++ b/drivers/staging/media/ipu3/ipu3-css-fw.c @@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css) /* Check and display fw header info */ css->fwp = (struct imgu_fw_header *)css->fw->data; - if (css->fw->size < sizeof(struct imgu_fw_header *) || + if (css->fw->size <= sizeof(struct imgu_fw_header) || css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h)) goto bad_fw; if (sizeof(struct imgu_fw_bi_file_h) + diff --git a/drivers/staging/media/ipu3/ipu3-css-fw.h b/drivers/staging/media/ipu3/ipu3-css-fw.h index 3c078f15a295..c0bc57fd678a 100644 --- a/drivers/staging/media/ipu3/ipu3-css-fw.h +++ b/drivers/staging/media/ipu3/ipu3-css-fw.h @@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h { struct imgu_fw_header { struct imgu_fw_bi_file_h file_header; - struct imgu_fw_info binary_header[1]; /* binary_nr items */ + struct imgu_fw_info binary_header[]; /* binary_nr items */ }; /******************* Firmware functions *******************/