From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796382808; b=Edx5xc2eKEQlF6OAPOF/eFcwjRwgo252F5chz5bbMlxvlibT7czGnvUJBaUD8dwiffv0 embLZh+2Tk+NtEZ0+/DwyglXtUxVKubJilV7jzPvWyWCukfxSZyh723Sf/4DFNKSkxJLD RZPyqV6SuS15EoyMc8wkyVq1QM7m8QpzR7Ya6zt7SMPgCjMhZA7F5J8KYDBlxem6NrE5S i1RAwQ6HeopFRr+EDKAVZ2O+BhyvqurFUAMW8mEUU7Fevw7hqF4xGFWIYCer/Eah3/NOE tFFa9Jt73o1AaLqwJC0ovTFaYLwQ2y2QEMLvoGKisJus91pA+L824nRSna3G50Zy4IQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382808; 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=vUaPs2DZ7YEOOQKU2/yqNeToXjtgx1hbiGcrE/V62hVgs4HoB8o4C1kZNV7WCKU/g+W7 vDazu8UR+cMEPMEcQIXLJ1K5QmsmJ2ZpUsvN6hPttqca3TXg+9BelUulyDXAzcwOidvyf DjYe66maADlmAGUYFxVYtVzRjo+zSsmVZwhStKvW+EqB3LwN9qvO6AFXMb8MdoGJ/ZsmK MEsEacDsTIPcn43OPG8sn5WUj4Y4ZHdCYrWdU/AtRkjY/TZAFRefVpOLYLcMbHvzZsYv5 /Bd86Ec4njHmaotGHEx6NETw5jEj8SvCDDWSZIozU7Vhx1yDr94xTyX00MpBe7bilfA== 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.182 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 0EF2F606A686 for ; Fri, 4 Dec 2026 11:13:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0EF2F606A686 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-f182.google.com with SMTP id d2e1a72fcca58-8a06e79e152so427056b3a.0 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=vjEvgRP0kjI6Exmjz/1CCT75ou1SMS4bfOunwovCtx4/1QNCaQeWbh7PRG3AiJKKIS 3HZSmUkUqIYnceOTu8n54pvUEEi8+0bL4Zzk17f2NN1x9LMdlqviGeB+ssUh3ZSijnko aA4K61+NvJZdtr6r0nnUPQnRln7G6LYXBaztpxyHyXIX/CNfaSAheUeLf0bCnxrz7LAo 0ojbJaWCOMDJ8GnqTpEsAl7qg+jD4CQYCvDIIW9PLqF/RNRTIGxTbr7OfWld9PWy/FwR fBnE8rKMllubPTuEuVyk8R25g7imJVxe3Xv2xGrU88+PRNmm2I2bi+s1hwos5Q2yZx74 w++g== X-Forwarded-Encrypted: i=1; AL+hErEBZvv7lqZ31jlLXaCO6UmLAU9iswLRfaq0AX7PDIpcmazEAC5zaZLCXDyd74gFzjzX3VkRfBWaEeI=@vger.kernel.org X-Gm-Message-State: APuU4Z9czLFWKbht/ACsIJEwasQICPJw9h0o5Q06PQar6W8rhsP2K2wf NO9N38lof3CJxwE5j1U/4pxMQaMHsYpvLe/Y3zbTPnAUJ2h4tOD8EuQ1 X-Gm-Gg: AZFdTZcCzvp7afg+Sn5kriYeLEFN4EM56PfmeF+fdC+IiApTSRvaKRPHg3oRvNzOYEB YFb8YzmUG9JInsbEoUBKNinNbBSkUSJwLxqfkblXfK2FB6uRTwq2aeyzhp4/1Ul+urbYJniG+D2 rkuRltzJneNSzIpm+e2IAywydWzppmXacjRn6c8280E9j7ZpaGaSUTb5/goFHI192ruqdoLOYV6 UgFnGbMCymqP5JOVEFuObcQ+NCEXfxQa1EXnn+S7W23r31RCt1cpFyVjA64tXFGFShybAd8m/wO wYPzr/E9PGpBHhPjSCF9jbG3+zV0agkP8jd9bA/8pOEBHWuRkbbLmss4olNPp0Bhv/kxhXgnIyP rPHb/c5QeMAyXg54Wq+SWGNKuZHT2NFE2yKhuJONfPfo/NPDsftHolDumm5UFrp4Ian2ZTUlhdW HmGXKFvmK6dkqdqad7c3RQZ98+GdTSpxLy2MYNLfkm+hrXcZoC7BKuocHnHQ1M8/E4d7SHAZvRs XzUnOuf23TIhVKm0zaEek9Uh/wGuxsQLtzzRd5tSzug0NHVceZPGFMyZR6J1x2brgaVsXtShk9D 3FV3DX/DjsclG199/G8K4tW5pTZNEU/ZkSxpByAuPpHyFNe1xDnW9eLdVSt9TvNgBAwPpFS5+3i kEFsWP6gRlCHrTdkWl+1BDWn/mgXx7vL1uIpe7Xr5i1YX53dE3v0PceukeA== 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-usb@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 */