Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2464434imm; Mon, 24 Sep 2018 04:59:20 -0700 (PDT) X-Google-Smtp-Source: ACcGV62uG4HOMhqWPwCB2zD2g4JbOCbWTm7lCv3Dc27zEZYPt0fkwWOTXvPrXM17iphg5EPlkdCt X-Received: by 2002:a17:902:d881:: with SMTP id b1-v6mr10458736plz.191.1537790360412; Mon, 24 Sep 2018 04:59:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537790360; cv=none; d=google.com; s=arc-20160816; b=jAc7pbEV/RiBj1fFNQuCRWweFVjNzNeJqCZdbt2U51Vmqzb9xNzZzWf7M8ibzB7jeh 23yA3VxyHsOVLGWmi5WAnWh07K3WAUq8dMaVIuJQ0MWmExyBaGVbcHs2dtXXefXI2bBw cj6g+ptlDOa1h+7rU9/Uxhcir+u9hImgjooXdmDXMDzgplaB7uK2qnV+r+8m0WjP+H+J 3RwWOZEIkzIlnVOL3fT36tk87Rf1xfZew7y4TcTDT0ez16hBfTCCzN95anlzudm2ERuw 2Vsc1BKQOs2VP1FSdol6B4WMfBk45oyswjIiyE8LG5JjTZ3qMkN9yrMoIXiSgMJQCTaU 4BJw== 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=zXR9/ee4/cdS2+LFhVPGZZqTOopdntCo9LBdlqaJ/lY=; b=hdeoWFi/xGt5BYa3WUwd4ELM7yBoosjVq/51yV4CqZDmHWqpCm+Wjo98ZdWsDx+l1Y NkvqOdBZouYBkqFDvnfuv4bTeGEjypddxucVuF+LBk0mBjYHG+jlSeB2wrE8lYEuBMvq j7g5z8WAjbtr3uy7Dg6I3F5H327OamRVyEnjaMCJe/KKA/wccEySTrVGzSQLwx7X9uPb Sxw9gIj0IcsDJyUw9tkKvCBYg38yn5qAsXsHIarJ5YKhOaIqBcjf2mwfAaHBAGIoogwR qZVcD3IYBv+lOthK7RTStH5GVA8ZOpeWEIdGM3o1R0JRCD6/kQjdXUSine1+kEVQX1wc nRig== 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 h86-v6si36406661pfj.120.2018.09.24.04.59.04; Mon, 24 Sep 2018 04:59:20 -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 S1730375AbeIXR76 (ORCPT + 99 others); Mon, 24 Sep 2018 13:59:58 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:52360 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729146AbeIXR76 (ORCPT ); Mon, 24 Sep 2018 13:59:58 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id C981FD64; Mon, 24 Sep 2018 11:58:12 +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.4 37/70] usb: Avoid use-after-free by flushing endpoints early in usb_set_interface() Date: Mon, 24 Sep 2018 13:52:36 +0200 Message-Id: <20180924113103.408499012@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180924113058.420454070@linuxfoundation.org> References: <20180924113058.420454070@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.4-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 @@ -1282,6 +1282,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 @@ -1317,6 +1322,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.