Skip to contentSkip to conversation

staging: emxx_udc: Add checks for dma_alloc_coherent()

PATCH Superseded Yuan Can 3 participants 4 messages Jan 19, 2023 Staging

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yuan Can

author
#0 lore raw

PATCH v2 1/1 staging: emxx_udc: Add checks for dma_alloc_coherent()

As the dma_alloc_coherent may return NULL, the return value needs to be checked to avoid NULL poineter dereference.

Fixes 33aa8d45a4fe ("staging: emxx_udc: Add Emma Mobile USB Gadget driver")

---
changes in v2:
- do the null pointer check right before the memcpy().

 drivers/staging/emxx_udc/emxx_udc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/staging/emxx_udc/emxx_udc.c +9 -3
index b4e19174bef2..4fce1a9a659c 100644
@@ -2592,9 +2592,15 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
 							  &ep->phys_buf,
 							  GFP_ATOMIC | GFP_DMA);
 		if (ep->epnum > 0)  {
-			if (ep->direct == USB_DIR_IN)
-				memcpy(ep->virt_buf, req->req.buf,
-				       req->req.length);
+			if (ep->direct == USB_DIR_IN) {
+				if (!ep->virt_buf) {
+					spin_unlock_irqrestore(&udc->lock,
+							       flags);
+					return -ENOMEM;
+				} else
+					memcpy(ep->virt_buf, req->req.buf,
+					       req->req.length);
+			}
 		}
 	}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Yuan Can

As the dma_alloc_coherent may return NULL, the return value needs to be checked to avoid NULL poineter dereference.

Fixes 33aa8d45a4fe ("staging: emxx_udc: Add Emma Mobile USB Gadget driver")

---
changes in v2:
- do the null pointer check right before the memcpy().

No. No. This should be:

drivers/staging/emxx_udc/emxx_udc.c +6 -1
index b4e19174bef2..f9765841c4aa 100644
@@ -2587,10 +2587,15 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
 		req->unaligned = false;
 
 	if (req->unaligned) {
-		if (!ep->virt_buf)
+		if (!ep->virt_buf) {
 			ep->virt_buf = dma_alloc_coherent(udc->dev, PAGE_SIZE,
 							  &ep->phys_buf,
 							  GFP_ATOMIC | GFP_DMA);
+			if (!ep->virt_buf) {
+				spin_unlock_irqrestore(&udc->lock, flags);
+				return -ENOMEM;
+			}
+		}
 		if (ep->epnum > 0)  {
 			if (ep->direct == USB_DIR_IN)
 				memcpy(ep->virt_buf, req->req.buf,
author
#2 lore raw
Quoted 23 lines · Dan Carpenter
Quoted 9 lines · Yuan Can

As the dma_alloc_coherent may return NULL, the return value needs to be checked to avoid NULL poineter dereference.

Fixes 33aa8d45a4fe ("staging: emxx_udc: Add Emma Mobile USB Gadget driver")

---
changes in v2:
- do the null pointer check right before the memcpy().

No. No. This should be:

drivers/staging/emxx_udc/emxx_udc.c +1 -1
index b4e19174bef2..f9765841c4aa 100644
@@ -2587,10 +2587,15 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
  		req->unaligned = false;
 
  	if (req->unaligned) {
-		if (!ep->virt_buf)
+		if (!ep->virt_buf) {
  			ep->virt_buf = dma_alloc_coherent(udc->dev, PAGE_SIZE,

drivers/staging/emxx_udc/emxx_udc.c static int nbu2ss_ep_queue(struct usb_ep *_ep, · line 2601

Quoted 10 lines
drivers/staging/emxx_udc/emxx_udc.c +5 -0
  							  &ep->phys_buf,
  							  GFP_ATOMIC | GFP_DMA);
+			if (!ep->virt_buf) {
+				spin_unlock_irqrestore(&udc->lock, flags);
+				return -ENOMEM;
+			}
+		}
  		if (ep->epnum > 0)  {
  			if (ep->direct == USB_DIR_IN)
  				memcpy(ep->virt_buf, req->req.buf,

Ok, thanks for this!

Signature · 2 lines
Best regards,
Yuan Can
Reviewed-by
#3 lore raw
In reply to Dan Carpenter
Quoted 25 lines · Dan Carpenter
Quoted 9 lines · Yuan Can

As the dma_alloc_coherent may return NULL, the return value needs to be checked to avoid NULL poineter dereference.

Fixes 33aa8d45a4fe ("staging: emxx_udc: Add Emma Mobile USB Gadget driver")

---
changes in v2:
- do the null pointer check right before the memcpy().

No. No. This should be:

drivers/staging/emxx_udc/emxx_udc.c +1 -1
index b4e19174bef2..f9765841c4aa 100644
@@ -2587,10 +2587,15 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
 		req->unaligned = false;
 
 	if (req->unaligned) {
-		if (!ep->virt_buf)
+		if (!ep->virt_buf) {
 			ep->virt_buf = dma_alloc_coherent(udc->dev, PAGE_SIZE,

drivers/staging/emxx_udc/emxx_udc.c static int nbu2ss_ep_queue(struct usb_ep *_ep, · line 2601

Quoted 10 lines
drivers/staging/emxx_udc/emxx_udc.c +5 -0
 							  &ep->phys_buf,
 							  GFP_ATOMIC | GFP_DMA);
+			if (!ep->virt_buf) {
+				spin_unlock_irqrestore(&udc->lock, flags);
+				return -ENOMEM;
+			}
+		}
 		if (ep->epnum > 0)  {
 			if (ep->direct == USB_DIR_IN)
 				memcpy(ep->virt_buf, req->req.buf,

Yes, FWIIW, I think so too.