Linux USB
 help / color / mirror / Atom feed
From: Jaegyun Lee <dreams200260@gmail.com>
To: gregkh@linuxfoundation.org
Cc: Jaegyun Lee <dreams200260@gmail.com>,
	nkapron@google.com, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH v4] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl
Date: Fri,  4 Dec 2026 20:37:50 +0900	[thread overview]
Message-ID: <179638424483.1056529.4365760725322283852@gmail.com> (raw)
In-Reply-To: <sashiko-outbox-249675@kernel.org>

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 do the
ffs->func = NULL store in ffs_func_unbind() under the same lock. A walk
that started before the store finishes before it, and every later
ioctl sees NULL, so the walk can no longer overlap the free of the eps
chunk.

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@gmail.com>
---
Changes in v4:
- Read ffs->func once into a local under the lock. v3 read it twice, and the unlocked NULL store of ffs_func_disable() or ffs_func_set_alt() could land between the two reads (sashiko-bot).
- Link to v3: https://patch.msgid.link/179638268904.917654.4905537541576241323@gmail.com

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -688,8 +688,12 @@
 	long ret;
 
 	if (code == FUNCTIONFS_INTERFACE_REVMAP) {
-		struct ffs_function *func = ffs->func;
-		ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
+		struct ffs_function *func;
+
+		scoped_guard(spinlock_irqsave, &ffs->eps_lock) {
+			func = ffs->func;
+			ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
+		}
 	} else if (gadget && gadget->ops->ioctl) {
 		ret = gadget->ops->ioctl(gadget, code, value);
 	} else {
@@ -4221,7 +4225,8 @@
 
 	if (ffs->func == func) {
 		ffs_func_eps_disable(func);
-		ffs->func = NULL;
+		scoped_guard(spinlock_irqsave, &ffs->eps_lock)
+			ffs->func = NULL;
 	}
 
 	/* Drain any pending AIO completions */

  reply	other threads:[~2026-12-04 11:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  7:43 [PATCH v2] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Jaegyun Lee via B4 Relay
2026-11-26  7:52 ` sashiko-bot
2026-12-03 22:17 ` Greg Kroah-Hartman
2026-12-04 11:13   ` [PATCH v3] " Jaegyun Lee
2026-12-04 11:27     ` sashiko-bot
2026-12-04 11:37       ` Jaegyun Lee [this message]
2026-12-04 11:52         ` [PATCH v4] " sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179638424483.1056529.4365760725322283852@gmail.com \
    --to=dreams200260@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox