* [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_func_unbind
@ 2026-11-20 21:06 Shuangpeng Bai
2026-11-20 21:12 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Shuangpeng Bai @ 2026-11-20 21:06 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel, Shuangpeng Bai, stable
ffs_func_unbind() calls functionfs_unbind(), which drops the reference
to ffs taken in functionfs_bind(). It then keeps using ffs: it takes
ffs->eps_lock to free the endpoint requests.
The superblock holds a separate reference, dropped when FunctionFS is
unmounted. If the gadget is unbound (for example by writing an empty
string to the configfs UDC attribute) while FunctionFS is being
unmounted, ffs_fs_kill_sb() can see FFS_FL_BOUND already cleared, skip
unregister_gadget_item(), and drop its reference. Whichever put comes
last frees ffs, and if that happens before ffs_func_unbind() is done,
the eps_lock access is a use-after-free. KASAN reports it as a
slab-use-after-free in _raw_spin_lock_irqsave() called from
ffs_func_unbind().
Before commit a3058a5d82e2 ("usb: gadget: f_fs: remove redundant
ffs_data_get()"), bind took two references and unbind dropped only one,
so ffs was never freed and the late access was harmless.
Do the endpoint cleanup first and call functionfs_unbind() last, so ffs
is not touched after the reference is dropped.
Fixes: a3058a5d82e2 ("usb: gadget: f_fs: remove redundant ffs_data_get()")
Cc: stable@vger.kernel.org
Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
---
v2: Don't take an extra reference. Instead, move functionfs_unbind() to
the end of ffs_func_unbind(), so ffs is not used after the binding
reference is dropped. (Greg)
v1: https://lore.kernel.org/all/20260815013818.39466-1-shuangpeng.kernel@gmail.com/
drivers/usb/gadget/function/f_fs.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..fe4564d68b18 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -4212,8 +4212,6 @@ static void ffs_func_unbind(struct usb_configuration *c,
drain_workqueue(ffs->io_completion_wq);
ffs_event_add(ffs, FUNCTIONFS_UNBIND);
- if (!--opts->refcnt)
- functionfs_unbind(ffs);
/* cleanup after autoconfig */
spin_lock_irqsave(&func->ffs->eps_lock, flags);
@@ -4236,6 +4234,12 @@ static void ffs_func_unbind(struct usb_configuration *c,
func->function.ssp_descriptors = NULL;
func->interfaces_nums = NULL;
+ /*
+ * This may drop the last reference to ffs, so it must be the
+ * last thing that touches ffs.
+ */
+ if (!--opts->refcnt)
+ functionfs_unbind(ffs);
}
static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_func_unbind
2026-11-20 21:06 [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_func_unbind Shuangpeng Bai
@ 2026-11-20 21:12 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20 21:12 UTC (permalink / raw)
To: Shuangpeng Bai; +Cc: linux-usb
> ffs_func_unbind() calls functionfs_unbind(), which drops the reference
> to ffs taken in functionfs_bind(). It then keeps using ffs: it takes
> ffs->eps_lock to free the endpoint requests.
>
> The superblock holds a separate reference, dropped when FunctionFS is
> unmounted. If the gadget is unbound (for example by writing an empty
> string to the configfs UDC attribute) while FunctionFS is being
> unmounted, ffs_fs_kill_sb() can see FFS_FL_BOUND already cleared, skip
> unregister_gadget_item(), and drop its reference. Whichever put comes
> last frees ffs, and if that happens before ffs_func_unbind() is done,
> the eps_lock access is a use-after-free. KASAN reports it as a
> slab-use-after-free in _raw_spin_lock_irqsave() called from
> ffs_func_unbind().
>
> Before commit a3058a5d82e2 ("usb: gadget: f_fs: remove redundant
> ffs_data_get()"), bind took two references and unbind dropped only one,
> so ffs was never freed and the late access was harmless.
>
> Do the endpoint cleanup first and call functionfs_unbind() last, so ffs
> is not touched after the reference is dropped.
>
> Fixes: a3058a5d82e2 ("usb: gadget: f_fs: remove redundant ffs_data_get()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120210616.2293426-1-shuangpeng.kernel@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 21:12 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 21:06 [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_func_unbind Shuangpeng Bai
2026-11-20 21:12 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox