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 1/4] usb: gadget: f_uac2: use per-instance feature unit IDs in control requests
Date: Thu,  3 Dec 2026 19:32:14 +0000	[thread overview]
Message-ID: <20261203193217.465889-2-vineet789thakran@gmail.com> (raw)
In-Reply-To: <20261203193217.465889-1-vineet789thakran@gmail.com>

The class-specific control request handlers look up the feature unit
IDs through the file-scope in_feature_unit_desc/out_feature_unit_desc
pointers. These pointers are shared by every f_uac2 instance, but each
instance allocates its own descriptors in afunc_bind() and
afunc_unbind() frees them and sets the pointers to NULL.

So once one instance unbinds, every other instance that is still bound
dereferences a NULL pointer on the next feature unit control request
from the host. With two gadgets, each with a uac2 function on its own
UDC, unbinding the first gadget and connecting the second to a host
gives:

  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000003
  pc : afunc_setup+0x2f8/0x568

afunc_bind() already stores each unit ID in agdev->params.p_fu.id and
agdev->params.c_fu.id, which live as long as the instance. Make the
USB_IN_FU_ID()/USB_OUT_FU_ID() macros read those per-instance values
instead, and fill them directly from the descriptors in afunc_bind().

This also means nothing uses the feature unit descriptors after bind,
which the following patch relies on.

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 | 30 ++++++++++++++--------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c
index a5f3d4d6c34b..dca8c6d7640c 100644
--- a/drivers/usb/gadget/function/f_uac2.c
+++ b/drivers/usb/gadget/function/f_uac2.c
@@ -31,8 +31,8 @@
  */
 #define USB_OUT_CLK_ID	(out_clk_src_desc.bClockID)
 #define USB_IN_CLK_ID	(in_clk_src_desc.bClockID)
-#define USB_OUT_FU_ID	(out_feature_unit_desc->bUnitID)
-#define USB_IN_FU_ID	(in_feature_unit_desc->bUnitID)
+#define USB_OUT_FU_ID(_agdev)	((_agdev)->params.c_fu.id)
+#define USB_IN_FU_ID(_agdev)	((_agdev)->params.p_fu.id)
 
 #define CONTROL_ABSENT	0
 #define CONTROL_RDONLY	1
@@ -1340,7 +1340,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 			sizeof(agdev->params.p_srates));
 	agdev->params.p_ssize = uac2_opts->p_ssize;
 	if (FUIN_EN(uac2_opts)) {
-		agdev->params.p_fu.id = USB_IN_FU_ID;
+		agdev->params.p_fu.id = in_feature_unit_desc->bUnitID;
 		agdev->params.p_fu.mute_present = uac2_opts->p_mute_present;
 		agdev->params.p_fu.volume_present = uac2_opts->p_volume_present;
 		agdev->params.p_fu.volume_min = uac2_opts->p_volume_min;
@@ -1352,7 +1352,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn)
 			sizeof(agdev->params.c_srates));
 	agdev->params.c_ssize = uac2_opts->c_ssize;
 	if (FUOUT_EN(uac2_opts)) {
-		agdev->params.c_fu.id = USB_OUT_FU_ID;
+		agdev->params.c_fu.id = out_feature_unit_desc->bUnitID;
 		agdev->params.c_fu.mute_present = uac2_opts->c_mute_present;
 		agdev->params.c_fu.volume_present = uac2_opts->c_volume_present;
 		agdev->params.c_fu.volume_min = uac2_opts->c_volume_min;
@@ -1586,11 +1586,11 @@ in_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 				"%s:%d control_selector=%d TODO!\n",
 				__func__, __LINE__, control_selector);
 		}
-	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
-			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
+	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev))) ||
+			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID(agdev)))) {
 		unsigned int is_playback = 0;
 
-		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
+		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev)))
 			is_playback = 1;
 
 		if (control_selector == UAC_FU_MUTE) {
@@ -1675,11 +1675,11 @@ in_rq_range(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 				"%s:%d control_selector=%d TODO!\n",
 				__func__, __LINE__, control_selector);
 		}
-	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
-			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
+	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev))) ||
+			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID(agdev)))) {
 		unsigned int is_playback = 0;
 
-		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
+		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev)))
 			is_playback = 1;
 
 		if (control_selector == UAC_FU_VOLUME) {
@@ -1765,11 +1765,11 @@ out_rq_cur_complete(struct usb_ep *ep, struct usb_request *req)
 		return;
 	}
 
-	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
-		(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
+	if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev))) ||
+		(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID(agdev)))) {
 		unsigned int is_playback = 0;
 
-		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID))
+		if (FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev)))
 			is_playback = 1;
 
 		if (control_selector == UAC_FU_MUTE) {
@@ -1819,8 +1819,8 @@ out_rq_cur(struct usb_function *fn, const struct usb_ctrlrequest *cr)
 			req->complete = uac2_cs_control_sam_freq;
 			return w_length;
 		}
-	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID)) ||
-			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID))) {
+	} else if ((FUIN_EN(opts) && (entity_id == USB_IN_FU_ID(agdev))) ||
+			(FUOUT_EN(opts) && (entity_id == USB_OUT_FU_ID(agdev)))) {
 		memcpy(&uac2->setup_cr, cr, sizeof(*cr));
 		req->context = agdev;
 		req->complete = out_rq_cur_complete;
-- 
2.43.0


  reply	other threads:[~2026-12-03 19:40 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 ` Vineet Thakran [this message]
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
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-2-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