public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Haimin Zhang <tcs.kernel@gmail.com>
Cc: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org,
	TCS Robot <tcs_robot@tencent.com>
Subject: Re: [PATCH v3] USB: ehci_brcm_hub_control: improve port index sanitizing
Date: Fri, 12 Nov 2021 10:36:15 -0500	[thread overview]
Message-ID: <20211112153615.GA32928@rowland.harvard.edu> (raw)
In-Reply-To: <20211112094921.90496-1-tcs.kernel@gmail.com>

On Fri, Nov 12, 2021 at 05:49:21PM +0800, Haimin Zhang wrote:
> Due to (wIndex & 0xff) - 1 can get an integer greater than 0xf, this
> can cause array index to be out of bounds since the size of array
> port_status is 0xf. Using macro function HCS_N_PORTS() can return
> a valid index less than 15. Macro function HCS_N_PORTS() is used
> to obtain a valid port index by logical AND 0xf.
> 
> Reported-by: TCS Robot <tcs_robot@tencent.com>
> Signed-off-by: Haimin Zhang <tcs.kernel@gmail.com>
> ---
>  drivers/usb/host/ehci-brcm.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/host/ehci-brcm.c b/drivers/usb/host/ehci-brcm.c
> index d3626bfa966b..d2bf7768349c 100644
> --- a/drivers/usb/host/ehci-brcm.c
> +++ b/drivers/usb/host/ehci-brcm.c
> @@ -62,8 +62,11 @@ static int ehci_brcm_hub_control(
>  	u32 __iomem	*status_reg;
>  	unsigned long flags;
>  	int retval, irq_disabled = 0;
> +	u32 temp;
>  
> -	status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
> +	temp = (wIndex & 0xff) - 1;
> +	temp = HCS_N_PORTS(temp);

This is a misuse of the HCS_N_PORTS macro; the fact that it works out 
okay is just a coincidence.  That macro was intended for extracting the 
number of ports from the hcs_params word in the EHCI register space.  It 
should not be used for any other purpose (including forcing a value to 
be in the range of valid port numbers).

If you want to do this correctly, do something like:

	temp = min_t(u32, temp, HCS_N_PORTS_MAX - 1);

Or even:

	if (temp >= HCS_N_PORTS_MAX)	/* Force valid port number */
		temp = 0;

Alan Stern

> +	status_reg = &ehci->regs->port_status[temp];
>  
>  	/*
>  	 * RESUME is cleared when GetPortStatus() is called 20ms after start
> -- 
> 2.30.1 (Apple Git-130)

      parent reply	other threads:[~2021-11-12 15:36 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-12  9:49 [PATCH v3] USB: ehci_brcm_hub_control: improve port index sanitizing Haimin Zhang
2021-11-12 10:01 ` Greg KH
2021-11-12 15:36 ` Alan Stern [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=20211112153615.GA32928@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=tcs.kernel@gmail.com \
    --cc=tcs_robot@tencent.com \
    /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