mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst()
@ 2026-11-20 23:07 Palla Raghunath
  2026-12-03 22:16 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Palla Raghunath @ 2026-11-20 23:07 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-usb, Neill Kapron, Michał Nazarewicz, Shuah Khan,
	Brigham Campbell, linux-kernel-mentees, linux-kernel,
	raghunathpalla.0209, stable, syzbot+6227549bd2c8a1ec8ba0

When a function instance is removed through configfs, ffs_free_inst()
first calls ffs_release_dev(), which takes ffs_dev_lock, detaches the
dev from its mount and drops the lock again. Only then does it retake
the lock and free the dev with _ffs_free_dev().

Between those two steps the dev is still on the ffs_devices list. If
someone mounts functionfs with the same instance name right then,
ffs_acquire_dev() finds the dev, marks it mounted and points the new
ffs_data at it. The dev is freed a moment later, and when that mount
is torn down, ffs_closed() writes to freed memory. syzbot hit this:

  BUG: KASAN: slab-use-after-free in ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline]
  BUG: KASAN: slab-use-after-free in ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305
  Write of size 1 at addr ffff8880237b1e4a by task syz-executor/11910
  Call Trace:
   ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline]
   ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305
   ffs_data_reset drivers/usb/gadget/function/f_fs.c:2336 [inline]
   ffs_fs_kill_sb+0x84/0x370 drivers/usb/gadget/function/f_fs.c:2180
   deactivate_locked_super+0xbe/0x110 fs/super.c:586
   cleanup_mnt+0x3d3/0x460 fs/namespace.c:1329
  ...
  Freed by task 17408:
   kfree+0x1c5/0x650 mm/slub.c:6923
   _ffs_free_dev drivers/usb/gadget/function/f_fs.c:4336 [inline]
   ffs_free_inst+0x233/0x2c0 drivers/usb/gadget/function/f_fs.c:4152
   usb_put_function_instance+0x95/0xc0 drivers/usb/gadget/functions.c:77
   config_item_release+0x13a/0x2d0 fs/configfs/item.c:137
   configfs_rmdir+0x885/0x950 fs/configfs/dir.c:1580

Fix it by doing the release and the free while holding the lock once.
To allow that, move the body of ffs_release_dev() into a new
_ffs_release_dev() that expects ffs_lock to be held already, annotated
with __must_hold() so the compiler can check that, and keep
ffs_release_dev() as a wrapper for ffs_data_put(). A racing mount now
either gets the dev before ffs_free_inst() runs, in which case the
release clears its pointer, or doesn't find the dev at all.

I was able to reproduce this in QEMU with a small program that keeps
creating and removing an ffs function directory in configfs while a
second thread mounts and unmounts functionfs with the same name. On an
unpatched kernel it hit the same KASAN report within about ten
minutes. With this patch applied, the same program ran for 30 minutes
without any problem.

Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references")
Cc: stable@vger.kernel.org
Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0
Acked-by: Michał Nazarewicz <mina86@mina86.com>
Signed-off-by: Palla Raghunath <raghunathpalla.0209@gmail.com>
---
v2:
 - Annotate _ffs_release_dev() with __must_hold(&ffs_lock) so the
   locking rule is checked by the compiler (Greg)
 - Fix the comment to name ffs_lock, the actual mutex
 - Add Michał's Acked-by

v1: https://lore.kernel.org/all/20261006204202.59604-1-raghunathpalla.0209@gmail.com/

 drivers/usb/gadget/function/f_fs.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..996fa0767de2 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -288,6 +288,8 @@ static struct ffs_dev *_ffs_find_dev(const char *name);
 static struct ffs_dev *_ffs_alloc_dev(void);
 static void _ffs_free_dev(struct ffs_dev *dev);
 static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data);
+static void _ffs_release_dev(struct ffs_dev *ffs_dev)
+	__must_hold(&ffs_lock);
 static void ffs_release_dev(struct ffs_dev *ffs_dev);
 static int ffs_ready(struct ffs_data *ffs);
 static void ffs_closed(struct ffs_data *ffs);
@@ -4147,8 +4149,8 @@ static void ffs_free_inst(struct usb_function_instance *f)
 	struct f_fs_opts *opts;
 
 	opts = to_f_fs_opts(f);
-	ffs_release_dev(opts->dev);
 	ffs_dev_lock();
+	_ffs_release_dev(opts->dev);
 	_ffs_free_dev(opts->dev);
 	ffs_dev_unlock();
 	kfree(opts);
@@ -4363,10 +4365,12 @@ static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data)
 	return ret;
 }
 
-static void ffs_release_dev(struct ffs_dev *ffs_dev)
+/*
+ * Same as ffs_release_dev(), for callers that already hold ffs_lock.
+ */
+static void _ffs_release_dev(struct ffs_dev *ffs_dev)
+	__must_hold(&ffs_lock)
 {
-	ffs_dev_lock();
-
 	if (ffs_dev && ffs_dev->mounted) {
 		ffs_dev->mounted = false;
 		if (ffs_dev->ffs_data) {
@@ -4377,7 +4381,12 @@ static void ffs_release_dev(struct ffs_dev *ffs_dev)
 		if (ffs_dev->ffs_release_dev_callback)
 			ffs_dev->ffs_release_dev_callback(ffs_dev);
 	}
+}
 
+static void ffs_release_dev(struct ffs_dev *ffs_dev)
+{
+	ffs_dev_lock();
+	_ffs_release_dev(ffs_dev);
 	ffs_dev_unlock();
 }
 
-- 
2.34.1


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

* Re: [PATCH v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst()
  2026-11-20 23:07 [PATCH v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst() Palla Raghunath
@ 2026-12-03 22:16 ` Greg Kroah-Hartman
  2026-12-04  5:50   ` Palla Raghunath
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-03 22:16 UTC (permalink / raw)
  To: Palla Raghunath
  Cc: linux-usb, Neill Kapron, Michał Nazarewicz, Shuah Khan,
	Brigham Campbell, linux-kernel-mentees, linux-kernel, stable,
	syzbot+6227549bd2c8a1ec8ba0

On Fri, Nov 20, 2026 at 11:07:11PM +0000, Palla Raghunath wrote:
> When a function instance is removed through configfs, ffs_free_inst()
> first calls ffs_release_dev(), which takes ffs_dev_lock, detaches the
> dev from its mount and drops the lock again. Only then does it retake
> the lock and free the dev with _ffs_free_dev().
> 
> Between those two steps the dev is still on the ffs_devices list. If
> someone mounts functionfs with the same instance name right then,
> ffs_acquire_dev() finds the dev, marks it mounted and points the new
> ffs_data at it. The dev is freed a moment later, and when that mount
> is torn down, ffs_closed() writes to freed memory. syzbot hit this:
> 
>   BUG: KASAN: slab-use-after-free in ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline]
>   BUG: KASAN: slab-use-after-free in ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305
>   Write of size 1 at addr ffff8880237b1e4a by task syz-executor/11910
>   Call Trace:
>    ffs_closed drivers/usb/gadget/function/f_fs.c:4427 [inline]
>    ffs_data_clear+0x543/0x5b0 drivers/usb/gadget/function/f_fs.c:2305
>    ffs_data_reset drivers/usb/gadget/function/f_fs.c:2336 [inline]
>    ffs_fs_kill_sb+0x84/0x370 drivers/usb/gadget/function/f_fs.c:2180
>    deactivate_locked_super+0xbe/0x110 fs/super.c:586
>    cleanup_mnt+0x3d3/0x460 fs/namespace.c:1329
>   ...
>   Freed by task 17408:
>    kfree+0x1c5/0x650 mm/slub.c:6923
>    _ffs_free_dev drivers/usb/gadget/function/f_fs.c:4336 [inline]
>    ffs_free_inst+0x233/0x2c0 drivers/usb/gadget/function/f_fs.c:4152
>    usb_put_function_instance+0x95/0xc0 drivers/usb/gadget/functions.c:77
>    config_item_release+0x13a/0x2d0 fs/configfs/item.c:137
>    configfs_rmdir+0x885/0x950 fs/configfs/dir.c:1580
> 
> Fix it by doing the release and the free while holding the lock once.
> To allow that, move the body of ffs_release_dev() into a new
> _ffs_release_dev() that expects ffs_lock to be held already, annotated
> with __must_hold() so the compiler can check that, and keep
> ffs_release_dev() as a wrapper for ffs_data_put(). A racing mount now
> either gets the dev before ffs_free_inst() runs, in which case the
> release clears its pointer, or doesn't find the dev at all.
> 
> I was able to reproduce this in QEMU with a small program that keeps
> creating and removing an ffs function directory in configfs while a
> second thread mounts and unmounts functionfs with the same name. On an
> unpatched kernel it hit the same KASAN report within about ten
> minutes. With this patch applied, the same program ran for 30 minutes
> without any problem.
> 
> Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references")
> Cc: stable@vger.kernel.org
> Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0
> Acked-by: Michał Nazarewicz <mina86@mina86.com>
> Signed-off-by: Palla Raghunath <raghunathpalla.0209@gmail.com>
> ---
> v2:
>  - Annotate _ffs_release_dev() with __must_hold(&ffs_lock) so the
>    locking rule is checked by the compiler (Greg)
>  - Fix the comment to name ffs_lock, the actual mutex
>  - Add Michał's Acked-by
> 
> v1: https://lore.kernel.org/all/20261006204202.59604-1-raghunathpalla.0209@gmail.com/
> 
>  drivers/usb/gadget/function/f_fs.c | 17 +++++++++++++----
>  1 file changed, 13 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index c64a268e98a4..996fa0767de2 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -288,6 +288,8 @@ static struct ffs_dev *_ffs_find_dev(const char *name);
>  static struct ffs_dev *_ffs_alloc_dev(void);
>  static void _ffs_free_dev(struct ffs_dev *dev);
>  static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data);
> +static void _ffs_release_dev(struct ffs_dev *ffs_dev)
> +	__must_hold(&ffs_lock);
>  static void ffs_release_dev(struct ffs_dev *ffs_dev);
>  static int ffs_ready(struct ffs_data *ffs);
>  static void ffs_closed(struct ffs_data *ffs);
> @@ -4147,8 +4149,8 @@ static void ffs_free_inst(struct usb_function_instance *f)
>  	struct f_fs_opts *opts;
>  
>  	opts = to_f_fs_opts(f);
> -	ffs_release_dev(opts->dev);
>  	ffs_dev_lock();
> +	_ffs_release_dev(opts->dev);
>  	_ffs_free_dev(opts->dev);
>  	ffs_dev_unlock();
>  	kfree(opts);
> @@ -4363,10 +4365,12 @@ static int ffs_acquire_dev(const char *dev_name, struct ffs_data *ffs_data)
>  	return ret;
>  }
>  
> -static void ffs_release_dev(struct ffs_dev *ffs_dev)
> +/*
> + * Same as ffs_release_dev(), for callers that already hold ffs_lock.
> + */
> +static void _ffs_release_dev(struct ffs_dev *ffs_dev)
> +	__must_hold(&ffs_lock)
>  {
> -	ffs_dev_lock();
> -
>  	if (ffs_dev && ffs_dev->mounted) {
>  		ffs_dev->mounted = false;
>  		if (ffs_dev->ffs_data) {
> @@ -4377,7 +4381,12 @@ static void ffs_release_dev(struct ffs_dev *ffs_dev)
>  		if (ffs_dev->ffs_release_dev_callback)
>  			ffs_dev->ffs_release_dev_callback(ffs_dev);
>  	}
> +}
>  
> +static void ffs_release_dev(struct ffs_dev *ffs_dev)
> +{
> +	ffs_dev_lock();
> +	_ffs_release_dev(ffs_dev);
>  	ffs_dev_unlock();
>  }
>  
> -- 
> 2.34.1
> 
> 

Isn't this fixed cleaner by:
	https://lore.kernel.org/r/20261120210616.2293426-1-shuangpeng.kernel@gmail.com

If not, please resend and explain why that commit does not fix this
issue.

thanks,

greg k-h

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

* Re: [PATCH v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst()
  2026-12-03 22:16 ` Greg Kroah-Hartman
@ 2026-12-04  5:50   ` Palla Raghunath
  0 siblings, 0 replies; 3+ messages in thread
From: Palla Raghunath @ 2026-12-04  5:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: linux-usb, Neill Kapron, Michał Nazarewicz, Shuangpeng Bai,
	Shuah Khan, Brigham Campbell, linux-kernel-mentees, linux-kernel,
	stable, syzbot+6227549bd2c8a1ec8ba0, raghunathpalla.0209

On Thu, Dec 03, 2026 at 11:16:58PM +0100, Greg Kroah-Hartman wrote:
> Isn't this fixed cleaner by:
> 	https://lore.kernel.org/r/20261120210616.2293426-1-shuangpeng.kernel@gmail.com
>
> If not, please resend and explain why that commit does not fix this
> issue.

I don't think it does. They are two different bugs on two different
objects.

Shuangpeng's patch is about struct ffs_data. ffs_func_unbind() drops
its reference with functionfs_unbind() and then still takes
ffs->eps_lock, so the patch moves functionfs_unbind() to the end. It
only changes ffs_func_unbind().

This one is about struct ffs_dev. In the syzbot report the freed object
was allocated in ffs_alloc_inst() and freed from ffs_free_inst():

  Freed by task 17408:
   _ffs_free_dev drivers/usb/gadget/function/f_fs.c:4336 [inline]
   ffs_free_inst+0x233/0x2c0 drivers/usb/gadget/function/f_fs.c:4152

ffs_free_inst() drops ffs_dev_lock between ffs_release_dev() and
_ffs_free_dev(), and the dev is still on ffs_devices in that window. A
mount with the same name can pick it up through ffs_acquire_dev(), and
the umount later writes to the freed dev in ffs_closed().

No gadget bind or unbind is involved. ffs_func_unbind() isn't in the
syzbot report, and my reproducer only races configfs mkdir/rmdir of
the function against mount/umount of functionfs, without ever binding
a UDC. Shuangpeng's patch doesn't touch ffs_free_inst(), so that window
is still there with it applied.

I'll resend v2 with this explanation below the --- line.

thanks,
Raghunath

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

end of thread, other threads:[~2026-12-04  5:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 23:07 [PATCH v2] usb: gadget: f_fs: fix use-after-free of ffs_dev in ffs_free_inst() Palla Raghunath
2026-12-03 22:16 ` Greg Kroah-Hartman
2026-12-04  5:50   ` Palla Raghunath

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

all inboxes | Powered by JetHome®