All of lore.kernel.org
 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 2/4] usb: gadget: f_uac2: fix feature unit descriptor leak
Date: Thu, 03 Dec 2026 19:54:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-248573@kernel.org> (raw)
In-Reply-To: <20261203193217.465889-3-vineet789thakran@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: f_uac2: concurrent afunc_bind() race on feature
  unit descriptors
--

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

usb: gadget: f_uac2: fix feature unit descriptor leak

This commit frees the file-scope in_feature_unit_desc and
out_feature_unit_desc pointers on every exit path of afunc_bind(),
including normal return, and removes the kfree() calls from
afunc_unbind().

> diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c
> index dca8c6d7640c6..6851832ca7d07 100644
> --- a/drivers/usb/gadget/function/f_uac2.c
> +++ b/drivers/usb/gadget/function/f_uac2.c

[ ... ]

> @@ -1366,15 +1366,17 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
>      agdev->notify = afunc_notify;
>  
>  	ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget");
> -	if (ret)
> -		goto err_free_descs;
> -
> -	return 0;
> +	if (ret) {
> +		usb_free_all_descriptors(fn);
> +		agdev->gadget = NULL;
> +	}
>  
> -err_free_descs:
> -	usb_free_all_descriptors(fn);
> -	agdev->gadget = NULL;
> -err_free_fu:
> +free_fu:
> +	/*
> +	 * The feature unit 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);

[Severity: Medium]
Can concurrent afunc_bind() calls race on out_feature_unit_desc and
in_feature_unit_desc now that every exit path frees and clears them?

Two separate configfs gadgets binding to distinct UDCs can run
afunc_bind() concurrently: gadget_dev_desc_UDC_store() only holds the
per-gadget gi->lock, and gadget_bind_driver() drops udc_lock before
invoking driver->bind().

CPU0                                CPU1
afunc_bind()
  out_feature_unit_desc = P1
  usb_assign_descriptors()
  g_audio_setup() (sleeps)
                                    afunc_bind()
                                      out_feature_unit_desc = P2
                                      setup_descriptor()
  free_fu:
    kfree(out_feature_unit_desc)
    out_feature_unit_desc = NULL
                                      usb_assign_descriptors()
                                        <-- use-after-free on P2

Because afunc_bind() sleeps in g_audio_setup() before reaching free_fu,
the race window is wide.  Depending on timing, CPU1 hits a
use-after-free or NULL pointer dereference, both CPUs can double free
P2 at free_fu, and P1 is still leaked when overwritten.

Serializing descriptor setup and usb_assign_descriptors() in
afunc_bind() with a file-scope mutex (as f_midi2_desc_mutex does in
drivers/usb/gadget/function/f_midi2.c) would prevent both the race and
the concurrent leak.

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

  reply	other threads:[~2026-12-03 19:55 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 [this message]
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
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-248573@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.