From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796022788; b=QEgT+3wnKxQWczdbCt8pmuHCEejaWDKf29k8AOhNfQFcMBXDfofJMWdewwi0x+7ILwtC LNKtpgr4RSW0FFAWK7lizdRSxs3FWnTJxCghVWlsl5R/Y2ryEvIrv2WHHSQCAk8gv8iqE O6L4NszbiRdU8i0MTyroT5K0VnIKPtaW92HY1bRX27p3fYwkK8u5ELhZOti51VecSOywL ThzFFV3gRhfd1op09XfPfo9em1LNvMI2LwPyy0qDjKXho39yMOo1HSlVIqqFvQCKmucZ+ apIDW1wuUE2PlQHI9bVWnYTZVK6ugClTtlaJtl11lgUZbQAu4js3mn1gzaSFmdT/u7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796022788; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=fb1+2VkzkIa3bKvwMx5qHJEUYCwLBSwodaI9I1Wgj1Q=; b=N4bOebt6xEKwstphql1Ecpzu4Gd9hHG71qmOLVuPhFoPsE8rClpH5FzsAJ+vA6dTF3JX IIrVO+y6Gwlza5M/beyagXRN5KxW/ieYMOazLMHYmhZq+9+Bcg+pyCNo0nWr21c5XQpdn bDka2OgQhTHqbZNPRRWTtforZtrJwTvtnih6H3wosmd96MCRcHd4ub8/FPX0A9+a2CXLI ST0KDDpqHGGC09Y6xt4OaB4ucEsU7jyLIH76eLviBXJNlIwJtvVkmCdwDI/DJU0w7RZ7y 9FpYeo5rXg2wv6dOCf/3EjZNuXXI0GKZzw5MWtUWfDI85QCoi+GV1hoOWrycoSCrbBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ib4Xp8Hp; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73B07606A681 for ; Mon, 30 Nov 2026 07:13:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 73B07606A681 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Ib4Xp8Hp Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-4b52fcb410dso4331921f8f.1 for ; Sun, 29 Nov 2026 23:13:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796022787; x=1796627587; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=fb1+2VkzkIa3bKvwMx5qHJEUYCwLBSwodaI9I1Wgj1Q=; b=Ib4Xp8HpL9r4CDb6Zyz/IJP7q8u5/VcUcnZmAmxaRpC4xkP5Ug8LDBBfcNKBmHjjiz Vw+Rc0NyCFF9RHSrDmah0sG1L1wu55Ic7QUOTb/iyBz9KCh1LmqUYn62eYiRxlyrC19e jtFz6RIkE3MwxEOXL7Z+cytLyJtOZRIiJ3VIVjIgrT8fLePZ6Wg1AaNyBKzoyEISIywN TlD1eBFCyO6vhhTeXPiJmUekXFcUxDvqlram0ISlVL+nuKX0Dlmk4Xu9nzezOTUEMyXk fqIPXwi4Q2atSQ3hyd9BCgDEssclg/BXWCBvKug6h8N9gGIRNkv9CkkzUcXKFz5wnl+a X7kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796022787; x=1796627587; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fb1+2VkzkIa3bKvwMx5qHJEUYCwLBSwodaI9I1Wgj1Q=; b=Yu7z5Z73btamr4B3+mtL5bYtl0UF9VbRTGWIW6LoD6c/y7zP86E4m9nDR9Rl3bDj3C D+Vx0mNaucR6a/eb0Fr6WQNuO5O46kQYByIoNcPBUVuPDdFwg7hl6F3JqqW1wnmyANn8 c5glX3Ibo1F1fTldvEVdc+KMkSCQpnx3XAbdr8dl8dp6bk6SMbOKy+otKeWxAae+2Lgn wp3QZfhvdf/t4wBQzeZOrD3bZOV0o4kjUiggRUJbhvqGC72E+EAsjALo1c1jYOIzo06q AY0dI9+A/GNQ71AFZH9MYjkaiKTF549pKxdfKbEXX5MuRQWR95Iu9ZegMBkGBIDbPI3M Ss2A== X-Forwarded-Encrypted: i=1; AL+hErFvZeiXl+6z+9jv7y0iYHPMf6TQnMwwG6YGpFHn1VuqghjB3AK39sA6pVhblOgSnxopogO0Sdt0VNA=@vger.kernel.org X-Gm-Message-State: APuU4Z/zLrzSQEIkA88pFP7c3doR7zLjsgmA4dnTGGJes2+N6Fjop7PQ mpasRpbFY+G76jxC9VmF9F+anmBXQ1XlMPa5Uzq+kqhirb3ZCTFtQ2Q2 X-Gm-Gg: AZFdTZc2pRs7wdPIH5gllumEUcpY7uP4SW8h5+4Udy4MYFRei5brDzeBKiZj0JCBRSo UOiM/SRGYnvSRDRc/J0VhnEn9m2TDx/p94rOt+SkUp2Ll3mGqDIh6Z34NNreFFdggitaNjRFIqN REMsw1GC1voUDbiOZnvOQPkN2vu+e0LlU97Mlh6Un2fBv/hSmtPEX02KjDYMAc9Ek/ND0m7vIb3 rfF1htUYVgFe9O5uvwkZsLZcxNlZLAGM9/JqmPnjAUO0jw0KfNP/sY1N5KFdOIhSTDcWiamfkVH WvqvZbH4KomlUtVcnR5qSzmTK2x4z6/RGUyobZ5ZxhIj+P10UgR0WnzYHMIZVu+cQDfWNcW2z9/ syR9zpKDvv7OEpfZAV1zQSae2gG4qfE/3O/1TRXgSUzOBDPRDYVzcMA3qp/W5tXgrUNZK7DCcWn 9l/FED1LjKaN+wrMPunFCu8RKxMC0m/F3Xbz1l7UpZPOz09Ustr10n5q8WTdDOVFhi+C4JEB4yq MMYeGd7aVcc/8ASHvtDlnLcpJ7xx/jnkcgXBhBO1tBQbKH+rh6sGDIeZbDNJZwgpPDjSbMawQwQ 3tjNhE5H75xH0NU1+XGDajjpGJ3RmaN8mLoxpYo= X-Received: by 2002:a05:600c:b8a:b0:4a0:20b4:ba93 with SMTP id 5b1f17b1804b1-4bb88818156mr647429245e9.16.1796022786541; Sun, 29 Nov 2026 23:13:06 -0800 (PST) Received: from foxbook (bfj60.neoplus.adsl.tpnet.pl. [83.28.47.60]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb88f148aasm367861175e9.0.2026.11.29.23.13.05 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Sun, 29 Nov 2026 23:13:05 -0800 (PST) Date: Mon, 30 Nov 2026 08:13:03 +0100 From: Michal Pecio To: Mathias Nyman Cc: , , stable@vger.kernel.org Subject: Re: [PATCH 09/10] xhci: fix use-after-free for errors mid TD on empty halted rings Message-ID: <20261130081303.2d5235d1.michal.pecio@gmail.com> In-Reply-To: <20261127121755.281420-10-mathias.nyman@linux.intel.com> References: <20261127121755.281420-1-mathias.nyman@linux.intel.com> <20261127121755.281420-10-mathias.nyman@linux.intel.com> X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 > 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 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 >