[PATCH] usb: gadget: udc: net2280: Fix NULL pointer dereference in net2280_free_request

From: Mikhail Arkhipov
Date: Wed Sep 11 2024 - 19:10:26 EST


When the function net2280_free_request is called with a NULL _ep pointer,
the function still tries to dereference _ep before returning. This leads
to a NULL pointer dereference and possible kernel panic.

This bug can be triggered when a USB endpoint is removed unexpectedly
and the driver attempts to free resources associated with it.

Move the NULL check before calling container_of to ensure that the
function does not attempt to dereference a NULL pointer. This prevents
the kernel from crashing when either _ep or _req is NULL.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Mikhail Arkhipov <m.arhipov@xxxxxxx>
---
drivers/usb/gadget/udc/net2280.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
index 1b929c519cd7..3e8280fa3207 100644
--- a/drivers/usb/gadget/udc/net2280.c
+++ b/drivers/usb/gadget/udc/net2280.c
@@ -582,13 +582,12 @@ static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
struct net2280_ep *ep;
struct net2280_request *req;

- ep = container_of(_ep, struct net2280_ep, ep);
if (!_ep || !_req) {
- dev_err(&ep->dev->pdev->dev, "%s: Invalid ep=%p or req=%p\n",
- __func__, _ep, _req);
+ dev_err(NULL, "%s: Invalid ep=%p or req=%p\n", __func__, _ep, _req);
return;
}

+ ep = container_of(_ep, struct net2280_ep, ep);
req = container_of(_req, struct net2280_request, req);
WARN_ON(!list_empty(&req->queue));
if (req->td)
--
2.39.3 (Apple Git-146)