Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp502187pxb; Thu, 9 Sep 2021 06:01:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz63VFelQZOgaqTqwi5oNY5CjVSmerqmpoIJsXGwWkFQ+gB27jATxQDcKLR2ZJlUr5m2Q+G X-Received: by 2002:a92:dcc6:: with SMTP id b6mr2258160ilr.307.1631192464591; Thu, 09 Sep 2021 06:01:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631192464; cv=none; d=google.com; s=arc-20160816; b=AwyDfTvr6NWbDoah3xG2NCX9uM0tvdkHokOZwKC3oNj4uVSiaihffHbuQDru3TYy79 P5ji8Blvzb/g3Q1aT88wg5R+MbkGWyFQSdsnSCSNjU7ymdFtmFKJrcqHHN3isMt9a5sz eqZGDDicZK21CGbM2N0xYF92LGjpUrf9K85yq04VKMa3/OlbZRYYrCncPSgB4pVHTkxP qTqdsUuz4yyPz8FMOXHKMvXyMGAIo+Q/iDBDymqOy03PpQUgaPbeH3ERtd5RCVJW7cJ/ kA4NO/hc/qtZEK9od5WFpTB74Jd108413d5GnfJ3/F4Hkge2LBNOFV5aUY6hidO7jq3d a1qQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0Us9xXKnqONkUASajxT2PSK/n0eLAbSSWkQH32iMt1A=; b=hi1e/hgKY/K7bCslemQHftcD3HX8SuriBLcQu0XjrVY+02SINmtoZAJMiSmc6sKU7T YNeOa9KcprqUnszhrTtS5wcfdq1oTIdbHEFTXC3vwo724JFfUr7PFSPFtoO12PeMvbS/ UYafYWjm43F5/dbpytqslyDmjWPXt9oX0lhDsw5LatMurXp8K7I0tjWfb+mJ2sF0XZPx An+Y5jOi5cj0bUbjOobjiOhrIm/3VKygBel0w/akoDSaQ4kM5OXK9Qb8HTn5krRR17C5 M4oFXZx60yURYPsIdKJeyNcpIBQG12J0BBWggGe5WDQb5M3A9Dha8YpPs7grODqT6Ih2 t1Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RKEUZbop; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f5si1895167ils.120.2021.09.09.06.00.48; Thu, 09 Sep 2021 06:01:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RKEUZbop; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1357329AbhIINAK (ORCPT + 99 others); Thu, 9 Sep 2021 09:00:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:40990 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353476AbhIIMtJ (ORCPT ); Thu, 9 Sep 2021 08:49:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B0AB461A88; Thu, 9 Sep 2021 11:56:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631188605; bh=K/PGg6kLoihHZ8O6+cHlmGT+jLvgPdnTCFTMk7Bxex0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RKEUZbopeVmJuteChhkbYXOX6LLMN4Lw9i4FBXXE5zeYtDNfRSZAR49YGqICCl4wc uFEz1CrJiZFQE+2mfFGfQ13TA44FedrG7UpjhBtZ0mPVngdIEPHrNo4Wa1iP8/fRJp upLPE1jGzLPIpe2lwluWE40RVGU5V/mmJUXA1UinQ2Vk+N12mABYCOxaJnmvzSrvOb iQkJ2OrOZy/9l0doltl4FYviJtHYBu3fk6Zv3obvCwx3ZaKYNSWIthFu6ulV0QRWig NaLoJPMRlnCcDA6X/pmJfAkYLn7CAPAI/p8SyIc8HtTYhrnWJ98t/wDbT4F0e5xdQc 7Vl8IaAJjOkiQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Li Jun , Zhipeng Wang , Peter Chen , Sasha Levin , linux-usb@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 077/109] usb: chipidea: host: fix port index underflow and UBSAN complains Date: Thu, 9 Sep 2021 07:54:34 -0400 Message-Id: <20210909115507.147917-77-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909115507.147917-1-sashal@kernel.org> References: <20210909115507.147917-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Li Jun [ Upstream commit e5d6a7c6cfae9e714a0e8ff64facd1ac68a784c6 ] If wIndex is 0 (and it often is), these calculations underflow and UBSAN complains, here resolve this by not decrementing the index when it is equal to 0, this copies the solution from commit 85e3990bea49 ("USB: EHCI: avoid undefined pointer arithmetic and placate UBSAN") Reported-by: Zhipeng Wang Signed-off-by: Li Jun Link: https://lore.kernel.org/r/1624004938-2399-1-git-send-email-jun.li@nxp.com Signed-off-by: Peter Chen Signed-off-by: Sasha Levin --- drivers/usb/chipidea/host.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/usb/chipidea/host.c b/drivers/usb/chipidea/host.c index 48e4a5ca1835..f5f56ee07729 100644 --- a/drivers/usb/chipidea/host.c +++ b/drivers/usb/chipidea/host.c @@ -233,18 +233,26 @@ static int ci_ehci_hub_control( ) { struct ehci_hcd *ehci = hcd_to_ehci(hcd); + unsigned int ports = HCS_N_PORTS(ehci->hcs_params); u32 __iomem *status_reg; - u32 temp; + u32 temp, port_index; unsigned long flags; int retval = 0; struct device *dev = hcd->self.controller; struct ci_hdrc *ci = dev_get_drvdata(dev); - status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1]; + port_index = wIndex & 0xff; + port_index -= (port_index > 0); + status_reg = &ehci->regs->port_status[port_index]; spin_lock_irqsave(&ehci->lock, flags); if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) { + if (!wIndex || wIndex > ports) { + retval = -EPIPE; + goto done; + } + temp = ehci_readl(ehci, status_reg); if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) { retval = -EPIPE; @@ -273,7 +281,7 @@ static int ci_ehci_hub_control( ehci_writel(ehci, temp, status_reg); } - set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports); + set_bit(port_index, &ehci->suspended_ports); goto done; } -- 2.30.2