usb: cdns3: Do not access memory after free
authorAndrew Goodbody <andrew.goodbody@linaro.org>
Wed, 13 Aug 2025 16:30:12 +0000 (17:30 +0100)
committerMarek Vasut <marek.vasut+usb@mailbox.org>
Fri, 29 Aug 2025 11:12:41 +0000 (13:12 +0200)
The call to cdns3_gadget_ep_free_request will free priv_req so do the
call to list_del_init which accesses the memory pointed to by priv_req
before the free.

This issue was found by Smatch.

Signed-off-by: Andrew Goodbody <andrew.goodbody@linaro.org>
Reviewed-by: Siddharth Vadapalli <s-vadapalli@ti.com>
drivers/usb/cdns3/gadget.c

index a30c40e..9eaf7e4 100644 (file)
@@ -557,10 +557,10 @@ static void cdns3_wa2_remove_old_request(struct cdns3_endpoint *priv_ep)
 
                trace_cdns3_wa2(priv_ep, "removes eldest request");
 
+               list_del_init(&priv_req->list);
                kfree(priv_req->request.buf);
                cdns3_gadget_ep_free_request(&priv_ep->endpoint,
                                             &priv_req->request);
-               list_del_init(&priv_req->list);
                --priv_ep->wa2_counter;
 
                if (!chain)
@@ -1959,10 +1959,10 @@ static int cdns3_gadget_ep_disable(struct usb_ep *ep)
        while (!list_empty(&priv_ep->wa2_descmiss_req_list)) {
                priv_req = cdns3_next_priv_request(&priv_ep->wa2_descmiss_req_list);
 
+               list_del_init(&priv_req->list);
                kfree(priv_req->request.buf);
                cdns3_gadget_ep_free_request(&priv_ep->endpoint,
                                             &priv_req->request);
-               list_del_init(&priv_req->list);
                --priv_ep->wa2_counter;
        }