mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] swiotlb: clear read bounce buffers on confidential guests
@ 2026-11-23 20:53 Shukai Ni
  2026-11-23 22:18 ` Edgecombe, Rick P
  0 siblings, 1 reply; 2+ messages in thread
From: Shukai Ni @ 2026-11-23 20:53 UTC (permalink / raw)
  To: Marek Szyprowski, Robin Murphy, Joerg Roedel, Will Deacon
  Cc: Halil Pasic, Tom Lendacky, Kiryl Shutsemau, Rick Edgecombe,
	Elena Reshetova, Carlos Bilbao, linux-coco, iommu, linux-kernel,
	stable, Shukai Ni, Jo Van Bulck

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@vger.kernel.org
Co-developed-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
Signed-off-by: Jo Van Bulck <jo.vanbulck@cs.kuleuven.be>
Signed-off-by: Shukai Ni <shukai.ni@kuleuven.be>
---

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


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

end of thread, other threads:[~2026-11-23 22:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 20:53 [PATCH] swiotlb: clear read bounce buffers on confidential guests Shukai Ni
2026-11-23 22:18 ` Edgecombe, Rick P

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®