From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796556819; b=11mxJhqqXf75DHZJOVWPL4ndbVrtxYmO0oUsi7LOyciJ82UZ0TgoF19pj9z31lF2sUjH Rsx4hQ8Mb52/GDHd1ggKAksMDbefppBaNmE19T5p+bRf+3FQxZ7IYbov7a0eyX21zNqn5 JHTUREDNyZW0EUxX1WWhcB6UUikNizRMfWREAzkSmsYGwgDo2vxKTsj4HLVg91wyWn21s DdIMmOcqS5CVZoH4yTtq7jBEEjSWwKMkvLZVKgUqIrKNPSQG0QR0+tH45mO+UGFFprQpL z0H+k87WHWlmQJuX4r5V3Y7FcZzaSI67w4ay5KcFmJ1xskeKnhLWcp31LAyiTjXY1MQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796556819; 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=ooqGpx4b5t/yQLrKrO+0BQTG2qorjnat82scWjOCTpg=; b=oETJ195ghsmCD6qQls1OKqy4h4LocZpsyCxfZeNwEYyUzu69vcJgKDWfnIAAlQLuNxdv 5rGjAbfg7blhNH/pd2gTgU0qnzcpShvAqP2/VkCiXdBJwAfusYFs3VborTIK6+6NehzOP e6OGXq4gjv2olYy9rmnsjsN3QXcU2SLUhr0ejijX3j1TNv79ogLwrl50Ux4qOfgS+RbAp kiki2rUFNEYWsb/MP2XNo+RNlbWa/sY0rOVsVdfR0ZT2T2Gudwl7D+3xB897aPIsm2KvE v1rfPZFiS5OCewF/5fBC4qdFDB90I5SktuVb2P/9PXW2Hl9ARMIAsjaa6fAg+2wykUw== 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=sMKTMCwV; arc=none smtp.remote-ip=74.125.82.44 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (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 A9437606A31F for ; Sun, 6 Dec 2026 11:33:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A9437606A31F 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=sMKTMCwV Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-1b546061d58so5550070c88.1 for ; Sun, 06 Dec 2026 03:33:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796556818; x=1797161618; 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=ooqGpx4b5t/yQLrKrO+0BQTG2qorjnat82scWjOCTpg=; b=sMKTMCwVGpIMsEk2chNSNXX5kSUo1A8NTXbZV6UELqfXuBinsp576RaV9mQ4uiN1PE O7mka3kBwt7jGo8DmTTTzbqeaQc1GHAKNTH2kf7svcCTfBM6fpoJnil2RZMepW8LvO7H uSaNfwVQ7XQglEZ+3mStaMT8d0VuI5zY+3MpyRO45JdmIbUPlUeT0SUkXxSYkF2j8T11 JwD6IlPgc/NHLl4aNfJ1oOM581WDELAZJO8CZSokG2aRKaJzz69UY9mHV1ujrh5Tcqhg CHyD2sLLd/PONB2boicWvGheOqBSV1lB/HOVOHTlobFmhI/oEdJWXBf5o3y627CN/Tnf MnpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796556818; x=1797161618; 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=ooqGpx4b5t/yQLrKrO+0BQTG2qorjnat82scWjOCTpg=; b=dR/qRnax0Roajk/sRSE2bb31HDAq+OuAbmiMJyUu1AnJnEJiwqHtmlmqxV7FOB4TPc 8Wy2UoMieJHoR49oWtexH96jWdNjhcigmKgOw8HvS5lYLQIdZjgK/O7iCxxa3Ma8lDrp IcA/oDyFSqO3BAkO5jTKphh4qMFH1MkbAghHcZvUIcAptUXfa/BfZj3OqVBAn6Gabraf OezhFcybZlsYm+8Qbx3G45/khKQanpM9tgibF0yFkc39a0iq7pZ5ldZodi0a602qSSK6 iRdBS8ACePq27nVLG2jxp4rNf/WrBmjJgnDUn3Wg210WW9G1MozXin1DSUVQmUy4x0EE N/Bw== X-Forwarded-Encrypted: i=1; AL+hErHLQz/JPurY0qy8k0fT504ydIKDmvsKYjQNwdLZKzme5rke9j5rQXS+CSJLtScjuvG5597mFCSaTQo=@vger.kernel.org X-Gm-Message-State: APuU4Z/qfkVu4hZWOZbD4NPBjsElmQaGx0tfvuT4/UXdxELv3C0WIOs2 J8kc+LAgxO1BbGlFwh1csyqdMfnn56h1dWrtf4xlAEOB5C0CA9UDHJxf X-Gm-Gg: AZFdTZfGIfA4q8meFs5bZjB3km9grgKLq/rTJCEAeQAJeqXl8JBvpPDuBR3OXECBPWL xKAt3YKA8Sc/7XgLDJSiP6N0Ux6FzTsozn08ojAR/u1C69jPAg2oOuqzM1O0OBe9bTDGllNFno7 CVcY/1h0jy3H2j5cdtnAr5693tC4N/JL+5wwQQO2NCHbZoZr6ktiOW+fRw5MGb09NlvQQCSwDfS VH/k86H0MsF9JuGSm/QBcKi3jttILXpgvKDdHuvPq7rbQzMTEF4PTOjybFqBAHSKJpcRtmHmWqH Ug36VwyYYhDs12xEIFP85Nn9FjRySJXIQLZDDMSZcdVbbws57bPZt6MFL+NbKZ1x/aGnvgQJhSc ez0t9FInVAAqGVu5TmiEiW2US9ACDF4VdavXoC9omkAkdpTskkJmd4uGNt6lr0Qmst9uerk0uYO /wjub/Y7nP9MoBSn/hEpT6/1woHfwwHZY/I8FIWSgaWQOzvui5qjZgp3es8gPyt5oTQmHuXyGYb 3Oni/VQZIpM0vQ4gferV+gFUa2xfekgX7eiLzu+YTqe8VNyVy9/tjfArAO0g9lGlck7IaKOrp8G JNFRIwoWeXpyvv/b1Y52+TgecXn/8viA7N8BN8SieA== X-Received: by 2002:a05:7022:1113:b0:1b3:af3a:e784 with SMTP id a92af1059eb24-1b5d711236dmr7497433c88.32.1796556817682; Sun, 06 Dec 2026 03:33:37 -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.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 03:33:37 -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 2/6] usb: gadget: f_uac2: serialize bind with a mutex Date: Sun, 6 Dec 2026 11:32:51 +0000 Message-ID: <20261206113255.91550-3-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 afunc_bind() sets up descriptors that are shared by all f_uac2 instances: setup_descriptor() rebuilds the static fs/hs/ss descriptor arrays, and the feature unit descriptors live in the file-scope in_feature_unit_desc/out_feature_unit_desc pointers, which afunc_unbind() frees. Nothing serializes this between instances. configfs only holds the per-gadget lock while binding, and the UDC core drops udc_lock before calling the gadget driver's bind(), so two gadgets bound to different UDCs can run afunc_bind() at the same time, or one can run afunc_bind() while the other runs afunc_unbind(). Binding and unbinding two such gadgets in parallel loops gives: BUG: KASAN: double-free in afunc_unbind+0x4c/0x70 Add a file-scope mutex, as f_midi2 does with f_midi2_desc_mutex, and hold it in afunc_bind() from the first write to the shared descriptors until it returns, and around the frees in afunc_unbind(). Fixes: 3fa4eaa6c082 ("usb: gadget: f_uac2: disable IN/OUT ep if unused") Fixes: eaf6cbe09920 ("usb: gadget: f_uac2: add volume and mute support") Cc: stable@vger.kernel.org Signed-off-by: Vineet Thakran --- drivers/usb/gadget/function/f_uac2.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index dca8c6d7640c..7a3179ad8f97 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "u_audio.h" @@ -246,6 +247,9 @@ static struct uac2_output_terminal_descriptor io_out_ot_desc = { static struct uac2_feature_unit_descriptor *in_feature_unit_desc; static struct uac2_feature_unit_descriptor *out_feature_unit_desc; +/* Serializes access to the descriptors shared by all instances */ +static DEFINE_MUTEX(f_uac2_desc_mutex); + static struct uac2_ac_header_descriptor ac_hdr_desc = { .bLength = sizeof ac_hdr_desc, .bDescriptorType = USB_DT_CS_INTERFACE, @@ -1059,6 +1063,8 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) if (ret) return ret; + guard(mutex)(&f_uac2_desc_mutex); + strings_fn[STR_ASSOC].s = uac2_opts->function_name; strings_fn[STR_IF_CTRL].s = uac2_opts->if_ctrl_name; strings_fn[STR_CLKSRC_IN].s = uac2_opts->clksrc_in_name; @@ -2306,6 +2312,7 @@ static void afunc_unbind(struct usb_configuration *c, struct usb_function *f) agdev->gadget = NULL; + guard(mutex)(&f_uac2_desc_mutex); kfree(out_feature_unit_desc); out_feature_unit_desc = NULL; kfree(in_feature_unit_desc); -- 2.43.0