Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp777170ybl; Wed, 14 Aug 2019 05:55:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqx8sFHzkfGJ87PFVZNmlKeEJemLmehhdwHNVVOTolgtTMSWfbiNdhJSm0LQlmVF2TkxfOH7 X-Received: by 2002:a17:90a:5288:: with SMTP id w8mr7186863pjh.61.1565787358021; Wed, 14 Aug 2019 05:55:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565787358; cv=none; d=google.com; s=arc-20160816; b=eknRBli2ESD5bF5Y+15lUEcW/NNPj2mNDqUA/zBmhHVaZ8esbcg244IfOspAMZ9GQB bLpEucvEDfSwsxaR7jOp7f0hpIp1aKHYWV2Dmr63+R3l1dXt1Y72X9rwV5SSXqR9vA3i W2D0BLAZFPSn1u3jsQ2CHww8LtKlul3k3HheQimk25sEwjujrhD5g83sVki3pQH0p8SW PIl+EIAiyioI7AojY7/rqnov9Iy7URZ9+1eRJW/1a/Dd3lbKurJj8biSj3TthnJdzwxv eIdGMDkZ2wPPKeLvVIqmjqXxzrNKvFvkfqfFstV5YYrOqgDA5AbrD6UphpKLmH6g1gzL mIjQ== 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=VhOtcUs6qecW9A5Ku6t6s7+WyBSfMvyZ0VSLJHIcnmU=; b=Fou2KRKpMS17eKj86Odk3pEOMw9X41gr0YyfVqOxD76c7Q8bSSo7gSqn22+bTvNl4G GN3SPKR5BBnmYkF+h4iRrGCT7vSOoFkSCjG2uL5NDHxlDlay4j+LJQh5bQoSoA6JcmsW KBDNiS4YEmsRkRzxgDAGu27sNmgV6sWeUtjncPPWciTtKbormuNw4L23PRXX5SOatl43 7tmbWv2bmt2bo9fe+sve5jnBImYDjNHHNwb3EkaDDo89JC2R+PTvbflSsRlTvINnqi/p ER9PTz5fBZ8jI4EJHeroAl3xrJuer3l4ZsK+OoDwb7HVu3T5T0fTN53oYco37xeICLwx Nyvg== 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 o29si36468066pgm.380.2019.08.14.05.55.42; Wed, 14 Aug 2019 05:55:58 -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 S1728040AbfHNMyf (ORCPT + 99 others); Wed, 14 Aug 2019 08:54:35 -0400 Received: from mail-ed1-f66.google.com ([209.85.208.66]:45425 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726263AbfHNMye (ORCPT ); Wed, 14 Aug 2019 08:54:34 -0400 Received: by mail-ed1-f66.google.com with SMTP id x19so103902668eda.12 for ; Wed, 14 Aug 2019 05:54:33 -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=VhOtcUs6qecW9A5Ku6t6s7+WyBSfMvyZ0VSLJHIcnmU=; b=TNdWJENz3Ot9nC8zCWp00REH4oLgmz7Gk74Qy9F9eE5ocrd8TjqHtz7L40wXk80yQu djbnucK7bXcRrIEXThfNMFCpyNEdprpa4XZkezMS3VT9bVyduwa33Uw7JqGlfkSdoGBQ b9efp0huXKpsppJUkQgnrRbnZ2OMhZQwweDsC3Kepi18dxPwBe8V0uWWS6QcDczPy1Cf DnAnGny8vXE5HRPBlCqUAt1elCr69izML7J3EY7AFee+oMK3gKJTrHBdKSMd1NKwwz3x 8LQinFjOUozGcNUf7bTbCO1sN9HVYx2Qfa73KCAPm+ztj8wbfWSLoNFnEMbIbjXvGOqV sHEA== X-Gm-Message-State: APjAAAWpHViFFVfoL/4q6l3lbu0XO2Go1daqqE5JCXwX7MpwWccthh7l 1eoJbdPG1YzH84wczzNuPgprcw== X-Received: by 2002:a50:fb82:: with SMTP id e2mr2969241edq.15.1565787272521; Wed, 14 Aug 2019 05:54:32 -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 ng6sm10794969ejb.13.2019.08.14.05.54.31 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 14 Aug 2019 05:54:31 -0700 (PDT) Subject: Re: [PATCH] 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> From: Hans de Goede Message-ID: <15aa45c7-6e45-d03f-9336-4291f8b2dc66@redhat.com> Date: Wed, 14 Aug 2019 14:54:31 +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: <1565782781938.37795@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 13:39, 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 Assuming this has been tested, overal this looks good to me. But there are 2 things to fix: 1) Maybe pick a more descriptive struct member name then pdev. pdev with pci-devices often points to a pci_device ... How about: role_switch_pdev ? 2) xhci_ext_cap_init() is not the last call which can fail in xhci_pci_probe(), since you now no longer use devm_add_action_or_reset for auto-cleanup, you must now manually cleanup by calling xhci_ext_cap_remove() when later steps of xhci_pci_probe() fail. it looks like you will need a new ext_cap_remove error-exit label for this put above the put_usb3_hcd label and goto this new label instead of to put_usb3_hcd in all error paths after a successful call to xhci_ext_cap_init() Regards, Hans > --- > drivers/usb/host/xhci-ext-caps.c | 22 ++++++++++++---------- > drivers/usb/host/xhci-pci.c | 4 ++++ > drivers/usb/host/xhci-pci.h | 19 +++++++++++++++++++ > drivers/usb/host/xhci.h | 1 + > 4 files changed, 36 insertions(+), 10 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..d2ab1e2a39c0 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->pdev = pdev; > > return 0; > } > @@ -88,3 +82,11 @@ 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->pdev) > + platform_device_unregister(priv->pdev); > +} > +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..a4d094df56f7 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, > }; > > @@ -393,6 +395,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..ead9618d7368 > --- /dev/null > +++ b/drivers/usb/host/xhci-pci.h > @@ -0,0 +1,19 @@ > +/* > + * 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 *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); >