linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: rtm@csail.mit.edu
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg KH <gregkh@linuxfoundation.org>, linux-usb@vger.kernel.org
Subject: Re: USB hub code can dereference NULL hub and hub->ports
Date: Wed, 22 Jan 2025 14:21:08 -0500	[thread overview]
Message-ID: <96145.1737573668@localhost> (raw)
In-Reply-To: Your message of "Wed, 22 Jan 2025 10:55:24 EST." <d86313f9-e77b-47a5-9a84-01d71493b15c@rowland.harvard.edu>

Alan,

Yes, your patch makes the NULL hub and hub->ports crashes
I've seen go away!

Robert

> Date: Wed, 22 Jan 2025 10:55:24 -0500
> From: Alan Stern <stern@rowland.harvard.edu>
> To: rtm@csail.mit.edu
> Cc: Greg KH <gregkh@linuxfoundation.org>, linux-usb@vger.kernel.org
> Subject: Re: USB hub code can dereference NULL hub and hub->ports
> 
> On Wed, Jan 22, 2025 at 06:37:45AM -0500, rtm@csail.mit.edu wrote:
> > > Great, can you submit patches to fix these issues now that you have a
> > > reliable test program to verify the problem?
> > 
> > I think the problem is (at least sometimes) not that hub->ports is
> > legitimately NULL and there's a missing check, but that
> > usb_hub_to_struct_hub() returns an object of the wrong type so that
> > hub->ports is junk, and only accidentally NULL in the demo I
> > previously submitted.
> > 
> > I've attached a new demo which crashes because hub->ports is
> > 0xcccccccccccccccc (on a kernel with red zones). The demo presents a
> > USB device whose DeviceClass is a hub (9), with two interfaces, but
> > the Vendor and Product indicate an FTDI serial adaptor.
> > 
> > First, usb_serial_probe() sets the interface zero dev->driver_data to
> > a struct usb_serial.
> > 
> > Later, when the hub code is trying to set up interface one, it calls
> > usb_hub_to_struct_hub(hdev):
> > 
> > struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
> > {
> >         if (!hdev || !hdev->actconfig || !hdev->maxchild)
> >                 return NULL;
> >         return usb_get_intfdata(hdev->actconfig->interface[0]);
> > }
> > 
> > interface[0], however, has been set up by the serial port code, and
> > its dev->driver_data is a struct usb_serial, not a struct usb_hub.
> 
> Okay, that explains the problem.  usb_hub_to_struct_hub() assumes that a 
> hub device will never have more than one interface, because that 
> requirement is in the USB spec.  But of course, a bogus or malicious 
> device can violate the requirement.
> 
> I think the best way to deal with this issue is to prevent the hub 
> driver from binding to non-compliant devices.  Does the patch below fix 
> the problem for you?
> 
> Alan Stern
> 
> 
> 
> Index: usb-devel/drivers/usb/core/hub.c
> ===================================================================
> --- usb-devel.orig/drivers/usb/core/hub.c
> +++ usb-devel/drivers/usb/core/hub.c
> @@ -1848,6 +1848,17 @@ static int hub_probe(struct usb_interfac
>  	hdev = interface_to_usbdev(intf);
>  
>  	/*
> +	 * The USB 2.0 spec prohibits hubs from having more than one
> +	 * configuration or interface, and we rely on this prohibition.
> +	 * Refuse to accept a device that violates it.
> +	 */
> +	if (hdev->descriptor.bNumConfigurations > 1 ||
> +			hdev->actconfig->desc.bNumInterfaces > 1) {
> +		dev_err(&intf->dev, "Invalid hub with more than one config or interface\n");
> +		return -EINVAL;
> +	}
> +
> +	/*
>  	 * Set default autosuspend delay as 0 to speedup bus suspend,
>  	 * based on the below considerations:
>  	 *

  reply	other threads:[~2025-01-22 19:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-20 17:27 USB hub code can dereference NULL hub and hub->ports rtm
2025-01-21  7:01 ` Greg KH
2025-01-22 11:37   ` rtm
2025-01-22 15:55     ` Alan Stern
2025-01-22 19:21       ` rtm [this message]
2025-01-22 19:26         ` [PATCH] USB: hub: Ignore non-compliant devices with too many configs or interfaces Alan Stern
2025-02-03 15:35           ` Alan Stern
2025-02-03 15:49             ` Greg KH

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=96145.1737573668@localhost \
    --to=rtm@csail.mit.edu \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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;
as well as URLs for NNTP newsgroup(s).