Skip to contentSkip to conversation

dma-mapping: Add vmap checks to dma_map_single()

PATCH In review Kees Cook 3 participants 11 messages Oct 29, 2019 – Oct 31, 2019

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

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

v4: use dev_WARN_ONCE() and improve report string (gregkh, robin)
v3: https://lore.kernel.org/lkml/[email protected]
v2: https://lore.kernel.org/lkml/201910041420.F6E55D29A@keescook
v1: https://lore.kernel.org/lkml/201910021341.7819A660@keescook

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

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 v4 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..54de3c496407 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 (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
+			  "wanted %zu bytes mapped 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 · 1 line
2.17.1
#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..54de3c496407 100644

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

Quoted 8 lines
include/linux/dma-mapping.h +5 -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 (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
+			  "wanted %zu bytes mapped in vmalloc\n", size)) {
+		return DMA_MAPPING_ERROR;
+	}

That's a very odd error string, I know if I saw it for the first time, I would have no idea what it meant. The USB message at least gives you a bit more context as to what went wrong and how to fix it.

How about something like "Memory is not DMA capabable, please fix the allocation of it to be correct", or "non-dma-able memory was attempted to be mapped, but this is impossible to to" or something else.

thanks,

greg k-h

#3 lore raw
Quoted 32 lines · Greg Kroah-Hartman
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..54de3c496407 100644

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

Quoted 8 lines
include/linux/dma-mapping.h +5 -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 (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
+			  "wanted %zu bytes mapped in vmalloc\n", size)) {
+		return DMA_MAPPING_ERROR;
+	}

That's a very odd error string, I know if I saw it for the first time, I would have no idea what it meant. The USB message at least gives you a bit more context as to what went wrong and how to fix it.

Quoted 3 lines

How about something like "Memory is not DMA capabable, please fix the allocation of it to be correct", or "non-dma-able memory was attempted to be mapped, but this is impossible to to" or something else.

I've fixed the message to "rejecting DMA map of vmalloc memory" and applied the patch.

author
#4 lore raw
Quoted 33 lines · Christoph Hellwig
Quoted 32 lines · Greg Kroah-Hartman
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..54de3c496407 100644

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

Quoted 8 lines
include/linux/dma-mapping.h +5 -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 (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
+			  "wanted %zu bytes mapped in vmalloc\n", size)) {
+		return DMA_MAPPING_ERROR;
+	}

That's a very odd error string, I know if I saw it for the first time, I would have no idea what it meant. The USB message at least gives you a bit more context as to what went wrong and how to fix it.

Quoted 6 lines
Quoted 3 lines

How about something like "Memory is not DMA capabable, please fix the allocation of it to be correct", or "non-dma-able memory was attempted to be mapped, but this is impossible to to" or something else.

I've fixed the message to "rejecting DMA map of vmalloc memory" and applied the patch.

Great; thank you!

Signature · 5 lines
Kees Cook
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#5 lore raw
In reply to Christoph Hellwig
Quoted 33 lines · Christoph Hellwig
Quoted 32 lines · Greg Kroah-Hartman
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..54de3c496407 100644

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

Quoted 8 lines
include/linux/dma-mapping.h +5 -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 (dev_WARN_ONCE(dev, is_vmalloc_addr(ptr),
+			  "wanted %zu bytes mapped in vmalloc\n", size)) {
+		return DMA_MAPPING_ERROR;
+	}

That's a very odd error string, I know if I saw it for the first time, I would have no idea what it meant. The USB message at least gives you a bit more context as to what went wrong and how to fix it.

Quoted 6 lines
Quoted 3 lines

How about something like "Memory is not DMA capabable, please fix the allocation of it to be correct", or "non-dma-able memory was attempted to be mapped, but this is impossible to to" or something else.

I've fixed the message to "rejecting DMA map of vmalloc memory" and applied the patch.

Looks good! You can apply patch 2/2 as well if you want to take that through your tree too.

thanks,

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

author
#8 lore raw

PATCH v4 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 · 1 line
2.17.1
maintainer Acked-by on v4 2/2
#9 lore raw

Re: [PATCH v4 2/2] usb: core: Remove redundant vmap checks

Quoted 7 lines · 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(-)

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