From patchwork Sat Nov 13 16:53:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Stern X-Patchwork-Id: 12617715 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4077DC433F5 for ; Sat, 13 Nov 2021 16:53:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 277A960FE3 for ; Sat, 13 Nov 2021 16:53:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235865AbhKMQ4O (ORCPT ); Sat, 13 Nov 2021 11:56:14 -0500 Received: from netrider.rowland.org ([192.131.102.5]:53141 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S235241AbhKMQ4O (ORCPT ); Sat, 13 Nov 2021 11:56:14 -0500 Received: (qmail 59821 invoked by uid 1000); 13 Nov 2021 11:53:20 -0500 Date: Sat, 13 Nov 2021 11:53:20 -0500 From: Alan Stern To: Greg KH , zhang haiming Cc: USB mailing list Subject: [PATCH v5] USB: ehci_brcm_hub_control: Improve port index sanitizing Message-ID: <20211113165320.GA59686@rowland.harvard.edu> References: <20211113045714.46373-1-tcs.kernel@gmail.com> <20211113154049.GA58521@rowland.harvard.edu> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Haimin Zhang Due to (wIndex & 0xff) - 1 can get an integer greater than 15, this can cause array index to be out of bounds since the size of array port_status is 15. This change prevents a possible out-of-bounds pointer computation by forcing the use of a valid port number. Reported-by: TCS Robot Signed-off-by: Haimin Zhang Signed-off-by: Alan Stern --- v5 (Alan Stern): Changed 0xf in patch description to 15. Added comment explaining why the port index is sanitized. drivers/usb/host/ehci-brcm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) Index: usb-devel/drivers/usb/host/ehci-brcm.c =================================================================== --- usb-devel.orig/drivers/usb/host/ehci-brcm.c +++ usb-devel/drivers/usb/host/ehci-brcm.c @@ -62,8 +62,12 @@ 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; + if (temp >= HCS_N_PORTS_MAX) /* Avoid index-out-of-bounds warning */ + temp = 0; + status_reg = &ehci->regs->port_status[temp]; /* * RESUME is cleared when GetPortStatus() is called 20ms after start