Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2509640imm; Mon, 24 Sep 2018 05:40:46 -0700 (PDT) X-Google-Smtp-Source: ACcGV62kYcyru85QTHVVhLbPanADLzBsGaTowvHqZLnE16d43+nBmfkl+uC3es1YLQF/r0hDexTA X-Received: by 2002:a17:902:4201:: with SMTP id g1-v6mr10735812pld.203.1537792846019; Mon, 24 Sep 2018 05:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537792845; cv=none; d=google.com; s=arc-20160816; b=tnM8bQkluqq30KjyRxxGmkPsbxLI0unnrs1QOv6tefYu5bDBOuQ7XAKlJwKp32NkYg Zm4D5uHXfSc3cxSapo1Fkjwp++/YkH3tg8bfhc/GeRbl1tW2JTWAIl2od7uVVLsytFRV HI0dObbiFJ7FJPeg6QVktULiRdtdcbjciYOy/YqQSeTHE7LCUtAD00bJWdfDAmRUOL4a Aydpnl+UYvZjXa/n3oR5ahwVE0TXZw3Dv3DAjX51CfovPgp74EdMWxjOK+7y1QmQybW5 1u+y+7vGW3T27DKvkCD/NvzAZNWNI82i7I3YxE3IgHgtlD1lFL4yV0SlZ+IlkZq3AKP/ CLPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from; bh=uqJGJ0VgkvXo332PjiAlzQg493+iB1t19zvUPPU9+Ws=; b=dQHw1SMYKB4oWxv2HjN/eDc79Khl+znIS3DPSuOoPgwsZfMTuAuR9quH5ofT2J4bqg GFILvKNpDQMoJdXi8+KBvCrZKrk49IddwD+Vvwq75gCa0PI3BG5RqPTDpB8gd91kI7iO N6N/y8gHqmac6WSYdly6HcswVpoiWK1DLin7yOQX66Yas4OMQypSh++lQR5ZIy8pve9+ VDBILKZQUVYg1+zw/OXB2DByOS/7w7fwJNfuJgAeUUJM3AlKSdewvf4tO/UqGj01ZPfi qHETTeRqS0gQGYKZ1VnzCiInv+4w6ej0KQVE62q2sjip6Cs2eH5yiIavtPmQoL35MMOE 3nvQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o69-v6si35083408pfi.279.2018.09.24.05.40.30; Mon, 24 Sep 2018 05:40:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388204AbeIXSkE (ORCPT + 99 others); Mon, 24 Sep 2018 14:40:04 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:58674 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387792AbeIXSkD (ORCPT ); Mon, 24 Sep 2018 14:40:03 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D25681097; Mon, 24 Sep 2018 12:38:06 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sudip Mukherjee , Mathias Nyman , Alan Stern Subject: [PATCH 4.18 123/235] usb: Avoid use-after-free by flushing endpoints early in usb_set_interface() Date: Mon, 24 Sep 2018 13:51:49 +0200 Message-Id: <20180924113118.409699898@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113103.999624566@linuxfoundation.org> References: <20180924113103.999624566@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mathias Nyman commit f9a5b4f58b280c1d26255376713c132f93837621 upstream. The steps taken by usb core to set a new interface is very different from what is done on the xHC host side. xHC hardware will do everything in one go. One command is used to set up new endpoints, free old endpoints, check bandwidth, and run the new endpoints. All this is done by xHC when usb core asks the hcd to check for available bandwidth. At this point usb core has not yet flushed the old endpoints, which will cause use-after-free issues in xhci driver as queued URBs are cancelled on a re-allocated endpoint. To resolve this add a call to usb_disable_interface() which will flush the endpoints before calling usb_hcd_alloc_bandwidth() Additional checks in xhci driver will also be implemented to gracefully handle stale URB cancel on freed and re-allocated endpoints Cc: Reported-by: Sudip Mukherjee Signed-off-by: Mathias Nyman Acked-by: Alan Stern Signed-off-by: Greg Kroah-Hartman --- drivers/usb/core/message.c | 11 +++++++++++ 1 file changed, 11 insertions(+) --- a/drivers/usb/core/message.c +++ b/drivers/usb/core/message.c @@ -1340,6 +1340,11 @@ void usb_enable_interface(struct usb_dev * is submitted that needs that bandwidth. Some other operating systems * allocate bandwidth early, when a configuration is chosen. * + * xHCI reserves bandwidth and configures the alternate setting in + * usb_hcd_alloc_bandwidth(). If it fails the original interface altsetting + * may be disabled. Drivers cannot rely on any particular alternate + * setting being in effect after a failure. + * * This call is synchronous, and may not be used in an interrupt context. * Also, drivers must not change altsettings while urbs are scheduled for * endpoints in that interface; all such urbs must first be completed @@ -1375,6 +1380,12 @@ int usb_set_interface(struct usb_device alternate); return -EINVAL; } + /* + * usb3 hosts configure the interface in usb_hcd_alloc_bandwidth, + * including freeing dropped endpoint ring buffers. + * Make sure the interface endpoints are flushed before that + */ + usb_disable_interface(dev, iface, false); /* Make sure we have enough bandwidth for this alternate interface. * Remove the current alt setting and add the new alt setting.