Skip to contentSkip to conversation

usb: gadget: f_fs: fix use-after-free in ffs_func_unbind

PATCH New Shuangpeng Bai 2 participants 2 messages Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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.

Fixes a3058a5d82e2 ("usb: gadget: f_fs: remove redundant ffs_data_get()")

---
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
index c64a268e98a4..fe4564d68b18 100644
@@ -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
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

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.

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()")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1