From: "Fangwen Deng (邓方文)" <Fangwen.Deng@mediatek.com>
To: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>
Cc: "Jarried Lin (林裕哲)" <Jarried.Lin@mediatek.com>,
"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
"Chunfeng Yun (云春峰)" <Chunfeng.Yun@mediatek.com>,
Project_Global_Chrome_Upstream_Group
<Project_Global_Chrome_Upstream_Group@mediatek.com>,
"AngeloGioacchino Del Regno"
<angelogioacchino.delregno@collabora.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Vince-WL Liu (劉文龍)" <Vince-WL.Liu@mediatek.com>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"Justin Yeh (葉英茂)" <Justin.Yeh@mediatek.com>,
"linux-mediatek@lists.infradead.org"
<linux-mediatek@lists.infradead.org>,
"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>
Subject: Re: [PATCH] usb: mtu3: cancel EP0 requests to prevent use-after-free
Date: Tue, 24 Nov 2026 02:56:41 +0000 [thread overview]
Message-ID: <e90c411cc33a6a073db5ba9c81e9ce510eb6fb1e.camel@mediatek.com> (raw)
In-Reply-To: <2026112053-curtain-astound-bf4e@gregkh>
On 五, 2026-11-20 at 13:27 +0100, Greg Kroah-Hartman wrote:
> On Fri, Nov 06, 2026 at 04:00:58PM +0800, Fangwen Deng wrote:
> > mtu3_ep0_dequeue() always returns -EINVAL and leaves the request on
> > mep->req_list without unlinking or completing it, so an EP0 request
> > can never be cancelled.
> >
> > FunctionFS assigns its temporary read buffer to ep0req and waits in
> > __ffs_ep0_queue_wait(). When that wait is interrupted by a signal
> > it
> > calls usb_ep_dequeue(), does not check the return value, and its
> > caller ffs_ep0_read() frees the buffer on every exit path. With
> > -EINVAL returned the request stays queued and MTU3 still owns it.
> > When the host then sends the OUT data stage, ep0_rx_state() writes
> > the received bytes through the stale req->buf, giving the host a
> > content-controlled write into freed kernel memory. That corrupts
> > whichever object reuses the slab and can panic the kernel.
> >
> > Implement mtu3_ep0_dequeue() following mtu3_gadget_dequeue() but
> > without QMU flush/start since EP0 uses PIO: validate the request
> > under mtu->lock, unlink it once and give it back with -ECONNRESET,
> > so ownership of the buffer returns to FunctionFS before it is
> > freed.
> >
> > Cancelling the request that backed the data stage also leaves the
> > transfer unable to finish, so retire the aborted transaction with a
> > protocol stall. That returns ep0_state to SETUP and stops a later
> > ep0_queue() from reading this transaction's leftover FIFO data into
> > the next request's buffer. The stall is skipped when the controller
> > is already powered down or no data stage is in flight.
> >
> > The same stub is why functionfs_unbind() cannot cancel a pending
> > EP0
> > request, which deadlocks against the FunctionFS thread holding
> > ffs->mutex.
> >
> > Fixes: df2069acb005 ("usb: Add MediaTek USB3 DRD driver")
> > Signed-off-by: Fangwen Deng <fangwen.deng@mediatek.com>
> > ---
> > drivers/usb/mtu3/mtu3_gadget_ep0.c | 55
> > ++++++++++++++++++++++++++++--
> > 1 file changed, 53 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/usb/mtu3/mtu3_gadget_ep0.c
> > b/drivers/usb/mtu3/mtu3_gadget_ep0.c
> > index e4fd1bb14a55..73a1af8b6c65 100644
> > --- a/drivers/usb/mtu3/mtu3_gadget_ep0.c
> > +++ b/drivers/usb/mtu3/mtu3_gadget_ep0.c
> > @@ -857,8 +857,59 @@ static int mtu3_ep0_queue(struct usb_ep *ep,
> >
> > static int mtu3_ep0_dequeue(struct usb_ep *ep, struct usb_request
> > *req)
> > {
> > - /* we just won't support this */
> > - return -EINVAL;
> > + struct mtu3_ep *mep;
> > + struct mtu3_request *mreq = NULL;
> > + struct mtu3_request *r = NULL;
> > + struct mtu3 *mtu;
> > + unsigned long flags;
> > + int ret = 0;
> > +
> > + if (!ep || !req)
> > + return -EINVAL;
> > +
> > + mep = to_mtu3_ep(ep);
> > + mtu = mep->mtu;
> > +
> > + spin_lock_irqsave(&mtu->lock, flags);
> > +
> > + mreq = to_mtu3_request(req);
> > +
> > + dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req);
>
> Don't add "trace" calls like this. And __func__ is NEVER needed for
> a
> dev_dbg() call.
>
> thanks,
>
> greg k-h
Thanks for the feedback. I'll remove this dev_dbg() call in the next
version.
Best regards,
Fangwen Deng
prev parent reply other threads:[~2026-11-24 2:56 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 8:00 Fangwen Deng
2026-11-20 12:27 ` Greg Kroah-Hartman
2026-11-24 2:56 ` Fangwen Deng (邓方文) [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=e90c411cc33a6a073db5ba9c81e9ce510eb6fb1e.camel@mediatek.com \
--to=fangwen.deng@mediatek.com \
--cc=Chunfeng.Yun@mediatek.com \
--cc=Jarried.Lin@mediatek.com \
--cc=Justin.Yeh@mediatek.com \
--cc=Project_Global_Chrome_Upstream_Group@mediatek.com \
--cc=Vince-WL.Liu@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=linux-usb@vger.kernel.org \
--cc=matthias.bgg@gmail.com \
/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
all inboxes | Powered by JetHome®