public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Florian Faber <faber@faberman.de>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: core: Fix possible memleak in usb_add_gadget
Date: Sun, 5 Sep 2021 16:56:06 +0200	[thread overview]
Message-ID: <YTTahtscWqQyY0CU@kroah.com> (raw)
In-Reply-To: <311d64c6-f0e2-da42-5619-1efe46df0007@faberman.de>

On Sat, Sep 04, 2021 at 05:34:29PM +0200, Florian Faber wrote:
> The memory for the udc structure allocated via kzalloc in line 1295 is not
> freed in the error handling code, leading to a memory leak in case of an
> error.
> 
> Singed-off-by: Florian Faber <faber@faberman.de>
> 
> ---
>  drivers/usb/gadget/udc/core.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
> index 14fdf918ecfe..a1270a44855a 100644
> --- a/drivers/usb/gadget/udc/core.c
> +++ b/drivers/usb/gadget/udc/core.c
> @@ -1346,6 +1346,8 @@ int usb_add_gadget(struct usb_gadget *gadget)
> 
>   err_put_udc:
>  	put_device(&udc->dev);
> +	kfree(udc);
> +	gadget->udc = NULL;
> 
>   error:
>  	return ret;
> -- 
> 2.33.0
> 
> Flo
> -- 
> Machines can do the work, so people have time to think.

Did you test this?  I think you will find that you just caused a
use-after-free :(

Please read the documentation for device_initialize() for why this is
not the correct thing to do here.

thanks,

greg k-h

  parent reply	other threads:[~2021-09-05 14:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-04 15:34 [PATCH] usb: core: Fix possible memleak in usb_add_gadget Florian Faber
2021-09-04 16:12 ` Alan Stern
2021-09-05 14:56 ` Greg KH [this message]
2021-09-05 15:16   ` Florian Faber
2021-09-05 16:27     ` Greg KH
2021-09-05 19:58       ` Florian Faber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YTTahtscWqQyY0CU@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=faber@faberman.de \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox