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 6/6] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak
Date: Sat,  5 Dec 2026 11:08:31 +0000	[thread overview]
Message-ID: <20261205110831.204588-7-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261205110831.204588-1-vineet789thakran@gmail.com>

f_audio_bind() allocates the AC header and feature unit descriptors and
stores them in the file-scope ac_header_desc, in_feature_unit_desc and
out_feature_unit_desc pointers, which are freed only in
f_audio_unbind(). When a configuration contains more than one f_uac1
instance, each bind overwrites the pointers left by the previous one,
and those descriptors are leaked. This is the same problem syzbot
reported for f_uac2.

usb_assign_descriptors() copies the descriptors into the function, and
since the previous patches nothing uses them after f_audio_bind(). Make
them local variables of f_audio_bind(), pass them to setup_descriptor(),
free them on every exit path of f_audio_bind(), including success, and
drop the now redundant kfree() calls from f_audio_unbind().

The static f_audio_desc and f_audio_ss_desc arrays used to take the
address of the file-scope pointers. Their entries become NULL
placeholders, and setup_descriptor() now fills in the AC header entry of
f_audio_ss_desc the same way it already does for f_audio_desc.

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 | 72 ++++++++++++----------------
 1 file changed, 31 insertions(+), 41 deletions(-)

diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c
index 012f8d4be0c0..71a34777f96f 100644
--- a/drivers/usb/gadget/function/f_uac1.c
+++ b/drivers/usb/gadget/function/f_uac1.c
@@ -81,9 +81,6 @@ static struct usb_interface_descriptor ac_interface_desc = {
 	.bInterfaceSubClass =	USB_SUBCLASS_AUDIOCONTROL,
 };
 
-/* B.3.2  Class-Specific AC Interface Descriptor */
-static struct uac1_ac_header_descriptor *ac_header_desc;
-
 static struct uac_input_terminal_descriptor usb_out_it_desc = {
 	.bLength =		UAC_DT_INPUT_TERMINAL_SIZE,
 	.bDescriptorType =	USB_DT_CS_INTERFACE,
@@ -124,9 +121,6 @@ static struct uac1_output_terminal_descriptor usb_in_ot_desc = {
 	/* .bSourceID =		DYNAMIC */
 };
 
-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);
 
@@ -264,15 +258,15 @@ static struct uac_iso_endpoint_descriptor as_iso_in_desc = {
 
 static struct usb_descriptor_header *f_audio_desc[] = {
 	(struct usb_descriptor_header *)&ac_interface_desc,
-	(struct usb_descriptor_header *)&ac_header_desc,
+	NULL, /* ac_header_desc, set by setup_descriptor() */
 
 	(struct usb_descriptor_header *)&usb_out_it_desc,
 	(struct usb_descriptor_header *)&io_out_ot_desc,
-	(struct usb_descriptor_header *)&out_feature_unit_desc,
+	NULL, /* out_feature_unit_desc, set by setup_descriptor() */
 
 	(struct usb_descriptor_header *)&io_in_it_desc,
 	(struct usb_descriptor_header *)&usb_in_ot_desc,
-	(struct usb_descriptor_header *)&in_feature_unit_desc,
+	NULL, /* in_feature_unit_desc, set by setup_descriptor() */
 
 	(struct usb_descriptor_header *)&ac_int_ep_desc,
 
@@ -336,7 +330,7 @@ static struct usb_ss_ep_comp_descriptor ss_as_in_ep_desc_comp = {
 
 static struct usb_descriptor_header *f_audio_ss_desc[] = {
 	(struct usb_descriptor_header *)&ac_interface_desc,
-	(struct usb_descriptor_header *)&ac_header_desc,
+	NULL, /* ac_header_desc, set by setup_descriptor() */
 
 	(struct usb_descriptor_header *)&usb_out_it_desc,
 	(struct usb_descriptor_header *)&io_out_ot_desc,
@@ -1085,7 +1079,10 @@ uac1_ac_header_descriptor *build_ac_header_desc(struct f_uac1_opts *opts)
 /* Use macro to overcome line length limitation */
 #define USBDHDR(p) (struct usb_descriptor_header *)(p)
 
-static void setup_descriptor(struct f_uac1_opts *opts)
+static void setup_descriptor(struct f_uac1_opts *opts,
+			     struct uac1_ac_header_descriptor *ac_header_desc,
+			     struct uac_feature_unit_descriptor *in_feature_unit_desc,
+			     struct uac_feature_unit_descriptor *out_feature_unit_desc)
 {
 	/* patch descriptors */
 	int i = 1; /* ID's start with 1 */
@@ -1143,6 +1140,7 @@ static void setup_descriptor(struct f_uac1_opts *opts)
 	i = 0;
 	f_audio_desc[i++] = USBDHDR(&ac_interface_desc);
 	f_audio_desc[i++] = USBDHDR(ac_header_desc);
+	f_audio_ss_desc[1] = USBDHDR(ac_header_desc);
 
 	if (EPOUT_EN(opts)) {
 		f_audio_desc[i++] = USBDHDR(&usb_out_it_desc);
@@ -1241,6 +1239,9 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	struct f_uac1			*uac1 = func_to_uac1(f);
 	struct g_audio			*audio = func_to_g_audio(f);
 	struct f_uac1_opts		*audio_opts;
+	struct uac1_ac_header_descriptor *ac_header_desc;
+	struct uac_feature_unit_descriptor *in_feature_unit_desc = NULL;
+	struct uac_feature_unit_descriptor *out_feature_unit_desc = NULL;
 	struct usb_ep			*ep = NULL;
 	struct usb_string		*us;
 	int				ba_iface_id;
@@ -1283,14 +1284,14 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 		out_feature_unit_desc = build_fu_desc(audio_opts->c_chmask);
 		if (!out_feature_unit_desc) {
 			status = -ENOMEM;
-			goto fail;
+			goto free_ac_header;
 		}
 	}
 	if (FUIN_EN(audio_opts)) {
 		in_feature_unit_desc = build_fu_desc(audio_opts->p_chmask);
 		if (!in_feature_unit_desc) {
 			status = -ENOMEM;
-			goto err_free_fu;
+			goto free_fu;
 		}
 	}
 
@@ -1378,7 +1379,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	/* allocate instance-specific interface IDs, and patch descriptors */
 	status = usb_interface_id(c, f);
 	if (status < 0)
-		goto err_free_fu;
+		goto free_fu;
 	ac_interface_desc.bInterfaceNumber = status;
 	uac1->ac_intf = status;
 	uac1->ac_alt = 0;
@@ -1388,7 +1389,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	if (EPOUT_EN(audio_opts)) {
 		status = usb_interface_id(c, f);
 		if (status < 0)
-			goto err_free_fu;
+			goto free_fu;
 		as_out_interface_alt_0_desc.bInterfaceNumber = status;
 		as_out_interface_alt_1_desc.bInterfaceNumber = status;
 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
@@ -1399,7 +1400,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	if (EPIN_EN(audio_opts)) {
 		status = usb_interface_id(c, f);
 		if (status < 0)
-			goto err_free_fu;
+			goto free_fu;
 		as_in_interface_alt_0_desc.bInterfaceNumber = status;
 		as_in_interface_alt_1_desc.bInterfaceNumber = status;
 		ac_header_desc->baInterfaceNr[ba_iface_id++] = status;
@@ -1417,7 +1418,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	if (FUOUT_EN(audio_opts) || FUIN_EN(audio_opts)) {
 		ep = usb_ep_autoconfig(cdev->gadget, &ac_int_ep_desc);
 		if (!ep)
-			goto err_free_fu;
+			goto free_fu;
 		uac1->int_ep = ep;
 		uac1->int_ep->desc = &ac_int_ep_desc;
 
@@ -1428,7 +1429,7 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	if (EPOUT_EN(audio_opts)) {
 		ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);
 		if (!ep)
-			goto err_free_fu;
+			goto free_fu;
 		ss_as_out_ep_desc.bEndpointAddress = as_out_ep_desc.bEndpointAddress;
 		audio->out_ep = ep;
 		audio->out_ep->desc = &as_out_ep_desc;
@@ -1437,19 +1438,20 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f)
 	if (EPIN_EN(audio_opts)) {
 		ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
 		if (!ep)
-			goto err_free_fu;
+			goto free_fu;
 		ss_as_in_ep_desc.bEndpointAddress = as_in_ep_desc.bEndpointAddress;
 		audio->in_ep = ep;
 		audio->in_ep->desc = &as_in_ep_desc;
 	}
 
-	setup_descriptor(audio_opts);
+	setup_descriptor(audio_opts, ac_header_desc, in_feature_unit_desc,
+			 out_feature_unit_desc);
 
 	/* copy descriptors, and track endpoint copies */
 	status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, f_audio_ss_desc,
 					f_audio_ss_desc);
 	if (status)
-		goto err_free_fu;
+		goto free_fu;
 
 	audio->out_ep_maxpsize = le16_to_cpu(as_out_ep_desc.wMaxPacketSize);
 	audio->in_ep_maxpsize = le16_to_cpu(as_in_ep_desc.wMaxPacketSize);
@@ -1486,20 +1488,18 @@ 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;
-
-	return 0;
+		usb_free_all_descriptors(f);
 
-err_card_register:
-	usb_free_all_descriptors(f);
-err_free_fu:
+free_fu:
+	/*
+	 * The feature unit and AC header descriptors are 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;
 }
 
@@ -1825,16 +1825,6 @@ static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f)
 	g_audio_cleanup(audio);
 	usb_free_all_descriptors(f);
 
-	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;
-	}
-
 	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 ` [PATCH v2 5/6] usb: gadget: f_uac1: serialize bind with a mutex Vineet Thakran
2026-12-05 11:08 ` Vineet Thakran [this message]
2026-12-05 11:20   ` [PATCH v2 6/6] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak 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-7-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