From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382809; b=OGI/rz3IHcmm8CodMtrI5yVURhYo/dT5/kEHKeYhAs6WqbG8RluxxjvdtKEysOsRRmmm oNhCiSgCIqbxHYVl10gmV+m4Asu79CvSK03tNPBa2KikY14cU9fTUUvs/1JuOfa97SpUA NIw68zOFJmIi39JvTAJufeUlhXdYqdkq764IWaL1IhIuqKOwe8Yp8Or3pRA94XEqM0jNq vrleed7Z0xrBfqgUYRiS32ygf8ZFmSaF1k72dtnj6qXncsDmWXIa4VbrGXL7UiI684fRn OviHVBb+i44QZFcEq8TZavAE9D7i7ovSEKvVOOLJw7C6QtisKmNy0R6EX4edulMmAAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382809; 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-Type:Content-Transfer-Encoding; bh=jtoqN13KI0MYxUS/b22rVQPUveSg4JUxIVwf762ffn0=; b=qxc3yya/wbZknPYjrPiOnwx5xln5CHTBTp5PslaXRE9tyA03HlTYwJHxnEfQ1gKFwGAk I8aNoKf6fKKAJhv2iZx7iu9NSybUrSPtB+EasEx6KDFPXks1lWFyQPGO7rExwlVtUKhkt Wx/40/0Y1P13rkXgZTot0ZPXXbeqCvlmfz8+56Omp9hWxkq6CwKJmD7x7Lk/SjOl8PgAb Gsg4k5tiQdflJKi67z4EIyHe9+Fy0xopAbq2iNEklhhGmY1tOxI3rRXPLg75j1j9NKu57 NkDYg9TwBbImjykDpUGohz61cNRme0aYyycaiftdFBDzJLAhy3z4LsKAlXdphHovRVA== 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=GSJ57Kcp; arc=none smtp.remote-ip=209.85.210.175 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 18192606A687 for ; Fri, 4 Dec 2026 11:13:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 18192606A687 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=GSJ57Kcp Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-8ca6a12efd9so459873b3a.1 for ; Fri, 04 Dec 2026 03:13:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796382807; x=1796987607; darn=vger.kernel.org; h=content-transfer-encoding:content-type: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=jtoqN13KI0MYxUS/b22rVQPUveSg4JUxIVwf762ffn0=; b=GSJ57Kcpb/ZRI+pkstQxwfcJvIkL/7MDJamxcMsVnaz/hTeyZVpM/ZRfh1DiXqcIGX xGDVieWTEEPLjlQNf1x5Fv1IAb5GaOJ3OtWU3BjYr9l+U3VE1trUjzd60iQHRVqXGZ8L 6WoPou5m0b2YrgfH3XC6wTUX+QymaLuKe282irPrQ8TRJMSG8jdXWiwh2cspAdY9d4DI foZWrTA8Bk29EX7d3Vlu76CGQQpFjVnLVWF9KetpZiiFBI5UgUZXSRqbmfpK91yRz1fE GQNI3JsJkrv0kswOERUHfKILobLaIErK4fLuci0tvg0NEzH4Z0NF1uMMYM9EW22wQHhQ qlxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796382807; x=1796987607; h=content-transfer-encoding:content-type: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=jtoqN13KI0MYxUS/b22rVQPUveSg4JUxIVwf762ffn0=; b=w9+7a1xq3jJXXngUyptiIsXfAJm0Y0MDNMrXQFrRbFwSQHcyYeyzJycrJWz7Gn5z8y pP2gQi24BRqcbxWyBFwzjKVS/71xPweSp8APnsPcjxM2KFpjp2uAybfrgkaOep6lnRq7 nfOMtYkKINmRyCvX5ZwxHUkkw7i5x2hkXfTQV7XtIWdufdo1mL7e+hEqgLnZXFvvxu+K /ARg/vv493IpQ5zGOLbhx924eg7kE5WPA871gZDm6fOr6+9Bvy6V+WJCWAsWqxjN7Z8M lYkUBpeh24Ai14oAm0Pg2JW+7CO4JuuCdpakk31M+juVZwuCeGvkuSivW9nUTqOrJb1M KSAA== X-Forwarded-Encrypted: i=1; AL+hErFvkJ9kzfYx9wayfEnc5Dk0A+xziJFcXtqYBiQNlXON2dd6GBVedCjw5IgOcRa0EOpnvnbxGhW1443394A=@vger.kernel.org X-Gm-Message-State: APuU4Z/ZylLJVWORwNiexhnV2S5l8An+biLKCXR/cZva59KxiiCe4drg /xolW4nB7F9PJw5Sr4EKYiPdFsG2bE8ZYsq3n7HdoKELL9ofi2OqTKXd X-Gm-Gg: AZFdTZdT96pxHkJ70c4IE+79Zw8egQ+3pDCdPppog3truuZ+XqDhGUURT9RNa7R9kMt kHazsMPQxQlQJQY+32rOUGKSjC+gx/onwds7UsAIhFdf25hoiu+3hv6ymLzwho5PFlcVvB3o7a5 5mUTI6M/Ti6109Rfl7gRMJh0wM13WI2GyDWJzLKRhQ1moV/0YVcAWSSF0/N8A76qvXXeigqhpqw A4aRgkLIvdtAbkBMzpMW3Q9B8/7NmmQzIk9blJkqgindZtYTtZslz8jcKrla+jirtWBKIUFJkYV JrFnFImKw82Id1gIG8lhlycOnmnPiqnwymh9AeSEXl49ejDRn6a6/LXyypp4b6qIczco2m2EcGJ KvKH2EGxoXJtJZrVKbScGuTXogcTMuG450sSQMLQX9aE3jQFIXyT9FWfCWgFte38g3yW7CUooQX QdAWhnvjR2sLjwG4k4NgJdQWoOIBwWw88/QAUwlTW+uznM6ELTR8ATDCxQCvwTBBdS7H7Id6/BD u4PmGRiag0Spvs/mZ1OO46sVKTjEil5CnaZdFcqRoCpeSW+dNp5UnRFJfOvbn2ijkZ6wX6imS0A Lw5hEHeJYqIrcbiUC6jxNgxpBMLM0j2+8jevReTNtEewKOGZdIYZ3tRaFz4HvGAFisfU09D5S6I Lfk9380pwYA8Aal7OU4Mhb/kBSivGHFZ2MFG4BM01y+NzSbj6czRlKLZl6A== X-Received: by 2002:a05:6a00:21d3:b0:899:27b5:214e with SMTP id d2e1a72fcca58-8de2da15e73mr1494010b3a.30.1796382807091; Fri, 04 Dec 2026 03:13:27 -0800 (PST) Received: from ssslab.. (ssslab.skku.edu. [115.145.172.159]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de120fee00sm1329614b3a.18.2026.12.04.03.13.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 03:13:26 -0800 (PST) From: Jaegyun Lee To: gregkh@linuxfoundation.org Cc: Jaegyun Lee , nkapron@google.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v3] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Date: Fri, 4 Dec 2026 20:13:21 +0900 Message-Id: <179638268904.917654.4905537541576241323@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <2026120308-corset-mushroom-abb1@gregkh> References: <2026120308-corset-mushroom-abb1@gregkh> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ffs_ep0_ioctl() snapshots ffs->func without any lock or reference: struct ffs_function *func = ffs->func; ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; ffs_func_revmap_intf() then walks func->interfaces_nums for interfaces_count iterations. ffs_func_unbind() first sets ffs->func = NULL, then after drain_workqueue() kfrees func->eps -- the allocation chunk that also holds interfaces_nums -- and later still writes func->interfaces_nums = NULL. An ioctl that read ffs->func before the NULL write can therefore be inside the revmap wa₩lk while the chunk is freed. John Keeping noted this in 2021: https://lkml.rescloud.iu.edu/2112.0/00724.html The following is the KASAN log: BUG: KASAN: slab-use-after-free in ffs_ep0_ioctl+0x29b/0x300 Read of size 2 at addr ffff888051b4925a by task park/9666 CPU: 1 UID: 0 PID: 9666 Comm: park Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) Call Trace: ffs_ep0_ioctl+0x29b/0x300 __x64_sys_ioctl+0x18f/0x210 do_syscall_64+0x132/0x7c0 Allocated by task 9675: __kmalloc_noprof+0x357/0x810 ffs_func_bind+0xbd1/0x1b00 usb_add_function+0x219/0x8a0 configfs_composite_bind+0xcd5/0x17f0 gadget_bind_driver+0x288/0xc80 Freed by task 9675: kfree+0x2ca/0x6e0 ffs_func_unbind+0x2f2/0x570 purge_configs_funcs+0x283/0x520 configfs_composite_unbind+0xae/0x210 gadget_unbind_driver+0x211/0x520 ... gadget_dev_desc_UDC_store+0x18a/0x2f0 The buggy address is located 602 bytes inside of freed 2048-byte region (kmalloc-2k) Take ffs->eps_lock around the REVMAP snapshot and walk, and do the ffs->func = NULL store in ffs_func_unbind() under the same lock. A walk that started before the store finishes before it, and every later ioctl sees NULL, so the walk can no longer overlap the free of the eps chunk. Reproduced and tested in QEMU (dummy_hcd) only, not on real UDC hardware. Sending from a different address than the earlier versions. Fixes: ddf8abd25994 ("USB: f_fs: the FunctionFS driver") Cc: stable@vger.kernel.org Assisted-by: ZCode:GLM-5.4 Signed-off-by: Jaegyun Lee --- Changes in v3: - Use guard() and scoped_guard(). - Link to v2: https://patch.msgid.link/20261126-ffs-revmap-v2-1-445e86b561dc@g.skku.edu diff --git a/drivers/usb/gadget/function/f_fs.c --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -688,8 +688,8 @@ long ret; if (code == FUNCTIONFS_INTERFACE_REVMAP) { - struct ffs_function *func = ffs->func; - ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; + guard(spinlock_irqsave)(&ffs->eps_lock); + ret = ffs->func ? ffs_func_revmap_intf(ffs->func, value) : -ENODEV; } else if (gadget && gadget->ops->ioctl) { ret = gadget->ops->ioctl(gadget, code, value); } else { @@ -4221,7 +4221,8 @@ if (ffs->func == func) { ffs_func_eps_disable(func); - ffs->func = NULL; + scoped_guard(spinlock_irqsave, &ffs->eps_lock) + ffs->func = NULL; } /* Drain any pending AIO completions */