[PATCH] swiotlb: clear read bounce buffers on confidential guests
From: Shukai Ni
Date: Mon Nov 23 2026 - 15:53:24 EST
On confidential guests (AMD SEV, Intel TDX) all device DMA is bounced
through swiotlb, whose pool is shared with and readable by the untrusted
host. swiotlb_tbl_map_single() copies the destination buffer's current
contents into the bounce buffer at map time even for DMA_FROM_DEVICE. For
a reused read buffer that still holds private data, the host can read
that data from the bounce buffer before the device overwrites it.
For DMA_FROM_DEVICE into a host-shared pool, zero the bounce buffer
instead. Zeroing rather than skipping the copy keeps a short read from
returning a previous transfer's data.
Fixes: ddbd89deb7d3 ("swiotlb: fix info leak with DMA_FROM_DEVICE")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Jo Van Bulck <jo.vanbulck@xxxxxxxxxxxxxx>
Signed-off-by: Jo Van Bulck <jo.vanbulck@xxxxxxxxxxxxxx>
Signed-off-by: Shukai Ni <shukai.ni@xxxxxxxxxxx>
---
Notes:
We are open to discussing a different approach: e.g. clearing the
residual when read back, scrubbing slots on free, or extending zeroing
for bidirectional DMA.
drivers/iommu/dma-iommu.c | 3 ++-
kernel/dma/swiotlb.c | 7 ++++++-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 78742aeba..42e0bfced 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -1187,7 +1187,8 @@ static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,
* Untrusted devices should not see padding areas with random leftover
* kernel data, so zero the pre- and post-padding.
* swiotlb_tbl_map_single() has initialized the bounce buffer proper to
- * the contents of the original memory buffer.
+ * the contents of the original memory buffer, or to zero for a read on
+ * a confidential guest.
*/
if (phys != (phys_addr_t)DMA_MAPPING_ERROR && dev_is_untrusted(dev)) {
size_t start, virt = (size_t)phys_to_virt(phys);
diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 786954bc5..351bc7c54 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -1630,8 +1630,13 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
* the original data, even if it's garbage, is necessary to match
* hardware behavior. Use of swiotlb is supposed to be transparent,
* i.e. swiotlb must not corrupt memory by clobbering unwritten bytes.
+ * Exception: on a confidential guest the bounce buffer is host-shared,
+ * so for a read zero it before sharing.
*/
- swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE, pool);
+ if (dir == DMA_FROM_DEVICE && mem->cc_shared)
+ memset(pool->vaddr + tlb_addr - pool->start, 0, mapping_size);
+ else
+ swiotlb_bounce(dev, tlb_addr, mapping_size, DMA_TO_DEVICE, pool);
return tlb_addr;
}
--
2.53.0