Skip to contentSkip to conversation

dma-mapping: Add vmap checks to dma_map_single()

PATCH Superseded Kees Cook 3 participants 5 messages Oct 10, 2019 – Oct 11, 2019

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

v3 cover dma-mapping: Add vmap checks to dma_map_single()

Duplicating patch 1 commit log:

As we've seen from USB and other areas[1], we need to always do runtime checks for DMA operating on memory regions that might be remapped. This adds vmap checks (similar to those already in USB but missing in other places) into dma_map_single() so all callers benefit from the checking.

[1] https://git.kernel.org/linus/3840c5b78803b2b6cc1ff820100a74a092c40cbb

-Kees

v3:
- switch to dev_warn() (gregkh, hch)
- split USB cleanup into a separate patch
v2: https://lore.kernel.org/lkml/201910041420.F6E55D29A@keescook
v1: https://lore.kernel.org/lkml/201910021341.7819A660@keescook

Kees Cook (2):
dma-mapping: Add vmap checks to dma_map_single()
usb: core: Remove redundant vmap checks

 drivers/usb/core/hcd.c      | 8 +-------
 include/linux/dma-mapping.h | 6 ++++++
 2 files changed, 7 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v3 1/2 dma-mapping: Add vmap checks to dma_map_single()

As we've seen from USB and other areas[1], we need to always do runtime checks for DMA operating on memory regions that might be remapped. This adds vmap checks (similar to those already in USB but missing in other places) into dma_map_single() so all callers benefit from the checking.

[1] https://git.kernel.org/linus/3840c5b78803b2b6cc1ff820100a74a092c40cbb

---
 include/linux/dma-mapping.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/dma-mapping.h +6 -0
index 4a1c4fca475a..ff4e91c66f44 100644
@@ -583,6 +583,12 @@ static inline unsigned long dma_get_merge_boundary(struct device *dev)
 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
 		size_t size, enum dma_data_direction dir, unsigned long attrs)
 {
+	/* DMA must never operate on areas that might be remapped. */
+	if (unlikely(is_vmalloc_addr(ptr))) {
+		dev_warn_once(dev, "bad map: %zu bytes in vmalloc\n", size);
+		return DMA_MAPPING_ERROR;
+	}
+
 	debug_dma_map_single(dev, ptr, size);
 	return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
 			size, dir, attrs);
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw
Quoted 18 lines · Kees Cook

As we've seen from USB and other areas[1], we need to always do runtime checks for DMA operating on memory regions that might be remapped. This adds vmap checks (similar to those already in USB but missing in other places) into dma_map_single() so all callers benefit from the checking.

[1] https://git.kernel.org/linus/3840c5b78803b2b6cc1ff820100a74a092c40cbb

---
 include/linux/dma-mapping.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/dma-mapping.h +0 -0
index 4a1c4fca475a..ff4e91c66f44 100644

include/linux/dma-mapping.h static inline unsigned long dma_get_merge_boundary(struct device *dev) · line 588

Quoted 6 lines
include/linux/dma-mapping.h +3 -0
@@ -583,6 +583,12 @@ static inline unsigned long dma_get_merge_boundary(struct device *dev)
 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
 		size_t size, enum dma_data_direction dir, unsigned long attrs)
 {
+	/* DMA must never operate on areas that might be remapped. */
+	if (unlikely(is_vmalloc_addr(ptr))) {
+		dev_warn_once(dev, "bad map: %zu bytes in vmalloc\n", size);

Can we get a bit better error text here? In USB we were at least giving people a hint as to what went wrong, "bad map" might not really make that much sense to a USB developer as to what they needed to do to fix this issue.

Other than that minor nit, I have no objection to this series, thanks for fixing this up!

greg k-h

reviewer
#3 lore raw
Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Kees Cook

As we've seen from USB and other areas[1], we need to always do runtime checks for DMA operating on memory regions that might be remapped. This adds vmap checks (similar to those already in USB but missing in other places) into dma_map_single() so all callers benefit from the checking.

[1] https://git.kernel.org/linus/3840c5b78803b2b6cc1ff820100a74a092c40cbb

---
  include/linux/dma-mapping.h | 6 ++++++
  1 file changed, 6 insertions(+)
Quoted 16 lines

include/linux/dma-mapping.h static inline unsigned long dma_get_merge_boundary(struct device *dev) · line 588

Quoted 11 lines
include/linux/dma-mapping.h +3 -0
index 4a1c4fca475a..ff4e91c66f44 100644
@@ -583,6 +583,12 @@ static inline unsigned long dma_get_merge_boundary(struct device *dev)
  static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  		size_t size, enum dma_data_direction dir, unsigned long attrs)
  {
+	/* DMA must never operate on areas that might be remapped. */
+	if (unlikely(is_vmalloc_addr(ptr))) {
+		dev_warn_once(dev, "bad map: %zu bytes in vmalloc\n", size);

Can we get a bit better error text here? In USB we were at least giving people a hint as to what went wrong, "bad map" might not really make that much sense to a USB developer as to what they needed to do to fix this issue.

Agreed, something along the lines of "dma_map_single of vmalloc'ed buffer..." might be clearer. Also I'd be inclined to use dev_WARN_ONCE() to include a diagnostically-useful backtrace, as the existing USB code would.

Thanks,
Robin.

author
#4 lore raw

PATCH v3 2/2 usb: core: Remove redundant vmap checks

In reply to Kees Cook

Now that the vmap area checks are being performed in the DMA infrastructure directly, there is no need to repeat them in USB.

---
 drivers/usb/core/hcd.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/usb/core/hcd.c +1 -7
index f225eaa98ff8..281568d464f9 100644
@@ -1410,10 +1410,7 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 		if (hcd->self.uses_pio_for_control)
 			return ret;
 		if (hcd_uses_dma(hcd)) {
-			if (is_vmalloc_addr(urb->setup_packet)) {
-				WARN_ONCE(1, "setup packet is not dma capable\n");
-				return -EAGAIN;
-			} else if (object_is_on_stack(urb->setup_packet)) {
+			if (object_is_on_stack(urb->setup_packet)) {
 				WARN_ONCE(1, "setup packet is on stack\n");
 				return -EAGAIN;
 			}
@@ -1479,9 +1476,6 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 					ret = -EAGAIN;
 				else
 					urb->transfer_flags |= URB_DMA_MAP_PAGE;
-			} else if (is_vmalloc_addr(urb->transfer_buffer)) {
-				WARN_ONCE(1, "transfer buffer not dma capable\n");
-				ret = -EAGAIN;
 			} else if (object_is_on_stack(urb->transfer_buffer)) {
 				WARN_ONCE(1, "transfer buffer is on stack\n");
 				ret = -EAGAIN;
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu