Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: f_fs: Fix use-after-free for epfile

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: a9e6f83c2df1 ("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>

authored by

Udipto Goswami and committed by
Greg Kroah-Hartman
ebe2b1ad b470947c

+42 -14
+42 -14
drivers/usb/gadget/function/f_fs.c
··· 1711 1711 1712 1712 static void ffs_data_closed(struct ffs_data *ffs) 1713 1713 { 1714 + struct ffs_epfile *epfiles; 1715 + unsigned long flags; 1716 + 1714 1717 ENTER(); 1715 1718 1716 1719 if (atomic_dec_and_test(&ffs->opened)) { 1717 1720 if (ffs->no_disconnect) { 1718 1721 ffs->state = FFS_DEACTIVATED; 1719 - if (ffs->epfiles) { 1720 - ffs_epfiles_destroy(ffs->epfiles, 1721 - ffs->eps_count); 1722 - ffs->epfiles = NULL; 1723 - } 1722 + spin_lock_irqsave(&ffs->eps_lock, flags); 1723 + epfiles = ffs->epfiles; 1724 + ffs->epfiles = NULL; 1725 + spin_unlock_irqrestore(&ffs->eps_lock, 1726 + flags); 1727 + 1728 + if (epfiles) 1729 + ffs_epfiles_destroy(epfiles, 1730 + ffs->eps_count); 1731 + 1724 1732 if (ffs->setup_state == FFS_SETUP_PENDING) 1725 1733 __ffs_ep0_stall(ffs); 1726 1734 } else { ··· 1775 1767 1776 1768 static void ffs_data_clear(struct ffs_data *ffs) 1777 1769 { 1770 + struct ffs_epfile *epfiles; 1771 + unsigned long flags; 1772 + 1778 1773 ENTER(); 1779 1774 1780 1775 ffs_closed(ffs); 1781 1776 1782 1777 BUG_ON(ffs->gadget); 1783 1778 1784 - if (ffs->epfiles) { 1785 - ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count); 1779 + spin_lock_irqsave(&ffs->eps_lock, flags); 1780 + epfiles = ffs->epfiles; 1781 + ffs->epfiles = NULL; 1782 + spin_unlock_irqrestore(&ffs->eps_lock, flags); 1783 + 1784 + /* 1785 + * potential race possible between ffs_func_eps_disable 1786 + * & ffs_epfile_release therefore maintaining a local 1787 + * copy of epfile will save us from use-after-free. 1788 + */ 1789 + if (epfiles) { 1790 + ffs_epfiles_destroy(epfiles, ffs->eps_count); 1786 1791 ffs->epfiles = NULL; 1787 1792 } 1788 1793 ··· 1943 1922 1944 1923 static void ffs_func_eps_disable(struct ffs_function *func) 1945 1924 { 1946 - struct ffs_ep *ep = func->eps; 1947 - struct ffs_epfile *epfile = func->ffs->epfiles; 1948 - unsigned count = func->ffs->eps_count; 1925 + struct ffs_ep *ep; 1926 + struct ffs_epfile *epfile; 1927 + unsigned short count; 1949 1928 unsigned long flags; 1950 1929 1951 1930 spin_lock_irqsave(&func->ffs->eps_lock, flags); 1931 + count = func->ffs->eps_count; 1932 + epfile = func->ffs->epfiles; 1933 + ep = func->eps; 1952 1934 while (count--) { 1953 1935 /* pending requests get nuked */ 1954 1936 if (ep->ep) ··· 1969 1945 1970 1946 static int ffs_func_eps_enable(struct ffs_function *func) 1971 1947 { 1972 - struct ffs_data *ffs = func->ffs; 1973 - struct ffs_ep *ep = func->eps; 1974 - struct ffs_epfile *epfile = ffs->epfiles; 1975 - unsigned count = ffs->eps_count; 1948 + struct ffs_data *ffs; 1949 + struct ffs_ep *ep; 1950 + struct ffs_epfile *epfile; 1951 + unsigned short count; 1976 1952 unsigned long flags; 1977 1953 int ret = 0; 1978 1954 1979 1955 spin_lock_irqsave(&func->ffs->eps_lock, flags); 1956 + ffs = func->ffs; 1957 + ep = func->eps; 1958 + epfile = ffs->epfiles; 1959 + count = ffs->eps_count; 1980 1960 while(count--) { 1981 1961 ep->ep->driver_data = ep; 1982 1962