Skip to content

Commit 0042178

Browse files
Udipto Goswamigregkh
authored andcommitted
usb: f_fs: Fix use-after-free for epfile
[ Upstream commit ebe2b1a ] Consider a case where ffs_func_eps_disable is called from ffs_func_disable as part of composition switch and at the same time ffs_epfile_release get called from userspace. ffs_epfile_release will free up the read buffer and call ffs_data_closed which in turn destroys ffs->epfiles and mark it as NULL. While this was happening the driver has already initialized the local epfile in ffs_func_eps_disable which is now freed and waiting to acquire the spinlock. Once spinlock is acquired the driver proceeds with the stale value of epfile and tries to free the already freed read buffer causing use-after-free. Following is the illustration of the race: CPU1 CPU2 ffs_func_eps_disable epfiles (local copy) ffs_epfile_release ffs_data_closed if (last file closed) ffs_data_reset ffs_data_clear ffs_epfiles_destroy spin_lock dereference epfiles Fix this races by taking epfiles local copy & assigning it under spinlock and if epfiles(local) is null then update it in ffs->epfiles then finally destroy it. Extending the scope further from the race, protecting the ep related structures, and concurrent accesses. Fixes: a9e6f83 ("usb: gadget: f_fs: stop sleeping in ffs_func_eps_disable") Co-developed-by: Udipto Goswami <quic_ugoswami@quicinc.com> Reviewed-by: John Keeping <john@metanate.com> Signed-off-by: Pratham Pratap <quic_ppratap@quicinc.com> Signed-off-by: Udipto Goswami <quic_ugoswami@quicinc.com> Link: https://lore.kernel.org/r/1643256595-10797-1-git-send-email-quic_ugoswami@quicinc.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 5a37fd9 commit 0042178

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

  • drivers/usb/gadget/function

‎drivers/usb/gadget/function/f_fs.c‎

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,16 +1710,24 @@ static void ffs_data_put(struct ffs_data *ffs)
17101710

17111711
static void ffs_data_closed(struct ffs_data *ffs)
17121712
{
1713+
struct ffs_epfile *epfiles;
1714+
unsigned long flags;
1715+
17131716
ENTER();
17141717

17151718
if (atomic_dec_and_test(&ffs->opened)) {
17161719
if (ffs->no_disconnect) {
17171720
ffs->state = FFS_DEACTIVATED;
1718-
if (ffs->epfiles) {
1719-
ffs_epfiles_destroy(ffs->epfiles,
1720-
ffs->eps_count);
1721-
ffs->epfiles = NULL;
1722-
}
1721+
spin_lock_irqsave(&ffs->eps_lock, flags);
1722+
epfiles = ffs->epfiles;
1723+
ffs->epfiles = NULL;
1724+
spin_unlock_irqrestore(&ffs->eps_lock,
1725+
flags);
1726+
1727+
if (epfiles)
1728+
ffs_epfiles_destroy(epfiles,
1729+
ffs->eps_count);
1730+
17231731
if (ffs->setup_state == FFS_SETUP_PENDING)
17241732
__ffs_ep0_stall(ffs);
17251733
} else {
@@ -1766,14 +1774,27 @@ static struct ffs_data *ffs_data_new(const char *dev_name)
17661774

17671775
static void ffs_data_clear(struct ffs_data *ffs)
17681776
{
1777+
struct ffs_epfile *epfiles;
1778+
unsigned long flags;
1779+
17691780
ENTER();
17701781

17711782
ffs_closed(ffs);
17721783

17731784
BUG_ON(ffs->gadget);
17741785

1775-
if (ffs->epfiles) {
1776-
ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1786+
spin_lock_irqsave(&ffs->eps_lock, flags);
1787+
epfiles = ffs->epfiles;
1788+
ffs->epfiles = NULL;
1789+
spin_unlock_irqrestore(&ffs->eps_lock, flags);
1790+
1791+
/*
1792+
* potential race possible between ffs_func_eps_disable
1793+
* & ffs_epfile_release therefore maintaining a local
1794+
* copy of epfile will save us from use-after-free.
1795+
*/
1796+
if (epfiles) {
1797+
ffs_epfiles_destroy(epfiles, ffs->eps_count);
17771798
ffs->epfiles = NULL;
17781799
}
17791800

@@ -1921,12 +1942,15 @@ static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
19211942

19221943
static void ffs_func_eps_disable(struct ffs_function *func)
19231944
{
1924-
struct ffs_ep *ep = func->eps;
1925-
struct ffs_epfile *epfile = func->ffs->epfiles;
1926-
unsigned count = func->ffs->eps_count;
1945+
struct ffs_ep *ep;
1946+
struct ffs_epfile *epfile;
1947+
unsigned short count;
19271948
unsigned long flags;
19281949

19291950
spin_lock_irqsave(&func->ffs->eps_lock, flags);
1951+
count = func->ffs->eps_count;
1952+
epfile = func->ffs->epfiles;
1953+
ep = func->eps;
19301954
while (count--) {
19311955
/* pending requests get nuked */
19321956
if (likely(ep->ep))
@@ -1944,14 +1968,18 @@ static void ffs_func_eps_disable(struct ffs_function *func)
19441968

19451969
static int ffs_func_eps_enable(struct ffs_function *func)
19461970
{
1947-
struct ffs_data *ffs = func->ffs;
1948-
struct ffs_ep *ep = func->eps;
1949-
struct ffs_epfile *epfile = ffs->epfiles;
1950-
unsigned count = ffs->eps_count;
1971+
struct ffs_data *ffs;
1972+
struct ffs_ep *ep;
1973+
struct ffs_epfile *epfile;
1974+
unsigned short count;
19511975
unsigned long flags;
19521976
int ret = 0;
19531977

19541978
spin_lock_irqsave(&func->ffs->eps_lock, flags);
1979+
ffs = func->ffs;
1980+
ep = func->eps;
1981+
epfile = ffs->epfiles;
1982+
count = ffs->eps_count;
19551983
while(count--) {
19561984
ep->ep->driver_data = ep;
19571985

0 commit comments

Comments
 (0)