* [PATCH] swiotlb: clear read bounce buffers on confidential guests @ 2026-11-23 20:53 ` Shukai Ni 2026-11-23 22:18 ` Edgecombe, Rick P 2026-11-24 15:17 ` Marek Szyprowski 0 siblings, 2 replies; 5+ 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 related [flat|nested] 5+ 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 2026-11-23 22:56 ` Shukai Ni 2026-11-24 15:17 ` Marek Szyprowski 1 sibling, 1 reply; 5+ messages in thread From: Edgecombe, Rick P @ 2026-11-23 22:18 UTC (permalink / raw) To: robin.murphy@arm.com, m.szyprowski@samsung.com, shukai.ni@kuleuven.be, joro@8bytes.org, will@kernel.org Cc: jo.vanbulck@cs.kuleuven.be, Reshetova, Elena, pasic@linux.ibm.com, kas@kernel.org, carlos.bilbao@kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, thomas.lendacky@amd.com, linux-coco@lists.linux.dev, stable@vger.kernel.org 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] 5+ messages in thread
* Re: [PATCH] swiotlb: clear read bounce buffers on confidential guests 2026-11-23 22:18 ` Edgecombe, Rick P @ 2026-11-23 22:56 ` Shukai Ni 0 siblings, 0 replies; 5+ messages in thread From: Shukai Ni @ 2026-11-23 22:56 UTC (permalink / raw) To: Edgecombe, Rick P, robin.murphy@arm.com, m.szyprowski@samsung.com, joro@8bytes.org, will@kernel.org Cc: jo.vanbulck@cs.kuleuven.be, Reshetova, Elena, pasic@linux.ibm.com, kas@kernel.org, carlos.bilbao@kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, thomas.lendacky@amd.com, linux-coco@lists.linux.dev, stable@vger.kernel.org On 2026/11/23 23:18, Edgecombe, Rick P wrote: > 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. Thanks for looking at this, and we had very similar thoughts when writing the patch. Our impression was that in many cases the driver doesn't own the buffer (page cache, dm-crypt, O_DIRECT), so fixing it there seemed hard, which is why we tried swiotlb instead. But we may be missing something. > 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. As far as we can tell, keeping the old contents for DMA_FROM_DEVICE was never really promised. Before ddbd89deb7d3, swiotlb didn't do it. That commit was mainly about not returning stale bounce data to the caller, which zeroing should still prevent. In case it helps, we have a PoC on SEV-SNP that leaks MariaDB data from a LUKS-encrypted disk through this path, and we're happy to share it. > 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. Yes, a short read into a pre-filled user buffer would now see zeros. We did a pass over the drivers commonly used in CVMs and didn't find any that rely on the old contents being kept, but we'd be glad to hear if someone knows of a case. Thanks, Shukai ^ permalink raw reply [flat|nested] 5+ 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 @ 2026-11-24 15:17 ` Marek Szyprowski 2026-11-24 15:36 ` Jason Gunthorpe 1 sibling, 1 reply; 5+ messages in thread From: Marek Szyprowski @ 2026-11-24 15:17 UTC (permalink / raw) To: Shukai Ni, 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, Jo Van Bulck, Jason Gunthorpe, Aneesh Kumar K.V Hi! CCed Jason and Aneesh. On 23.11.2026 21:53, 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> I'm not sure if DMA-mapping or SWIOTLB is the right place for such fixups. Drivers should be aware that doing DMA from unsecure device to the given secure page might reveal its content to the host or other parties and prepare for that (like zeroing that page). > --- > > 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; > } > Best regards -- Marek Szyprowski, PhD Samsung R&D Institute Poland ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] swiotlb: clear read bounce buffers on confidential guests 2026-11-24 15:17 ` Marek Szyprowski @ 2026-11-24 15:36 ` Jason Gunthorpe 0 siblings, 0 replies; 5+ messages in thread From: Jason Gunthorpe @ 2026-11-24 15:36 UTC (permalink / raw) To: Marek Szyprowski Cc: Shukai Ni, Robin Murphy, Joerg Roedel, Will Deacon, Halil Pasic, Tom Lendacky, Kiryl Shutsemau, Rick Edgecombe, Elena Reshetova, Carlos Bilbao, linux-coco, iommu, linux-kernel, stable, Jo Van Bulck, Aneesh Kumar K.V On Tue, Nov 24, 2026 at 04:17:20PM +0100, Marek Szyprowski wrote: > Hi! > > CCed Jason and Aneesh. > > On 23.11.2026 21:53, 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> > > > I'm not sure if DMA-mapping or SWIOTLB is the right place for such fixups. > > Drivers should be aware that doing DMA from unsecure device to the given > secure page might reveal its content to the host or other parties and > prepare for that (like zeroing that page). I think given the semantics we have to support partial write we don't have a choice but to continue them as it is the DMA API. The driver would have to opt out of partial write support somehow to trigger this, and in that case it is more of an optimization and shouldn't be linked to CC. I'm curious if there is a real issue here with a definitive issue on a driver or it is just speculation? Jason ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 15:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <CGME20261123205317eucas1p2f96032281e0f5e9f17acc7ec343acced@eucas1p2.samsung.com>
2026-11-23 20:53 ` [PATCH] swiotlb: clear read bounce buffers on confidential guests Shukai Ni
2026-11-23 22:18 ` Edgecombe, Rick P
2026-11-23 22:56 ` Shukai Ni
2026-11-24 15:17 ` Marek Szyprowski
2026-11-24 15:36 ` Jason Gunthorpe
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox