mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Pandey, Radhey Shyam" <radheys@amd.com>
To: Pei Xiao <xiaopei01@kylinos.cn>,
	Thinh.Nguyen@synopsys.com, gregkh@linuxfoundation.org,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: Re: [PATCH v2] usb: dwc3: gadget: Fix use-after-free in dwc3_gadget_free_endpoints due to race condition
Date: Sat, 8 Aug 2026 16:43:20 +0530	[thread overview]
Message-ID: <46ccf549-339b-4085-a47f-430c307d624e@amd.com> (raw)
In-Reply-To: <331d1d5133496d2b4184e05f8848adb06930a138.1785893865.git.xiaopei01@kylinos.cn>

On 8/5/2026 7:10 AM, Pei Xiao wrote:
> In dwc3_gadget_init_endpoint, &dep->nostream_work is bound with
> dwc3_nostream_work, and dwc3_gadget_endpoint_stream_event can queue
> this delayed work on system_percpu_wq when a DEPEVT_STREAM_NOSTREAM
> event is received.
> 
> If we remove the gadget, dwc3_gadget_free_endpoints makes cleanup and
> the memory allocated for dep with kzalloc() is released by kfree(dep),
> while the delayed work mentioned above may still be pending or
> running. The sequence of operations that may lead to a UAF bug is as
> follows:
> 
> CPU0                                      CPU1
> 
>                                            | dwc3_thread_interrupt
>                                            | dwc3_endpoint_interrupt
>                                            | dwc3_gadget_endpoint_stream_event
>                                            | queue_delayed_work(system_percpu_wq,
>                                            |                    &dep->nostream_work)
> dwc3_gadget_free_endpoints                |
> dwc3_free_trb_pool(dep)                   |
> list_del(&dep->endpoint.ep_list)          |
> dwc3_debugfs_remove_endpoint_dir(dep)     |
> kfree(dep)                                |
> // dep is freed                           |
>                                            | dwc3_nostream_work
>                                            | // use dep (use-after-free)
> 
> Fix it by canceling the delayed work before kfree(dep) in
> dwc3_gadget_free_endpoints.
> 
> Fixes: dcfe437492e2 ("usb: dwc3: gadget: Reinitiate stream for all host NoStream behavior")
> Assisted-by: Codex:deepseek-v4-flash
> Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>

Reviewed-by: Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>
Thanks!> ---
> changes in v2:
> add Acked-by add Cc stable
> ---
>   drivers/usb/dwc3/gadget.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index fa0f16ffafef..fa944856f956 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -3508,6 +3508,7 @@ static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
>   		}
>   
>   		dwc3_debugfs_remove_endpoint_dir(dep);
> +		cancel_delayed_work_sync(&dep->nostream_work);
>   		kfree(dep);
>   	}
>   }


      reply	other threads:[~2026-08-08 11:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05  1:40 Pei Xiao
2026-08-08 11:13 ` Pandey, Radhey Shyam [this message]

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=46ccf549-339b-4085-a47f-430c307d624e@amd.com \
    --to=radheys@amd.com \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=xiaopei01@kylinos.cn \
    /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

all inboxes | Powered by JetHome®