All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jaegyun Lee" <dreams200260@g.skku.edu>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl
Date: Thu, 26 Nov 2026 07:03:24 +0000	[thread overview]
Message-ID: <sashiko-outbox-234927@kernel.org> (raw)
In-Reply-To: <20261126-ffs-revmap-v1-1-e1322f6fa290@g.skku.edu>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: f_fs: UAF and stale ffs->func in
  ffs_func_unbind() after unbind
--

commit 54f606e52beb80500efc1407df54d86b84f48d2a
Author: Jaegyun Lee <dreams200260@g.skku.edu>

usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl

This commit protects FUNCTIONFS_INTERFACE_REVMAP and the teardown of
ffs->func and func->eps in ffs_func_unbind() with ffs->eps_lock.

> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index c64a268e98a43..c824d1d5ce571 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
[ ... ]
> @@ -4203,10 +4208,8 @@ static void ffs_func_unbind(struct usb_configuration *c,
>  	unsigned count = ffs->eps_count;
>  	unsigned long flags;
>  
> -	if (ffs->func == func) {
> +	if (ffs->func == func)
>  		ffs_func_eps_disable(func);
> -		ffs->func = NULL;
> -	}
>  
>  	/* Drain any pending AIO completions */
>  	drain_workqueue(ffs->io_completion_wq);
> @@ -4217,13 +4220,14 @@ static void ffs_func_unbind(struct usb_configuration *c,
>  
>  	/* cleanup after autoconfig */
>  	spin_lock_irqsave(&func->ffs->eps_lock, flags);
> +	if (ffs->func == func)
> +		ffs->func = NULL;

[Severity: Medium]
Can moving ffs->func = NULL after functionfs_unbind(ffs) cause a
use-after-free on ffs or an out-of-bounds read in ffs_func_revmap_intf()?

functionfs_unbind() clears FFS_FL_BOUND and drops the gadget's reference
with ffs_data_put(ffs).  Once FFS_FL_BOUND is clear, ffs_closed() skips
unregister_gadget_item() and no longer serializes on gi->lock against the
running unbind, so a concurrent unmount in ffs_fs_kill_sb() can drop the
superblock reference and free ffs before ffs_func_unbind() takes
func->ffs->eps_lock:

CPU0                                  CPU1
ffs_func_unbind()
  ffs_event_add(..., FUNCTIONFS_UNBIND)
  functionfs_unbind(ffs)
    clear_bit(FFS_FL_BOUND, &ffs->flags)
    ffs_data_put(ffs)
                                      ffs_fs_kill_sb()
                                        ffs_data_reset(ffs)
                                          ...
                                            ffs_closed()
                                              /* FFS_FL_BOUND is clear */
                                        ffs_data_put(ffs)  <-- frees ffs
  spin_lock_irqsave(&func->ffs->eps_lock)  <-- use-after-free
  if (ffs->func == func)
    ffs->func = NULL;

In addition, leaving ffs->func set across functionfs_unbind() means
ffs_data_reset() can reset ffs->state to FFS_READ_DESCRIPTORS while
ffs->func still points to the old function.  If userspace writes new
descriptors with a larger ffs->interfaces_count and calls
FUNCTIONFS_INTERFACE_REVMAP before ffs_func_unbind() acquires eps_lock,
ffs_func_revmap_intf() walks the old func->interfaces_nums array using
the new count.

Performing the eps_lock cleanup block (clearing ffs->func, freeing
ep->req, and freeing func->eps) before ffs_event_add() and
functionfs_unbind() would keep ffs pinned and FFS_FL_BOUND set until
func teardown is complete.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-ffs-revmap-v1-1-e1322f6fa290@g.skku.edu?part=1

      reply	other threads:[~2026-11-26  7:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  6:40 [PATCH] usb: gadget: f_fs: fix UAF read in INTERFACE_REVMAP ioctl Jaegyun Lee via B4 Relay
2026-11-26  6:40 ` Jaegyun Lee
2026-11-26  7:03 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-234927@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dreams200260@g.skku.edu \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.