usb: gadget: f_fs: fix use-after-free in ffs_func_unbind
Subsystems (2)
PATCH v2 1/1 usb: gadget: f_fs: fix use-after-free in ffs_func_unbind
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.
---
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/[email protected]/
drivers/usb/gadget/function/f_fs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_fs.c +6 -2
@@ -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)
Signature · 1 line
2.43.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 18 lines
ffs_func_unbind()callsfunctionfs_unbind(), which drops the reference to ffs taken infunctionfs_bind(). It then keeps using ffs: it takesffs->eps_lockto 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 seeFFS_FL_BOUNDalready cleared, skipunregister_gadget_item(), and drop its reference. Whichever put comes last frees ffs, and if that happens beforeffs_func_unbind()is done, theeps_lockaccess is a use-after-free. KASAN reports it as a slab-use-after-free in_raw_spin_lock_irqsave()called fromffs_func_unbind().Before commit
a3058a5d82e2("usb: gadget:f_fs: remove redundantffs_data_get()"), bind took two references and unbind dropped only one, so ffs was never freed and the late access was harmless.
Quoted 6 lines
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()")Signed-off-by Shuangpeng Bai <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1