Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Michal Pecio <michal.pecio@gmail.com>
To: Mathias Nyman <mathias.nyman@linux.intel.com>
Cc: <gregkh@linuxfoundation.org>, <linux-usb@vger.kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH 09/10] xhci: fix use-after-free for errors mid TD on empty halted rings
Date: Mon, 30 Nov 2026 08:13:03 +0100	[thread overview]
Message-ID: <20261130081303.2d5235d1.michal.pecio@gmail.com> (raw)
In-Reply-To: <20261127121755.281420-10-mathias.nyman@linux.intel.com>

On Fri, 27 Nov 2026 14:17:54 +0200, Mathias Nyman wrote:
> If a TD with an error mid TD is followed by an event that doesn't point
> to that TD, handle_tx_event() gives back the TD with xhci_dequeue_td().
> If this was the last TD of the URB, the URB is given back and the TD,
> part of the freed urb_priv, is freed with it.
> 
> If the TD list is then empty

... and we are getting more transfer events, then it's a HW bug.
Or a driver bug which should be fixed.

> and the endpoint is halted,

Then it's another HW bug because isochronous endpoints don't halt.

> handle_tx_event() jumps to check_endpoint_halted and passes the now
> dangling td pointer to xhci_handle_halted_endpoint(), which adds it
> to the endpoint's cancelled TD list. This causes a use-after-free and
> corrupts the cancelled TD list.
> 
> Set td to NULL after giving it back. xhci_handle_halted_endpoint()
> handles a NULL td, and td is reassigned if there are more TDs on the
> ring.
> 
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/bug/linux-2b321b4d-f99f-4acd-9590-8dd98a82ebb5
> Fixes: f42a36bae064 ("usb: xhci: Fix handling errors mid TD followed by other errors")
> Cc: stable@vger.kernel.org # 6.12+
> Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>

I don't disagree with the diff even if I never cared about that while
working on this code before, but commit message could be more honest:

  Hardware is buggy enough that we would rather waste one or two bytes
  of code than risk that bogus events corrupt memory by completing the
  same TD twice if it's the last TD but we keep receiving more events
  and they suggest that this isochronous endpoint has halted.

I'm personally not sure if HW is buggy enough for *this* to happen, but
we know that it is plenty buggy in general. Now that I think about it,
ISTR a reported case of Stall Error on isoc in some Intel chipset.


And there is a simpler fix which decreases rather than increases driver
code size: get rid of check_endpoint_halted altogether, because every
case of transfer events on an empty endpoint or No-Op is a bug and much
has been done to solve those bugs since 810a624bd1b64 10 years ago.

The author even understood the bug, but didn't seem to appreciate that
cancelling URBs concurrently with their execution is not intended and
never a good idea for a multitude of reasons:

https://marc.info/?l=linux-usb&m=149612023718060&w=2

  4. Every time a URB is killed, the endpoint ring is restarted and URBs
  that are not killed yet are executed. If we are unlucky, the URB's TRB
  is gets cancelled right after it has been executed but before
  xhci_irq() gets to run, causing this condition.

Regards,
Michal

> ---
>  drivers/usb/host/xhci-ring.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
> index fca23daf6add..7ccd89a4b995 100644
> --- a/drivers/usb/host/xhci-ring.c
> +++ b/drivers/usb/host/xhci-ring.c
> @@ -2891,6 +2891,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
>  	if (td && td->error_mid_td && !trb_in_td(td, ep_trb_dma)) {
>  		xhci_dbg(xhci, "Missing TD completion event after mid TD error\n");
>  		xhci_dequeue_td(xhci, td, ep_ring, td->status);
> +		td = NULL;
>  	}
>  
>  	if (list_empty(&ep_ring->td_list)) {
> -- 
> 2.43.0
> 

  reply	other threads:[~2026-11-30  7:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261127121755.281420-1-mathias.nyman@linux.intel.com>
2026-11-27 12:17 ` [PATCH 01/10] usb: xhci: Fix ep0 maxpacket update on enabled slots Mathias Nyman
2026-11-27 12:17 ` [PATCH 03/10] xhci: Give back URB under lock in xhci_urb_dequeue() Mathias Nyman
2026-11-27 12:17 ` [PATCH 05/10] xhci: sideband: avoid use-after-free of stale virt device pointer Mathias Nyman
2026-11-27 12:17 ` [PATCH 06/10] xhci: sideband: check virt device under xhci lock when stopping endpoints Mathias Nyman
2026-11-27 12:17 ` [PATCH 08/10] usb: xhci: Demote isoch buffer overrun message to debug level Mathias Nyman
2026-11-27 12:17 ` [PATCH 09/10] xhci: fix use-after-free for errors mid TD on empty halted rings Mathias Nyman
2026-11-30  7:13   ` Michal Pecio [this message]
2026-11-27 12:17 ` [PATCH 10/10] xhci: fix NULL pointer dereference of rhub_port in xhci_urb_enqueue() Mathias Nyman

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=20261130081303.2d5235d1.michal.pecio@gmail.com \
    --to=michal.pecio@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=stable@vger.kernel.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