Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl
@ 2026-11-26  6:40 Jaegyun Lee via B4 Relay
  2026-11-26  7:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jaegyun Lee via B4 Relay @ 2026-11-26  6:40 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Neill Kapron
  Cc: linux-usb, linux-kernel, stable, Jaegyun Lee

From: Jaegyun Lee <dreams200260@g.skku.edu>

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 move the
ffs->func = NULL write and the free of the eps chunk in
ffs_func_unbind() under the same lock.

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 <dreams200260@g.skku.edu>
---
 drivers/usb/gadget/function/f_fs.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a43..c824d1d5ce571 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 iflags;
+
+		spin_lock_irqsave(&ffs->eps_lock, iflags);
+		func = ffs->func;
 		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
+		spin_unlock_irqrestore(&ffs->eps_lock, iflags);
 	} else if (gadget && gadget->ops->ioctl) {
 		ret = gadget->ops->ioctl(gadget, code, value);
 	} else {
@@ -4203,10 +4208,8 @@ static void ffs_func_unbind(struct usb_configuration *c,
 	unsigned count = ffs->eps_count;
 	unsigned long flags;
 
-	if (ffs->func == func) {
+	if (ffs->func == func)
 		ffs_func_eps_disable(func);
-		ffs->func = NULL;
-	}
 
 	/* Drain any pending AIO completions */
 	drain_workqueue(ffs->io_completion_wq);
@@ -4217,13 +4220,14 @@ static void ffs_func_unbind(struct usb_configuration *c,
 
 	/* cleanup after autoconfig */
 	spin_lock_irqsave(&func->ffs->eps_lock, flags);
+	if (ffs->func == func)
+		ffs->func = NULL;
 	while (count--) {
 		if (ep->ep && ep->req)
 			usb_ep_free_request(ep->ep, ep->req);
 		ep->req = NULL;
 		++ep;
 	}
-	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
 	kfree(func->eps);
 	func->eps = NULL;
 	/*
@@ -4235,6 +4239,7 @@ static void ffs_func_unbind(struct usb_configuration *c,
 	func->function.ss_descriptors = NULL;
 	func->function.ssp_descriptors = NULL;
 	func->interfaces_nums = NULL;
+	spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
 
 }
 

---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261126-ffs-revmap-78010561eb60

Best regards,
--  
Jaegyun Lee <dreams200260@g.skku.edu>



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl
  2026-11-26  6:40 [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Jaegyun Lee via B4 Relay
@ 2026-11-26  7:03 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26  7:03 UTC (permalink / raw)
  To: Jaegyun Lee; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: f_fs: UAF and stale ffs->func in
  ffs_func_unbind() after unbind
--

commit 54f606e52beb80500efc1407df54d86b84f48d2a
Author: Jaegyun Lee <dreams200260@g.skku.edu>

usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl

This commit protects FUNCTIONFS_INTERFACE_REVMAP and the teardown of
ffs->func and func->eps in ffs_func_unbind() with ffs->eps_lock.

> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index c64a268e98a43..c824d1d5ce571 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
[ ... ]
> @@ -4203,10 +4208,8 @@ static void ffs_func_unbind(struct usb_configuration *c,
>  	unsigned count = ffs->eps_count;
>  	unsigned long flags;
>  
> -	if (ffs->func == func) {
> +	if (ffs->func == func)
>  		ffs_func_eps_disable(func);
> -		ffs->func = NULL;
> -	}
>  
>  	/* Drain any pending AIO completions */
>  	drain_workqueue(ffs->io_completion_wq);
> @@ -4217,13 +4220,14 @@ static void ffs_func_unbind(struct usb_configuration *c,
>  
>  	/* cleanup after autoconfig */
>  	spin_lock_irqsave(&func->ffs->eps_lock, flags);
> +	if (ffs->func == func)
> +		ffs->func = NULL;

[Severity: Medium]
Can moving ffs->func = NULL after functionfs_unbind(ffs) cause a
use-after-free on ffs or an out-of-bounds read in ffs_func_revmap_intf()?

functionfs_unbind() clears FFS_FL_BOUND and drops the gadget's reference
with ffs_data_put(ffs).  Once FFS_FL_BOUND is clear, ffs_closed() skips
unregister_gadget_item() and no longer serializes on gi->lock against the
running unbind, so a concurrent unmount in ffs_fs_kill_sb() can drop the
superblock reference and free ffs before ffs_func_unbind() takes
func->ffs->eps_lock:

CPU0                                  CPU1
ffs_func_unbind()
  ffs_event_add(..., FUNCTIONFS_UNBIND)
  functionfs_unbind(ffs)
    clear_bit(FFS_FL_BOUND, &ffs->flags)
    ffs_data_put(ffs)
                                      ffs_fs_kill_sb()
                                        ffs_data_reset(ffs)
                                          ...
                                            ffs_closed()
                                              /* FFS_FL_BOUND is clear */
                                        ffs_data_put(ffs)  <-- frees ffs
  spin_lock_irqsave(&func->ffs->eps_lock)  <-- use-after-free
  if (ffs->func == func)
    ffs->func = NULL;

In addition, leaving ffs->func set across functionfs_unbind() means
ffs_data_reset() can reset ffs->state to FFS_READ_DESCRIPTORS while
ffs->func still points to the old function.  If userspace writes new
descriptors with a larger ffs->interfaces_count and calls
FUNCTIONFS_INTERFACE_REVMAP before ffs_func_unbind() acquires eps_lock,
ffs_func_revmap_intf() walks the old func->interfaces_nums array using
the new count.

Performing the eps_lock cleanup block (clearing ffs->func, freeing
ep->req, and freeing func->eps) before ffs_event_add() and
functionfs_unbind() would keep ffs pinned and FFS_FL_BOUND set until
func teardown is complete.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-ffs-revmap-v1-1-e1322f6fa290@g.skku.edu?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26  7:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  6:40 [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Jaegyun Lee via B4 Relay
2026-11-26  7:03 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox