From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796356320; b=ss2e/tFhLO1e/MeQJ87IUsAPT8IGgGmsbGmQ7khzCi6Wpkj76IM+2SelY1BwLWFJoObS pzadxQhb7IIsgAITGW60RqZtKjv7/znkrtD1j/tc1gT2Z0yAZwDPHeBvgHSURiWAWwX/+ OKTulpuwzbeaagu4JfDnnhQgnuziXi9wccjFSBbTAyHWCn2ZNWXSCcoKMxpl5CEFmUFxY pd1uiTMs6Fu1Ykay7oFS5Gf2biM79wxjPdFEkzTOiDQ2xd6Ih6MeXMIkbMtau0dgMds4r i8xFlCbWEI2vASuFR76ec7LuwQSlCvGLLl8cSMjmCq6peqq2/pBCQRPa1RLyiGwiEpQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796356320; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=MGFF4qZKJ8ryUMYElqadTHlrVPjnPijnMpmgSf4tgHo=; b=LcECyY+E8uzY0f17f3kTNMcy/oJHty1M9W8SaaD4HMYHujBYWO9vCs124vpOP6vZhrO5 ibhpeWZETZUzSUGA2zyq+OLlRSj/8gkZZyO0MMehHINE3NiMm1zPRt3rD5rd1ECTBxI+s nM6Cj/L7acnboG+hh5Ft0mEqbO/8p9LjJ8jG7PTXRAqyZjRYgwCKxaOz5uSGcmD7qwvDb Km7fgF5wk1TtcS40Zd4QI11dn6srViHQQMauyTWeKXRinqUjuaZviinoZQ9ubpjvBiOot yA0H1PQJTYqCslyCSOjmGQt+Nd6oiR4jN47TOxsfD/4maNDdPlfZ9Syiouulgy8ZOzQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RaEQJ2pC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91EE1606A322; Fri, 4 Dec 2026 03:52:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=RaEQJ2pC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74F1F1F000C2; Fri, 4 Dec 2026 03:51:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796356320; bh=MGFF4qZKJ8ryUMYElqadTHlrVPjnPijnMpmgSf4tgHo=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=RaEQJ2pC7EaG/IAg9j7UUyGLUgUcEQ215LzrAudkZDzl/K0f0sVdhU9SdfTx6GOzq 58I/pyFZW+uK3s1wV25hAiv1GyZY7jDfpM7Lf9okEzyCWiwkTuuVPf8ZO5OpOwbRag ziHAaLZk8CWrB+cRWNmXraUBKV5mDzUqMBimtMcY= Date: Thu, 3 Dec 2026 23:17:17 +0100 From: Greg Kroah-Hartman To: dreams200260@g.skku.edu Cc: Neill Kapron , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v2] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Message-ID: <2026120308-corset-mushroom-abb1@gregkh> References: <20261126-ffs-revmap-v2-1-445e86b561dc@g.skku.edu> 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=us-ascii Content-Disposition: inline In-Reply-To: <20261126-ffs-revmap-v2-1-445e86b561dc@g.skku.edu> On Thu, Nov 26, 2026 at 04:43:40PM +0900, Jaegyun Lee via B4 Relay wrote: > From: Jaegyun Lee > > 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 walk 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. > > 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 v2: > - Keep clearing ffs->func in its original place in ffs_func_unbind(), > before functionfs_unbind(), and only do that store under eps_lock; > leave the eps free path unchanged (Sashiko review). > - Link to v1: https://patch.msgid.link/20261126-ffs-revmap-v1-1-e1322f6fa290@g.skku.edu > --- > drivers/usb/gadget/function/f_fs.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c > index c64a268e98a43..df55c2cba6101 100644 > --- a/drivers/usb/gadget/function/f_fs.c > +++ b/drivers/usb/gadget/function/f_fs.c > @@ -687,8 +687,13 @@ static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) > long ret; > > if (code == FUNCTIONFS_INTERFACE_REVMAP) { > - struct ffs_function *func = ffs->func; > + struct ffs_function *func; > + unsigned long flags; > + > + spin_lock_irqsave(&ffs->eps_lock, flags); guard() please