Linux USB
 help / color / mirror / Atom feed
From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: Mikhail Arkhipov <m.arhipov@rosa.ru>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Felipe Balbi" <balbi@ti.com>,
	"Ben Dooks" <ben.dooks@codethink.co.uk>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	lvc-project@linuxtesting.org
Subject: Re: [PATCH] usb: gadget: r8a66597-udc: Fix double free in r8a66597_probe
Date: Tue, 17 Sep 2024 16:50:17 +0200	[thread overview]
Message-ID: <20240917145017.dCZwT_uL@linutronix.de> (raw)
In-Reply-To: <20240916222937.12878-1-m.arhipov@rosa.ru>

On 2024-09-17 01:29:37 [+0300], Mikhail Arkhipov wrote:
> The function r8a66597_free_request is called to free r8a66597->ep0_req,
> but the pointer is not set to NULL afterward, which may lead to a double
> free if the function is called again.
> 
> If the probe process fails and the r8a66597_probe function subsequently
> goes through its error handling path. Since r8a66597_free_request is called
> multiple times in different error handling sections, it leads to an attempt
> to free the same memory twice.
> 
> Set r8a66597->ep0_req to NULL after calling r8a66597_free_request
> to prevent any further attempts to free this pointer.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Fixes: 0f91349b89f3 ("usb: gadget: convert all users to the new udc infrastructure")
> Signed-off-by: Mikhail Arkhipov <m.arhipov@rosa.ru>

Looking at how the code looks now and how it looks back then, I simply
haven't seen it. I would suggest to instead assigning NULL simply remove
the second block. The request gets allocated shortly before
usb_add_gadget_udc() is invoked. It does not make sense to have this
conditional check all the way from clean_up2 where it is not allocated.

>  drivers/usb/gadget/udc/r8a66597-udc.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/gadget/udc/r8a66597-udc.c b/drivers/usb/gadget/udc/r8a66597-udc.c
> index db4a10a979f9..43b81cae7d17 100644
> --- a/drivers/usb/gadget/udc/r8a66597-udc.c
> +++ b/drivers/usb/gadget/udc/r8a66597-udc.c
> @@ -1952,12 +1952,14 @@ static int r8a66597_probe(struct platform_device *pdev)
>  
>  err_add_udc:
>  	r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
> +	r8a66597->ep0_req = NULL;
>  clean_up2:
>  	if (r8a66597->pdata->on_chip)
>  		clk_disable_unprepare(r8a66597->clk);
>  
>  	if (r8a66597->ep0_req)
>  		r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
> +		r8a66597->ep0_req = NULL;
>  
>  	return ret;
>  }

Sebastian

  reply	other threads:[~2024-09-17 14:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-16 22:29 [PATCH] usb: gadget: r8a66597-udc: Fix double free in r8a66597_probe Mikhail Arkhipov
2024-09-17 14:50 ` Sebastian Andrzej Siewior [this message]
2024-09-17 16:55 ` kernel test robot
2024-09-17 16:55 ` kernel test robot

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=20240917145017.dCZwT_uL@linutronix.de \
    --to=bigeasy@linutronix.de \
    --cc=balbi@ti.com \
    --cc=ben.dooks@codethink.co.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=m.arhipov@rosa.ru \
    --cc=u.kleine-koenig@pengutronix.de \
    /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