From: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
To: "Gustavo A. R. Silva" <gustavoars@kernel.org>,
Minas Harutyunyan <Minas.Harutyunyan@synopsys.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-hardening@vger.kernel.org"
<linux-hardening@vger.kernel.org>
Subject: Re: [PATCH][next] usb: dwc2: Use flex_array_size() helper
Date: Fri, 14 Jan 2022 18:50:54 +0000 [thread overview]
Message-ID: <d1ec4e8a-574a-d841-850c-3f136e404d1a@synopsys.com> (raw)
In-Reply-To: <20220111075513.GA76424@embeddedor>
Hi,
Gustavo A. R. Silva wrote:
> Make use of the flex_array_size() helper to calculate the size of a
> flexible array member within an enclosing structure.
>
> This helper offers defense-in-depth against potential integer
> overflows, while at the same time makes it explicitly clear that
> we are dealing with a flexible array member.
>
> Link: https://urldefense.com/v3/__https://github.com/KSPP/linux/issues/160__;!!A4F2R9G_pg!L8p7TlfbH0sNpL9gtsVuWEZVnP8euMZtJKpc71sXxzEw4MAKWuQ28dJJc0cJTI3bWXLm$
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
> drivers/usb/dwc2/hcd.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
> index f63a27d11fac..2c21498662cd 100644
> --- a/drivers/usb/dwc2/hcd.c
> +++ b/drivers/usb/dwc2/hcd.c
> @@ -4054,8 +4054,9 @@ struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
> * For single_tt we need one schedule. For multi_tt
> * we need one per port.
> */
> - bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
> - sizeof(dwc_tt->periodic_bitmaps[0]);
> + bitmap_size =
> + flex_array_size(dwc_tt, periodic_bitmaps,
> + DWC2_ELEMENTS_PER_LS_BITMAP);
> if (urb->dev->tt->multi)
> bitmap_size *= urb->dev->tt->hub->maxchild;
>
This doesn't look right even though the result will be the same. The
return value from flex_array_size() may not be the final array size as you
can see that bitmap_size gets updated again a couple lines down.
How about this instead:
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index f63a27d11fac..0936ff8b57db 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -4048,18 +4048,17 @@ struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
dwc_tt = urb->dev->tt->hcpriv;
if (!dwc_tt) {
- size_t bitmap_size;
+ int count;
/*
* For single_tt we need one schedule. For multi_tt
* we need one per port.
*/
- bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
- sizeof(dwc_tt->periodic_bitmaps[0]);
+ count = DWC2_ELEMENTS_PER_LS_BITMAP;
if (urb->dev->tt->multi)
- bitmap_size *= urb->dev->tt->hub->maxchild;
+ count *= urb->dev->tt->hub->maxchild;
- dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
+ dwc_tt = kzalloc(struct_size(dwc_tt, periodic_bitmaps, count),
mem_flags);
if (!dwc_tt)
return NULL;
BR,
Thinh
prev parent reply other threads:[~2022-01-14 18:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-11 7:55 [PATCH][next] usb: dwc2: Use flex_array_size() helper Gustavo A. R. Silva
2022-01-14 18:50 ` Thinh Nguyen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d1ec4e8a-574a-d841-850c-3f136e404d1a@synopsys.com \
--to=thinh.nguyen@synopsys.com \
--cc=Minas.Harutyunyan@synopsys.com \
--cc=gregkh@linuxfoundation.org \
--cc=gustavoars@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox