Skip to contentSkip to conversation

dma-mapping: Move vmap address checks into dma_map_single()

PATCH In review Kees Cook 5 participants 6 messages Oct 4, 2019 – Oct 11, 2019

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH v2 1/1 dma-mapping: Move vmap address checks into dma_map_single()

As we've seen from USB and other areas, we need to always do runtime checks for DMA operating on memory regions that might be remapped. This moves the existing checks from USB into dma_map_single(), but leaves the slightly heavier checks as they are.

---
v2: Only add is_vmalloc_addr()
v1: https://lore.kernel.org/lkml/201910021341.7819A660@keescook

---
 drivers/usb/core/hcd.c      | 8 +-------
 include/linux/dma-mapping.h | 7 +++++++
 2 files changed, 8 insertions(+), 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;
include/linux/dma-mapping.h +7 -0
index 4a1c4fca475a..12dbd07f74f2 100644
@@ -583,6 +583,13 @@ 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 (WARN_ONCE(is_vmalloc_addr(ptr),
+		      "%s %s: driver maps %lu bytes from vmalloc area\n",
+		      dev ? dev_driver_string(dev) : "unknown driver",
+		      dev ? dev_name(dev) : "unknown device", 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 · 9 lines
2.17.1


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

As we've seen from USB and other areas, we need to always do runtime checks for DMA operating on memory regions that might be remapped. This moves the existing checks from USB into dma_map_single(), but leaves the slightly heavier checks as they are.

Nearly the same might be necessary for drivers/usb/gadget/udc/core.c::usb_gadget_map_request_by_dev(). You might also want to check other subsystems, SPI tries to be fairly smart about vmalloc'd and kmap'd buffer, some I2C drivers have explicit checks (they should probably rely on the core), and finally MTD is an area where this has also been historically dealt with.

Quoted 53 lines

---
v2: Only add is_vmalloc_addr()
v1: https://lore.kernel.org/lkml/201910021341.7819A660@keescook

---
 drivers/usb/core/hcd.c      | 8 +-------
 include/linux/dma-mapping.h | 7 +++++++
 2 files changed, 8 insertions(+), 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;
include/linux/dma-mapping.h +7 -0
index 4a1c4fca475a..12dbd07f74f2 100644
@@ -583,6 +583,13 @@ 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 (WARN_ONCE(is_vmalloc_addr(ptr),
+		      "%s %s: driver maps %lu bytes from vmalloc area\n",
+		      dev ? dev_driver_string(dev) : "unknown driver",
+		      dev ? dev_name(dev) : "unknown device", 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 · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw
In reply to Kees Cook
Quoted 46 lines · Kees Cook

As we've seen from USB and other areas, we need to always do runtime checks for DMA operating on memory regions that might be remapped. This moves the existing checks from USB into dma_map_single(), but leaves the slightly heavier checks as they are.

---
v2: Only add is_vmalloc_addr()
v1: https://lore.kernel.org/lkml/201910021341.7819A660@keescook

---
 drivers/usb/core/hcd.c      | 8 +-------
 include/linux/dma-mapping.h | 7 +++++++
 2 files changed, 8 insertions(+), 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;
include/linux/dma-mapping.h +0 -0
index 4a1c4fca475a..12dbd07f74f2 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,13 @@ 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 (WARN_ONCE(is_vmalloc_addr(ptr),
+		      "%s %s: driver maps %lu bytes from vmalloc area\n",
+		      dev ? dev_driver_string(dev) : "unknown driver",
+		      dev ? dev_name(dev) : "unknown device", size))

If you use dev_warn() here you get all of that "unknown driver/device" checking and handling set properly. And it's in the "standard" format that userspace tools know how to check.

thanks,

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

#3 lore raw
In reply to Kees Cook

Please split the usb and dma-mapping parts into separate patches.

Quoted 5 lines
include/linux/dma-mapping.h +0 -0
index 4a1c4fca475a..12dbd07f74f2 100644

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

Quoted 9 lines
include/linux/dma-mapping.h +6 -0
@@ -583,6 +583,13 @@ 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 (WARN_ONCE(is_vmalloc_addr(ptr),
+		      "%s %s: driver maps %lu bytes from vmalloc area\n",
+		      dev ? dev_driver_string(dev) : "unknown driver",
+		      dev ? dev_name(dev) : "unknown device", size))
+		return DMA_MAPPING_ERROR;

a NULL device isn't supported any more, so we can remove the handling for it here.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#4 lore raw
In reply to Kees Cook
I love your patch! Perhaps something to improve:

Hi Kees,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[cannot apply to v5.4-rc1 next-20191004]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Kees-Cook/dma-mapping-Move-vmap-address-checks-into-dma_map_single/20191005-073954
config: i386-randconfig-d001-201939 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-13) 7.4.0 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

In file included from include/linux/init.h:5:0,

                    from sound/pci/ad1889.c:23:
   include/linux/dma-mapping.h: In function 'dma_map_single_attrs':
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
    #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                       ^~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:58:61: note: in definition of macro '__trace_if_var'
    #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                                ^~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:69:3: note: in definition of macro '__trace_if_value'
     (cond) ?     \
      ^~~~
   include/linux/compiler.h:56:28: note: in expansion of macro '__trace_if_var'
    #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                               ^~~~~~~~~~~~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
--
   In file included from include/linux/export.h:44:0,
                    from include/linux/linkage.h:7,
                    from include/linux/kernel.h:8,
                    from include/linux/delay.h:22,
                    from sound/pci//hda/hda_intel.c:23:
   include/linux/dma-mapping.h: In function 'dma_map_single_attrs':
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
    #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                       ^~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:58:61: note: in definition of macro '__trace_if_var'
    #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
                                                                ^~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
   include/linux/dma-mapping.h:588:9: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Wformat=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/linux/compiler.h:69:3: note: in definition of macro '__trace_if_value'
     (cond) ?     \
      ^~~~
   include/linux/compiler.h:56:28: note: in expansion of macro '__trace_if_var'
    #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
                               ^~~~~~~~~~~~~~
>> include/linux/dma-mapping.h:587:2: note: in expansion of macro 'if'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
     ^~
   include/asm-generic/bug.h:124:3: note: in expansion of macro '__WARN_printf'
      __WARN_printf(TAINT_WARN, format);   \
      ^~~~~~~~~~~~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~
   In file included from sound/pci//hda/hda_intel_trace.h:54:0,
                    from sound/pci//hda/hda_intel.c:58:
   include/trace/define_trace.h: At top level:
   include/trace/define_trace.h:95:42: fatal error: ./hda_intel_trace.h: No such file or directory
    #include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
                                             ^

compilation terminated.

vim +/if +587 include/linux/dma-mapping.h

582
583 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
584 size_t size, enum dma_data_direction dir, unsigned long attrs)
585 {
586 /* DMA must never operate on areas that might be remapped. */

 > 587		if (WARN_ONCE(is_vmalloc_addr(ptr),
 > 588			      "%s %s: driver maps %lu bytes from vmalloc area\n",

589 dev ? dev_driver_string(dev) : "unknown driver",
590 dev ? dev_name(dev) : "unknown device", size))
591 return DMA_MAPPING_ERROR;
592
593 debug_dma_map_single(dev, ptr, size);
594 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
595 size, dir, attrs);
596 }
597

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw
In reply to Kees Cook
I love your patch! Yet something to improve:

Hi Kees,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.4-rc2 next-20191010]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url: https://github.com/0day-ci/linux/commits/Kees-Cook/dma-mapping-Move-vmap-address-checks-into-dma_map_single/20191005-073954
config: sh-magicpanelr2_defconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from arch/sh/include/asm/bug.h:112:0,

                    from include/linux/bug.h:5,
                    from include/linux/thread_info.h:12,
                    from include/asm-generic/preempt.h:5,
                    from ./arch/sh/include/generated/asm/preempt.h:1,
                    from include/linux/preempt.h:78,
                    from include/linux/spinlock.h:51,
                    from include/linux/seqlock.h:36,
                    from include/linux/time.h:6,
                    from include/linux/stat.h:19,
                    from include/linux/module.h:10,
                    from arch/sh/kernel/io.c:8:
   include/linux/dma-mapping.h: In function 'dma_map_single_attrs':
>> include/linux/dma-mapping.h:588:9: error: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t {aka unsigned int}' [-Werror=format=]
            "%s %s: driver maps %lu bytes from vmalloc area\n",
            ^
   include/asm-generic/bug.h:92:17: note: in definition of macro '__WARN_printf'
      __warn_printk(arg);     \
                    ^~~
   include/asm-generic/bug.h:155:3: note: in expansion of macro 'WARN'
      WARN(1, format);    \
      ^~~~
   include/linux/dma-mapping.h:587:6: note: in expansion of macro 'WARN_ONCE'
     if (WARN_ONCE(is_vmalloc_addr(ptr),
         ^~~~~~~~~

cc1: all warnings being treated as errors

vim +588 include/linux/dma-mapping.h

582
583 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
584 size_t size, enum dma_data_direction dir, unsigned long attrs)
585 {
586 /* DMA must never operate on areas that might be remapped. */
587 if (WARN_ONCE(is_vmalloc_addr(ptr),

 > 588			      "%s %s: driver maps %lu bytes from vmalloc area\n",

589 dev ? dev_driver_string(dev) : "unknown driver",
590 dev ? dev_name(dev) : "unknown device", size))
591 return DMA_MAPPING_ERROR;
592
593 debug_dma_map_single(dev, ptr, size);
594 return dma_map_page_attrs(dev, virt_to_page(ptr), offset_in_page(ptr),
595 size, dir, attrs);
596 }
597

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation