From: Michael Wu <michael@allwinnertech.com>
To: John Keeping <john@metanate.com>
Cc: quic_linyyuan@quicinc.com, balbi@kernel.org,
gregkh@linuxfoundation.org, axboe@kernel.dk,
quic_pkondeti@quicinc.com, wcheng@codeaurora.org,
quic_ugoswami@quicinc.com, andrew_gabbasov@mentor.com,
plr.vincent@gmail.com, gustavoars@kernel.org,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
allwinner-opensource-support@allwinnertech.com
Subject: Re: [PATCH] usb: f_fs: Fix crash during gadget function switching
Date: Thu, 2 Jun 2022 18:36:47 +0800 [thread overview]
Message-ID: <f2e4f523-9d56-9b5d-cc8e-c9d2c3660996@allwinnertech.com> (raw)
In-Reply-To: <YpUJkxWBNuZiW7Xk@donbot>
On 5/31/2022 2:14 AM, John Keeping wrote:
> On Tue, May 10, 2022 at 04:01:05PM +0800, Michael Wu wrote:
>> On arm64 android12 and possibly other platforms, during the usb gadget
>> function switching procedure (e.g. from mtp to midi), a synchronization
>> issue could occur, which causes an use-after-free panic as shown below:
>
> I assume this is the path through ffs_epfile_io() with !io_data->aio.
> It looks like there is no check there for epfile->ep == ep which the
> other paths do check.
>
> Does the patch below fix the problem without needing to add a new
> completion?
>
Hi John,
Thanks for your suggestion. I've tested your patch and it did work --
When my issue occurs, (epfile->ep != ep) is satisfied, and the error is
handled.
> -- >8 --
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -1084,16 +1084,22 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
> */
> usb_ep_dequeue(ep->ep, req);
> wait_for_completion(&done);
> - interrupted = ep->status < 0;
> + interrupted = true;
> }
>
> - if (interrupted)
> + spin_lock_irq(&epfile->ffs->eps_lock);
> + if (epfile->ep != ep)
> + ret = -ESHUTDOWN;
> + else if (interrupted && ep->status < 0)
> ret = -EINTR;
> - else if (io_data->read && ep->status > 0)
> - ret = __ffs_epfile_read_data(epfile, data, ep->status,
> - &io_data->data);
> else
> ret = ep->status;
> + spin_unlock_irq(&epfile->ffs->eps_lock);
> +
> + if (io_data->read && ret > 0)
> + ret = __ffs_epfile_read_data(epfile, data, ret,
> + &io_data->data);
> +
> goto error_mutex;
> } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
> ret = -ENOMEM;
Tested-by: Michael Wu <michael@allwinnertech.com>
I also tested Linyu's patch [1][2]. It also works.
Is there a preference on these solutions?
[1]
https://lore.kernel.org/linux-usb/1654056916-2062-2-git-send-email-quic_linyyuan@quicinc.com/
[2]
https://lore.kernel.org/linux-usb/1654056916-2062-3-git-send-email-quic_linyyuan@quicinc.com/
--
Regards,
Michael Wu
next prev parent reply other threads:[~2022-06-02 10:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-10 8:01 [PATCH] usb: f_fs: Fix crash during gadget function switching Michael Wu
2022-05-30 18:14 ` John Keeping
2022-06-02 10:36 ` Michael Wu [this message]
2022-06-02 13:05 ` John Keeping
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=f2e4f523-9d56-9b5d-cc8e-c9d2c3660996@allwinnertech.com \
--to=michael@allwinnertech.com \
--cc=allwinner-opensource-support@allwinnertech.com \
--cc=andrew_gabbasov@mentor.com \
--cc=axboe@kernel.dk \
--cc=balbi@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=gustavoars@kernel.org \
--cc=john@metanate.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=plr.vincent@gmail.com \
--cc=quic_linyyuan@quicinc.com \
--cc=quic_pkondeti@quicinc.com \
--cc=quic_ugoswami@quicinc.com \
--cc=wcheng@codeaurora.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).