Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2254519ybl; Thu, 15 Aug 2019 08:54:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqykwXjUkvnCOKM2cVq5DhmbtUxneyLzEJ9GGuMx8MCI2i+HzoMSInfiaOZQfbtqgI51e+OA X-Received: by 2002:a63:4846:: with SMTP id x6mr4067237pgk.332.1565884463861; Thu, 15 Aug 2019 08:54:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565884463; cv=none; d=google.com; s=arc-20160816; b=SD21cITmDaJ3nfXz2/DWXb5IkzHob8jzv2nhX/KObUI+yr11f33uoOWWYi5lG8nPHi th3+08a+7bB1my/LR1XVf1fb9935kx2R7uO6cpGN4TauwkKNm/prIZfa3+qdxopv4Fo2 eBBxy8irQ2H/SWzXS2n2Ef/T2pDBXxQkaUXtdytoSrG/bkH8GC1+C6fX5dg2BQ3W3YjP o3d8su03yD9AdUjHhVI80vQftLD4nm0/JZ/TLG5ym47X4ovPF8UsdT9jm/fei2/1fy1h WZ++v6xdG1Md2/NdYrG1EVzo/CNNcT3Mtv7b3zl/L374eP/YRAwkEN19FkdQ/cEMeOna kNTg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=XyD06YZS2Z9vjlAjxD/F+RLqC6OCWBrblTJoR3CHyz0=; b=lHYPu2jNxUwNbjT5aejkWFFY66dFmlOX3PlOCSjpRZY3L4sOF2n+iCZ1WoS4ZtLxPw e8FT5Vp6clSgpbDAr/QsU8Y9QaXtFTs1mA5oopBUYTB+OIjHlXXOl+OG1rA1dVhNn1jE 5rdzC9sRQ/gTS9SR0NE6RPXuYcv7OAMazmdCQjsu20Q3Y1JvGAdaEj8sjFLmn3PO0/Pp 6Jukbx/FHcwcY5B44jxj7e1Su0mjBoXk0pPNfR7rOYqOci6UDg38Pw9LTINZ9tE0he3C lm0QzwwrGu/keH6bZtlKkRYyiCjEej0xJ9jFgZp1+4gz4triknmGT76sc68o93RsmL0Z L2mA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a22si1188863pjv.56.2019.08.15.08.54.08; Thu, 15 Aug 2019 08:54:23 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729565AbfHOP13 (ORCPT + 99 others); Thu, 15 Aug 2019 11:27:29 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:51550 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726120AbfHOP13 (ORCPT ); Thu, 15 Aug 2019 11:27:29 -0400 Received: by mail-wm1-f66.google.com with SMTP id 207so1605743wma.1 for ; Thu, 15 Aug 2019 08:27:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=XyD06YZS2Z9vjlAjxD/F+RLqC6OCWBrblTJoR3CHyz0=; b=AYmc6659pGyiAZLYbmUkciln9wAf5qCeAHgfuQ5LtehtSDZxWO4EA4jmnbnTMUWGMO Vmwj9yGcmdIm4aghq03zpaAzOJjOooju8z9Zb1JgGh2SNCvWwrEX6h/WTpjGk0wRgvP1 UB7hWymnt0ERHAu3uA7QlNG9jmaGCIvFFkxnmoBPW2wvwBZMaQKK2KrDNZp7ZqHsOcsG CdIzir7Sf84ZwzEk/ZWtoJktmpXDscxTdq24XTA2r2TLCnaQqLbfvcYEglRfX9qHC4ud P2SSiYWv8GOKOQWznBx/H3XI0HiQVUII0LgVGBqLExJ9XjD86UklRUcl9XDwotShbIy6 aUdA== X-Gm-Message-State: APjAAAXnp/HbMJt+C5UfaD/jfuR0S79tXN6bVdOJvFRuIOTExXnxqxUA ec9XK3AASuX1XtLJPoaPcpbxww== X-Received: by 2002:a1c:f20a:: with SMTP id s10mr3397376wmc.145.1565882846338; Thu, 15 Aug 2019 08:27:26 -0700 (PDT) Received: from shalem.localdomain (84-106-84-65.cable.dynamic.v4.ziggo.nl. [84.106.84.65]) by smtp.gmail.com with ESMTPSA id d16sm3726349wrj.47.2019.08.15.08.27.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Aug 2019 08:27:25 -0700 (PDT) Subject: Re: [PATCH v2] usb: xhci-pci: reorder removal to avoid use-after-free To: "Schmid, Carsten" Cc: "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-pci@vger.kernel.org" References: <1565782781938.37795@mentor.com> <15aa45c7-6e45-d03f-9336-4291f8b2dc66@redhat.com> <29aadcf136bb4d5285afb4fc5b500b49@SVR-IES-MBX-03.mgc.mentorg.com> <662c2014-f52c-a4a7-cbf0-78d43c3a4f22@redhat.com> <1565793165678.11527@mentor.com> From: Hans de Goede Message-ID: <3cb06e75-70a8-0f59-ae08-5bbd99abbead@redhat.com> Date: Thu, 15 Aug 2019 17:27:24 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <1565793165678.11527@mentor.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On 14-08-19 16:32, Schmid, Carsten wrote: > On driver removal, the platform_device_unregister call > attached through devm_add_action_or_reset was executed > after usb_hcd_pci_remove. > This lead to a use-after-free for the iomem resorce of > the xhci-ext-caps driver in the platform removal > because the parent of the resource was freed earlier. > > Fix this by reordering of the removal sequence. > > Signed-off-by: Carsten Schmid Patch looks good to me: Reviewed-by: Hans de Goede Regards, Hans > --- > v2: > - more speaking name for private data element > - consider failure in driver init sequence > - fix minor issues found by checkpatch.pl > --- > drivers/usb/host/xhci-ext-caps.c | 25 +++++++++++++++---------- > drivers/usb/host/xhci-pci.c | 8 +++++++- > drivers/usb/host/xhci-pci.h | 20 ++++++++++++++++++++ > drivers/usb/host/xhci.h | 1 + > 4 files changed, 43 insertions(+), 11 deletions(-) > create mode 100644 drivers/usb/host/xhci-pci.h > > diff --git a/drivers/usb/host/xhci-ext-caps.c b/drivers/usb/host/xhci-ext-caps.c > index 399113f9fc5c..28a7d53ecf2c 100644 > --- a/drivers/usb/host/xhci-ext-caps.c > +++ b/drivers/usb/host/xhci-ext-caps.c > @@ -7,21 +7,19 @@ > > #include > #include "xhci.h" > +#include "xhci-pci.h" > > #define USB_SW_DRV_NAME "intel_xhci_usb_sw" > #define USB_SW_RESOURCE_SIZE 0x400 > > -static void xhci_intel_unregister_pdev(void *arg) > -{ > - platform_device_unregister(arg); > -} > - > static int xhci_create_intel_xhci_sw_pdev(struct xhci_hcd *xhci, u32 cap_offset) > { > struct usb_hcd *hcd = xhci_to_hcd(xhci); > struct device *dev = hcd->self.controller; > struct platform_device *pdev; > struct resource res = { 0, }; > + struct xhci_pci_priv *priv = (struct xhci_pci_priv *)xhci->priv; > + > int ret; > > pdev = platform_device_alloc(USB_SW_DRV_NAME, PLATFORM_DEVID_NONE); > @@ -52,11 +50,7 @@ static int xhci_create_intel_xhci_sw_pdev(struct xhci_hcd *xhci, u32 cap_offset) > return ret; > } > > - ret = devm_add_action_or_reset(dev, xhci_intel_unregister_pdev, pdev); > - if (ret) { > - dev_err(dev, "couldn't add unregister action for intel_xhci_usb_sw pdev\n"); > - return ret; > - } > + priv->role_switch_pdev = pdev; > > return 0; > } > @@ -88,3 +82,14 @@ int xhci_ext_cap_init(struct xhci_hcd *xhci) > return 0; > } > EXPORT_SYMBOL_GPL(xhci_ext_cap_init); > + > +void xhci_ext_cap_remove(struct xhci_hcd *xhci) > +{ > + struct xhci_pci_priv *priv = (struct xhci_pci_priv *)xhci->priv; > + > + if (priv->role_switch_pdev) { > + platform_device_unregister(priv->role_switch_pdev); > + priv->role_switch_pdev = NULL; > + } > +} > +EXPORT_SYMBOL_GPL(xhci_ext_cap_remove); > diff --git a/drivers/usb/host/xhci-pci.c b/drivers/usb/host/xhci-pci.c > index c2fe218e051f..f2201f380c17 100644 > --- a/drivers/usb/host/xhci-pci.c > +++ b/drivers/usb/host/xhci-pci.c > @@ -14,6 +14,7 @@ > #include > > #include "xhci.h" > +#include "xhci-pci.h" > #include "xhci-trace.h" > > #define SSIC_PORT_NUM 2 > @@ -62,6 +63,7 @@ static struct hc_driver __read_mostly xhci_pci_hc_driver; > static int xhci_pci_setup(struct usb_hcd *hcd); > > static const struct xhci_driver_overrides xhci_pci_overrides __initconst = { > + .extra_priv_size = sizeof(struct xhci_pci_priv), > .reset = xhci_pci_setup, > }; > > @@ -350,7 +352,7 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) > retval = usb_add_hcd(xhci->shared_hcd, dev->irq, > IRQF_SHARED); > if (retval) > - goto put_usb3_hcd; > + goto remove_ext_cap; > /* Roothub already marked as USB 3.0 speed */ > > if (!(xhci->quirks & XHCI_BROKEN_STREAMS) && > @@ -368,6 +370,8 @@ static int xhci_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) > > return 0; > > +remove_ext_cap: > + xhci_ext_cap_remove(xhci); > put_usb3_hcd: > usb_put_hcd(xhci->shared_hcd); > dealloc_usb2_hcd: > @@ -393,6 +397,8 @@ static void xhci_pci_remove(struct pci_dev *dev) > xhci->shared_hcd = NULL; > } > > + xhci_ext_cap_remove(xhci); > + > /* Workaround for spurious wakeups at shutdown with HSW */ > if (xhci->quirks & XHCI_SPURIOUS_WAKEUP) > pci_set_power_state(dev, PCI_D3hot); > diff --git a/drivers/usb/host/xhci-pci.h b/drivers/usb/host/xhci-pci.h > new file mode 100644 > index 000000000000..fc0cde231679 > --- /dev/null > +++ b/drivers/usb/host/xhci-pci.h > @@ -0,0 +1,20 @@ > +/* SPDX-License-Identifier: GPL-2.0 > + * > + * xhci-pci.h - xHCI extended capability handling platform Glue. > + * > + * Copyright (C) 2019 Mentor Graphics (Deutschland) GmbH > + * Derived from xhci-plat.h > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * version 2 as published by the Free Software Foundation. > + */ > + > +#ifndef _XHCI_PCI_H > +#define _XHCI_PCI_H > + > +struct xhci_pci_priv { > + struct platform_device *role_switch_pdev; > +}; > + > +#endif /* _XHCI_PCI_H */ > diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h > index fabbce1c542a..847d2021fc2c 100644 > --- a/drivers/usb/host/xhci.h > +++ b/drivers/usb/host/xhci.h > @@ -2052,6 +2052,7 @@ void xhci_init_driver(struct hc_driver *drv, > const struct xhci_driver_overrides *over); > int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id); > int xhci_ext_cap_init(struct xhci_hcd *xhci); > +void xhci_ext_cap_remove(struct xhci_hcd *xhci); > > int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup); > int xhci_resume(struct xhci_hcd *xhci, bool hibernated); >