Linux USB
 help / color / mirror / Atom feed
From: Vineet Thakran <vineet789thakran@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>
Cc: Ruslan Bilovol <ruslan.bilovol@gmail.com>,
	Pavel Hofman <pavel.hofman@ivitera.com>,
	linux-usb@vger.kernel.org,
	Vineet Thakran <vineet789thakran@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2 5/6] usb: gadget: f_uac1: serialize bind with a mutex
Date: Sat,  5 Dec 2026 11:08:30 +0000	[thread overview]
Message-ID: <20261205110831.204588-6-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261205110831.204588-1-vineet789thakran@gmail.com>

f_audio_bind() sets up descriptors that are shared by all f_uac1
instances: setup_descriptor() rebuilds the static f_audio_desc array,
and the AC header and feature unit descriptors live in the file-scope
ac_header_desc, in_feature_unit_desc and out_feature_unit_desc
pointers, which f_audio_unbind() frees.

Nothing serializes this between instances. configfs only holds the
per-gadget lock while binding, and the UDC core drops udc_lock before
calling the gadget driver's bind(), so two gadgets bound to different
UDCs can run f_audio_bind() at the same time, or one can run
f_audio_bind() while the other runs f_audio_unbind(). Binding and
unbinding two such gadgets in parallel loops gives:

  BUG: KASAN: slab-use-after-free in f_audio_bind+0x2898/0x28f4

Add a file-scope mutex, as f_midi2 does with f_midi2_desc_mutex, and
hold it in f_audio_bind() from the first write to the shared descriptors
until it returns, and around the frees in f_audio_unbind().

Fixes: 254cb1e0d78c ("usb: gadget: f_uac1: disable IN/OUT ep if unused")
Fixes: 0356e6283c71 ("usb: gadget: f_uac1: add volume and mute support")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
 drivers/usb/gadget/function/f_uac1.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c
index 6a5c00d3a8e6..012f8d4be0c0 100644
--- a/drivers/usb/gadget/function/f_uac1.c
+++ b/drivers/usb/gadget/function/f_uac1.c
@@ -16,6 +16,7 @@
 
 #include <linux/usb/audio.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 
 #include "u_audio.h"
 #include "u_uac1.h"
@@ -126,6 +127,9 @@ static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
 static struct uac_feature_unit_descriptor *in_feature_unit_desc;
 static struct uac_feature_unit_descriptor *out_feature_unit_desc;
 
+/* Serializes access to the descriptors shared by all instances */
+static DEFINE_MUTEX(f_uac1_desc_mutex);
+
 /* AC IN Interrupt Endpoint */
 static struct usb_endpoint_descriptor ac_int_ep_desc = {
 	.bLength = USB_DT_ENDPOINT_SIZE,
@@ -1249,6 +1253,8 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 
 	audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst);
 
+	guard(mutex)(&f_uac1_desc_mutex);
+
 	strings_uac1[STR_AC_IF].s = audio_opts->function_name;
 
 	strings_uac1[STR_USB_OUT_IT].s = audio_opts->c_it_name;
@@ -1819,13 +1825,15 @@ static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
 	g_audio_cleanup(audio);
 	usb_free_all_descriptors(f);
 
-	kfree(out_feature_unit_desc);
-	out_feature_unit_desc = NULL;
-	kfree(in_feature_unit_desc);
-	in_feature_unit_desc = NULL;
+	scoped_guard(mutex, &f_uac1_desc_mutex) {
+		kfree(out_feature_unit_desc);
+		out_feature_unit_desc = NULL;
+		kfree(in_feature_unit_desc);
+		in_feature_unit_desc = NULL;
 
-	kfree(ac_header_desc);
-	ac_header_desc = NULL;
+		kfree(ac_header_desc);
+		ac_header_desc = NULL;
+	}
 
 	audio->gadget = NULL;
 }
-- 
2.43.0


  parent reply	other threads:[~2026-12-05 11:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 11:08 [PATCH v2 0/6] usb: gadget: f_uac1/f_uac2: fix descriptor leaks, NULL deref and bind race with multiple instances Vineet Thakran
2026-12-05 11:08 ` [PATCH v2 1/6] usb: gadget: f_uac2: use per-instance feature unit IDs in control requests Vineet Thakran
2026-12-05 11:08 ` [PATCH v2 2/6] usb: gadget: f_uac2: serialize bind with a mutex Vineet Thakran
2026-12-05 11:08 ` [PATCH v2 3/6] usb: gadget: f_uac2: fix feature unit descriptor leak Vineet Thakran
2026-12-05 11:08 ` [PATCH v2 4/6] usb: gadget: f_uac1: use per-instance feature unit IDs in control requests Vineet Thakran
2026-12-05 11:08 ` Vineet Thakran [this message]
2026-12-05 11:08 ` [PATCH v2 6/6] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak Vineet Thakran
2026-12-05 11:20   ` sashiko-bot
2026-12-05 11:24 ` [PATCH v2 0/6] usb: gadget: f_uac1/f_uac2: fix descriptor leaks, NULL deref and bind race 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=20261205110831.204588-6-vineet789thakran@gmail.com \
    --to=vineet789thakran@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=pavel.hofman@ivitera.com \
    --cc=ruslan.bilovol@gmail.com \
    --cc=stable@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