Skip to content

Commit a2e075f

Browse files
AlanSterngregkh
authored andcommitted
USB: gadgetfs: Fix race between mounting and unmounting
commit d18dcfe upstream. The syzbot fuzzer and Gerald Lee have identified a use-after-free bug in the gadgetfs driver, involving processes concurrently mounting and unmounting the gadgetfs filesystem. In particular, gadgetfs_fill_super() can race with gadgetfs_kill_sb(), causing the latter to deallocate the_device while the former is using it. The output from KASAN says, in part: BUG: KASAN: use-after-free in instrument_atomic_read_write include/linux/instrumented.h:102 [inline] BUG: KASAN: use-after-free in atomic_fetch_sub_release include/linux/atomic/atomic-instrumented.h:176 [inline] BUG: KASAN: use-after-free in __refcount_sub_and_test include/linux/refcount.h:272 [inline] BUG: KASAN: use-after-free in __refcount_dec_and_test include/linux/refcount.h:315 [inline] BUG: KASAN: use-after-free in refcount_dec_and_test include/linux/refcount.h:333 [inline] BUG: KASAN: use-after-free in put_dev drivers/usb/gadget/legacy/inode.c:159 [inline] BUG: KASAN: use-after-free in gadgetfs_kill_sb+0x33/0x100 drivers/usb/gadget/legacy/inode.c:2086 Write of size 4 at addr ffff8880276d7840 by task syz-executor126/18689 CPU: 0 PID: 18689 Comm: syz-executor126 Not tainted 6.1.0-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022 Call Trace: <TASK> ... atomic_fetch_sub_release include/linux/atomic/atomic-instrumented.h:176 [inline] __refcount_sub_and_test include/linux/refcount.h:272 [inline] __refcount_dec_and_test include/linux/refcount.h:315 [inline] refcount_dec_and_test include/linux/refcount.h:333 [inline] put_dev drivers/usb/gadget/legacy/inode.c:159 [inline] gadgetfs_kill_sb+0x33/0x100 drivers/usb/gadget/legacy/inode.c:2086 deactivate_locked_super+0xa7/0xf0 fs/super.c:332 vfs_get_super fs/super.c:1190 [inline] get_tree_single+0xd0/0x160 fs/super.c:1207 vfs_get_tree+0x88/0x270 fs/super.c:1531 vfs_fsconfig_locked fs/fsopen.c:232 [inline] The simplest solution is to ensure that gadgetfs_fill_super() and gadgetfs_kill_sb() are serialized by making them both acquire a new mutex. Signed-off-by: Alan Stern <[email protected]> Reported-and-tested-by: [email protected] Reported-and-tested-by: Gerald Lee <[email protected]> Link: https://lore.kernel.org/linux-usb/CAO3qeMVzXDP-JU6v1u5Ags6Q-bb35kg3=C6d04DjzA9ffa5x1g@mail.gmail.com/ Fixes: e5d82a7 ("vfs: Convert gadgetfs to use the new mount API") CC: <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 2da67bf commit a2e075f

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

drivers/usb/gadget/legacy/inode.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static void put_ep (struct ep_data *data)
229229
*/
230230

231231
static const char *CHIP;
232+
static DEFINE_MUTEX(sb_mutex); /* Serialize superblock operations */
232233

233234
/*----------------------------------------------------------------------*/
234235

@@ -2013,13 +2014,20 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
20132014
{
20142015
struct inode *inode;
20152016
struct dev_data *dev;
2017+
int rc;
20162018

2017-
if (the_device)
2018-
return -ESRCH;
2019+
mutex_lock(&sb_mutex);
2020+
2021+
if (the_device) {
2022+
rc = -ESRCH;
2023+
goto Done;
2024+
}
20192025

20202026
CHIP = usb_get_gadget_udc_name();
2021-
if (!CHIP)
2022-
return -ENODEV;
2027+
if (!CHIP) {
2028+
rc = -ENODEV;
2029+
goto Done;
2030+
}
20232031

20242032
/* superblock */
20252033
sb->s_blocksize = PAGE_SIZE;
@@ -2056,13 +2064,17 @@ gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
20562064
* from binding to a controller.
20572065
*/
20582066
the_device = dev;
2059-
return 0;
2067+
rc = 0;
2068+
goto Done;
20602069

2061-
Enomem:
2070+
Enomem:
20622071
kfree(CHIP);
20632072
CHIP = NULL;
2073+
rc = -ENOMEM;
20642074

2065-
return -ENOMEM;
2075+
Done:
2076+
mutex_unlock(&sb_mutex);
2077+
return rc;
20662078
}
20672079

20682080
/* "mount -t gadgetfs path /dev/gadget" ends up here */
@@ -2084,13 +2096,15 @@ static int gadgetfs_init_fs_context(struct fs_context *fc)
20842096
static void
20852097
gadgetfs_kill_sb (struct super_block *sb)
20862098
{
2099+
mutex_lock(&sb_mutex);
20872100
kill_litter_super (sb);
20882101
if (the_device) {
20892102
put_dev (the_device);
20902103
the_device = NULL;
20912104
}
20922105
kfree(CHIP);
20932106
CHIP = NULL;
2107+
mutex_unlock(&sb_mutex);
20942108
}
20952109

20962110
/*----------------------------------------------------------------------*/

0 commit comments

Comments
 (0)