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=1796556835; b=XMWd6wZJVfBPfBtHgmcQgZ3N50GANZgbl8xtTzLGuYcIyQS5vJHtCNS6VQapdp85oH2s ri36hu7dneNf5nMKLNl8zq0C/2kZnrVdO0BDIU1QPtAxtTSZXp2qCGylWRYyws+3Ut8w+ Daabe5reuHQU+X4SAgNCRuXUSUZcU1xn30toUiox8NmQOlTrku5o6UDtteIlP/T9uAnbx Lh2vhnS0w6Z6fPJ01RUxR3MuMz/pWVSl8DFfKViFZb+DTUq6+wX7i1JsCHMya/6esTM9b 0xBy2Dg+EoNSYJ4QlPeChD7nxrqbBLK0lx9ckiIXlcxisDuyuwV3oTBxWtX5vhVIChA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796556835; 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=uB/gjZhnIVSzG+2vTKLecMfJkArkIkjmfh18O1r5Ug8=; b=qRP13OvxX2jmNq5ml7lpRT6p1NiTFxG0Tuwvp2PgtLvUCH+eO0A9Jvg+kWShZ+6SmJng Xo3kupjucfqZUMkH9ebFllmIfUa0PI+aiWzmsTRsuyB2qs5Do+PXXbEPTvX9QKY37myQa +DdfW8rIRDvn0zTpEOXHRRbG9RALP4lGwcB3Uqtg7mT/ctq7/SLIjV0FQwENb1wCD4+ZQ fcrPPyEw8nSrp4rqv1RdBs3yQjXbQBkn3SrxwcQmz8SoqwAWCld1wh2YA7YuNi1HH2Uni iF06YIcXzozsydAcWkeO05AgxYG++jcIURgJDQq7NrbHJolLAyf7UeEb56VlBFhEDcg== 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=h+tOCgEc; 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 4255F606A321 for ; Sun, 6 Dec 2026 11:33:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4255F606A321 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=h+tOCgEc Received: by mail-dl1-f49.google.com with SMTP id a92af1059eb24-15ea896ddc8so3165620c88.0 for ; Sun, 06 Dec 2026 03:33:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796556834; x=1797161634; 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=uB/gjZhnIVSzG+2vTKLecMfJkArkIkjmfh18O1r5Ug8=; b=h+tOCgEcX5neZ6JbBxWd8H22idEr9o9ZXjjq5/HhexpOjwnn3v/fKH7iaBpv6DstGP T10o68N3qeES8t69JG86ACgML7PUSKwkKRHfbgXSlzCt8wBbOkpFKpf4J9SKjWLrVW0V 2WYRLFdrjKd2Mk89lI7WzskDHGpCmXIG2q6aF1CUlM/zMIlJ/KZQN8RfLFScw66NpqYP NXZFT2CAsR2qdm+BbMwnZzWAWBn+UJQbI+abPfX+Lmwnri01yQC/u1uETtRUbBYKDOKK zz/wk8nCzy3YvO9NM17ISR3jmkYW6YOxUI0SaGGkb+XNVbQIbtdq/rwo/YS7pvcuashk gi/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796556834; x=1797161634; 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=uB/gjZhnIVSzG+2vTKLecMfJkArkIkjmfh18O1r5Ug8=; b=i9BkzUZM/pVhngn4kji7GRAiU0w2N1hWCvr2oGqciZl9qIARDLE7h3OmEnzB4Qi1LA c/NvaWFdYGUDnXyKb+cnt97m1+8MDMCczzlOuHcS99nOn4WzJd6u05RmrS8gAeBl4SPX TarHKd5z5mQ/w66BNRj5YBSUAi44nObRrRUqzuJNqvZKhEM5hJ+94klitXvn6Go56bul itb93EQuTBU9zQMJj5ZA8576IGAh8xuMJ/FisgYGDbppTPHtKZ+UxLF/41WurNO7VgUR 90k0Rstu1Q+n+B6xngvSCebgrMZrsu4leTN8tL+3YiTpHW+6cUGfCUO7Ki2OsFlWdSa1 Gxgw== X-Forwarded-Encrypted: i=1; AL+hErGJ37Q6l4y7OMcZ70ulHYe+jOCFhsPs31hmIBo5N8YWpHHZP/voydhyieqvdu7VFPq9dW3aaO2/GuI=@vger.kernel.org X-Gm-Message-State: APuU4Z+HQp6rTTP2O7spgOOLixN37rbS4yO6S5lhRZ3QK9XNXkDeKEnv ChDuKMI3o7V4rzOgFX8rifO7jQhmdEBFe7+hHKluOLU9tjvXHW+H3QIx X-Gm-Gg: AZFdTZeHtwiPhXeHOZS4Qo3YgCDzbRli8DQA3vl77YG+79VYZc2B1AZZTosvyG7FIvA 9o0RwQEUxpmtuXL+HuJOYJ8MXupjFsL//SXHySqIbpPbEsloU7aWbFx2/bQgYJ+hHqPo24G360/ J2Mgvn6C3e6JeWST6hSek53HHzmUnspZGn1hTQ330rsEmC32r9B6PZH0qgltbadeJ0Udf3Qm0Ks MwWiimUddx7lrrv98ylO+/hReFv+KrUiptNM3pOyth54TaIaHDGE71r3yun6qcxjlNkMrNom0ki 9z8mYkSqfWOAZoakXLyXQjZ68CKQAJ7VTGN7JVBUDLGvTFXeH7k9zUZb6grVC44TKzrfhVOmM74 VwqyxGMX2xxPs0JVaHGsvK1QV7dInxeQ8F2nFTL0lZp/AVCtpIjIW+WWgd0FJj/LFxRaEURPnnB PCCVyN9DNiSG61I3GttB/ZcdeL3qyq1uFhuLKGOckgLOo4EqoiGLaSSVp6aFaKgNfagmsRqpMLR v/55isqdwrPqy3x0xbLyM4fhcR1Fza1fGUhlk/1b0BeX+o6agEIYyFHf1xU31TxQyvPplwdOdmV I/x/ON2hN8EajTfaRNQ3M/hzIDVBu+8h3qA5a/CL X-Received: by 2002:a05:701b:2715:b0:1b4:caae:3324 with SMTP id a92af1059eb24-1b5d70e6a8bmr7439671c88.21.1796556834430; Sun, 06 Dec 2026 03:33:54 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 03:33:53 -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 5/6] usb: gadget: f_uac1: serialize bind with a mutex Date: Sun, 6 Dec 2026 11:32:54 +0000 Message-ID: <20261206113255.91550-6-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() sets up descriptors that are shared by all f_uac1 instances: setup_descriptor() rebuilds the static f_audio_desc array, and the AC header and feature unit descriptors live in the file-scope ac_header_desc, in_feature_unit_desc and out_feature_unit_desc pointers, which f_audio_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 f_audio_bind() at the same time, or one can run f_audio_bind() while the other runs f_audio_unbind(). Binding and unbinding two such gadgets in parallel loops gives: BUG: KASAN: slab-use-after-free in f_audio_bind+0x2898/0x28f4 Add a file-scope mutex, as f_midi2 does with f_midi2_desc_mutex, and hold it in f_audio_bind() from the first write to the shared descriptors until it returns, and around the frees in f_audio_unbind(). 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 | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/usb/gadget/function/f_uac1.c b/drivers/usb/gadget/function/f_uac1.c index 6a5c00d3a8e6..012f8d4be0c0 100644 --- a/drivers/usb/gadget/function/f_uac1.c +++ b/drivers/usb/gadget/function/f_uac1.c @@ -16,6 +16,7 @@ #include #include +#include #include "u_audio.h" #include "u_uac1.h" @@ -126,6 +127,9 @@ static struct uac1_output_terminal_descriptor usb_in_ot_desc = { 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); + /* AC IN Interrupt Endpoint */ static struct usb_endpoint_descriptor ac_int_ep_desc = { .bLength = USB_DT_ENDPOINT_SIZE, @@ -1249,6 +1253,8 @@ static int f_audio_bind(struct usb_configuration *c, struct usb_function *f) audio_opts = container_of(f->fi, struct f_uac1_opts, func_inst); + guard(mutex)(&f_uac1_desc_mutex); + strings_uac1[STR_AC_IF].s = audio_opts->function_name; strings_uac1[STR_USB_OUT_IT].s = audio_opts->c_it_name; @@ -1819,13 +1825,15 @@ static void f_audio_unbind(struct usb_configuration *c, struct usb_function *f) g_audio_cleanup(audio); usb_free_all_descriptors(f); - kfree(out_feature_unit_desc); - out_feature_unit_desc = NULL; - kfree(in_feature_unit_desc); - in_feature_unit_desc = NULL; + 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; + kfree(ac_header_desc); + ac_header_desc = NULL; + } audio->gadget = NULL; } -- 2.43.0