Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp239246ybt; Thu, 25 Jun 2020 20:58:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUladSg9HN6rAWNh/yHfJLrHeXpBKMGBNj0nPn0HAB6KrjXUQtBgOVf2uycYAznfimBRH0 X-Received: by 2002:a17:906:3650:: with SMTP id r16mr789942ejb.465.1593143896290; Thu, 25 Jun 2020 20:58:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593143896; cv=none; d=google.com; s=arc-20160816; b=aLuteUTgn/8mtRAK6M8okA6dfjV+ApLISt680ei/WXXNQ4PQ7gBHEfHe3RgHtTJZQY lStbwrtywvE1meCYIozRBpm7+XBzcHfjxfARHP17MBxPpJZVSitdWHCF8hZCDwNfNVr/ Wk9qUPM71n9r1MrupH+YvYapLCqFhBMrTt/sTwD0Hr7ePyLzjANBPcQ8po1soXlkDqwm 5+PLH82RXHCO431NZ9uao9wIpnVNlOwYONfNMuTe2Zqkddy+KhEt17VyQtvxDr8kCab1 Zm48/4YvuBrh0qFTJ6jpKsk+TI/oEOhdsnRdMOmWAypRVqZHPgZuhnNYIECR4hg6LzH0 2bVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=EfF/SDXieMxdBzzRNR3vjXRP9ELsApXSIPoFiYsV8L8=; b=mXDbldNS+CZTTpJVpaeIzBQ0DY7AbG93OYWHFezp7J0n22kKpKq1QOnZ5xvyFSn3Lh l78+z0In2eB8dGDQn17HY8WyvD/aawaQT4iwhohp/+7eOeQ1xdqhSbLtfRtr5YVXW4Ba bYGXpJChQSutdaKFuf/R+rV1hFj8QqYXOSUI2QrMh/TqYjjow4nHbxTfucu9V6xRCmgb Fu7L8cgdYzGoqWbDmcw9/l4JF93w5LvRWrBdcT53z45CToIchKU5214HX1NuVgNE+DA3 iLbcaR5ytA3PHw5wIQzT2MJ018UrfAfNM9Zm+Ia/sLx0M1qWyp4iJ/Td7Glw0rm3lpxo Hbmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=paDQtPIb; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w19si8297072eju.41.2020.06.25.20.57.11; Thu, 25 Jun 2020 20:58:16 -0700 (PDT) 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; dkim=pass header.i=@google.com header.s=20161025 header.b=paDQtPIb; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726061AbgFZA1V (ORCPT + 99 others); Thu, 25 Jun 2020 20:27:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726012AbgFZA1U (ORCPT ); Thu, 25 Jun 2020 20:27:20 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E267C08C5C1 for ; Thu, 25 Jun 2020 17:27:20 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id l62so5398440pje.9 for ; Thu, 25 Jun 2020 17:27:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EfF/SDXieMxdBzzRNR3vjXRP9ELsApXSIPoFiYsV8L8=; b=paDQtPIbzZ2WSuFquOtQehWbSHMmSB+a126xQsSFdnJ+LWvXx9zh3xoaXaxtDIWPS1 aj+RavY/oV4sdZMGj+JxuTxWLKrxKs/vuctrDwS3ssgObe36pmdICn64FKPmAyRHjxwe XtehEjQ0v87qVFGKmNVDHwVMoI5Pn9GxvrlDvItnji8r2/359JdayOqIPyCmWStKA9+v 5bmemCJMXczy9khjne/ZecgDVafiUFo3pgwAosZuPIctUt449NK/rI3VYJ1hM3BWMdSf CUP5JOLmNVSWSZ/xqN/vEyuLzjYjfWCuHZX6swe8v49YWnPDTEIbd5ny/cAj9qxjVpkU YbuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EfF/SDXieMxdBzzRNR3vjXRP9ELsApXSIPoFiYsV8L8=; b=dWStTuv3n1GBtQJqeUFZU6o1VoBYYsp5gXJt2dhq9BbK3mGI6QMc9Zw7yRBYwdPepA afS1bwrEq+Hei7g4HG8DbObXqGSKjQQjeIKAgOW3QffN5pSVTNPyYB75DEQWLr/QxsfX B81GxpurAJT6KB8cKvz09beVvnmCnWHBCyqz7usHDudseUBSYXZ8e+UtlG/iaQODbEPo gpLVn4LRTTDZS5fIE7/6DGNj/fc+iykl0v/LSj98WvzHlFWDPBLbwOXHA1ijKNmfmQ4D hnSz11jd/azPwZCIKKeWabkU5DIFHhIyjuCyyC/J7O8EiftmyLC2Wrx70ww151viPk1O ze4g== X-Gm-Message-State: AOAM532axoqIDA9W1nR6DMW2OUP9GqICbI790UPTjqXPoA3T5lTmPHVX +cEMSGAlNMCzM7G8HkRFUEvALl0yeQCv X-Received: by 2002:a17:90a:6343:: with SMTP id v3mr611640pjs.196.1593131239458; Thu, 25 Jun 2020 17:27:19 -0700 (PDT) Date: Thu, 25 Jun 2020 17:27:10 -0700 In-Reply-To: <20200626002710.110200-1-rajatja@google.com> Message-Id: <20200626002710.110200-2-rajatja@google.com> Mime-Version: 1.0 References: <20200626002710.110200-1-rajatja@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH 2/2] pci: Add parameter to disable attaching untrusted devices From: Rajat Jain To: David Woodhouse , Lu Baolu , Joerg Roedel , Bjorn Helgaas , "Rafael J. Wysocki" , Len Brown , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org, Raj Ashok , lalithambika.krishnakumar@intel.com, Mika Westerberg , Jean-Philippe Brucker , Prashant Malani , Benson Leung , Todd Broch , Alex Levin , Mattias Nissler , Rajat Jain , Bernie Keany , Aaron Durbin , Diego Rivas , Duncan Laurie , Furquan Shaikh , Jesse Barnes , Christian Kellner , Alex Williamson , Greg Kroah-Hartman , oohall@gmail.com Cc: Rajat Jain Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce a PCI parameter that disables the automatic attachment of untrusted devices to their drivers. Signed-off-by: Rajat Jain --- Context: I set out to implement the approach outlined in https://lkml.org/lkml/2020/6/9/1331 https://lkml.org/lkml/2020/6/15/1453 But to my surprise, I found that the new hotplugged PCI devices were getting automatically attached to drivers even though /sys/bus/pci/drivers_autoprobe was set to 0. I realized that the device core's "drivers_autoprobe": * only disables the *initial* probe of the device (i.e. from device_add()). If a subsystem calls device_attach() explicitly for its devices like PCI subsystem does, the drivers_autoprobe setting does not matter. The core will attach device to the driver. This looks like correct semantic behavior to me because PCI is explicitly calling device_attach(), which is a way to explicitly ask the core to find and attach a driver for a device. * "drivers_autoprobe" cannot be controlled at boot time (to restrict any drivers before userspace comes up). The options I considered were: 1) Change device_attach() so that it takes into consideration the drivers_autoprobe property. Not sure if this is semantically correct thing to do though. If I do this, then the only way a driver can be attached to the drivers would be via userspace (/sys/bus/pci/drivers/bind) (Good for our use case though!). 2) Make the drivers_autoprobe property available to PCI to use (currently it is private to device core). The PCI could use this to determine whether or not to call device_attach(). This still leaves the other problem (of not being able to set drivers_autoprobe via command line open). 3) I found the pci_dev->match_driver, which seemed similar to what I am trying to do, but can't be controlled from userspace. I considered populating that field based on drivers_autoprobe (still need (2)). But the problem is that there is the AMD IOMMU driver which is setting this independently, so setting the match_driver based on drivers_autoprobe may not be a good idea. May be we can populate it for untrusted devicesi, based on the parameter that I'm introducing? 4) This patch was my option 4 that helps fix both the problems for me. drivers/pci/bus.c | 11 ++++++++--- drivers/pci/pci.c | 9 +++++++++ drivers/pci/pci.h | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index 3cef835b375fd..336aeeb4c4ebf 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -321,9 +321,14 @@ void pci_bus_add_device(struct pci_dev *dev) pci_bridge_d3_update(dev); dev->match_driver = true; - retval = device_attach(&dev->dev); - if (retval < 0 && retval != -EPROBE_DEFER) - pci_warn(dev, "device attach failed (%d)\n", retval); + + if (dev->untrusted && pci_dont_attach_untrusted_devs) { + pci_info(dev, "not attaching untrusted device\n"); + } else { + retval = device_attach(&dev->dev); + if (retval < 0 && retval != -EPROBE_DEFER) + pci_warn(dev, "device attach failed (%d)\n", retval); + } pci_dev_assign_added(dev, true); } diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index ce096272f52b1..dec1f9ef27d71 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -127,6 +127,13 @@ static bool pcie_ats_disabled; /* If set, the PCI config space of each device is printed during boot. */ bool pci_early_dump; +/* + * If set, the devices with "untrusted" flag shall not be attached automatically + * Userspace will need to attach them manually: + * echo > /sys/bus/pci/drivers//bind + */ +bool pci_dont_attach_untrusted_devs; + bool pci_ats_disabled(void) { return pcie_ats_disabled; @@ -6522,6 +6529,8 @@ static int __init pci_setup(char *str) pci_add_flags(PCI_SCAN_ALL_PCIE_DEVS); } else if (!strncmp(str, "disable_acs_redir=", 18)) { disable_acs_redir_param = str + 18; + } else if (!strcmp(str, "dont_attach_untrusted_devs")) { + pci_dont_attach_untrusted_devs = true; } else { pr_err("PCI: Unknown option `%s'\n", str); } diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 6d3f758671064..30ffad047d926 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -13,6 +13,7 @@ extern const unsigned char pcie_link_speed[]; extern bool pci_early_dump; +extern bool pci_dont_attach_untrusted_devs; bool pcie_cap_has_lnkctl(const struct pci_dev *dev); bool pcie_cap_has_rtctl(const struct pci_dev *dev); -- 2.27.0.212.ge8ba1cc988-goog