usb: cdns3: change place of 'priv_ep' assignment in cdns3_gadget_ep_dequeue(), cdns3_...
authorAndrey Strachuk <strochuk@ispras.ru>
Mon, 18 Jul 2022 16:00:52 +0000 (19:00 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 Aug 2022 12:23:58 +0000 (14:23 +0200)
[ Upstream commit c3ffc9c4ca44bfe9562166793d133e1fb0630ea6 ]

If 'ep' is NULL, result of ep_to_cdns3_ep(ep) is invalid pointer
and its dereference with priv_ep->cdns3_dev may cause panic.

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

Fixes: 7733f6c32e36 ("usb: cdns3: Add Cadence USB3 DRD Driver")
Acked-by: Peter Chen <peter.chen@kernel.org>
Signed-off-by: Andrey Strachuk <strochuk@ispras.ru>
Link: https://lore.kernel.org/r/20220718160052.4188-1-strochuk@ispras.ru
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/usb/cdns3/cdns3-gadget.c

index d6d515d598dc0c22dcb75a905641cdda3ccf7c31..e0cf62e650758021c34826493984eda9c50937c4 100644 (file)
@@ -2281,14 +2281,15 @@ static int cdns3_gadget_ep_enable(struct usb_ep *ep,
        int val;
 
        priv_ep = ep_to_cdns3_ep(ep);
-       priv_dev = priv_ep->cdns3_dev;
-       comp_desc = priv_ep->endpoint.comp_desc;
 
        if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
                dev_dbg(priv_dev->dev, "usbss: invalid parameters\n");
                return -EINVAL;
        }
 
+       comp_desc = priv_ep->endpoint.comp_desc;
+       priv_dev = priv_ep->cdns3_dev;
+
        if (!desc->wMaxPacketSize) {
                dev_err(priv_dev->dev, "usbss: missing wMaxPacketSize\n");
                return -EINVAL;
@@ -2596,7 +2597,7 @@ int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
                            struct usb_request *request)
 {
        struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
-       struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
+       struct cdns3_device *priv_dev;
        struct usb_request *req, *req_temp;
        struct cdns3_request *priv_req;
        struct cdns3_trb *link_trb;
@@ -2607,6 +2608,8 @@ int cdns3_gadget_ep_dequeue(struct usb_ep *ep,
        if (!ep || !request || !ep->desc)
                return -EINVAL;
 
+       priv_dev = priv_ep->cdns3_dev;
+
        spin_lock_irqsave(&priv_dev->lock, flags);
 
        priv_req = to_cdns3_request(request);