Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

usb: cdns3: gadget: Use-after-free during failed initialization and exit of cdnsp gadget

In the __cdnsp_gadget_init() and cdnsp_gadget_exit() functions, the gadget
structure (pdev->gadget) was freed before its endpoints.
The endpoints are linked via the ep_list in the gadget structure.
Freeing the gadget first leaves dangling pointers in the endpoint list.
When the endpoints are subsequently freed, this results in a use-after-free.

Fix:
By separating the usb_del_gadget_udc() operation into distinct "del" and
"put" steps, cdnsp_gadget_free_endpoints() can be executed prior to the
final release of the gadget structure with usb_put_gadget().

A patch similar to bb9c74a5bd14("usb: dwc3: gadget: Free gadget structure
only after freeing endpoints").

Signed-off-by: Chen Yufeng <chenyufeng@iie.ac.cn>
Link: https://lore.kernel.org/r/20250905094842.1232-1-chenyufeng@iie.ac.cn
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Chen Yufeng and committed by
Greg Kroah-Hartman
87c5ff56 e9c20632

+6 -2
+6 -2
drivers/usb/cdns3/cdnsp-gadget.c
··· 1976 1976 return 0; 1977 1977 1978 1978 del_gadget: 1979 - usb_del_gadget_udc(&pdev->gadget); 1979 + usb_del_gadget(&pdev->gadget); 1980 + cdnsp_gadget_free_endpoints(pdev); 1981 + usb_put_gadget(&pdev->gadget); 1982 + goto halt_pdev; 1980 1983 free_endpoints: 1981 1984 cdnsp_gadget_free_endpoints(pdev); 1982 1985 halt_pdev: ··· 2001 1998 devm_free_irq(pdev->dev, cdns->dev_irq, pdev); 2002 1999 pm_runtime_mark_last_busy(cdns->dev); 2003 2000 pm_runtime_put_autosuspend(cdns->dev); 2004 - usb_del_gadget_udc(&pdev->gadget); 2001 + usb_del_gadget(&pdev->gadget); 2005 2002 cdnsp_gadget_free_endpoints(pdev); 2003 + usb_put_gadget(&pdev->gadget); 2006 2004 cdnsp_mem_cleanup(pdev); 2007 2005 kfree(pdev); 2008 2006 cdns->gadget_dev = NULL;