Re: [PATCH] usb: gadget: net2280: fix memory leak on probe error handling paths

From: Evgeny Novikov
Date: Wed Jul 22 2020 - 15:56:20 EST


Hi Alan,

I have neither an appropriate hardware nor an experience to deal with issues that you mentioned. Our framework does not allow to detect them as well at the moment. At last, it seems that rather many drivers can suffer from these issues. So, it would be much better if somebody else will suggest necessary fixes and test them carefully.

BTW, you have already discussed the race within net2280_remove() with my colleague about 3 years ago. But you did not achieve a consensus at that time and no fixes were made after all.

Anyway, one can consider both issues independently on the one fixed by the patch.

--Â
Evgeny Novikov
Linux Verification Center, ISP RAS
http://linuxtesting.org

22.07.2020, 17:17, "Alan Stern" <stern@xxxxxxxxxxxxxxxxxxx>:
> On Tue, Jul 21, 2020 at 11:15:58PM +0300, Evgeny Novikov wrote:
>> ÂDriver does not release memory for device on error handling paths in
>> Ânet2280_probe() when gadget_release() is not registered yet.
>>
>> ÂThe patch fixes the bug like in other similar drivers.
>>
>> ÂFound by Linux Driver Verification project (linuxtesting.org).
>>
>> ÂSigned-off-by: Evgeny Novikov <novikov@xxxxxxxxx>
>> Â---
>> ÂÂdrivers/usb/gadget/udc/net2280.c | 4 +++-
>> ÂÂ1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> Âdiff --git a/drivers/usb/gadget/udc/net2280.c b/drivers/usb/gadget/udc/net2280.c
>> Âindex 5eff85eeaa5a..d5fe071b2db2 100644
>> Â--- a/drivers/usb/gadget/udc/net2280.c
>> Â+++ b/drivers/usb/gadget/udc/net2280.c
>> Â@@ -3781,8 +3781,10 @@ static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
>> ÂÂÂÂÂÂÂÂÂÂreturn 0;
>>
>> ÂÂdone:
>> Â- if (dev)
>> Â+ if (dev) {
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂnet2280_remove(pdev);
>> Â+ kfree(dev);
>> Â+ }
>> ÂÂÂÂÂÂÂÂÂÂreturn retval;
>> ÂÂ}
>
> This patch seems to be the tip of an iceberg. Following through its
> implications led to a couple of discoveries.
>
> usb_del_gadget_udc() calls device_unregister(&gadget->dev). Once this
> call returns, gadget has to be regarded as a stale pointer. But the
> very next line of code does:
>
> ÂÂÂÂÂÂÂÂmemset(&gadget->dev, 0x00, sizeof(gadget->dev));
>
> for no apparent reason. I'm amazed this hasn't caused problems already.
> Is there any justification for keeping this memset? It's hard to
> imagine that it does any good.
>
> Similarly, net2280_remove() calls usb_del_gadget_udc(&dev->gadget) at
> its start, and so dev must be a stale pointer for the entire remainder
> of the routine. But it gets used repeatedly. Surely we ought to have
> a device_get() and device_put() in there.
>
> Alan Stern