* [PATCH] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount
@ 2026-10-06 21:05 Mohammad Mosafer
2026-10-06 21:17 ` sashiko-bot
2026-10-06 21:49 ` [PATCH v2] " Mohammad Mosafer
0 siblings, 2 replies; 12+ messages in thread
From: Mohammad Mosafer @ 2026-10-06 21:05 UTC (permalink / raw)
To: linux-usb; +Cc: gregkh, linux-kernel, syzbot+6227549bd2c8a1ec8ba0
ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only
then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In
between, the dev is still linked on the ffs_devices list while already
marked unmounted, so a concurrent mount(2) of functionfs finds it by
name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev
(ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev,
and when that mount is torn down, ffs_closed() dereferences the stale
ffs->private_data:
BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530
Write of size 1 at addr ffff88810594664a by task repro/116
ffs_data_clear+0x438/0x530
ffs_fs_kill_sb+0x7b/0x510
deactivate_locked_super+0xa9/0x200
cleanup_mnt+0x255/0x380
... reached via umount(2)
Freed by task 112:
kfree+0x127/0x3b0
ffs_free_inst+0x10c/0x1a0
usb_put_function_instance+0x8a/0xc0
configfs_rmdir+0x773/0x9c0
Allocated by task 113:
ffs_alloc_inst+0x109/0x360
function_make+0x138/0x330
configfs_mkdir+0x48b/0x1090
Hold ffs_dev_lock across the release and the free so that a released
dev is never findable, splitting ffs_release_dev() into a lock-assuming
_ffs_release_dev() (matching the _ffs_* convention in this file) plus a
locking wrapper for the remaining callers.
The race was reproduced with a multi-threaded harness racing configfs
mkdir/rmdir of the ffs instance against mount/umount of functionfs on
a KASAN kernel: the unpatched kernel reports the use-after-free
reliably (2/2 runs), the patched kernel survives an extended soak with
identical churn (2/2 runs clean).
Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0
Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility")
Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com>
---
drivers/usb/gadget/function/f_fs.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..5e7179b4250e 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -288,6 +288,7 @@ 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);
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 +4148,17 @@ 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);
+
+ /*
+ * Release and free the dev under a single ffs_dev_lock critical
+ * section. Between ffs_release_dev() and _ffs_free_dev() the dev
+ * would still be on the ffs_devices list while already unmounted,
+ * so a concurrent ffs_acquire_dev() could link a fresh ffs_data to
+ * the doomed dev, leaving it with a dangling ->private_data that is
+ * dereferenced in ffs_closed() when that mount is torn down.
+ */
ffs_dev_lock();
+ _ffs_release_dev(opts->dev);
_ffs_free_dev(opts->dev);
ffs_dev_unlock();
kfree(opts);
@@ -4363,10 +4373,11 @@ 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)
+/*
+ * ffs_dev_lock must be taken by the caller
+ */
+static void _ffs_release_dev(struct ffs_dev *ffs_dev)
{
- ffs_dev_lock();
-
if (ffs_dev && ffs_dev->mounted) {
ffs_dev->mounted = false;
if (ffs_dev->ffs_data) {
@@ -4377,7 +4388,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 related [flat|nested] 12+ messages in thread* Re: [PATCH] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-10-06 21:05 [PATCH] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount Mohammad Mosafer @ 2026-10-06 21:17 ` sashiko-bot 2026-10-06 21:49 ` [PATCH v2] " Mohammad Mosafer 1 sibling, 0 replies; 12+ messages in thread From: sashiko-bot @ 2026-10-06 21:17 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] usb: ffs: TOCTOU use-after-free in ffs_data_put() -- commit 215b73da4a8e129ed558d6bf4fd1b28619b9f490 Author: Mohammad Mosafer <mohsafer@gmail.com> usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount This commit fixes a use-after-free in ffs_closed() during umount by holding ffs_dev_lock across the release and free operations in ffs_free_inst(). It splits ffs_release_dev() into a lockless _ffs_release_dev() and a locked wrapper to ensure the device cannot be found by a concurrent mount(2) while it is being torn down. Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") > diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c > index c64a268e98a43..5e7179b4250ef 100644 > --- a/drivers/usb/gadget/function/f_fs.c > +++ b/drivers/usb/gadget/function/f_fs.c [ ... ] > @@ -4147,8 +4148,17 @@ 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); > + > + /* > + * Release and free the dev under a single ffs_dev_lock critical > + * section. Between ffs_release_dev() and _ffs_free_dev() the dev > + * would still be on the ffs_devices list while already unmounted, > + * so a concurrent ffs_acquire_dev() could link a fresh ffs_data to > + * the doomed dev, leaving it with a dangling ->private_data that is > + * dereferenced in ffs_closed() when that mount is torn down. > + */ > ffs_dev_lock(); > + _ffs_release_dev(opts->dev); > _ffs_free_dev(opts->dev); > ffs_dev_unlock(); > kfree(opts); [Severity: High] Is it possible for this change to trigger a time-of-check to time-of-use use-after-free of the ffs_dev object? Looking at ffs_data_put() in drivers/usb/gadget/function/f_fs.c, it evaluates the device pointer locklessly before calling ffs_release_dev(): ffs_data_put() ... ffs_release_dev(ffs->private_data); If a concurrent configfs_rmdir() invokes ffs_free_inst(), could the following sequence occur? Thread 1 ffs_data_put() evaluates ffs->private_data (obtaining the ffs_dev pointer) calls ffs_release_dev(), blocking on ffs_dev_lock() Thread 2 ffs_free_inst() ffs_dev_lock(); _ffs_release_dev(opts->dev); _ffs_free_dev(opts->dev); // device is freed here ffs_dev_unlock(); Thread 1 ffs_release_dev() acquires ffs_dev_lock() _ffs_release_dev(ffs_dev) // ffs_dev is now stale Inside _ffs_release_dev(), the code checks: if (ffs_dev && ffs_dev->mounted) This would dereference the stale ffs_dev pointer. Can this sequence occur when userspace unmounts and manipulates configfs concurrently? -- Sashiko AI review · https://sashiko.dev/#/patchset/20261006210523.150724-1-mohsafer@gmail.com?part=1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-10-06 21:05 [PATCH] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount Mohammad Mosafer 2026-10-06 21:17 ` sashiko-bot @ 2026-10-06 21:49 ` Mohammad Mosafer 2026-10-06 22:00 ` sashiko-bot 2026-11-20 10:18 ` Greg KH 1 sibling, 2 replies; 12+ messages in thread From: Mohammad Mosafer @ 2026-10-06 21:49 UTC (permalink / raw) To: linux-usb; +Cc: gregkh, linux-kernel, syzbot+6227549bd2c8a1ec8ba0 ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In between, the dev is still linked on the ffs_devices list while already marked unmounted, so a concurrent mount(2) of functionfs finds it by name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, and when that mount is torn down, ffs_closed() dereferences the stale ffs->private_data: BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 Write of size 1 at addr ffff88810594664a by task repro/116 ffs_data_clear+0x438/0x530 ffs_fs_kill_sb+0x7b/0x510 deactivate_locked_super+0xa9/0x200 cleanup_mnt+0x255/0x380 ... reached via umount(2) Freed by task 112: kfree+0x127/0x3b0 ffs_free_inst+0x10c/0x1a0 usb_put_function_instance+0x8a/0xc0 configfs_rmdir+0x773/0x9c0 Allocated by task 113: ffs_alloc_inst+0x109/0x360 function_make+0x138/0x330 configfs_mkdir+0x48b/0x1090 Hold ffs_dev_lock across the release and the free so that a released dev is never findable, splitting ffs_release_dev() into a lock-assuming _ffs_release_dev() (matching the _ffs_* convention in this file). For the same reason, re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the argument read there was unlocked, so a concurrent ffs_free_inst() could unlink and free the dev while the putter waited on the mutex, and the subsequent ffs_release_dev() would dereference the freed dev. The now unreached ffs_release_dev() wrapper is dropped; its remaining callers use _ffs_release_dev() with the lock held. The race was reproduced with a multi-threaded harness racing configfs mkdir/rmdir of the ffs instance against mount/umount of functionfs on a KASAN kernel: the unpatched kernel reports the use-after-free reliably (2/2 runs), the patched kernel survives an extended soak with identical churn (2/2 runs clean). Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> --- Changes in v2: - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the unlocked argument read raced with a concurrent ffs_free_inst() freeing the dev while the putter waited on ffs_dev_lock (flagged by the Sashiko review bot). - Drop the now-unused ffs_release_dev() wrapper. drivers/usb/gadget/function/f_fs.c | 31 ++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index c64a268e98a4..960b73dc06b3 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -288,7 +288,7 @@ 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); +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); static void ffs_reset_work(struct work_struct *work); @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) if (refcount_dec_and_test(&ffs->ref)) { pr_info("%s(): freeing\n", __func__); ffs_data_clear(ffs); - ffs_release_dev(ffs->private_data); + /* + * ffs->private_data must be re-read under ffs_dev_lock: + * a concurrent ffs_free_inst() may have NULLed it and + * freed the dev it pointed to. + */ + ffs_dev_lock(); + _ffs_release_dev(ffs->private_data); + ffs_dev_unlock(); BUG_ON(waitqueue_active(&ffs->ev.waitq) || swait_active(&ffs->ep0req_completion.wait) || waitqueue_active(&ffs->wait)); @@ -4147,8 +4154,17 @@ 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); + + /* + * Release and free the dev under a single ffs_dev_lock critical + * section. Between ffs_release_dev() and _ffs_free_dev() the dev + * would still be on the ffs_devices list while already unmounted, + * so a concurrent ffs_acquire_dev() could link a fresh ffs_data to + * the doomed dev, leaving it with a dangling ->private_data that is + * dereferenced in ffs_closed() when that mount is torn down. + */ ffs_dev_lock(); + _ffs_release_dev(opts->dev); _ffs_free_dev(opts->dev); ffs_dev_unlock(); kfree(opts); @@ -4363,10 +4379,11 @@ 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) +/* + * ffs_dev_lock must be taken by the caller + */ +static void _ffs_release_dev(struct ffs_dev *ffs_dev) { - ffs_dev_lock(); - if (ffs_dev && ffs_dev->mounted) { ffs_dev->mounted = false; if (ffs_dev->ffs_data) { @@ -4377,8 +4394,6 @@ 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); } - - ffs_dev_unlock(); } static int ffs_ready(struct ffs_data *ffs) -- 2.34.1 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-10-06 21:49 ` [PATCH v2] " Mohammad Mosafer @ 2026-10-06 22:00 ` sashiko-bot 2026-11-20 10:18 ` Greg KH 1 sibling, 0 replies; 12+ messages in thread From: sashiko-bot @ 2026-10-06 22:00 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > [ ... ] > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> Sashiko has reviewed this patch and found no issues. It looks great! -- Sashiko AI review · https://sashiko.dev/#/patchset/20261006214940.158352-1-mohsafer@gmail.com?part=1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-10-06 21:49 ` [PATCH v2] " Mohammad Mosafer 2026-10-06 22:00 ` sashiko-bot @ 2026-11-20 10:18 ` Greg KH 2026-11-20 19:24 ` [PATCH v3] " Mohammad Mosafer 1 sibling, 1 reply; 12+ messages in thread From: Greg KH @ 2026-11-20 10:18 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb, linux-kernel, syzbot+6227549bd2c8a1ec8ba0 On Tue, Oct 06, 2026 at 04:49:40PM -0500, Mohammad Mosafer wrote: > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > ... reached via umount(2) > Freed by task 112: > kfree+0x127/0x3b0 > ffs_free_inst+0x10c/0x1a0 > usb_put_function_instance+0x8a/0xc0 > configfs_rmdir+0x773/0x9c0 > Allocated by task 113: > ffs_alloc_inst+0x109/0x360 > function_make+0x138/0x330 > configfs_mkdir+0x48b/0x1090 > > Hold ffs_dev_lock across the release and the free so that a released > dev is never findable, splitting ffs_release_dev() into a lock-assuming > _ffs_release_dev() (matching the _ffs_* convention in this file). For > the same reason, re-read ffs->private_data under ffs_dev_lock in > ffs_data_put(): the argument read there was unlocked, so a concurrent > ffs_free_inst() could unlink and free the dev while the putter waited > on the mutex, and the subsequent ffs_release_dev() would dereference > the freed dev. The now unreached ffs_release_dev() wrapper is dropped; > its remaining callers use _ffs_release_dev() with the lock held. > > The race was reproduced with a multi-threaded harness racing configfs > mkdir/rmdir of the ffs instance against mount/umount of functionfs on > a KASAN kernel: the unpatched kernel reports the use-after-free > reliably (2/2 runs), the patched kernel survives an extended soak with > identical churn (2/2 runs clean). > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> > --- > > Changes in v2: > - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the > unlocked argument read raced with a concurrent ffs_free_inst() freeing > the dev while the putter waited on ffs_dev_lock (flagged by the > Sashiko review bot). > - Drop the now-unused ffs_release_dev() wrapper. > drivers/usb/gadget/function/f_fs.c | 31 ++++++++++++++++++++++-------- > 1 file changed, 23 insertions(+), 8 deletions(-) > > diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c > index c64a268e98a4..960b73dc06b3 100644 > --- a/drivers/usb/gadget/function/f_fs.c > +++ b/drivers/usb/gadget/function/f_fs.c > @@ -288,7 +288,7 @@ 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); > +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); > static void ffs_reset_work(struct work_struct *work); > @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) > if (refcount_dec_and_test(&ffs->ref)) { > pr_info("%s(): freeing\n", __func__); > ffs_data_clear(ffs); > - ffs_release_dev(ffs->private_data); > + /* > + * ffs->private_data must be re-read under ffs_dev_lock: > + * a concurrent ffs_free_inst() may have NULLed it and > + * freed the dev it pointed to. > + */ > + ffs_dev_lock(); > + _ffs_release_dev(ffs->private_data); > + ffs_dev_unlock(); > BUG_ON(waitqueue_active(&ffs->ev.waitq) || > swait_active(&ffs->ep0req_completion.wait) || > waitqueue_active(&ffs->wait)); > @@ -4147,8 +4154,17 @@ 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); > + > + /* > + * Release and free the dev under a single ffs_dev_lock critical > + * section. Between ffs_release_dev() and _ffs_free_dev() the dev > + * would still be on the ffs_devices list while already unmounted, > + * so a concurrent ffs_acquire_dev() could link a fresh ffs_data to > + * the doomed dev, leaving it with a dangling ->private_data that is > + * dereferenced in ffs_closed() when that mount is torn down. > + */ That comment isn't needed, right? > ffs_dev_lock(); > + _ffs_release_dev(opts->dev); > _ffs_free_dev(opts->dev); > ffs_dev_unlock(); > kfree(opts); > @@ -4363,10 +4379,11 @@ 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) > +/* > + * ffs_dev_lock must be taken by the caller Then enforce it please. thanks, greg k-h ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-11-20 10:18 ` Greg KH @ 2026-11-20 19:24 ` Mohammad Mosafer 2026-11-20 19:40 ` sashiko-bot 0 siblings, 1 reply; 12+ messages in thread From: Mohammad Mosafer @ 2026-11-20 19:24 UTC (permalink / raw) To: gregkh Cc: linux-usb, linux-kernel, syzbot+6227549bd2c8a1ec8ba0, Mohammad Mosafer ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In between, the dev is still linked on the ffs_devices list while already marked unmounted, so a concurrent mount(2) of functionfs finds it by name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, and when that mount is torn down, ffs_closed() dereferences the stale ffs->private_data: BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 Write of size 1 at addr ffff88810594664a by task repro/116 ffs_data_clear+0x438/0x530 ffs_fs_kill_sb+0x7b/0x510 deactivate_locked_super+0xa9/0x200 cleanup_mnt+0x255/0x380 ... reached via umount(2) Freed by task 112: kfree+0x127/0x3b0 ffs_free_inst+0x10c/0x1a0 usb_put_function_instance+0x8a/0xc0 configfs_rmdir+0x773/0x9c0 Allocated by task 113: ffs_alloc_inst+0x109/0x360 function_make+0x138/0x330 configfs_mkdir+0x48b/0x1090 Hold ffs_dev_lock across the release and the free so that a released dev is never findable, splitting ffs_release_dev() into a lock-assuming _ffs_release_dev() (matching the _ffs_* convention in this file) and enforcing its caller-holds-the-lock contract with lockdep_assert_held(). For the same reason, re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the argument read there was unlocked, so a concurrent ffs_free_inst() could unlink and free the dev while the putter waited on the mutex, and the subsequent ffs_release_dev() would dereference the freed dev. The now unreached ffs_release_dev() wrapper is dropped; its remaining callers use _ffs_release_dev() with the lock held. The race was reproduced with a multi-threaded harness racing configfs mkdir/rmdir of the ffs instance against mount/umount of functionfs on a KASAN kernel: the unpatched kernel reports the use-after-free reliably (2/2 runs), the patched kernel survives an extended soak with identical churn (2/2 runs clean). Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> --- Hi Greg, Right - the comment duplicated the commit message, so it is gone. The lock contract is now enforced with lockdep_assert_held(&ffs_lock) instead of the comment; ffs_lock is the mutex behind the ffs_dev_lock()/ffs_dev_unlock() wrappers. Changes in v3: - Drop the explanatory comment in ffs_free_inst(); it duplicated the commit message (Greg KH). - Enforce the caller-holds-the-lock contract of _ffs_release_dev() with lockdep_assert_held(&ffs_lock) instead of a comment (Greg KH). Link to v2: https://lore.kernel.org/r/20261006214940.158352-1-mohsafer@gmail.com Changes in v2: - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the unlocked argument read raced with a concurrent ffs_free_inst() freeing the dev while the putter waited on ffs_dev_lock (flagged by the Sashiko review bot). - Drop the now-unused ffs_release_dev() wrapper. drivers/usb/gadget/function/f_fs.c | 19 +++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index c64a268e98a4..3aab4c28e310 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -288,7 +288,7 @@ 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); +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); static void ffs_reset_work(struct work_struct *work); @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) if (refcount_dec_and_test(&ffs->ref)) { pr_info("%s(): freeing\n", __func__); ffs_data_clear(ffs); - ffs_release_dev(ffs->private_data); + /* + * ffs->private_data must be re-read under ffs_dev_lock: + * a concurrent ffs_free_inst() may have NULLed it and + * freed the dev it pointed to. + */ + ffs_dev_lock(); + _ffs_release_dev(ffs->private_data); + ffs_dev_unlock(); BUG_ON(waitqueue_active(&ffs->ev.waitq) || swait_active(&ffs->ep0req_completion.wait) || waitqueue_active(&ffs->wait)); @@ -4147,8 +4154,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,9 +4370,9 @@ 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) +static void _ffs_release_dev(struct ffs_dev *ffs_dev) { - ffs_dev_lock(); + lockdep_assert_held(&ffs_lock); if (ffs_dev && ffs_dev->mounted) { ffs_dev->mounted = false; @@ -4377,8 +4384,6 @@ 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); } - - ffs_dev_unlock(); } static int ffs_ready(struct ffs_data *ffs) -- 2.34.1 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v3] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-11-20 19:24 ` [PATCH v3] " Mohammad Mosafer @ 2026-11-20 19:40 ` sashiko-bot 2026-11-20 19:46 ` [PATCH v4] " Mohammad Mosafer 0 siblings, 1 reply; 12+ messages in thread From: sashiko-bot @ 2026-11-20 19:40 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] usb: gadget: f_fs: Fixes trailer cites 5920cda627688c instead of ecfbd7b9054b -- commit cfe09aaba9522f24cae9e22dc082e6797e54adda Author: Mohammad Mosafer <mohsafer@gmail.com> > usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount > > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > ... reached via umount(2) > Freed by task 112: > kfree+0x127/0x3b0 > ffs_free_inst+0x10c/0x1a0 > usb_put_function_instance+0x8a/0xc0 > configfs_rmdir+0x773/0x9c0 > Allocated by task 113: > ffs_alloc_inst+0x109/0x360 > function_make+0x138/0x330 > configfs_mkdir+0x48b/0x1090 > > Hold ffs_dev_lock across the release and the free so that a released > dev is never findable, splitting ffs_release_dev() into a lock-assuming > _ffs_release_dev() (matching the _ffs_* convention in this file) and > enforcing its caller-holds-the-lock contract with lockdep_assert_held(). > For the same reason, re-read ffs->private_data under ffs_dev_lock in > ffs_data_put(): the argument read there was unlocked, so a concurrent > ffs_free_inst() could unlink and free the dev while the putter waited > on the mutex, and the subsequent ffs_release_dev() would dereference > the freed dev. The now unreached ffs_release_dev() wrapper is dropped; > its remaining callers use _ffs_release_dev() with the lock held. > > The race was reproduced with a multi-threaded harness racing configfs > mkdir/rmdir of the ffs instance against mount/umount of functionfs on > a KASAN kernel: the unpatched kernel reports the use-after-free > reliably (2/2 runs), the patched kernel survives an extended soak with > identical churn (2/2 runs clean). > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: 5920cda627688c ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> [Severity: Low] In 5920cda62768 ("usb: gadget: FunctionFS: convert to new function interface with backward compatibility"), ffs_free_inst() only acquired ffs_dev_lock() once around ffs_free_dev(opts->dev) without calling ffs_release_dev(), and ffs_release_dev() took struct ffs_data * and read ffs_data->private_data under ffs_dev_lock(). Both the lock gap between ffs_release_dev() and _ffs_free_dev() in ffs_free_inst() and the unlocked ffs->private_data read in ffs_data_put() were introduced later by ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references"). Should the Fixes tag reference that commit instead? Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") -- Sashiko AI review · https://sashiko.dev/#/patchset/20261120192415.335646-1-mohsafer@gmail.com?part=1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-11-20 19:40 ` sashiko-bot @ 2026-11-20 19:46 ` Mohammad Mosafer 2026-11-20 19:52 ` sashiko-bot 2026-12-03 22:16 ` Greg KH 0 siblings, 2 replies; 12+ messages in thread From: Mohammad Mosafer @ 2026-11-20 19:46 UTC (permalink / raw) To: gregkh Cc: linux-usb, linux-kernel, syzbot+6227549bd2c8a1ec8ba0, Mohammad Mosafer ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In between, the dev is still linked on the ffs_devices list while already marked unmounted, so a concurrent mount(2) of functionfs finds it by name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, and when that mount is torn down, ffs_closed() dereferences the stale ffs->private_data: BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 Write of size 1 at addr ffff88810594664a by task repro/116 ffs_data_clear+0x438/0x530 ffs_fs_kill_sb+0x7b/0x510 deactivate_locked_super+0xa9/0x200 cleanup_mnt+0x255/0x380 ... reached via umount(2) Freed by task 112: kfree+0x127/0x3b0 ffs_free_inst+0x10c/0x1a0 usb_put_function_instance+0x8a/0xc0 configfs_rmdir+0x773/0x9c0 Allocated by task 113: ffs_alloc_inst+0x109/0x360 function_make+0x138/0x330 configfs_mkdir+0x48b/0x1090 Hold ffs_dev_lock across the release and the free so that a released dev is never findable, splitting ffs_release_dev() into a lock-assuming _ffs_release_dev() (matching the _ffs_* convention in this file) and enforcing its caller-holds-the-lock contract with lockdep_assert_held(). For the same reason, re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the argument read there was unlocked, so a concurrent ffs_free_inst() could unlink and free the dev while the putter waited on the mutex, and the subsequent ffs_release_dev() would dereference the freed dev. The now unreached ffs_release_dev() wrapper is dropped; its remaining callers use _ffs_release_dev() with the lock held. The race was reproduced with a multi-threaded harness racing configfs mkdir/rmdir of the ffs instance against mount/umount of functionfs on a KASAN kernel: the unpatched kernel reports the use-after-free reliably (2/2 runs), the patched kernel survives an extended soak with identical churn (2/2 runs clean). Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> --- Hi Greg, Right - the comment duplicated the commit message, so it is gone. The lock contract is now enforced with lockdep_assert_held(&ffs_lock) instead of the comment; ffs_lock is the mutex behind the ffs_dev_lock()/ffs_dev_unlock() wrappers. The Fixes tag is corrected in this version: the release-then-free window in ffs_free_inst() and the unlocked ffs->private_data read in ffs_data_put() were introduced by ecfbd7b9054b, not by 5920cda627688c which predates the bug. Changes in v4: - Fix the Fixes tag: the defective shape (the release-then-free window in ffs_free_inst() and the unlocked ffs->private_data read in ffs_data_put()) was introduced by ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references"), not by 5920cda627688c, which predates the bug (Sashiko review bot). Link to v3: https://lore.kernel.org/r/20261120192415.335646-1-mohsafer@gmail.com Changes in v3: - Drop the explanatory comment in ffs_free_inst(); it duplicated the commit message (Greg KH). - Enforce the caller-holds-the-lock contract of _ffs_release_dev() with lockdep_assert_held(&ffs_lock) instead of a comment (Greg KH). Link to v2: https://lore.kernel.org/r/20261006214940.158352-1-mohsafer@gmail.com Changes in v2: - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the unlocked argument read raced with a concurrent ffs_free_inst() freeing the dev while the putter waited on ffs_dev_lock (flagged by the Sashiko review bot). - Drop the now-unused ffs_release_dev() wrapper. drivers/usb/gadget/function/f_fs.c | 19 +++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index c64a268e98a4..3aab4c28e310 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -288,7 +288,7 @@ 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); +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); static void ffs_reset_work(struct work_struct *work); @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) if (refcount_dec_and_test(&ffs->ref)) { pr_info("%s(): freeing\n", __func__); ffs_data_clear(ffs); - ffs_release_dev(ffs->private_data); + /* + * ffs->private_data must be re-read under ffs_dev_lock: + * a concurrent ffs_free_inst() may have NULLed it and + * freed the dev it pointed to. + */ + ffs_dev_lock(); + _ffs_release_dev(ffs->private_data); + ffs_dev_unlock(); BUG_ON(waitqueue_active(&ffs->ev.waitq) || swait_active(&ffs->ep0req_completion.wait) || waitqueue_active(&ffs->wait)); @@ -4147,8 +4154,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,9 +4370,9 @@ 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) +static void _ffs_release_dev(struct ffs_dev *ffs_dev) { - ffs_dev_lock(); + lockdep_assert_held(&ffs_lock); if (ffs_dev && ffs_dev->mounted) { ffs_dev->mounted = false; @@ -4377,8 +4384,6 @@ 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); } - - ffs_dev_unlock(); } static int ffs_ready(struct ffs_data *ffs) -- 2.34.1 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v4] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-11-20 19:46 ` [PATCH v4] " Mohammad Mosafer @ 2026-11-20 19:52 ` sashiko-bot 2026-12-03 22:16 ` Greg KH 1 sibling, 0 replies; 12+ messages in thread From: sashiko-bot @ 2026-11-20 19:52 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > [ ... ] > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261120194656.336875-1-mohsafer@gmail.com?part=1 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v4] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-11-20 19:46 ` [PATCH v4] " Mohammad Mosafer 2026-11-20 19:52 ` sashiko-bot @ 2026-12-03 22:16 ` Greg KH 2026-12-04 18:25 ` [PATCH v5] " Mohammad Mosafer 1 sibling, 1 reply; 12+ messages in thread From: Greg KH @ 2026-12-03 22:16 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb, linux-kernel, syzbot+6227549bd2c8a1ec8ba0 On Fri, Nov 20, 2026 at 01:46:56PM -0600, Mohammad Mosafer wrote: > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > ... reached via umount(2) > Freed by task 112: > kfree+0x127/0x3b0 > ffs_free_inst+0x10c/0x1a0 > usb_put_function_instance+0x8a/0xc0 > configfs_rmdir+0x773/0x9c0 > Allocated by task 113: > ffs_alloc_inst+0x109/0x360 > function_make+0x138/0x330 > configfs_mkdir+0x48b/0x1090 > > Hold ffs_dev_lock across the release and the free so that a released > dev is never findable, splitting ffs_release_dev() into a lock-assuming > _ffs_release_dev() (matching the _ffs_* convention in this file) and > enforcing its caller-holds-the-lock contract with lockdep_assert_held(). > For the same reason, re-read ffs->private_data under ffs_dev_lock in > ffs_data_put(): the argument read there was unlocked, so a concurrent > ffs_free_inst() could unlink and free the dev while the putter waited > on the mutex, and the subsequent ffs_release_dev() would dereference > the freed dev. The now unreached ffs_release_dev() wrapper is dropped; > its remaining callers use _ffs_release_dev() with the lock held. > > The race was reproduced with a multi-threaded harness racing configfs > mkdir/rmdir of the ffs instance against mount/umount of functionfs on > a KASAN kernel: the unpatched kernel reports the use-after-free > reliably (2/2 runs), the patched kernel survives an extended soak with > identical churn (2/2 runs clean). > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> > --- > > Hi Greg, > > Right - the comment duplicated the commit message, so it is gone. The > lock contract is now enforced with lockdep_assert_held(&ffs_lock) > instead of the comment; ffs_lock is the mutex behind the > ffs_dev_lock()/ffs_dev_unlock() wrappers. > > The Fixes tag is corrected in this version: the release-then-free > window in ffs_free_inst() and the unlocked ffs->private_data read in > ffs_data_put() were introduced by ecfbd7b9054b, not by 5920cda627688c > which predates the bug. > > Changes in v4: > - Fix the Fixes tag: the defective shape (the release-then-free window > in ffs_free_inst() and the unlocked ffs->private_data read in > ffs_data_put()) was introduced by ecfbd7b9054b ("usb: gadget: f_fs: > Fix setting of device and driver data cross-references"), not by > 5920cda627688c, which predates the bug (Sashiko review bot). > > Link to v3: https://lore.kernel.org/r/20261120192415.335646-1-mohsafer@gmail.com > > Changes in v3: > - Drop the explanatory comment in ffs_free_inst(); it duplicated the > commit message (Greg KH). > - Enforce the caller-holds-the-lock contract of _ffs_release_dev() > with lockdep_assert_held(&ffs_lock) instead of a comment (Greg KH). > > Link to v2: https://lore.kernel.org/r/20261006214940.158352-1-mohsafer@gmail.com > > Changes in v2: > - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the > unlocked argument read raced with a concurrent ffs_free_inst() freeing > the dev while the putter waited on ffs_dev_lock (flagged by the > Sashiko review bot). > - Drop the now-unused ffs_release_dev() wrapper. > > drivers/usb/gadget/function/f_fs.c | 19 +++++++++++++------- > 1 file changed, 12 insertions(+), 7 deletions(-) > > diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c > index c64a268e98a4..3aab4c28e310 100644 > --- a/drivers/usb/gadget/function/f_fs.c > +++ b/drivers/usb/gadget/function/f_fs.c > @@ -288,7 +288,7 @@ 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); > +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); > static void ffs_reset_work(struct work_struct *work); > @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) > if (refcount_dec_and_test(&ffs->ref)) { > pr_info("%s(): freeing\n", __func__); > ffs_data_clear(ffs); > - ffs_release_dev(ffs->private_data); > + /* > + * ffs->private_data must be re-read under ffs_dev_lock: > + * a concurrent ffs_free_inst() may have NULLed it and > + * freed the dev it pointed to. > + */ > + ffs_dev_lock(); > + _ffs_release_dev(ffs->private_data); > + ffs_dev_unlock(); > BUG_ON(waitqueue_active(&ffs->ev.waitq) || > swait_active(&ffs->ep0req_completion.wait) || > waitqueue_active(&ffs->wait)); > @@ -4147,8 +4154,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,9 +4370,9 @@ 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) > +static void _ffs_release_dev(struct ffs_dev *ffs_dev) > { > - ffs_dev_lock(); > + lockdep_assert_held(&ffs_lock); > > if (ffs_dev && ffs_dev->mounted) { > ffs_dev->mounted = false; > @@ -4377,8 +4384,6 @@ 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); > } > - > - ffs_dev_unlock(); > } > > static int ffs_ready(struct ffs_data *ffs) > -- > 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] 12+ messages in thread
* [PATCH v5] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-12-03 22:16 ` Greg KH @ 2026-12-04 18:25 ` Mohammad Mosafer 2026-12-04 18:43 ` sashiko-bot 0 siblings, 1 reply; 12+ messages in thread From: Mohammad Mosafer @ 2026-12-04 18:25 UTC (permalink / raw) To: gregkh Cc: linux-kernel, linux-usb, syzbot+6227549bd2c8a1ec8ba0, Mohammad Mosafer, Shuangpeng Bai, Palla Raghunath ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In between, the dev is still linked on the ffs_devices list while already marked unmounted, so a concurrent mount(2) of functionfs finds it by name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, and when that mount is torn down, ffs_closed() dereferences the stale ffs->private_data: BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 Write of size 1 at addr ffff88810594664a by task repro/116 ffs_data_clear+0x438/0x530 ffs_fs_kill_sb+0x7b/0x510 deactivate_locked_super+0xa9/0x200 cleanup_mnt+0x255/0x380 ... reached via umount(2) Freed by task 112: kfree+0x127/0x3b0 ffs_free_inst+0x10c/0x1a0 usb_put_function_instance+0x8a/0xc0 configfs_rmdir+0x773/0x9c0 Allocated by task 113: ffs_alloc_inst+0x109/0x360 function_make+0x138/0x330 configfs_mkdir+0x48b/0x1090 Hold ffs_dev_lock across the release and the free so that a released dev is never findable, splitting ffs_release_dev() into a lock-assuming _ffs_release_dev() (matching the _ffs_* convention in this file) and enforcing its caller-holds-the-lock contract with lockdep_assert_held(). For the same reason, re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the argument read there was unlocked, so a concurrent ffs_free_inst() could unlink and free the dev while the putter waited on the mutex, and the subsequent ffs_release_dev() would dereference the freed dev. The now unreached ffs_release_dev() wrapper is dropped; its remaining callers use _ffs_release_dev() with the lock held. The race was reproduced with a multi-threaded harness racing configfs mkdir/rmdir of the ffs instance against mount/umount of functionfs on a KASAN kernel: the unpatched kernel reports the use-after-free reliably (6/6 runs), the patched kernel survives an extended soak with identical churn (5/5 runs clean). Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> --- Hi Greg, No, that one fixes a different use-after-free. Shuangpeng's patch is about struct ffs_data: ffs_func_unbind() keeps using ffs after functionfs_unbind() has dropped the bind reference, so that patch moves that call to the end of the function. This patch is about struct ffs_dev: ffs_free_inst() releases the dev, drops ffs_dev_lock, and only then takes it again to free the dev; in between a concurrent mount(2) still finds the released dev on ffs_devices in ffs_acquire_dev() and attaches to it. That patch only reorders ffs_func_unbind() and leaves ffs_free_inst(), ffs_acquire_dev() and ffs_release_dev() as they are, so that window is unchanged. No gadget bind is involved: the syzbot report has only configfs mkdir/rmdir and umount frames, and the reproducer never links the function into a configuration or binds a UDC, so ffs_func_unbind() is not called at all. Checked on v7.3 + KASAN with the same reproducer (60 s and 300 s runs), counting the KASAN report quoted above: KASAN report clean unpatched 6/6 - ffs_func_unbind fix alone 4/6 1/6 that fix + this patch 0 6/6 this patch alone 0 5/5 One more run with the ffs_func_unbind fix alone ended in an Oops in ffs_closed() instead of a KASAN report: a NULL dereference on opts->func_inst.group.cg_item.ci_parent->ci_parent; That one does not need this race. A concurrent rmdir clears ci_parent between its check and its use there, and a 20 ms stall at that point reproduces the same Oops on this patch with no use-after-free involved. So that run says nothing about this race, and I have left it out of the table. This does fix the same race as Raghunath's patch [1], which I see is in your usb-testing tree. Its change to ffs_free_inst() is the same as the one here. What this patch does in addition is ffs_data_put(), where the last put still does: ffs_data_clear(ffs); ffs_release_dev(ffs->private_data); The pointer is read before ffs_lock is taken. If ffs_free_inst() runs in between (the putter is preempted right there), the putter goes on to read ffs_dev->mounted from a dev that has just been freed. With this patch the read happens under the lock and sees the NULL that ffs_free_inst() left behind. The plain reproducer does not hit that second window: 5/5 runs are clean with Raghunath's patch alone. With the putter stalled for 20 ms between that read and the lock, KASAN reports on that tree in 4 of 11 runs (a hit needs the rmdir of that very instance to land inside the stall): BUG: KASAN: slab-use-after-free in ffs_data_put+0x2dd/0x300 Read of size 1 at addr ffff8881091a4849 by task repro/116 ffs_data_put+0x2dd/0x300 deactivate_locked_super+0xa9/0x200 cleanup_mnt+0x9a/0x130 Freed by task 112: kfree+0x127/0x3b0 ffs_free_inst+0x1d7/0x290 usb_put_function_instance+0x8a/0xc0 configfs_rmdir+0x773/0x9c0 and not with this patch and the same stall (11/11 runs clean). If you would rather keep Raghunath's patch, I can send the ffs_data_put() part as a follow-up on top of it. Reproducer, the script that builds and checks each of these trees, and the logs: https://github.com/mohsafer/linux-kernel-ffs-uaf-fix [1] https://lore.kernel.org/r/20261204055346.42890-1-raghunathpalla.0209@gmail.com Changes in v5: - No code change. Explain why the ffs_func_unbind() fix does not fix this issue (Greg KH), and how this relates to the other fix for the same race; both checked against each other with the reproducer. Link to v4: https://lore.kernel.org/r/20261120194656.336875-1-mohsafer@gmail.com Changes in v4: - Fix the Fixes tag: the defective shape (the release-then-free window in ffs_free_inst() and the unlocked ffs->private_data read in ffs_data_put()) was introduced by ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references"), not by 5920cda627688c, which predates the bug (Sashiko review bot). Link to v3: https://lore.kernel.org/r/20261120192415.335646-1-mohsafer@gmail.com Changes in v3: - Drop the explanatory comment in ffs_free_inst(); it duplicated the commit message (Greg KH). - Enforce the caller-holds-the-lock contract of _ffs_release_dev() with lockdep_assert_held(&ffs_lock) instead of a comment (Greg KH). Link to v2: https://lore.kernel.org/r/20261006214940.158352-1-mohsafer@gmail.com Changes in v2: - Re-read ffs->private_data under ffs_dev_lock in ffs_data_put(): the unlocked argument read raced with a concurrent ffs_free_inst() freeing the dev while the putter waited on ffs_dev_lock (flagged by the Sashiko review bot). - Drop the now-unused ffs_release_dev() wrapper. --- drivers/usb/gadget/function/f_fs.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index c64a268e98a4..3aab4c28e310 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -288,7 +288,7 @@ 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); +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); static void ffs_reset_work(struct work_struct *work); @@ -2231,7 +2231,14 @@ static void ffs_data_put(struct ffs_data *ffs) if (refcount_dec_and_test(&ffs->ref)) { pr_info("%s(): freeing\n", __func__); ffs_data_clear(ffs); - ffs_release_dev(ffs->private_data); + /* + * ffs->private_data must be re-read under ffs_dev_lock: + * a concurrent ffs_free_inst() may have NULLed it and + * freed the dev it pointed to. + */ + ffs_dev_lock(); + _ffs_release_dev(ffs->private_data); + ffs_dev_unlock(); BUG_ON(waitqueue_active(&ffs->ev.waitq) || swait_active(&ffs->ep0req_completion.wait) || waitqueue_active(&ffs->wait)); @@ -4147,8 +4154,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,9 +4370,9 @@ 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) +static void _ffs_release_dev(struct ffs_dev *ffs_dev) { - ffs_dev_lock(); + lockdep_assert_held(&ffs_lock); if (ffs_dev && ffs_dev->mounted) { ffs_dev->mounted = false; @@ -4377,8 +4384,6 @@ 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); } - - ffs_dev_unlock(); } static int ffs_ready(struct ffs_data *ffs) -- 2.34.1 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v5] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount 2026-12-04 18:25 ` [PATCH v5] " Mohammad Mosafer @ 2026-12-04 18:43 ` sashiko-bot 0 siblings, 0 replies; 12+ messages in thread From: sashiko-bot @ 2026-12-04 18:43 UTC (permalink / raw) To: Mohammad Mosafer; +Cc: linux-usb > ffs_free_inst() releases the ffs_dev with ffs_release_dev() and only > then re-acquires ffs_dev_lock to free it with _ffs_free_dev(). In > between, the dev is still linked on the ffs_devices list while already > marked unmounted, so a concurrent mount(2) of functionfs finds it by > name in ffs_acquire_dev() and links a fresh ffs_data to the doomed dev > (ffs_data->private_data = dev). _ffs_free_dev() then kfrees the dev, > and when that mount is torn down, ffs_closed() dereferences the stale > ffs->private_data: > > BUG: KASAN: slab-use-after-free in ffs_data_clear+0x438/0x530 > Write of size 1 at addr ffff88810594664a by task repro/116 > ffs_data_clear+0x438/0x530 > ffs_fs_kill_sb+0x7b/0x510 > deactivate_locked_super+0xa9/0x200 > cleanup_mnt+0x255/0x380 > [ ... ] > > Reported-by: syzbot+6227549bd2c8a1ec8ba0@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=6227549bd2c8a1ec8ba0 > Fixes: ecfbd7b9054b ("usb: gadget: f_fs: Fix setting of device and driver data cross-references") > Signed-off-by: Mohammad Mosafer <mohsafer@gmail.com> Sashiko has reviewed this patch and found no issues. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261204182530.178997-1-mohsafer@gmail.com?part=1 ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-04 18:43 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-10-06 21:05 [PATCH] usb: gadget: f_fs: fix use-after-free in ffs_closed() on umount Mohammad Mosafer 2026-10-06 21:17 ` sashiko-bot 2026-10-06 21:49 ` [PATCH v2] " Mohammad Mosafer 2026-10-06 22:00 ` sashiko-bot 2026-11-20 10:18 ` Greg KH 2026-11-20 19:24 ` [PATCH v3] " Mohammad Mosafer 2026-11-20 19:40 ` sashiko-bot 2026-11-20 19:46 ` [PATCH v4] " Mohammad Mosafer 2026-11-20 19:52 ` sashiko-bot 2026-12-03 22:16 ` Greg KH 2026-12-04 18:25 ` [PATCH v5] " Mohammad Mosafer 2026-12-04 18:43 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox