* [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
* Re: [PATCH] swiotlb: clear read bounce buffers on confidential guests
2026-11-23 20:53 [PATCH] swiotlb: clear read bounce buffers on confidential guests Shukai Ni
@ 2026-11-23 22:18 ` Edgecombe, Rick P
0 siblings, 0 replies; 2+ messages in thread
From: Edgecombe, Rick P @ 2026-11-23 22:18 UTC (permalink / raw)
To: robin.murphy, m.szyprowski, shukai.ni, joro, will
Cc: jo.vanbulck, Reshetova, Elena, pasic, kas, carlos.bilbao, iommu,
linux-kernel, thomas.lendacky, linux-coco, stable
On Mon, 2026-11-23 at 21:53 +0100, Shukai Ni wrote:
> 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.
There is an existing usptream position around coco driver hardening, that the
drivers should be fixed. In some ways this another thing that could be fixed in
a driver? The driver could zero memory before sharing it for DMA_FROM_DEVICE.
But the swiotlb was supposed to be sort of a generic solution to the problem of
not inadvertently sharing private memory. Instead of marking an entire page as
shared (which might have private contents elsewhere on the page), the bounce
buffer allows for only sharing the minimum specified.
So you could maybe look at this issue as fitting into the general problem of not
inadvertently sharing the private data inside the buffer. But also that is
covering for driver gaps.
It seems like the fixes commit was arguing that copying the contents to the
swiotlb buffer was more transparent. So this patch kind of goes back against
that argument. That seems to be another point that the drivers should be fixed.
If this is essentially more of a potentially behavior altering kernel hardening,
then I wonder about putting it behind some kind of hardening config like
CONFIG_INIT_ON_ALLOC_DEFAULT_ON.
Ohh, but I guess the problem is the buffer getting copied could have come from
userspace? So then you would have to fix userspace... But then this patch could
also potentially break userspace if there is any partial write assumptions. Oh
boy.
^ 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®