From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796556841; b=PH9EtJ05pxc/mj4OdsHQ5OK9oiISstH7X+sEerEwJmQgkCmZuSZTZ8ygv9WH8PaR44kM jD9f5duYipAx02fC10wx63jaql8PVJE+8BFs2bJRTEuAViWsA9VisztV/LJNFOO+tTi/7 kPBrh4t5Ide1UggKQvqWiC2hu5dkzgXmihgRxUiTy5S8AUL5jQOc3FkXo4a7PqW9eW0Oi 6/wen3oO/hs0fK6EMr2XO+bII31ZXGK7e2CeEqCf+ZYpLpK1c3YXa2KPcQGskGfe5H3Lf mMcN1MFkjK6uz0Kz9xQHGRagjNm25BIu0U0dD8i2oG3zun7sPyJHLEo0ABCW2qLLqBw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796556841; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=QeZl0nOUGRxEaW04/9jYU/7HwqR8mue6iECaeUwjxbc=; b=RzwsU24RgbgaYV4IzeDWtNXo/8PvxYxsb2OQTMfCzV7Am4qKWheytcuU4kr0EtUxb5Li 9oXxRgkJfjP3CJvxgP3e+JNefFHaeHhd3PgZsXHb/1LEnZiS96x8R9/clycGE6P7vVasI l1JT6N1YA97mT/6AaR1yQ8/+xAo851YRCpIwBZEWiuEOuoGa/+D0dEPPmERvmQgVBZEuq lgoB1P8ah4Et6exvREfLNegNKw2COfnIUoyjvcputWb0J2qwuSJknMpQ2Dx2y45l3ufHh Udrd3PfQyB/qt+ygE2AU5D6txKYqntjJPkPFgEYzC4dE2edCC8tne+oa1ocwzbKueKA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=voV1iAS7; arc=none smtp.remote-ip=74.125.82.49 Received: from mail-dl1-f49.google.com (mail-dl1-f49.google.com [74.125.82.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFA4F606A31F for ; Sun, 6 Dec 2026 11:34:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CFA4F606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=voV1iAS7 Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-1b5d5f0ba39so2748062c88.0 for ; Sun, 06 Dec 2026 03:34:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796556840; x=1797161640; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QeZl0nOUGRxEaW04/9jYU/7HwqR8mue6iECaeUwjxbc=; b=voV1iAS71caLhRd02ilmkXab0sUPUHY3KmuFNKBWsx4HzXt85qgUsDvyeLBwZwgXyY mWrjOxuek3bNG1D6QxzUkp5MTQGetfTAjcPmoZ6YyEkrVEB6/JknQlLoxZBsXlPG+fEF wg3UN9BYk10jYV4zoaKLfZAHAsvB/sh0TS6Jegsml+u25vU9Gxzvrpp7cfhXrIiBV71h jFhbh/CuLErc/79WVQHIpIDAG7xuyZIl3VLX/uIsjwF16m2YI4otISkuuQnpD67nn9Cz QSUaTQwrRaGFwt6NBQPWREE+mqR0CBbOKIzCSpz41DuwUCKTUIZLyDImqg+9VrhBb4Ta otLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796556840; x=1797161640; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QeZl0nOUGRxEaW04/9jYU/7HwqR8mue6iECaeUwjxbc=; b=EoPQxE0W4MnDxvlpW4/FWC0Se3aEypuGgaWTs+An+54U45KoQKoXV0yoO69vZtu9+H pI/8YhQrGbb3Hbw/TaQbY9SzNudozP8beIimkBvkS9CTJ2Gjxo3e4tupVpnP9+beXcAz QniT7QNxSoCuAT74bq1UByrzeonGp5IH76fBDGjLO2JQ7r1M+OX0k/sVnjaaz1p7Uz8G PNsVhga+DeMQVBh6oeJPHivs+2nAn1K363W1mJ+Z1yuDSP0oUh/nv94b4Qz2zGwPQrnR oFjSLus7ZfVVHiU8I4bA2HXgnBSHuYEcpjo4vCPVWKr1lsHMjA4iQggdtZcj0AMo6zA0 MRVg== X-Forwarded-Encrypted: i=1; AL+hErHT8Qp106YQ0govroOO+oLq0vz2IjmHYxdNFaCyu9hGhQZUWdMA048efY9Pcb6l09VNfmNSn1kAQZ8=@vger.kernel.org X-Gm-Message-State: APuU4Z/d3pbYSTBa0d+CYI19QsM9x1bbmyYxI8PyX9v4oFXp4ACaDtje XqFzdr38BdfFb23+fFtfELUCAjpdZ/LxCSzjwiPXLTdEW88zUqY8fHsk X-Gm-Gg: AZFdTZeCCTLPzUqTJaUJ94Vtg/67KxxN2qVKxEiDGdiPyADwVnGt7vNVMPV2etuX+OC wdaIt7rFEtRjnAGEcJoOZAbczU1nXf90R41K6G1J7wzonBxyFVGZKEJotwndDpugTtUZQuopM+O ZPxEB6vKb0BnoOu8a0v7XieIC2/iHL5Y2Nu3GGn955G5OtemEiK8ibBQcJKEcpNddcKEMQ5tg+/ xkMyoQi/eHHqdfox51bDyXhS9F5nRM3vEGV5vDOWYGlrBkChQvw2Z6EgDNpjYdtI0wLj/2bNajh GetoO+I0JvbGgVzicdaLRmvC2kQIRAncnMS85fDGTB9dyPSMPWCAYcPLTNgaonuuzot3muLTN1Q FUElEULV+L3jn6xpfkXEwxGlZgmmSs0MwwRFB2c4apzIt9EAZMWgzIYvTezQ3kEqWUkAX9k18zr lbeUpxtDl4okutSWpcP9kkkmULiNRZuq6+Ad/0j/fzlGgPeAZylDB0oLUcuyIdtNrP9WhU+9tVa BRApx8vmhd67BHcVr8vPJotqtnGROP0/ec23mTanWallhIf6HI8+LfGyqPUb4s4zSWTXmYAdpXu HjwQn/MdIE6xwALZnAWGAFdqYQld3Xcds6CzQgXq X-Received: by 2002:a05:7022:b19c:20b0:191:444:425 with SMTP id a92af1059eb24-1b5d705ed06mr7159190c88.2.1796556839833; Sun, 06 Dec 2026 03:33:59 -0800 (PST) Received: from vineet-kernel.. ([106.51.245.14]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d6480265sm26512724c88.14.2026.12.06.03.33.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 03:33:59 -0800 (PST) From: Vineet Thakran To: Greg Kroah-Hartman , Neill Kapron Cc: Ruslan Bilovol , Pavel Hofman , linux-usb@vger.kernel.org, Vineet Thakran , stable@vger.kernel.org Subject: [PATCH v3 6/6] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak Date: Sun, 6 Dec 2026 11:32:55 +0000 Message-ID: <20261206113255.91550-7-vineet789thakran@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261206113255.91550-1-vineet789thakran@gmail.com> References: <20261206113255.91550-1-vineet789thakran@gmail.com> X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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") Fixes: b8fb6db6cb04 ("usb: f_uac1: adds support for SS and SSP") Cc: stable@vger.kernel.org Signed-off-by: Vineet Thakran --- 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