From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.218.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796029708; b=sxf6BVi6JNmQZa1/WWoiqIGkvHAodl/wtcjg7DMSWxtXw5jYn8q3n2S9l9DfAkILe9Nk 1tCOwCcpxONNhSTFYcGMw+vCESt/jw/IlIZwQjd+I/F7rvnAzu20dJhnLBcqTUYv5mo/G v/HBBoP+TYXSFK7NjJSSML2wS5OLNoUtdweJEbcdbtRH4Gtl8kPw/rmnasKYD5WY/Noq2 t8R4lTyeKqmHNI17LJ3c7JBi0+Nph5ASs5ZF3fowH6HieBvG98m+O8k96U6LfhAVhvyrs CuSrD/qJ1Qd2VVzbA0vWAEbMtfV5E4aAakCE4nacxfNiVH5Nl8xjTm3ntsGHfX5Iruw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796029708; 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=aLs9LNZRlkEYl8j6UagRKxTgzWVWWBbx50TycLahuUY=; b=KZmvq2fOSRtbUSUGLgadKPHyswkYSNSptZFCTnpmQnT9f22sHwP0RAzOeXkhTPPRkqPM Z0cvV74EeSnJINGg3Yy37uSrpxalbU3Dwy8uHeyK12ccthqUDLPL/teF08Kl7iTSE0EZ7 ndJwikIGsIXGJVI/GLNnmE4Rs48tW8FCfBCypEDXjCMqx4wv3cA7oD6Q4bH8FHj/lcBmA gNBPOpSrJ9PIy/oy6lZBx3v/9zMxQ1MomeNiaYN19eUi/VUDohT7R/PYXjG5S97E2nRCE HSZG1to7k5k/3NAiL22vLGr6ToyyU4XBhvMRJVj5mWsc5vwPnE/qUWa5Mt+yIUh/L0Q== 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="TodxY/nG"; arc=none smtp.remote-ip=209.85.218.48 Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (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 E7C85606A325 for ; Mon, 30 Nov 2026 09:08:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E7C85606A325 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=TodxY/nG Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-c4d2b741ed9so1636376766b.2 for ; Mon, 30 Nov 2026 01:08:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796029706; x=1796634506; 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=aLs9LNZRlkEYl8j6UagRKxTgzWVWWBbx50TycLahuUY=; b=TodxY/nG7qVkl5G1aSpNJXxj9Jbv4NnrXrBcy73KR+7wxPbVN+964Bg4k55AA22rw7 7YHjNO3mA6dkzQU5daMjGky59Rv2BjXNA+855AISKP/ODFf4NlRMZZ8EHW8NmwLRtR9l 3t1JAJWm4Dtp/j8PhsREmltDU7kLaHQPM/nm+EA0UwKjj86EYJe4zTAS5JZZXjdPKAk6 a+UCNzMHZZFGO3Fyh+253x9yPV//PZ3g7jbhEWWQ+VhfY1D6idrE8g5yOUIwC2cRoknD HInqzSyTo2xqD/d3MpyPXT+oRpOpy0/+MQ+N99K7jlEX8cFYKCxJB8+MpRGNxb4AeMXU rgOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796029706; x=1796634506; 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=aLs9LNZRlkEYl8j6UagRKxTgzWVWWBbx50TycLahuUY=; b=ZJywzUuHHplRnxRooE1Hy1UM2ghfWHh51EmhhDSMwQDTeNMk6cmofHMLRS1F5vS8EB +rj9MiPogNiLutQ5U42DdaBeXJl4eZ3d9mPNApKYgh0QT0JpaIcAj6AlCX7D4EVro3IH 5CWU5XOaib2hjPe2WqjooA/xIiY0UJZ4NbdPS0oXqkEoqt1eYI9URtapMsbfgQ9lleoN Ehq3UlrA3oE9r6BN7HpAheI4EN1tZl5sv9JhUF8rnShCGfTBKAwVmWkwOpD9auItH1qX dtatr6SvOTvtxeVblHL1m25Bm8AijloUykWt2mG/k2oIkg8DJvLJ0XT014dBHgKReILA ks0Q== X-Forwarded-Encrypted: i=1; AL+hErH+EqzAQI8bk0Am+znjInBtNY89vlyBwMOsw9A1ng69271+iJ/zOlvXn0kJ+hB5q7R7EYbwuzPIRxk=@vger.kernel.org X-Gm-Message-State: APuU4Z+bqDvHviCZhoAJVKKgqP1OiovMY/jWHuMZWWhtqG0FTAy637WT NnYmj57rd0Dnb+r6yU9JiD9XXXLFo+5M5c+sIEWNTj7GztkARytFUV2F X-Gm-Gg: AZFdTZdeEKzCekrlswPlNGsHBal+iV5yJ6r+EVmW9/mJEXiRSC5lWGitD9fW3GhQcSq JLxhhwMuTDI7ZD/k06NPQ4hNGx00F+SpVVz6eaF/eh6qbKLlgnJKK51pWHnp22AAl2CCOTFd9d8 qSq8kMAxPmwO+XFBRGtpcUdNNn7hzXtr16FL0tUMV2m5vwaNFikN5a81U61QHecJb9C56a3jAol +HizwbRMfl99KdDwZapX+tb2Vqbf1LyQXynxE/ous/Nk83L8ntZRWyBbGU5fBdGJdS6XoJzfa+O 8k33BqBfDedRd+SWFGXqH+WhOO4BgQ5CpHmkhw1JNjBbT+rMJEGnQpxJ6G9KmUXEzFX+7Ix8pih njIlorW6kIewMAahwBAh85XqrLdz4tRanYVXQp9LljwhA7pzcB1bxLxJ4oY7qiJf6P6LfeeCiBi ury+eGG14gB0BPGwthnNEhtV+Ce60fx0DBghW/2pAXpKKWTKf9ktFqiTCKTNlPPA0SS8Olj1aq4 xi0fQP8Wf/DaCQ2qWiyDuh6eytWYE2/NbbmmTDx7r3JeUYSlxM+4IY3997DzgUM+kKUD4eHytzy rH65fuH7f+g4uXt5UqfxiJP26Ows1Ycxd5KYGcc= X-Received: by 2002:a17:907:3cb:b0:c51:40e:847c with SMTP id a640c23a62f3a-c513a84ab4emr1634539066b.23.1796029706178; Mon, 30 Nov 2026 01:08:26 -0800 (PST) Received: from foxbook (bfj60.neoplus.adsl.tpnet.pl. [83.28.47.60]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab658e2d1bsm65464539f8f.8.2026.11.30.01.08.25 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 30 Nov 2026 01:08:26 -0800 (PST) Date: Mon, 30 Nov 2026 10:08:29 +0100 From: Michal Pecio To: Mathias Nyman Cc: , Subject: Re: [PATCH 02/10] xhci: check for halted host before accessing virt devices in xhci_check_args() Message-ID: <20261130100829.6eab5258.michal.pecio@gmail.com> In-Reply-To: <20261127121755.281420-3-mathias.nyman@linux.intel.com> References: <20261127121755.281420-1-mathias.nyman@linux.intel.com> <20261127121755.281420-3-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:47 +0200, Mathias Nyman wrote: > xhci_resume() may halt and reset xHC. It then frees all virt devices > without holding xhci->lock, before the host is reinitialized and > started again. It seems it could take this lock, if debugfs_remove() is factored out. > xhci_check_args() reads xhci->devs[udev->slot_id], and only checks > for a halted host after it has dereferenced the virt device. > > If an URB is submitted while the virt devices are freed, this can lead > to a NULL pointer dereference, or a use-after-free of a virt device > whose rings and contexts were already freed. > > This is possible during runtime resume. Userspace can allow a device > opened via usbfs to runtime suspend with USBDEVFS_ALLOW_SUSPEND, and > still submit URBs to it. Interesting, is usbfs supposed to allow this? It seems the main mechanism preventing submission to suspended devices is udev->can_submit, which is only tested by usb_hcd_link_urb_to_ep() after a lot of HCD code has already executed and (somehow) survived. Regards, Michal > Check for a halted host first in xhci_check_args(). XHCI_STATE_HALTED is > set under xhci->lock by xhci_halt() before the virt devices are freed, > and cleared only when xhci_start() starts the host again. > > Fixes: 203a86613fb3 ("xhci: Avoid NULL pointer deref when host dies.") > Signed-off-by: Mathias Nyman > --- > drivers/usb/host/xhci.c | 18 +++++++++++++++--- > 1 file changed, 15 insertions(+), 3 deletions(-) > > diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c > index 6e0729a07e5f..b3df40f22d55 100644 > --- a/drivers/usb/host/xhci.c > +++ b/drivers/usb/host/xhci.c > @@ -1514,6 +1514,14 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, > } > > xhci = hcd_to_xhci(hcd); > + > + /* > + * Check halted before touching virt devices. Resume halts the host and > + * frees all virt devices without xhci->lock if it needs to be reset. > + */ > + if (xhci->xhc_state & XHCI_STATE_HALTED) > + return -ENODEV; > + > if (check_virt_dev) { > if (!udev->slot_id || !xhci->devs[udev->slot_id]) { > xhci_dbg(xhci, "xHCI %s called with unaddressed device\n", > @@ -1529,9 +1537,6 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, > } > } > > - if (xhci->xhc_state & XHCI_STATE_HALTED) > - return -ENODEV; > - > return 1; > } > > @@ -4132,7 +4137,14 @@ static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) > if (ret <= 0 && ret != -ENODEV) > return; > > + /* Halted host returns -ENODEV before virt device is checked */ > + if (!udev->slot_id || !xhci->devs[udev->slot_id]) > + return; > + > virt_dev = xhci->devs[udev->slot_id]; > + if (virt_dev->udev != udev) > + return; > + > slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); > trace_xhci_free_dev(slot_ctx); > > -- > 2.43.0 >