From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796468973; b=3M85gMN9NoQkaLKrg0M7XDgeOI0Wrzm2rKCKjGUKJ0Vw6JDVTCVT71SO6OTBw2Kd1o48 IUbnDCr+0WMQbuPD/FL29uWagXSZDLW8XQFaCZQSH1VDGSEbzMJ5C/ci6YK2K06dwIrFg aCwH1CGi0AIctTNG6leR7IgsWsyy8iEa2QDHHdakBdWH4SsIAivGMPacGQHgHFwrlA5Vf 6YX3c5SzSNLffsuh02bt1DMwWYTwxSITmrCVo7XKSDDzfKdXtUhkPw4SB5ECoFf2u6XQq lEzzAX15FDYEg5Cr+XW/HMFQTcfofO9jgutbq2cePNC2RANsCme416Ls0Yx54bpH1Fg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796468973; 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=w+VIJ78erRn8NL1aV1XYBI3BA5Odg7iQtWhniVbyu8w=; b=Mc+H8MHJLvASuge0DVfKM5djFRC38pPxdJtv7A1zJxguqVWVaOVgX9Aa/9tpZ8i294EC 0bs4CAmEeuMiRyX/EF9v9+3ewFkSnUdTNYghIpSKTb5dhn1OhiBKF3G2vyHpahQIth2l4 WraIqt/6UPzZB2yjj/zP7aynhK43BKyiL9tBUXcsmQS8raLuB7+iIRMtDjV+ulkDh8HnT /Yp34O5M4Q2icY5x0aAvT3N5+8auvA8jEox8dBCUDYmJDZUpLF8emrS9usN3BXFYHNCNJ LX5yZIpSQSKIdbm794Be0pllx39K715c/coimduy7xoAnOL7UFKiROKIG1YhhIOYHFg== 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=Y8VYNp3J; arc=none smtp.remote-ip=74.125.82.169 Received: from mail-dy1-f169.google.com (mail-dy1-f169.google.com [74.125.82.169]) (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 DE1F6606A32F for ; Sat, 5 Dec 2026 11:09:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DE1F6606A32F 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=Y8VYNp3J Received: by mail-dy1-f169.google.com with SMTP id 5a478bee46e88-385c497bf94so4274716eec.1 for ; Sat, 05 Dec 2026 03:09:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796468972; x=1797073772; 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=w+VIJ78erRn8NL1aV1XYBI3BA5Odg7iQtWhniVbyu8w=; b=Y8VYNp3JKmR37vbHL6GjsIybW5EYzSm3LaqZcahIeZiRDM39CHFvTZ5M5ulotJmO5M rn8pdaaH7BcNVJ1ygWiezO3YsyDDPnXMFm6R8Xh2SQEFnxG5TreqXEtROReEbX0TEENv LEGBXqL3ZNGdDrcBkZZE/l92HLEh0L4kCQWKZTm+lIIl4Uv+iv+yuynMscq6T3OY3bxc jLuPCg6MqcacRJJGcpgxDtNlkmNQRSb7UNUdogI5Q5SF5QfUvVY2v3ClI/Rt+qJgzeSK S8bQZ5rwZ/kMPRJfSHPXmqXvZUj6oLSOTghXWg/+4PQGuhR6EMu901nJHymLv3Cpu2R4 LtYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796468972; x=1797073772; 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=w+VIJ78erRn8NL1aV1XYBI3BA5Odg7iQtWhniVbyu8w=; b=JG+E3LgGv1fOxVmhoTdkfLWy31TmKBcBYB5Dx6w2Rnx1G24abEMIQPAsfzTd9ojUAr ABmjsIDW8ueOi+ECmId6SQRvL4EHtP2s+/sA1utjncVQeBnP2f9Zwd9sk+hnNylj2FEV XapGsJdL9087dwKvF1WXZrt3qXdw04nWUx4lgLZxBwxDtdGvH9Im5yrZFOR1Xrqd9Axe RFhQd1Jl/1p6NATbCzkr5RJOkYqQmMgJe8bxNc4Ht29ndlIkkyxsqQScVLUsDdNaK6uS 7DY4/Ifai512VRJWc6uh8jj6+ko5aLHaGdv4QjRFnGHPP5jQ6h+5BjseQMsr0UyFogyY F7bQ== X-Forwarded-Encrypted: i=1; AL+hErHp2Nx+lt6POD4YtGDZ04ZVawahhR6dXq1Hxq1fYwEkmLuDczf0dt02pM5Fiex/CIdSJjh1ZQJkGIE=@vger.kernel.org X-Gm-Message-State: AB4q+uFjrGBQvIQ1ZqwKsbrhzauntMMPyOqesRLBv+uH/mjMzaNsFol4 ZFTA9C0Kg8mwuaTKtROG0AqMcI6R/AO9UeXnymonb/Zk0yhRXTd4FJ1l X-Gm-Gg: AZFdTZdhXeX2oz1dKnHz/6ba4iYt4e1imCt2KCQhS09J4hlolB6vujcHcoR0txG07U0 BWBnFBu3U5G+ueI6LO9ZMQKAZEzAyuURoH92DpWLVoYR/MLDUR2omqp1LFmIt3kUtFyjPZF2afZ 4cR/4lFcgJ0Xbs8zTE21EalDfWbhwdAL76mwrxYlcYzPX7VNkYIi1Eb+bH32j8Ij7Pbb9Z+ptBa mDXu9GpH/qPj0yRQfTMp/+lbVng5Gdok5rwoCQrFMnWN9CpWvdwUaCW+h5r+4KQ01Y3Odr/sJWB mIAAyfN2P//XpANjMX24ilTtwXl2HXleMc/UedNRJc76bcM41HkQXqBlmKPTTv4B/IZmFM1T1zx jX7TpOklGjw28Oh5xq1IXQuZtOmsMbHjmNGoOCrtkgl9QSSDZptQem8Hb6Q+wTGXqquobPzOqNl CoFwQmrXkGWzChC8s7pyScNUbj6VEEUlMIvZIvMMYxwZT8HPpYz8MSurlXb64b3DsTAO9n/Fw6c +WfyIlVtlxdgmlae1p1kp4ZV22GCXiW5j95DmVJmbIzz0JsgA6e9MlvYQJyItc4ZohoZpq8aFXX EZ4wCjQo+tGsoa0tckC1/JUctbuIthAB6aOD2IRSf7e6Cz00HI4RdY42gEF6eWRs X-Received: by 2002:a05:7301:279f:b0:387:4ea2:b362 with SMTP id 5a478bee46e88-387521de23amr4783421eec.30.1796468971860; Sat, 05 Dec 2026 03:09:31 -0800 (PST) Received: from vineet-kernel.. ([115.242.144.166]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875226fa94sm16929678eec.29.2026.12.05.03.09.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 03:09:31 -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 v2 6/6] usb: gadget: f_uac1: fix feature unit and AC header descriptor leak Date: Sat, 5 Dec 2026 11:08:31 +0000 Message-ID: <20261205110831.204588-7-vineet789thakran@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261205110831.204588-1-vineet789thakran@gmail.com> References: <20261205110831.204588-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") 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