From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796468952; b=KkuQUAuQWPa3M0Vy0wHU0FzZlQoiIFO6iGxa6nTCeE+m6jnSHtrOfWqfTC4/UO50+CPz sVw6FtbBjKvqkfuRm8s/Jz1ebAZ+G6kb9ArDNPuXk93I/LQcD5motEQr2J5t1SedL3rXU IsCiHfNT7xsXa/sUKNoxZZ/Oc1BbTanh7MmFaxn/H6xScJ6lmOTX2SYgIMlsv+DATnqCY gvUNJZMEoxlUYqRGPEneHb+PASKkODPXHiHsr50rFYn+JCFbNErGln9Kvug07CsXRVtIU g/0dB24nMQMLc9yawH5o6vK8WQLolovy8uOi0TxI1x+rIVpRm9agOSs/Lf3tPwpWuZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796468952; 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=ja4ifDzAFAjpAKIVaP/uSveL521tQyHvIMnncfgnp1MkX4kGSEuliYKj0fmZweOc3SWv fDhCnsVSCS2JxoU8stlu+iX2A3vFgB1a56kGHsmdGf4Zgj5dnViMlxi3Kga26L8/wH0SM nlepBwDZSbKAbulwMvD9OQKSzKx6XCDgh4liboWm1Ulrb0VkKhphLi4SkWYwFoDdsKFmS W/wZnlBxPAcRkupyrGboRTuJK6YmJDewCW9FqJCBTo9M5Zb7H8ER4gLSC+NCSqKwBUFNI l2P68rxC+UiVqLjbQQZk8MNKrqJBxkk4yUOmzItR+YB49l5O7do3r8hmaximkxEFGBw== 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="nka/HRLs"; arc=none smtp.remote-ip=74.125.82.170 Received: from mail-dy1-f170.google.com (mail-dy1-f170.google.com [74.125.82.170]) (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 0B2FA606A332 for ; Sat, 5 Dec 2026 11:09:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0B2FA606A332 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=nka/HRLs Received: by mail-dy1-f170.google.com with SMTP id 5a478bee46e88-3866a784546so2481878eec.0 for ; Sat, 05 Dec 2026 03:09:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796468951; x=1797073751; 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=nka/HRLsjdqCvHU+V8WWwhTaE+G+OKxStWU07GBisFRUjiJUAODLv4mFqaaCUqjx4/ yL032RYN3M0zWyhpMxKwl28/ag3AZDV0jgKJo8aZQYxjBAQ/DDzexwxK2DQ4DzYEuDmK l6uMhilr/7NXX/HYBFIJeB/uT0CX9C0zT/9PpPMt3CwOxybdZ4OlHCVxwqzFQq3W88WQ uLIPIPp8/lI9+90aKYUn0Rqhtd2xMtZzzSg751Rm3mdJ82Cxg2ZYkoSL8cauktg/4JIx xqLzNwFOlKwIdbzp7mz64HcCeGoMy/9Isi3pnnxqatwOlkHfPljDPzKBoQ82E6EXAoPM tnsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796468951; x=1797073751; 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=yAIZqiYnHFXB/qD35N+I/vGNXPowODmgMbWMQ6HVAgU33I4e5I/FE6FFmI0yX3sS73 ZYe57vC91UxhW6Fuhk5d171iyzipq/FGfFJLcyn2gky08UIkAJl4Ro47R8R2fthberHZ HcmLghzslrrqF859CQaEePkn78c91WhZ80F7iEnIN9GjEhYMPv6DVMakXYuOr/eW2p1m Nv5IJyRF47r8rCPtY6n2ex9t4RotyQssY37IeRJUfojWa5cs6FeBl2Dz523fwVrmFB/7 V0HH2AsqKACXWyJ33XgspLHd70mC4E2i6r25uPYSl50mLpNOfsXCqf1dSRIw03y3EsHi t5bA== X-Forwarded-Encrypted: i=1; AL+hErFMO1l/UYkZ+5izH8Lr+WDimX7PwT60xzdF0u1qipTpoD7tvD6+LIUG0M7JXIxwi/phdeOcAJtyALk=@vger.kernel.org X-Gm-Message-State: AB4q+uEnnbHoXCugiTIY89EaMNRXuXkh80pD1vkg34Itfs4b+3jThzH/ IPyHgEIwRR8SYKNOISgkMYn5xIe97whnS5KTWYKClHWjnNMy7e36mB6CvukzWiug X-Gm-Gg: AZFdTZe2r9SvCSa80fYD/ueFvqVdXhdJYE/1xxcHORB818sUPAZxl1uBiDSEZG51UFv diK90OhvsWet/mVa00SM17nPFqtXTj4yVl1YsTuz83Tuhivl8Mm6118Y7AYvOo6lAAjk1lSVkQk ZbnVFzjB81bC9eWXVxbfsN6sLIm+X5HZQfpovEEnpwQ9rAn2YgCECeaTdVe0HKP3yoSv/9eqhN7 3JWvDWa61/IRQOx/muK8uDy+GtexkOOoBY+R+mesb27zCtlxpnPpNyRM4EU9DDajmplA8c68qlB Oj3Fe6nDOsW2V0xJyp0zHGFeL37TZ8lv/K4fhX825Do6Xk4GJf32i6z6syAznauqZY7Gw4LN+Hn 874N4Y28zT5kPOaTbDxZn6RWEUQtLpMfyBc/fgD35jfmIvCGSKhwt2zthyY2JTQ4PQLRCSL3WEi yDSOCoLeix4JIkoBgNdH6yqtZQi76ts9vAY6BNp+6WW+NPLo6nfME/3KiwNKtbJE2rR+beizTzx R1//qAwjL7mKPDVoJ6R2eQ0JCFADz+Het20Mkwb+88pOkHQTvBLokS7V9IxITjEjDqN36oVRu9r s4rz0mZFUY9k1QcPWTocBclshlZ33IPZ8JRMg2R2TzY= X-Received: by 2002:a05:693c:83da:20b0:385:cbc5:7eeb with SMTP id 5a478bee46e88-387520dc6a0mr4529699eec.15.1796468951074; Sat, 05 Dec 2026 03:09:11 -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.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 03:09:10 -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 2/6] usb: gadget: f_uac2: serialize bind with a mutex Date: Sat, 5 Dec 2026 11:08:27 +0000 Message-ID: <20261205110831.204588-3-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 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