Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp534245pxb; Wed, 27 Jan 2021 14:10:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJxmKq1Y7U9S2lQvjaGsBGWCN+2fbA22oFnMSuCr8U3JhjLN+zLz16i3WrF9L/Y/Wmc2lfba X-Received: by 2002:a50:9f65:: with SMTP id b92mr10881160edf.74.1611785434974; Wed, 27 Jan 2021 14:10:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611785434; cv=none; d=google.com; s=arc-20160816; b=zR2W4RmqWr+kFznGyD8z0g4zgengacSmet5wcGKZgKD9HURqHmJJ+b2gm1xT+XmfrM /kUAhYxIalu/2/3EizOjp1VurKkakL2NnHTQLUCxfwNWhwsriy17vCSuFers66CRxxAK uS0W//jMzxa98xb4+1VLiLW4T1htzIUTUUap/gi4xMkv1Vpk0vC+QT/aJun2YVej3SEs nn2ykOjlz5VIAsN26US8ehhwdiUkXxTOmgHaOEu0t7cSSf098bGOBB1Ah1+ZSfpStBZy HcnfhFBzHspaSDWBg/jEcsJWvbtBc2Nc2I0nzJcZ9z+SdIuiM+9qmrcTmY6/gwrkwy8K aWag== 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=7V6UV3s91NYjMJUglr7PCkNwA5Vbst/CrJxNIWF3OU0=; b=ryJPef9YDY0qsbNjGkzzNRctXcvRS0szTGMB1cSgS+oiH8A4Blv8hjyAMUSmaspkDq 8dj5nvn65ViNTl9odPcHLNTuWCO7mWc1wGqEvzbjMf6eg6+a4xjNFmGDZrDyip4x2aiN koUtbzNlL5x+pYWj4Wr1MvO16A7ZdvLhHZT3mRDlAZrjoWKLWLb8kcNurvD85Z65RG93 kxM6zluR5TE7+9t5S5ynOAvf6f2S1BM2jzM6ySqqwkF82sX+p1seCrvj6coMgvoYeCfG Bdd17UTGg9gJTHpzp0eovbiXOYrlhE+O9pXfy3mHTloeQXTBs9Yz5XRwIZoFDKtrretp 2L+g== 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 o26si1724438edw.74.2021.01.27.14.10.08; Wed, 27 Jan 2021 14:10:34 -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 S232502AbhA0Kmk (ORCPT + 99 others); Wed, 27 Jan 2021 05:42:40 -0500 Received: from gloria.sntech.de ([185.11.138.130]:42078 "EHLO gloria.sntech.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235841AbhA0KkZ (ORCPT ); Wed, 27 Jan 2021 05:40:25 -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 1l4iEU-0000kX-2Y; Wed, 27 Jan 2021 11:39:30 +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 , Gerhard Klostermeier , stable@vger.kernel.org Subject: [PATCH v3] usb: dwc2: Fix endpoint direction check in ep_from_windex Date: Wed, 27 Jan 2021 11:39:19 +0100 Message-Id: <20210127103919.58215-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") Reported-by: Gerhard Klostermeier Signed-off-by: Heiko Stuebner Cc: stable@vger.kernel.org --- changes in v3: - added Reported-by tag 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