Skip to contentSkip to conversation

staging: emxx_udc: Add checks for dma_alloc_coherent()

PATCH Superseded Yuan Can 2 participants 3 messages Nov 25, 2022 – Jan 19, 2023 Staging

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yuan Can

author
#0 lore raw

PATCH 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")
---
 drivers/staging/emxx_udc/emxx_udc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/emxx_udc/emxx_udc.c +4 -0
index b6abd3770e81..3c0c9c5eb733 100644
@@ -2594,6 +2594,10 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
 			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,
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 14 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")
---
 drivers/staging/emxx_udc/emxx_udc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/emxx_udc/emxx_udc.c +0 -0
index b6abd3770e81..3c0c9c5eb733 100644

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

Quoted 7 lines
drivers/staging/emxx_udc/emxx_udc.c +4 -0
@@ -2594,6 +2594,10 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
 			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;
+		}

This should be checked in the if statment, not outside of it, right?

thanks,

greg k-h

author
#2 lore raw
Quoted 10 lines · Greg KH
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")
---
  drivers/staging/emxx_udc/emxx_udc.c | 4 ++++
  1 file changed, 4 insertions(+)
Quoted 13 lines

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

Quoted 12 lines
drivers/staging/emxx_udc/emxx_udc.c +4 -0
index b6abd3770e81..3c0c9c5eb733 100644
@@ -2594,6 +2594,10 @@ static int nbu2ss_ep_queue(struct usb_ep *_ep,
  			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;
+		}

This should be checked in the if statment, not outside of it, right?

Yes, the next version will do the check just before the memcpy, thanks for the suggestion.

Signature · 2 lines
Best regards,
Yuan Can