Skip to contentSkip to conversation

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

PATCH Superseded Shuangpeng Bai 2 participants 3 messages Aug 15 – Nov 20

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_fs: fix use-after-free in ffs_func_unbind

ffs_func_unbind() continues to access ffs after calling functionfs_unbind(), including taking ffs->eps_lock for endpoint cleanup. However, functionfs_unbind() drops the binding reference to ffs.

A concurrent FunctionFS unmount can drop the independent superblock reference. Either put can then release the final reference and free ffs before ffs_func_unbind() finishes its endpoint cleanup.

Take a temporary ffs reference for the duration of ffs_func_unbind() and drop it only after all post-unbind cleanup is complete.

Fixes a3058a5d82e2 ("usb: gadget: f_fs: remove redundant ffs_data_get()")
---
 drivers/usb/gadget/function/f_fs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/function/f_fs.c +4 -0
index 44218be1e676..8acad4564b6c 100644
@@ -4089,6 +4089,9 @@ static void ffs_func_unbind(struct usb_configuration *c,
 	unsigned count = ffs->eps_count;
 	unsigned long flags;
 
+	/* Keep ffs alive until all post-unbind cleanup is complete. */
+	ffs_data_get(ffs);
+
 	if (ffs->func == func) {
 		ffs_func_eps_disable(func);
 		ffs->func = NULL;
@@ -4122,6 +4125,7 @@ static void ffs_func_unbind(struct usb_configuration *c,
 	func->function.ssp_descriptors = NULL;
 	func->interfaces_nums = NULL;
 
+	ffs_data_put(ffs);
 }
 
 static struct usb_function *ffs_alloc(struct usb_function_instance *fi)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 23 lines · Shuangpeng Bai

ffs_func_unbind() continues to access ffs after calling functionfs_unbind(), including taking ffs->eps_lock for endpoint cleanup. However, functionfs_unbind() drops the binding reference to ffs.

A concurrent FunctionFS unmount can drop the independent superblock reference. Either put can then release the final reference and free ffs before ffs_func_unbind() finishes its endpoint cleanup.

Take a temporary ffs reference for the duration of ffs_func_unbind() and drop it only after all post-unbind cleanup is complete.

Fixes a3058a5d82e2 ("usb: gadget: f_fs: remove redundant ffs_data_get()")
---
 drivers/usb/gadget/function/f_fs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/usb/gadget/function/f_fs.c +0 -0
index 44218be1e676..8acad4564b6c 100644

drivers/usb/gadget/function/f_fs.c static void ffs_func_unbind(struct usb_configuration *c, · line 4093

Quoted 5 lines
drivers/usb/gadget/function/f_fs.c +2 -0
@@ -4089,6 +4089,9 @@ static void ffs_func_unbind(struct usb_configuration *c,
 	unsigned count = ffs->eps_count;
 	unsigned long flags;
 
+	/* Keep ffs alive until all post-unbind cleanup is complete. */
+	ffs_data_get(ffs);

Are you sure this is ok? This means you now have a double reference.

thanks,

greg k-h

author
#2 lore raw
Quoted 7 lines · Greg KH

On Fri, Aug 14, 2026 at 09:38:18PM -0400, Shuangpeng Bai wrote:

Are you sure this is ok? This means you now have a double reference.

The get is paired with a put at the end of the function, but taking and dropping an extra reference isn't the cleanest way to fix this. The real problem is the ordering: ffs_func_unbind() keeps using ffs after functionfs_unbind() drops the reference. I'll send a v2 that moves functionfs_unbind() to the end of ffs_func_unbind() instead, so no extra reference is needed.

Best,
Shuangpeng

Quoted 4 lines

thanks,

greg k-h