Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Shuangpeng Bai <shuangpeng.kernel@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_func_unbind
Date: Fri, 20 Nov 2026 16:06:16 -0500	[thread overview]
Message-ID: <20261120210616.2293426-1-shuangpeng.kernel@gmail.com> (raw)

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()")
Cc: stable@vger.kernel.org
Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
---
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/20260815013818.39466-1-shuangpeng.kernel@gmail.com/

 drivers/usb/gadget/function/f_fs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..fe4564d68b18 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -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)
-- 
2.43.0


                 reply	other threads:[~2026-11-20 21:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261120210616.2293426-1-shuangpeng.kernel@gmail.com \
    --to=shuangpeng.kernel@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --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