linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] dma-mapping: Add vmap checks to dma_map_single()
@ 2019-10-10 22:28 Kees Cook
  2019-10-10 22:28 ` [PATCH v3 1/2] " Kees Cook
  2019-10-10 22:28 ` [PATCH v3 2/2] usb: core: Remove redundant vmap checks Kees Cook
  0 siblings, 2 replies; 5+ messages in thread
From: Kees Cook @ 2019-10-10 22:28 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Kees Cook, Robin Murphy, Laura Abbott, Marek Szyprowski,
	Jesper Dangaard Brouer, Allison Randal, Greg Kroah-Hartman,
	Thomas Gleixner, Stephen Boyd, Dan Carpenter,
	Semmle Security Reports, iommu, linux-kernel

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(-)

-- 
2.17.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3 1/2] dma-mapping: Add vmap checks to dma_map_single()
  2019-10-10 22:28 [PATCH v3 0/2] dma-mapping: Add vmap checks to dma_map_single() Kees Cook
@ 2019-10-10 22:28 ` Kees Cook
  2019-10-11  5:02   ` Greg Kroah-Hartman
  2019-10-10 22:28 ` [PATCH v3 2/2] usb: core: Remove redundant vmap checks Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Kees Cook @ 2019-10-10 22:28 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Kees Cook, Robin Murphy, Laura Abbott, Marek Szyprowski,
	Jesper Dangaard Brouer, Allison Randal, Greg Kroah-Hartman,
	Thomas Gleixner, Stephen Boyd, Dan Carpenter,
	Semmle Security Reports, iommu, linux-kernel

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

Suggested-by: Laura Abbott <labbott@redhat.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 include/linux/dma-mapping.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
index 4a1c4fca475a..ff4e91c66f44 100644
--- a/include/linux/dma-mapping.h
+++ b/include/linux/dma-mapping.h
@@ -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);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v3 2/2] usb: core: Remove redundant vmap checks
  2019-10-10 22:28 [PATCH v3 0/2] dma-mapping: Add vmap checks to dma_map_single() Kees Cook
  2019-10-10 22:28 ` [PATCH v3 1/2] " Kees Cook
@ 2019-10-10 22:28 ` Kees Cook
  1 sibling, 0 replies; 5+ messages in thread
From: Kees Cook @ 2019-10-10 22:28 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Kees Cook, Robin Murphy, Laura Abbott, Marek Szyprowski,
	Jesper Dangaard Brouer, Allison Randal, Greg Kroah-Hartman,
	Thomas Gleixner, Stephen Boyd, Dan Carpenter,
	Semmle Security Reports, iommu, linux-kernel

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

Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/usb/core/hcd.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index f225eaa98ff8..281568d464f9 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -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;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/2] dma-mapping: Add vmap checks to dma_map_single()
  2019-10-10 22:28 ` [PATCH v3 1/2] " Kees Cook
@ 2019-10-11  5:02   ` Greg Kroah-Hartman
  2019-10-11  9:33     ` Robin Murphy
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2019-10-11  5:02 UTC (permalink / raw)
  To: Kees Cook
  Cc: Christoph Hellwig, Robin Murphy, Laura Abbott, Marek Szyprowski,
	Jesper Dangaard Brouer, Allison Randal, Thomas Gleixner,
	Stephen Boyd, Dan Carpenter, Semmle Security Reports, iommu,
	linux-kernel

On Thu, Oct 10, 2019 at 03:28:28PM -0700, Kees Cook wrote:
> 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
> 
> Suggested-by: Laura Abbott <labbott@redhat.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  include/linux/dma-mapping.h | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
> index 4a1c4fca475a..ff4e91c66f44 100644
> --- a/include/linux/dma-mapping.h
> +++ b/include/linux/dma-mapping.h
> @@ -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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3 1/2] dma-mapping: Add vmap checks to dma_map_single()
  2019-10-11  5:02   ` Greg Kroah-Hartman
@ 2019-10-11  9:33     ` Robin Murphy
  0 siblings, 0 replies; 5+ messages in thread
From: Robin Murphy @ 2019-10-11  9:33 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Kees Cook
  Cc: Christoph Hellwig, Laura Abbott, Marek Szyprowski,
	Jesper Dangaard Brouer, Allison Randal, Thomas Gleixner,
	Stephen Boyd, Dan Carpenter, Semmle Security Reports, iommu,
	linux-kernel

On 2019-10-11 6:02 am, Greg Kroah-Hartman wrote:
> On Thu, Oct 10, 2019 at 03:28:28PM -0700, Kees Cook wrote:
>> 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
>>
>> Suggested-by: Laura Abbott <labbott@redhat.com>
>> Signed-off-by: Kees Cook <keescook@chromium.org>
>> ---
>>   include/linux/dma-mapping.h | 6 ++++++
>>   1 file changed, 6 insertions(+)
>>
>> diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
>> index 4a1c4fca475a..ff4e91c66f44 100644
>> --- a/include/linux/dma-mapping.h
>> +++ b/include/linux/dma-mapping.h
>> @@ -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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-10-11  9:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-10 22:28 [PATCH v3 0/2] dma-mapping: Add vmap checks to dma_map_single() Kees Cook
2019-10-10 22:28 ` [PATCH v3 1/2] " Kees Cook
2019-10-11  5:02   ` Greg Kroah-Hartman
2019-10-11  9:33     ` Robin Murphy
2019-10-10 22:28 ` [PATCH v3 2/2] usb: core: Remove redundant vmap checks Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).