iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement
@ 2020-05-20 15:21 Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 1/4] PCI/ATS: Only enable ATS for trusted devices Jean-Philippe Brucker
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-20 15:21 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: Jean-Philippe Brucker, ashok.raj, will, hch, alex.williamson,
	robin.murphy, dwmw2

IOMMU drivers currently check themselves if a device is untrusted
(plugged into an external-facing port) before enabling ATS. Move the
check to drivers/pci. The only functional change should be to the AMD
IOMMU driver. With this change all IOMMU drivers block 'Translated' PCIe
transactions and Translation Requests from untrusted devices.

Since v1 [1] I added tags, addressed comments on patches 1 and 3, and
fixed a regression in patch 3.

[1] https://lore.kernel.org/linux-iommu/20200515104359.1178606-1-jean-philippe@linaro.org/

Jean-Philippe Brucker (4):
  PCI/ATS: Only enable ATS for trusted devices
  iommu/amd: Use pci_ats_supported()
  iommu/arm-smmu-v3: Use pci_ats_supported()
  iommu/vt-d: Use pci_ats_supported()

 include/linux/pci-ats.h     |  3 +++
 drivers/iommu/amd_iommu.c   | 12 ++++--------
 drivers/iommu/arm-smmu-v3.c | 20 +++++++-------------
 drivers/iommu/intel-iommu.c |  9 +++------
 drivers/pci/ats.c           | 18 +++++++++++++++++-
 5 files changed, 34 insertions(+), 28 deletions(-)

-- 
2.26.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/4] PCI/ATS: Only enable ATS for trusted devices
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
@ 2020-05-20 15:22 ` Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 2/4] iommu/amd: Use pci_ats_supported() Jean-Philippe Brucker
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-20 15:22 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: Jean-Philippe Brucker, Joerg Roedel, ashok.raj, will, hch,
	alex.williamson, robin.murphy, dwmw2

Add pci_ats_supported(), which checks whether a device has an ATS
capability, and whether it is trusted.  A device is untrusted if it is
plugged into an external-facing port such as Thunderbolt and could be
spoofing an existing device to exploit weaknesses in the IOMMU
configuration.  PCIe ATS is one such weaknesses since it allows
endpoints to cache IOMMU translations and emit transactions with
'Translated' Address Type (10b) that partially bypass the IOMMU
translation.

The SMMUv3 and VT-d IOMMU drivers already disallow ATS and transactions
with 'Translated' Address Type for untrusted devices.  Add the check to
pci_enable_ats() to let other drivers (AMD IOMMU for now) benefit from
it.

By checking ats_cap, the pci_ats_supported() helper also returns whether
ATS was globally disabled with pci=noats, and could later include more
things, for example whether the whole PCIe hierarchy down to the
endpoint supports ATS.

Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 include/linux/pci-ats.h |  3 +++
 drivers/pci/ats.c       | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/include/linux/pci-ats.h b/include/linux/pci-ats.h
index d08f0869f121..f75c307f346d 100644
--- a/include/linux/pci-ats.h
+++ b/include/linux/pci-ats.h
@@ -6,11 +6,14 @@
 
 #ifdef CONFIG_PCI_ATS
 /* Address Translation Service */
+bool pci_ats_supported(struct pci_dev *dev);
 int pci_enable_ats(struct pci_dev *dev, int ps);
 void pci_disable_ats(struct pci_dev *dev);
 int pci_ats_queue_depth(struct pci_dev *dev);
 int pci_ats_page_aligned(struct pci_dev *dev);
 #else /* CONFIG_PCI_ATS */
+static inline bool pci_ats_supported(struct pci_dev *d)
+{ return false; }
 static inline int pci_enable_ats(struct pci_dev *d, int ps)
 { return -ENODEV; }
 static inline void pci_disable_ats(struct pci_dev *d) { }
diff --git a/drivers/pci/ats.c b/drivers/pci/ats.c
index 390e92f2d8d1..b761c1f72f67 100644
--- a/drivers/pci/ats.c
+++ b/drivers/pci/ats.c
@@ -30,6 +30,22 @@ void pci_ats_init(struct pci_dev *dev)
 	dev->ats_cap = pos;
 }
 
+/**
+ * pci_ats_supported - check if the device can use ATS
+ * @dev: the PCI device
+ *
+ * Returns true if the device supports ATS and is allowed to use it, false
+ * otherwise.
+ */
+bool pci_ats_supported(struct pci_dev *dev)
+{
+	if (!dev->ats_cap)
+		return false;
+
+	return (dev->untrusted == 0);
+}
+EXPORT_SYMBOL_GPL(pci_ats_supported);
+
 /**
  * pci_enable_ats - enable the ATS capability
  * @dev: the PCI device
@@ -42,7 +58,7 @@ int pci_enable_ats(struct pci_dev *dev, int ps)
 	u16 ctrl;
 	struct pci_dev *pdev;
 
-	if (!dev->ats_cap)
+	if (!pci_ats_supported(dev))
 		return -EINVAL;
 
 	if (WARN_ON(dev->ats_enabled))
-- 
2.26.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/4] iommu/amd: Use pci_ats_supported()
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 1/4] PCI/ATS: Only enable ATS for trusted devices Jean-Philippe Brucker
@ 2020-05-20 15:22 ` Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 3/4] iommu/arm-smmu-v3: " Jean-Philippe Brucker
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-20 15:22 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: Jean-Philippe Brucker, Joerg Roedel, ashok.raj, will, hch,
	alex.williamson, robin.murphy, dwmw2

The pci_ats_supported() function checks if a device supports ATS and is
allowed to use it. In addition to checking that the device has an ATS
capability and that the global pci=noats is not set
(pci_ats_disabled()), it also checks if a device is untrusted.

A device is untrusted if it is plugged into an external-facing port such
as Thunderbolt and could be spoofing an existing device to exploit
weaknesses in the IOMMU configuration. By calling pci_ats_supported() we
keep DTE[I]=0 for untrusted devices and abort transactions with
Pretranslated Addresses.

Reviewed-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 drivers/iommu/amd_iommu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c
index 1dc3718560d0..8b7a9e811d33 100644
--- a/drivers/iommu/amd_iommu.c
+++ b/drivers/iommu/amd_iommu.c
@@ -313,16 +313,15 @@ static struct iommu_group *acpihid_device_group(struct device *dev)
 static bool pci_iommuv2_capable(struct pci_dev *pdev)
 {
 	static const int caps[] = {
-		PCI_EXT_CAP_ID_ATS,
 		PCI_EXT_CAP_ID_PRI,
 		PCI_EXT_CAP_ID_PASID,
 	};
 	int i, pos;
 
-	if (pci_ats_disabled())
+	if (!pci_ats_supported(pdev))
 		return false;
 
-	for (i = 0; i < 3; ++i) {
+	for (i = 0; i < 2; ++i) {
 		pos = pci_find_ext_capability(pdev, caps[i]);
 		if (pos == 0)
 			return false;
@@ -3150,11 +3149,8 @@ int amd_iommu_device_info(struct pci_dev *pdev,
 
 	memset(info, 0, sizeof(*info));
 
-	if (!pci_ats_disabled()) {
-		pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS);
-		if (pos)
-			info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
-	}
+	if (pci_ats_supported(pdev))
+		info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP;
 
 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI);
 	if (pos)
-- 
2.26.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 3/4] iommu/arm-smmu-v3: Use pci_ats_supported()
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 1/4] PCI/ATS: Only enable ATS for trusted devices Jean-Philippe Brucker
  2020-05-20 15:22 ` [PATCH v2 2/4] iommu/amd: Use pci_ats_supported() Jean-Philippe Brucker
@ 2020-05-20 15:22 ` Jean-Philippe Brucker
  2020-05-21 10:29   ` Will Deacon
  2020-05-20 15:22 ` [PATCH v2 4/4] iommu/vt-d: " Jean-Philippe Brucker
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-20 15:22 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: Jean-Philippe Brucker, ashok.raj, will, hch, alex.williamson,
	robin.murphy, dwmw2

The new pci_ats_supported() function checks if a device supports ATS and
is allowed to use it.

Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 drivers/iommu/arm-smmu-v3.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
index 82508730feb7..c5730557dbe3 100644
--- a/drivers/iommu/arm-smmu-v3.c
+++ b/drivers/iommu/arm-smmu-v3.c
@@ -2652,26 +2652,20 @@ static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master)
 	}
 }
 
-#ifdef CONFIG_PCI_ATS
 static bool arm_smmu_ats_supported(struct arm_smmu_master *master)
 {
-	struct pci_dev *pdev;
+	struct device *dev = master->dev;
 	struct arm_smmu_device *smmu = master->smmu;
-	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 
-	if (!(smmu->features & ARM_SMMU_FEAT_ATS) || !dev_is_pci(master->dev) ||
-	    !(fwspec->flags & IOMMU_FWSPEC_PCI_RC_ATS) || pci_ats_disabled())
+	if (!(smmu->features & ARM_SMMU_FEAT_ATS))
 		return false;
 
-	pdev = to_pci_dev(master->dev);
-	return !pdev->untrusted && pdev->ats_cap;
-}
-#else
-static bool arm_smmu_ats_supported(struct arm_smmu_master *master)
-{
-	return false;
+	if (!(fwspec->flags & IOMMU_FWSPEC_PCI_RC_ATS))
+		return false;
+
+	return dev_is_pci(dev) && pci_ats_supported(to_pci_dev(dev));
 }
-#endif
 
 static void arm_smmu_enable_ats(struct arm_smmu_master *master)
 {
-- 
2.26.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 4/4] iommu/vt-d: Use pci_ats_supported()
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
                   ` (2 preceding siblings ...)
  2020-05-20 15:22 ` [PATCH v2 3/4] iommu/arm-smmu-v3: " Jean-Philippe Brucker
@ 2020-05-20 15:22 ` Jean-Philippe Brucker
  2020-05-27  9:38 ` [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
  2020-05-27 12:36 ` Joerg Roedel
  5 siblings, 0 replies; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-20 15:22 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: Jean-Philippe Brucker, ashok.raj, will, hch, alex.williamson,
	robin.murphy, dwmw2

The pci_ats_supported() helper checks if a device supports ATS and is
allowed to use it. By checking the ATS capability it also integrates the
pci_ats_disabled() check from pci_ats_init(). Simplify the vt-d checks.

Acked-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 drivers/iommu/intel-iommu.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c
index 0182cff2c7ac..ed21ce6d1238 100644
--- a/drivers/iommu/intel-iommu.c
+++ b/drivers/iommu/intel-iommu.c
@@ -1454,8 +1454,7 @@ static void iommu_enable_dev_iotlb(struct device_domain_info *info)
 	    !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32))
 		info->pri_enabled = 1;
 #endif
-	if (!pdev->untrusted && info->ats_supported &&
-	    pci_ats_page_aligned(pdev) &&
+	if (info->ats_supported && pci_ats_page_aligned(pdev) &&
 	    !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) {
 		info->ats_enabled = 1;
 		domain_update_iotlb(info->domain);
@@ -2611,10 +2610,8 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu,
 	if (dev && dev_is_pci(dev)) {
 		struct pci_dev *pdev = to_pci_dev(info->dev);
 
-		if (!pdev->untrusted &&
-		    !pci_ats_disabled() &&
-		    ecap_dev_iotlb_support(iommu->ecap) &&
-		    pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) &&
+		if (ecap_dev_iotlb_support(iommu->ecap) &&
+		    pci_ats_supported(pdev) &&
 		    dmar_find_matched_atsr_unit(pdev))
 			info->ats_supported = 1;
 
-- 
2.26.2

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 3/4] iommu/arm-smmu-v3: Use pci_ats_supported()
  2020-05-20 15:22 ` [PATCH v2 3/4] iommu/arm-smmu-v3: " Jean-Philippe Brucker
@ 2020-05-21 10:29   ` Will Deacon
  0 siblings, 0 replies; 8+ messages in thread
From: Will Deacon @ 2020-05-21 10:29 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: ashok.raj, linux-pci, alex.williamson, hch, iommu, bhelgaas,
	robin.murphy, dwmw2, linux-arm-kernel

On Wed, May 20, 2020 at 05:22:02PM +0200, Jean-Philippe Brucker wrote:
> The new pci_ats_supported() function checks if a device supports ATS and
> is allowed to use it.
> 
> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
> ---
>  drivers/iommu/arm-smmu-v3.c | 20 +++++++-------------
>  1 file changed, 7 insertions(+), 13 deletions(-)

Acked-by: Will Deacon <will@kernel.org>

Will
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
                   ` (3 preceding siblings ...)
  2020-05-20 15:22 ` [PATCH v2 4/4] iommu/vt-d: " Jean-Philippe Brucker
@ 2020-05-27  9:38 ` Jean-Philippe Brucker
  2020-05-27 12:36 ` Joerg Roedel
  5 siblings, 0 replies; 8+ messages in thread
From: Jean-Philippe Brucker @ 2020-05-27  9:38 UTC (permalink / raw)
  To: linux-pci, linux-arm-kernel, iommu, joro, bhelgaas
  Cc: ashok.raj, will, hch, alex.williamson, robin.murphy, dwmw2

Hi Joerg, Bjorn,

On Wed, May 20, 2020 at 05:21:59PM +0200, Jean-Philippe Brucker wrote:
> IOMMU drivers currently check themselves if a device is untrusted
> (plugged into an external-facing port) before enabling ATS. Move the
> check to drivers/pci. The only functional change should be to the AMD
> IOMMU driver. With this change all IOMMU drivers block 'Translated' PCIe
> transactions and Translation Requests from untrusted devices.

This seems ready for v5.8. I guess it could go through the IOMMU tree
since there are a little more IOMMU changes?

Thanks,
Jean
> 
> Since v1 [1] I added tags, addressed comments on patches 1 and 3, and
> fixed a regression in patch 3.
> 
> [1] https://lore.kernel.org/linux-iommu/20200515104359.1178606-1-jean-philippe@linaro.org/
> 
> Jean-Philippe Brucker (4):
>   PCI/ATS: Only enable ATS for trusted devices
>   iommu/amd: Use pci_ats_supported()
>   iommu/arm-smmu-v3: Use pci_ats_supported()
>   iommu/vt-d: Use pci_ats_supported()
> 
>  include/linux/pci-ats.h     |  3 +++
>  drivers/iommu/amd_iommu.c   | 12 ++++--------
>  drivers/iommu/arm-smmu-v3.c | 20 +++++++-------------
>  drivers/iommu/intel-iommu.c |  9 +++------
>  drivers/pci/ats.c           | 18 +++++++++++++++++-
>  5 files changed, 34 insertions(+), 28 deletions(-)
> 
> -- 
> 2.26.2
> 
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement
  2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
                   ` (4 preceding siblings ...)
  2020-05-27  9:38 ` [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
@ 2020-05-27 12:36 ` Joerg Roedel
  5 siblings, 0 replies; 8+ messages in thread
From: Joerg Roedel @ 2020-05-27 12:36 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: alex.williamson, ashok.raj, linux-pci, robin.murphy, hch, iommu,
	bhelgaas, will, dwmw2, linux-arm-kernel

On Wed, May 20, 2020 at 05:21:59PM +0200, Jean-Philippe Brucker wrote:
> IOMMU drivers currently check themselves if a device is untrusted
> (plugged into an external-facing port) before enabling ATS. Move the
> check to drivers/pci. The only functional change should be to the AMD
> IOMMU driver. With this change all IOMMU drivers block 'Translated' PCIe
> transactions and Translation Requests from untrusted devices.
> 
> Since v1 [1] I added tags, addressed comments on patches 1 and 3, and
> fixed a regression in patch 3.
> 
> [1] https://lore.kernel.org/linux-iommu/20200515104359.1178606-1-jean-philippe@linaro.org/
> 
> Jean-Philippe Brucker (4):
>   PCI/ATS: Only enable ATS for trusted devices
>   iommu/amd: Use pci_ats_supported()
>   iommu/arm-smmu-v3: Use pci_ats_supported()
>   iommu/vt-d: Use pci_ats_supported()
> 
>  include/linux/pci-ats.h     |  3 +++
>  drivers/iommu/amd_iommu.c   | 12 ++++--------
>  drivers/iommu/arm-smmu-v3.c | 20 +++++++-------------
>  drivers/iommu/intel-iommu.c |  9 +++------
>  drivers/pci/ats.c           | 18 +++++++++++++++++-
>  5 files changed, 34 insertions(+), 28 deletions(-)

Applied, thanks.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-05-27 12:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-20 15:21 [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
2020-05-20 15:22 ` [PATCH v2 1/4] PCI/ATS: Only enable ATS for trusted devices Jean-Philippe Brucker
2020-05-20 15:22 ` [PATCH v2 2/4] iommu/amd: Use pci_ats_supported() Jean-Philippe Brucker
2020-05-20 15:22 ` [PATCH v2 3/4] iommu/arm-smmu-v3: " Jean-Philippe Brucker
2020-05-21 10:29   ` Will Deacon
2020-05-20 15:22 ` [PATCH v2 4/4] iommu/vt-d: " Jean-Philippe Brucker
2020-05-27  9:38 ` [PATCH v2 0/4] PCI, iommu: Factor 'untrusted' check for ATS enablement Jean-Philippe Brucker
2020-05-27 12:36 ` Joerg Roedel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).