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 2/6] usb: gadget: f_uac2: serialize bind with a mutex
Date: Sat, 5 Dec 2026 11:08:27 +0000 [thread overview]
Message-ID: <20261205110831.204588-3-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261205110831.204588-1-vineet789thakran@gmail.com>
afunc_bind() sets up descriptors that are shared by all f_uac2
instances: setup_descriptor() rebuilds the static fs/hs/ss descriptor
arrays, and the feature unit descriptors live in the file-scope
in_feature_unit_desc/out_feature_unit_desc pointers, which
afunc_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 afunc_bind() at the same time, or one can run afunc_bind()
while the other runs afunc_unbind(). Binding and unbinding two such
gadgets in parallel loops gives:
BUG: KASAN: double-free in afunc_unbind+0x4c/0x70
Add a file-scope mutex, as f_midi2 does with f_midi2_desc_mutex, and
hold it in afunc_bind() from the first write to the shared descriptors
until it returns, and around the frees in afunc_unbind().
Fixes: 3fa4eaa6c082 ("usb: gadget: f_uac2: disable IN/OUT ep if unused")
Fixes: eaf6cbe09920 ("usb: gadget: f_uac2: add volume and mute support")
Cc: stable@vger.kernel.org
Signed-off-by: Vineet Thakran <vineet789thakran@gmail.com>
---
drivers/usb/gadget/function/f_uac2.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c
index dca8c6d7640c..7a3179ad8f97 100644
--- a/drivers/usb/gadget/function/f_uac2.c
+++ b/drivers/usb/gadget/function/f_uac2.c
@@ -13,6 +13,7 @@
#include <linux/usb/audio.h>
#include <linux/usb/audio-v2.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include "u_audio.h"
@@ -246,6 +247,9 @@ static struct uac2_output_terminal_descriptor io_out_ot_desc = {
static struct uac2_feature_unit_descriptor *in_feature_unit_desc;
static struct uac2_feature_unit_descriptor *out_feature_unit_desc;
+/* Serializes access to the descriptors shared by all instances */
+static DEFINE_MUTEX(f_uac2_desc_mutex);
+
static struct uac2_ac_header_descriptor ac_hdr_desc = {
.bLength = sizeof ac_hdr_desc,
.bDescriptorType = USB_DT_CS_INTERFACE,
@@ -1059,6 +1063,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
if (ret)
return ret;
+ guard(mutex)(&f_uac2_desc_mutex);
+
strings_fn[STR_ASSOC].s = uac2_opts->function_name;
strings_fn[STR_IF_CTRL].s = uac2_opts->if_ctrl_name;
strings_fn[STR_CLKSRC_IN].s = uac2_opts->clksrc_in_name;
@@ -2306,6 +2312,7 @@ static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
agdev->gadget = NULL;
+ guard(mutex)(&f_uac2_desc_mutex);
kfree(out_feature_unit_desc);
out_feature_unit_desc = NULL;
kfree(in_feature_unit_desc);
--
2.43.0
next prev 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 ` Vineet Thakran [this message]
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 ` [PATCH v2 5/6] usb: gadget: f_uac1: serialize bind with a mutex Vineet Thakran
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-3-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