Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1631078 > unrolled thread

[PATCH v3 0/2] usb: Check for DMA capable buffer sanity

Started byFlorian Fainelli <f.fainelli@gmail.com>
First post2017-04-26 03:00 +0200
Last post2017-05-05 23:10 +0200
Articles 4 — 1 participant

Back to article view | Back to linux.kernel


Contents

  [PATCH v3 0/2] usb: Check for DMA capable buffer sanity Florian Fainelli <f.fainelli@gmail.com> - 2017-04-26 03:00 +0200
    [PATCH v3 1/2] usb: core: Check URB setup_packet and transfer_buffer sanity Florian Fainelli <f.fainelli@gmail.com> - 2017-04-26 03:00 +0200
    [PATCH v3 2/2] usb: udc: core: Error if req->buf is either from vmalloc or stack Florian Fainelli <f.fainelli@gmail.com> - 2017-04-26 03:10 +0200
    Re: [PATCH v3 0/2] usb: Check for DMA capable buffer sanity Florian Fainelli <f.fainelli@gmail.com> - 2017-05-05 23:10 +0200

#1631078 — [PATCH v3 0/2] usb: Check for DMA capable buffer sanity

FromFlorian Fainelli <f.fainelli@gmail.com>
Date2017-04-26 03:00 +0200
Subject[PATCH v3 0/2] usb: Check for DMA capable buffer sanity
Message-ID<tAjtf-3t4-7@gated-at.bofh.it>
Changes in v3:

- added check in usb_gadget_map_request_by_dev (Felipe), new patch
- improved commit message description (Clemens)
- added additiona checks for urb->setup_packet (Alan)

Changes in v2:

- moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Florian Fainelli (2):
  usb: core: Check URB setup_packet and transfer_buffer sanity
  usb: udc: core: Error if req->buf is either from vmalloc or stack

 drivers/usb/core/hcd.c        | 12 ++++++++++++
 drivers/usb/gadget/udc/core.c |  9 +++++++++
 2 files changed, 21 insertions(+)

-- 
2.9.3

[toc] | [next] | [standalone]


#1631082 — [PATCH v3 1/2] usb: core: Check URB setup_packet and transfer_buffer sanity

FromFlorian Fainelli <f.fainelli@gmail.com>
Date2017-04-26 03:00 +0200
Subject[PATCH v3 1/2] usb: core: Check URB setup_packet and transfer_buffer sanity
Message-ID<tAjtf-3t4-15@gated-at.bofh.it>
In reply to#1631078
Update usb_hcd_map_urb_for_dma() to check for an URB's setup_packet and
transfer_buffer sanity. We first check that urb->setup_packet is neither
coming from vmalloc space nor is an on stack buffer, and if that's the
case, produce a warning and return an error. For urb->transfer_buffer
there is an existing is_vmalloc_addr() check so we just supplement that
with an object_is_on_stack() check, produce a warning if that is the case
and also return an error.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/usb/core/hcd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 49550790a3cb..26d710eec7da 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -26,6 +26,7 @@
 #include <linux/module.h>
 #include <linux/version.h>
 #include <linux/kernel.h>
+#include <linux/sched/task_stack.h>
 #include <linux/slab.h>
 #include <linux/completion.h>
 #include <linux/utsname.h>
@@ -1523,6 +1524,14 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 		if (hcd->self.uses_pio_for_control)
 			return ret;
 		if (IS_ENABLED(CONFIG_HAS_DMA) && hcd->self.uses_dma) {
+			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)) {
+				WARN_ONCE(1, "setup packet is on stack\n");
+				return -EAGAIN;
+			}
+
 			urb->setup_dma = dma_map_single(
 					hcd->self.sysdev,
 					urb->setup_packet,
@@ -1587,6 +1596,9 @@ int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
 			} 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;
 			} else {
 				urb->transfer_dma = dma_map_single(
 						hcd->self.sysdev,
-- 
2.9.3

[toc] | [prev] | [next] | [standalone]


#1631086 — [PATCH v3 2/2] usb: udc: core: Error if req->buf is either from vmalloc or stack

FromFlorian Fainelli <f.fainelli@gmail.com>
Date2017-04-26 03:10 +0200
Subject[PATCH v3 2/2] usb: udc: core: Error if req->buf is either from vmalloc or stack
Message-ID<tAjCV-3LV-1@gated-at.bofh.it>
In reply to#1631078
Check that req->buf is a valid DMA capable address, produce a warning
and return an error if it's either coming from vmalloc space or is an on
stack buffer.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
 drivers/usb/gadget/udc/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index efce68e9a8e0..a03aec574f64 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -23,6 +23,7 @@
 #include <linux/list.h>
 #include <linux/err.h>
 #include <linux/dma-mapping.h>
+#include <linux/sched/task_stack.h>
 #include <linux/workqueue.h>
 
 #include <linux/usb/ch9.h>
@@ -798,6 +799,14 @@ int usb_gadget_map_request_by_dev(struct device *dev,
 
 		req->num_mapped_sgs = mapped;
 	} else {
+		if (is_vmalloc_addr(req->buf)) {
+			dev_err(dev, "buffer is not dma capable\n");
+			return -EFAULT;
+		} else if (object_is_on_stack(req->buf)) {
+			dev_err(dev, "buffer is on stack\n");
+			return -EFAULT;
+		}
+
 		req->dma = dma_map_single(dev, req->buf, req->length,
 				is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
 
-- 
2.9.3

[toc] | [prev] | [next] | [standalone]


#1636731

FromFlorian Fainelli <f.fainelli@gmail.com>
Date2017-05-05 23:10 +0200
Message-ID<tDSE9-77p-7@gated-at.bofh.it>
In reply to#1631078
On 04/25/2017 05:56 PM, Florian Fainelli wrote:
> Changes in v3:
> 
> - added check in usb_gadget_map_request_by_dev (Felipe), new patch
> - improved commit message description (Clemens)
> - added additiona checks for urb->setup_packet (Alan)
> 
> Changes in v2:
> 
> - moved the check from usb_start_wait_urb() to usb_hcd_map_urb_for_dma()

Is this version looking good now? Thanks!

> 
> Florian Fainelli (2):
>   usb: core: Check URB setup_packet and transfer_buffer sanity
>   usb: udc: core: Error if req->buf is either from vmalloc or stack
> 
>  drivers/usb/core/hcd.c        | 12 ++++++++++++
>  drivers/usb/gadget/udc/core.c |  9 +++++++++
>  2 files changed, 21 insertions(+)
> 


-- 
Florian

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web