Skip to content

Commit 6a19da1

Browse files
Udipto Goswamigregkh
authored andcommitted
usb: gadget: f_fs: Prevent race during ffs_ep0_queue_wait
While performing fast composition switch, there is a possibility that the process of ffs_ep0_write/ffs_ep0_read get into a race condition due to ep0req being freed up from functionfs_unbind. Consider the scenario that the ffs_ep0_write calls the ffs_ep0_queue_wait by taking a lock &ffs->ev.waitq.lock. However, the functionfs_unbind isn't bounded so it can go ahead and mark the ep0req to NULL, and since there is no NULL check in ffs_ep0_queue_wait we will end up in use-after-free. Fix this by making a serialized execution between the two functions using a mutex_lock(ffs->mutex). Fixes: ddf8abd ("USB: f_fs: the FunctionFS driver") Signed-off-by: Udipto Goswami <[email protected]> Tested-by: Krishna Kurapati <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cde3788 commit 6a19da1

File tree

1 file changed

+5
-0
lines changed
  • drivers/usb/gadget/function

1 file changed

+5
-0
lines changed

drivers/usb/gadget/function/f_fs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
279279
struct usb_request *req = ffs->ep0req;
280280
int ret;
281281

282+
if (!req)
283+
return -EINVAL;
284+
282285
req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
283286

284287
spin_unlock_irq(&ffs->ev.waitq.lock);
@@ -1892,10 +1895,12 @@ static void functionfs_unbind(struct ffs_data *ffs)
18921895
ENTER();
18931896

18941897
if (!WARN_ON(!ffs->gadget)) {
1898+
mutex_lock(&ffs->mutex);
18951899
usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
18961900
ffs->ep0req = NULL;
18971901
ffs->gadget = NULL;
18981902
clear_bit(FFS_FL_BOUND, &ffs->flags);
1903+
mutex_unlock(&ffs->mutex);
18991904
ffs_data_put(ffs);
19001905
}
19011906
}

0 commit comments

Comments
 (0)