All of lore.kernel.org
 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>
Subject: Re: [PATCH 02/10] xhci: check for halted host before accessing virt devices in xhci_check_args()
Date: Mon, 30 Nov 2026 10:08:29 +0100	[thread overview]
Message-ID: <20261130100829.6eab5258.michal.pecio@gmail.com> (raw)
In-Reply-To: <20261127121755.281420-3-mathias.nyman@linux.intel.com>

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 <mathias.nyman@linux.intel.com>
> ---
>  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
> 

  reply	other threads:[~2026-11-30  9:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 12:17 [PATCH 00/10] xhci fixes for usb-linus Mathias Nyman
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 02/10] xhci: check for halted host before accessing virt devices in xhci_check_args() Mathias Nyman
2026-11-30  9:08   ` Michal Pecio [this message]
2026-11-30 15:14     ` Alan Stern
2026-12-01 11:23     ` 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 04/10] xhci: Fix reporting incorrect supported speeds in roothub BOS descriptor 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 07/10] usb: xhci: tegra: Fix missing semi-colon 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
2026-11-27 12:17 ` [PATCH 10/10] xhci: fix NULL pointer dereference of rhub_port in xhci_urb_enqueue() Mathias Nyman
2026-11-27 12:28 ` [PATCH 00/10] xhci fixes for usb-linus sashiko-bot

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=20261130100829.6eab5258.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 \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.