Skip to content

Commit 82a10a8

Browse files
AlanSterngregkh
authored andcommitted
USB: g_file_storage: fix use-after-free bug when closing files
This patch (as1231) fixes a use-after-free bug in g_file_storage. A device's name may not be available after the device is unregistered, even if the device structure itself is still allocated. Since close_backing_file() prints a LUN's name for debugging, it shouldn't be called after the LUN has been unregistered. That whole area needed to be cleaned up; the backing files were getting closed in a couple of different places. The patch fixes things so that they get closed in just one place, as part of the unbind procedure, immediately before the LUN is unregistered. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent c065c60 commit 82a10a8

1 file changed

Lines changed: 4 additions & 16 deletions

File tree

‎drivers/usb/gadget/file_storage.c‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,6 @@ static struct fsg_dev *the_fsg;
738738
static struct usb_gadget_driver fsg_driver;
739739

740740
static void close_backing_file(struct lun *curlun);
741-
static void close_all_backing_files(struct fsg_dev *fsg);
742741

743742

744743
/*-------------------------------------------------------------------------*/
@@ -3593,12 +3592,10 @@ static int fsg_main_thread(void *fsg_)
35933592
fsg->thread_task = NULL;
35943593
spin_unlock_irq(&fsg->lock);
35953594

3596-
/* In case we are exiting because of a signal, unregister the
3597-
* gadget driver and close the backing file. */
3598-
if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags)) {
3595+
/* If we are exiting because of a signal, unregister the
3596+
* gadget driver. */
3597+
if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
35993598
usb_gadget_unregister_driver(&fsg_driver);
3600-
close_all_backing_files(fsg);
3601-
}
36023599

36033600
/* Let the unbind and cleanup routines know the thread has exited */
36043601
complete_and_exit(&fsg->thread_notifier, 0);
@@ -3703,14 +3700,6 @@ static void close_backing_file(struct lun *curlun)
37033700
}
37043701
}
37053702

3706-
static void close_all_backing_files(struct fsg_dev *fsg)
3707-
{
3708-
int i;
3709-
3710-
for (i = 0; i < fsg->nluns; ++i)
3711-
close_backing_file(&fsg->luns[i]);
3712-
}
3713-
37143703

37153704
static ssize_t show_ro(struct device *dev, struct device_attribute *attr, char *buf)
37163705
{
@@ -3845,6 +3834,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
38453834
if (curlun->registered) {
38463835
device_remove_file(&curlun->dev, &dev_attr_ro);
38473836
device_remove_file(&curlun->dev, &dev_attr_file);
3837+
close_backing_file(curlun);
38483838
device_unregister(&curlun->dev);
38493839
curlun->registered = 0;
38503840
}
@@ -4190,7 +4180,6 @@ static int __init fsg_bind(struct usb_gadget *gadget)
41904180
out:
41914181
fsg->state = FSG_STATE_TERMINATED; // The thread is dead
41924182
fsg_unbind(gadget);
4193-
close_all_backing_files(fsg);
41944183
complete(&fsg->thread_notifier);
41954184
return rc;
41964185
}
@@ -4284,7 +4273,6 @@ static void __exit fsg_cleanup(void)
42844273
/* Wait for the thread to finish up */
42854274
wait_for_completion(&fsg->thread_notifier);
42864275

4287-
close_all_backing_files(fsg);
42884276
kref_put(&fsg->ref, fsg_release);
42894277
}
42904278
module_exit(fsg_cleanup);

0 commit comments

Comments
 (0)