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>,
	syzbot+da5ce3f4a00c8cbe0d4e@syzkaller.appspotmail.com,
	stable@vger.kernel.org
Subject: [PATCH 2/4] usb: gadget: f_uac2: fix feature unit descriptor leak
Date: Thu,  3 Dec 2026 19:32:15 +0000	[thread overview]
Message-ID: <20261203193217.465889-3-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261203193217.465889-1-vineet789thakran@gmail.com>

afunc_bind() allocates the feature unit descriptors and stores them in
the file-scope in_feature_unit_desc/out_feature_unit_desc pointers,
which are freed only in afunc_unbind(). When a configuration contains
more than one f_uac2 instance, each bind overwrites the pointers left
by the previous one, and those descriptors are leaked:

  unreferenced object 0xffff888111d43640 (size 32):
    build_fu_desc+0x3f/0x70 drivers/usb/gadget/function/f_uac2.c:773
    afunc_bind+0x1414/0x16a0 drivers/usb/gadget/function/f_uac2.c:1062
    usb_add_function+0xca/0x270 drivers/usb/gadget/composite.c:333
    configfs_composite_bind+0x667/0x9b0 drivers/usb/gadget/configfs.c:1803

usb_assign_descriptors() copies the descriptors into the function, and
since the previous patch nothing uses them after afunc_bind(). Free them
on every exit path of afunc_bind(), including success, and drop the now
redundant kfree() calls from afunc_unbind().

The set_ep_max_packet_size_bint() error paths also returned without
freeing the descriptors. They cannot trigger today, as that function
only fails for an unknown speed, but route them through the same
cleanup so that every exit path releases the descriptors.

Reported-by: syzbot+da5ce3f4a00c8cbe0d4e@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=da5ce3f4a00c8cbe0d4e
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 | 53 +++++++++++++---------------
 1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c
index dca8c6d7640c..6851832ca7d0 100644
--- a/drivers/usb/gadget/function/f_uac2.c
+++ b/drivers/usb/gadget/function/f_uac2.c
@@ -1095,7 +1095,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		in_feature_unit_desc = build_fu_desc(p_channels);
 		if (!in_feature_unit_desc) {
 			ret = -ENOMEM;
-			goto err_free_fu;
+			goto free_fu;
 		}
 	}
 
@@ -1163,7 +1163,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	ret = usb_interface_id(cfg, fn);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		goto err_free_fu;
+		goto free_fu;
 	}
 	iad_desc.bFirstInterface = ret;
 
@@ -1175,7 +1175,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		ret = usb_interface_id(cfg, fn);
 		if (ret < 0) {
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-			goto err_free_fu;
+			goto free_fu;
 		}
 		std_as_out_if0_desc.bInterfaceNumber = ret;
 		std_as_out_if1_desc.bInterfaceNumber = ret;
@@ -1205,7 +1205,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		ret = usb_interface_id(cfg, fn);
 		if (ret < 0) {
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-			goto err_free_fu;
+			goto free_fu;
 		}
 		std_as_in_if0_desc.bInterfaceNumber = ret;
 		std_as_in_if1_desc.bInterfaceNumber = ret;
@@ -1219,7 +1219,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		if (!uac2->int_ep) {
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 			ret = -ENODEV;
-			goto err_free_fu;
+			goto free_fu;
 		}
 
 		std_ac_if_desc.bNumEndpoints = 1;
@@ -1235,42 +1235,42 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 					USB_SPEED_FULL, true);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	ret = set_ep_max_packet_size_bint(dev, uac2_opts, &fs_epout_desc,
 					USB_SPEED_FULL, false);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epin_desc,
 					USB_SPEED_HIGH, true);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	ret = set_ep_max_packet_size_bint(dev, uac2_opts, &hs_epout_desc,
 					USB_SPEED_HIGH, false);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epin_desc,
 					USB_SPEED_SUPER, true);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	ret = set_ep_max_packet_size_bint(dev, uac2_opts, &ss_epout_desc,
 					USB_SPEED_SUPER, false);
 	if (ret < 0) {
 		dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
-		return ret;
+		goto free_fu;
 	}
 
 	if (EPOUT_EN(uac2_opts)) {
@@ -1278,7 +1278,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		if (!agdev->out_ep) {
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 			ret = -ENODEV;
-			goto err_free_fu;
+			goto free_fu;
 		}
 		if (EPOUT_FBACK_IN_EN(uac2_opts)) {
 			agdev->in_ep_fback = usb_ep_autoconfig(gadget,
@@ -1287,7 +1287,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 				dev_err(dev, "%s:%d Error!\n",
 					__func__, __LINE__);
 				ret = -ENODEV;
-				goto err_free_fu;
+				goto free_fu;
 			}
 		}
 	}
@@ -1297,7 +1297,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 		if (!agdev->in_ep) {
 			dev_err(dev, "%s:%d Error!\n", __func__, __LINE__);
 			ret = -ENODEV;
-			goto err_free_fu;
+			goto free_fu;
 		}
 	}
 
@@ -1331,7 +1331,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 	ret = usb_assign_descriptors(fn, fs_audio_desc, hs_audio_desc, ss_audio_desc,
 				     ss_audio_desc);
 	if (ret)
-		goto err_free_fu;
+		goto free_fu;
 
 	agdev->gadget = gadget;
 
@@ -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);
@@ -2305,11 +2307,6 @@ static void afunc_unbind(struct usb_configuration *c, struct usb_function *f)
 	usb_free_all_descriptors(f);
 
 	agdev->gadget = NULL;
-
-	kfree(out_feature_unit_desc);
-	out_feature_unit_desc = NULL;
-	kfree(in_feature_unit_desc);
-	in_feature_unit_desc = NULL;
 }
 
 static struct usb_function *afunc_alloc(struct usb_function_instance *fi)
-- 
2.43.0


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

Thread overview: 7+ 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 ` Vineet Thakran [this message]
2026-12-03 19:54   ` [PATCH 2/4] usb: gadget: f_uac2: fix feature unit descriptor leak 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

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=20261203193217.465889-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 \
    --cc=syzbot+da5ce3f4a00c8cbe0d4e@syzkaller.appspotmail.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