Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vineet Thakran" <vineet789thakran@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 4/4] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak
Date: Thu, 03 Dec 2026 19:52:43 +0000	[thread overview]
Message-ID: <sashiko-outbox-248567@kernel.org> (raw)
In-Reply-To: <20261203193217.465889-5-vineet789thakran@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: f_uac1: concurrent f_audio_bind() UAF and
  double-free on globals
--

commit 20d563ef92940baa4fb226b974c5c6ad509ae61d
Author: Vineet Thakran <vineet789thakran@gmail.com>

usb: gadget: f_uac1: fix feature unit and AC header descriptor leak

This commit frees the file-scope AC header and feature unit descriptors
on every exit path of f_audio_bind(), including normal success, and
removes the kfree() calls from f_audio_unbind().

> diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c
> index 6a5c00d3a8e63..2c892db84c656 100644
> --- a/drivers/usb/gadget/function/f_uac1.c
> +++ b/drivers/usb/gadget/function/f_uac1.c

[ ... ]

> @@ -1480,18 +1480,19 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
>  
>  	status = g_audio_setup(audio, "UAC1_PCM", "UAC1_Gadget");
>  	if (status)
> -		goto err_card_register;
> +		usb_free_all_descriptors(f);
>  
> -	return 0;
> -
> -err_card_register:
> -	usb_free_all_descriptors(f);
> -err_free_fu:
> +free_fu:
> +	/*
> +	 * The feature unit and AC header descriptors are shared by all
> +	 * instances and only needed until usb_assign_descriptors() has
> +	 * copied them, so release them on every exit path.
> +	 */
>  	kfree(out_feature_unit_desc);
>  	out_feature_unit_desc = NULL;
>  	kfree(in_feature_unit_desc);
>  	in_feature_unit_desc = NULL;
> -fail:
> +free_ac_header:
>  	kfree(ac_header_desc);
>  	ac_header_desc = NULL;
>  	return status;

[Severity: Medium]
Can freeing ac_header_desc, out_feature_unit_desc, and
in_feature_unit_desc on the normal exit path of f_audio_bind() cause a
use-after-free, NULL pointer dereference, or double free when two gadgets
bind concurrently on separate UDCs?

Multi-UDC binding is not globally serialized: gadget_dev_desc_UDC_store()
only holds the per-gadget gi->lock, and gadget_bind_driver() releases
udc_lock before calling driver->bind().  With f_audio_bind() now freeing
these globals after sleeping in g_audio_setup(), two binds can race:

CPU0                                  CPU1
f_audio_bind()
  ac_header_desc = build_ac_...()
  ...
  g_audio_setup() (sleeps)
                                      f_audio_bind()
                                        ac_header_desc = build_ac_...()
                                        out_feature_unit_desc = ...
  kfree(out_feature_unit_desc)
  out_feature_unit_desc = NULL
  kfree(ac_header_desc)
  ac_header_desc = NULL
                                        setup_descriptor(...)
                                          <-- NULL deref / use-after-free

CPU0 frees and clears the descriptors allocated by CPU1 while CPU1 is
still using them, causing a use-after-free or NULL pointer dereference.
If both CPUs enter free_fu before either writes NULL, both pass the same
pointer to kfree() and trigger a double free.

Making ac_header_desc, out_feature_unit_desc, and in_feature_unit_desc
local variables in f_audio_bind() (passed to setup_descriptor()) removes
their shared lifetime, and a file-scope mutex around descriptor setup
would serialize the remaining shared static descriptors.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203193217.465889-1-vineet789thakran@gmail.com?part=4

  reply	other threads:[~2026-12-03 19:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 19:32 [PATCH 0/4] usb: gadget: f_uac1/f_uac2: fix descriptor leaks and NULL deref with multiple instances Vineet Thakran
2026-12-03 19:32 ` [PATCH 1/4] usb: gadget: f_uac2: use per-instance feature unit IDs in control requests Vineet Thakran
2026-12-03 19:32 ` [PATCH 2/4] usb: gadget: f_uac2: fix feature unit descriptor leak Vineet Thakran
2026-12-03 19:54   ` sashiko-bot
2026-12-03 19:32 ` [PATCH 3/4] usb: gadget: f_uac1: use per-instance feature unit IDs in control requests Vineet Thakran
2026-12-03 19:32 ` [PATCH 4/4] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak Vineet Thakran
2026-12-03 19:52   ` sashiko-bot [this message]
2026-12-03 19:56 ` [PATCH 0/4] usb: gadget: f_uac1/f_uac2: fix descriptor leaks and NULL deref with multiple instances sashiko-bot

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=sashiko-outbox-248567@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vineet789thakran@gmail.com \
    /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