Skip to contentSkip to conversation

usb: gadget: net2272: assert for a valid dma request

PATCH In review [email protected] 3 participants 4 messages Aug 30, 2020 – Aug 31, 2020 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: net2272: assert for a valid dma request

From: Tom Rix <[email protected]>

clang static analysis flags this representive problem

net2272.c:1541:8: warning: Dereference of null pointer

    if ((req->req.length % ep->ep.maxpacket != 0) ||
         ^~~~~~~~~~~~~~~

This is mostly not a problem.

In net2272_handle_dma(), even though every path through the routine dereferences req, it is set to NULL when the ep->queue is empty. If the eq->queue was ever empty this would have oops.

So the else statement should not be needed. If it is, flag it as a BUG.

---
 drivers/usb/gadget/udc/net2272.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/net2272.c +2 -3
index 44d1ea2307bb..8301723a8b1a 100644
@@ -1506,17 +1506,16 @@ static int net2272_stop(struct usb_gadget *_gadget)
 static void
 net2272_handle_dma(struct net2272_ep *ep)
 {
-	struct net2272_request *req;
+	struct net2272_request *req = NULL;
 	unsigned len;
 	int status;
 
 	if (!list_empty(&ep->queue))
 		req = list_entry(ep->queue.next,
 				struct net2272_request, queue);
-	else
-		req = NULL;
 
 	dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
+	BUG_ON(!req);
 
 	/* Ensure DREQ is de-asserted */
 	net2272_write(ep->dev, DMAREQ,
Signature · 1 line
2.18.1
#1 lore raw
Quoted 14 lines · On Sun, Aug 30, 2020 at 11:36:46AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representive problem

net2272.c:1541:8: warning: Dereference of null pointer

    if ((req->req.length % ep->ep.maxpacket != 0) ||
         ^~~~~~~~~~~~~~~

This is mostly not a problem.

In net2272_handle_dma(), even though every path through the routine dereferences req, it is set to NULL when the ep->queue is empty. If the eq->queue was ever empty this would have oops.

Quoted 4 lines

So the else statement should not be needed. If it is, flag it as a BUG.

This patch really seems to be overkill. In particular, Linus Torvalds feels very strongly that people should not add BUG or BUG_ON calls except in extreme situations -- which this isn't.

Quoted 15 lines
---
 drivers/usb/gadget/udc/net2272.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/net2272.c +1 -1
index 44d1ea2307bb..8301723a8b1a 100644
@@ -1506,17 +1506,16 @@ static int net2272_stop(struct usb_gadget *_gadget)
 static void
 net2272_handle_dma(struct net2272_ep *ep)
 {
-	struct net2272_request *req;
+	struct net2272_request *req = NULL;
 	unsigned len;

drivers/usb/gadget/udc/net2272.c static int net2272_stop(struct usb_gadget *_gadget) · line 1518

Quoted 10 lines
drivers/usb/gadget/udc/net2272.c +1 -2
 	int status;
 
 	if (!list_empty(&ep->queue))
 		req = list_entry(ep->queue.next,
 				struct net2272_request, queue);
-	else
-		req = NULL;
 
 	dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
+	BUG_ON(!req);

There's no point in adding this. If the function goes on to dereference a NULL pointer, you'll get the same effect in any case -- an oops.

If you want to make the point that req had better not be NULL, just get rid of the "if" test entirely. You could even change the list_entry to list_first_entry.

Alan Stern

author
#2 lore raw
Quoted 46 lines · Alan Stern
Quoted 14 lines · On Sun, Aug 30, 2020 at 11:36:46AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representive problem

net2272.c:1541:8: warning: Dereference of null pointer

    if ((req->req.length % ep->ep.maxpacket != 0) ||
         ^~~~~~~~~~~~~~~

This is mostly not a problem.

In net2272_handle_dma(), even though every path through the routine dereferences req, it is set to NULL when the ep->queue is empty. If the eq->queue was ever empty this would have oops.

Quoted 4 lines

So the else statement should not be needed. If it is, flag it as a BUG.

This patch really seems to be overkill. In particular, Linus Torvalds feels very strongly that people should not add BUG or BUG_ON calls except in extreme situations -- which this isn't.

Quoted 23 lines
---
 drivers/usb/gadget/udc/net2272.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/net2272.c +1 -3
index 44d1ea2307bb..8301723a8b1a 100644
@@ -1506,17 +1506,16 @@ static int net2272_stop(struct usb_gadget *_gadget)
 static void
 net2272_handle_dma(struct net2272_ep *ep)
 {
-	struct net2272_request *req;
+	struct net2272_request *req = NULL;
 	unsigned len;
 	int status;
 
 	if (!list_empty(&ep->queue))
 		req = list_entry(ep->queue.next,
 				struct net2272_request, queue);
-	else
-		req = NULL;
 
Quoted 8 lines

drivers/usb/gadget/udc/net2272.c static int net2272_stop(struct usb_gadget *_gadget) · line 1518

Quoted 2 lines
drivers/usb/gadget/udc/net2272.c +1 -0
 	dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
+	BUG_ON(!req);

There's no point in adding this. If the function goes on to dereference a NULL pointer, you'll get the same effect in any case -- an oops.

If you want to make the point that req had better not be NULL, just get rid of the "if" test entirely. You could even change the list_entry to list_first_entry.

Since nothing is really going to change, drop this patch.

Tom

Quoted 2 lines

Alan Stern

maintainer
#3 lore raw
In reply to [email protected]
Quoted 33 lines · On Sun, Aug 30, 2020 at 11:36:46AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this representive problem

net2272.c:1541:8: warning: Dereference of null pointer

    if ((req->req.length % ep->ep.maxpacket != 0) ||
         ^~~~~~~~~~~~~~~

This is mostly not a problem.

In net2272_handle_dma(), even though every path through the routine dereferences req, it is set to NULL when the ep->queue is empty. If the eq->queue was ever empty this would have oops.

So the else statement should not be needed. If it is, flag it as a BUG.

---
 drivers/usb/gadget/udc/net2272.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/usb/gadget/udc/net2272.c +1 -1
index 44d1ea2307bb..8301723a8b1a 100644
@@ -1506,17 +1506,16 @@ static int net2272_stop(struct usb_gadget *_gadget)
 static void
 net2272_handle_dma(struct net2272_ep *ep)
 {
-	struct net2272_request *req;
+	struct net2272_request *req = NULL;
 	unsigned len;

drivers/usb/gadget/udc/net2272.c static int net2272_stop(struct usb_gadget *_gadget) · line 1518

Quoted 10 lines
drivers/usb/gadget/udc/net2272.c +1 -2
 	int status;
 
 	if (!list_empty(&ep->queue))
 		req = list_entry(ep->queue.next,
 				struct net2272_request, queue);
-	else
-		req = NULL;
 
 	dev_vdbg(ep->dev->dev, "handle_dma %s req %p\n", ep->ep.name, req);
+	BUG_ON(!req);

No, fix this properly, don't paper over the issue by crashing (as Alan said, never add a BUG_ON() for something that you can recover from, and really, never add a BUG_ON() call at all, that's just being a lazy programmer.)

If this can cause a NULL dereference later on, than correctly handle the error here please.

thanks,

greg k-h