[PATCH v2 2/8] iommu/dma: Fail unaligned map requests for untrusted devs

From: Sven Peter
Date: Sat Aug 28 2021 - 11:38:30 EST


If swiotlb is enabled we should never try to create any mappings that
would expose more memory than requested to the device.
WARN_ON and refuse those mappings just in case.

Signed-off-by: Sven Peter <sven@xxxxxxxxxxxxx>
---
drivers/iommu/dma-iommu.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index e8eae34e9e4f..d6e273ec3de6 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -534,13 +534,20 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
struct iommu_dma_cookie *cookie = domain->iova_cookie;
struct iova_domain *iovad = &cookie->iovad;
size_t iova_off = iova_offset(iovad, phys);
+ size_t size_aligned = iova_align(iovad, size + iova_off);
dma_addr_t iova;

if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
iommu_deferred_attach(dev, domain))
return DMA_MAPPING_ERROR;

- size = iova_align(iovad, size + iova_off);
+ if (IS_ENABLED(CONFIG_SWIOTLB) && dev_is_untrusted(dev)) {
+ if (WARN_ON(iova_off))
+ return DMA_MAPPING_ERROR;
+ if (WARN_ON(size_aligned != size))
+ return DMA_MAPPING_ERROR;
+ }
+ size = size_aligned;

iova = iommu_dma_alloc_iova(domain, size, dma_mask, dev);
if (!iova)
--
2.25.1