Skip to contentSkip to conversation

dma: swiotlb: add KMSAN annotations to swiotlb_bounce()

PATCH Applied Shigeru Yoshida 2 participants 2 messages Mar 15 – Mar 20 DMA mapping helpers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma: swiotlb: add KMSAN annotations to swiotlb_bounce()

When a device performs DMA to a bounce buffer, KMSAN is unaware of the write and does not mark the data as initialized. When swiotlb_bounce() later copies the bounce buffer back to the original buffer, memcpy propagates the uninitialized shadow to the original buffer, causing false positive uninit-value reports.

Fix this by calling kmsan_unpoison_memory() on the bounce buffer before copying it back in the DMA_FROM_DEVICE path, so that memcpy naturally propagates initialized shadow to the destination.

---
 kernel/dma/swiotlb.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/dma/swiotlb.c +19 -2
index d8e6f1d889d5..9fd73700ddcf 100644
@@ -30,6 +30,7 @@
 #include <linux/gfp.h>
 #include <linux/highmem.h>
 #include <linux/io.h>
+#include <linux/kmsan-checks.h>
 #include <linux/iommu-helper.h>
 #include <linux/init.h>
 #include <linux/memblock.h>
@@ -901,10 +902,19 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
 
 			local_irq_save(flags);
 			page = pfn_to_page(pfn);
-			if (dir == DMA_TO_DEVICE)
+			if (dir == DMA_TO_DEVICE) {
+				/*
+				 * Ideally, kmsan_check_highmem_page()
+				 * could be used here to detect infoleaks,
+				 * but callers may map uninitialized buffers
+				 * that will be written by the device,
+				 * causing false positives.
+				 */
 				memcpy_from_page(vaddr, page, offset, sz);
-			else
+			} else {
+				kmsan_unpoison_memory(vaddr, sz);
 				memcpy_to_page(page, offset, vaddr, sz);
+			}
 			local_irq_restore(flags);
 
 			size -= sz;
@@ -913,8 +923,15 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
 			offset = 0;
 		}
 	} else if (dir == DMA_TO_DEVICE) {
+		/*
+		 * Ideally, kmsan_check_memory() could be used here to detect
+		 * infoleaks (uninitialized data being sent to device), but
+		 * callers may map uninitialized buffers that will be written
+		 * by the device, causing false positives.
+		 */
 		memcpy(vaddr, phys_to_virt(orig_addr), size);
 	} else {
+		kmsan_unpoison_memory(vaddr, size);
 		memcpy(phys_to_virt(orig_addr), vaddr, size);
 	}
 }
Signature · 1 line
2.52.0
maintainer Applied
#1 lore raw
Quoted 6 lines · Shigeru Yoshida

When a device performs DMA to a bounce buffer, KMSAN is unaware of the write and does not mark the data as initialized. When swiotlb_bounce() later copies the bounce buffer back to the original buffer, memcpy propagates the uninitialized shadow to the original buffer, causing false positive uninit-value reports.

Quoted 8 lines

Fix this by calling kmsan_unpoison_memory() on the bounce buffer before copying it back in the DMA_FROM_DEVICE path, so that memcpy naturally propagates initialized shadow to the destination.

Applied to dma-mapping-fixes. Thanks!

Quoted 44 lines
---
  kernel/dma/swiotlb.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)
kernel/dma/swiotlb.c +14 -2
index d8e6f1d889d5..9fd73700ddcf 100644
@@ -30,6 +30,7 @@
  #include <linux/gfp.h>
  #include <linux/highmem.h>
  #include <linux/io.h>
+#include <linux/kmsan-checks.h>
  #include <linux/iommu-helper.h>
  #include <linux/init.h>
  #include <linux/memblock.h>
@@ -901,10 +902,19 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
 
  			local_irq_save(flags);
  			page = pfn_to_page(pfn);
-			if (dir == DMA_TO_DEVICE)
+			if (dir == DMA_TO_DEVICE) {
+				/*
+				 * Ideally, kmsan_check_highmem_page()
+				 * could be used here to detect infoleaks,
+				 * but callers may map uninitialized buffers
+				 * that will be written by the device,
+				 * causing false positives.
+				 */
  				memcpy_from_page(vaddr, page, offset, sz);
-			else
+			} else {
+				kmsan_unpoison_memory(vaddr, sz);
  				memcpy_to_page(page, offset, vaddr, sz);
+			}
  			local_irq_restore(flags);
 
  			size -= sz;
@@ -913,8 +923,15 @@ static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size
  			offset = 0;
  		}
  	} else if (dir == DMA_TO_DEVICE) {
+		/*
+		 * Ideally, kmsan_check_memory() could be used here to detect

kernel/dma/swiotlb.c static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size · line 937

Quoted 10 lines
kernel/dma/swiotlb.c +5 -0
+		 * infoleaks (uninitialized data being sent to device), but
+		 * callers may map uninitialized buffers that will be written
+		 * by the device, causing false positives.
+		 */
  		memcpy(vaddr, phys_to_virt(orig_addr), size);
  	} else {
+		kmsan_unpoison_memory(vaddr, size);
  		memcpy(phys_to_virt(orig_addr), vaddr, size);
  	}
  }

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland