Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp519385pxb; Wed, 27 Jan 2021 13:42:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJxtdN9Odinjbha1zaMau5d+/fKYRnhs8oF++/e/Y9N79wNrOcIU/GBN9JEfNkWgDT93KiAW X-Received: by 2002:aa7:ca51:: with SMTP id j17mr11139289edt.124.1611783764981; Wed, 27 Jan 2021 13:42:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611783764; cv=none; d=google.com; s=arc-20160816; b=ktsh1HOImaQtBFmz+oIE4cltfQhzb+X61HPwRubGxM5keYAQTPQYkKZj5s6IotDaMc Aufw3A4A/SHSLnlqvyUmTz6Fs1jQ4toUnc185llazWnm/jfPVIPUg8Xrg2/uMM7EDkob FWs/cmxfZVJ2904sI6FUyaCgeio5cctDBdkHjTpMHg3SHuxkVTRMZlQma7/nRsgqBUEW OWUatu3kMu2YVYRmRSg+UZRKn6JHS+RBFbmq5z745/uWKu7HzbL1Yz1eOx4oSYFfXr9t SQwvOe45HbfBET5PSW7eyLeYxKGdzeRjBEb57N5NfzLt13V023gFJAnAxAWLAEt/kFAI mVUA== 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 :message-id:date:subject:cc:to:from; bh=ts8ZXLGZ1oujuNvWd8QGzwmJzrEpwdaQfGVPR0xRZo0=; b=qWQH2cEYIhrSels/kTLqwGsnKAdt4COCtWN3p1z6BW34c8gwBtFzhD9EqdKw0PkD0p 3Dvls/Y1Qud2A5zfG2wlh+XiLWnIj9zLXAjsvpVw/el8i8A+n7PjiO1/MPynthwfiNfV MKjLA4GqXhjvp3H8RimVfDatzdZtBnIg7xWuBjWnpzTGuJPlTmu9CGv1jFsZAiMK22sW hsRKE8v3QCWW3y3DtsDZnw08drqa6KN9G6PbJZl7sPwzvrWP6F2tYiG8EzSrIwtVG06U e2T1HTrlOytVruAbB5ui0NyBvW3+8XIoy5vMHUeHXWf4sHBJajHMhM96RJRv5xg6be/Y OpIA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i1si1606407edx.383.2021.01.27.13.42.20; Wed, 27 Jan 2021 13:42:44 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233208AbhA0Iyb (ORCPT + 99 others); Wed, 27 Jan 2021 03:54:31 -0500 Received: from gloria.sntech.de ([185.11.138.130]:41610 "EHLO gloria.sntech.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232904AbhA0Ivy (ORCPT ); Wed, 27 Jan 2021 03:51:54 -0500 Received: from ip5f5aa64a.dynamic.kabel-deutschland.de ([95.90.166.74] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l4gX6-00007Q-CP; Wed, 27 Jan 2021 09:50:36 +0100 From: Heiko Stuebner To: hminas@synopsys.com, gregkh@linuxfoundation.org Cc: christoph.muellner@theobroma-systems.com, paulz@synopsys.com, yousaf.kaukab@intel.com, balbi@ti.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, heiko@sntech.de, Heiko Stuebner , stable@vger.kernel.org Subject: [PATCH v2] usb: dwc2: Fix endpoint direction check in ep_from_windex Date: Wed, 27 Jan 2021 09:50:34 +0100 Message-Id: <20210127085034.36397-1-heiko@sntech.de> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiko Stuebner dwc2_hsotg_process_req_status uses ep_from_windex() to retrieve the endpoint for the index provided in the wIndex request param. In a test-case with a rndis gadget running and sending a malformed packet to it like: dev.ctrl_transfer( 0x82, # bmRequestType 0x00, # bRequest 0x0000, # wValue 0x0001, # wIndex 0x00 # wLength ) it is possible to cause a crash: [ 217.533022] dwc2 ff300000.usb: dwc2_hsotg_process_req_status: USB_REQ_GET_STATUS [ 217.559003] Unable to handle kernel read from unreadable memory at virtual address 0000000000000088 ... [ 218.313189] Call trace: [ 218.330217] ep_from_windex+0x3c/0x54 [ 218.348565] usb_gadget_giveback_request+0x10/0x20 [ 218.368056] dwc2_hsotg_complete_request+0x144/0x184 This happens because ep_from_windex wants to compare the endpoint direction even if index_to_ep() didn't return an endpoint due to the direction not matching. The fix is easy insofar that the actual direction check is already happening when calling index_to_ep() which will return NULL if there is no endpoint for the targeted direction, so the offending check can go away completely. Fixes: c6f5c050e2a7 ("usb: dwc2: gadget: add bi-directional endpoint support") Signed-off-by: Heiko Stuebner Cc: stable@vger.kernel.org --- changes in v2: - remove unused struct dwc2_hsotg_ep *ep; drivers/usb/dwc2/gadget.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c index 0a0d11151cfb..ad4c94366dad 100644 --- a/drivers/usb/dwc2/gadget.c +++ b/drivers/usb/dwc2/gadget.c @@ -1543,7 +1543,6 @@ static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, u32 windex) { - struct dwc2_hsotg_ep *ep; int dir = (windex & USB_DIR_IN) ? 1 : 0; int idx = windex & 0x7F; @@ -1553,12 +1552,7 @@ static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, if (idx > hsotg->num_of_eps) return NULL; - ep = index_to_ep(hsotg, idx, dir); - - if (idx && ep->dir_in != dir) - return NULL; - - return ep; + return index_to_ep(hsotg, idx, dir); } /** -- 2.29.2