All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Sam Sun <samsun1006219@gmail.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org,
	 Greg KH <gregkh@linuxfoundation.org>,
	swboyd@chromium.org, ricardo@marliere.net,  hkallweit1@gmail.com,
	heikki.krogerus@linux.intel.com,  mathias.nyman@linux.intel.com,
	royluo@google.com,  syzkaller-bugs@googlegroups.com,
	xrivendell7@gmail.com
Subject: Re: [Linux kernel bug] general protection fault in disable_store
Date: Wed, 17 Apr 2024 15:39:02 +0800	[thread overview]
Message-ID: <CAEkJfYMPQJn+kYzxFwwix3fwKeu3aAdYKgp+Ksvq=o4CoTXEWQ@mail.gmail.com> (raw)
In-Reply-To: <5f3526a6-6ede-4181-a4ff-076e022cfb49@rowland.harvard.edu>

On Wed, Apr 17, 2024 at 12:35 AM Alan Stern <stern@rowland.harvard.edu> wrote:
>
> On Tue, Apr 16, 2024 at 05:05:55PM +0800, Sam Sun wrote:
> > On Mon, Apr 15, 2024 at 10:47 PM Alan Stern <stern@rowland.harvard.edu> wrote:
> > >
> > > Actually, I've got a completely different patch which I think will fix
> > > the problem you encountered.  Instead of using mutual exclusion to
> > > avoid the race, it prevents the two routines from being called at the
> > > same time so the race can't occur in the first place.  It also should
> > > guarantee the usb_hub_to_struct_hub() doesn't return NULL when
> > > disable_store() calls it.
> > >
> > > Can you try the patch below, instead of (not along with) the first
> > > patch?  Thanks.
> > >
> > > Alan Stern
>
> > I tried this patch and it worked. I agree this patch is better and it
> > avoids introducing new locks.
>
> It turns out that patch is no good.  The reason is mentioned in the
> changelog for commit 543d7784b07f ("USB: fix race between hub_disconnect
> and recursively_mark_NOTATTACHED"); it says that the port devices have to
> be removed _after_ maxchild has been set to 0.
>

I checked the commit you mentioned. Maybe your first fix is all we
need to fix the problem? At least no race would occur for
hdev->maxchild and usb_set_intfdata().

> So okay, here's a third attempt to fix the problem.  This doesn't try to
> avoid the race or anything like that.  Instead it just adds checks for
> usb_hub_to_struct_hub() returning NULL.  That should be enough to prevent
> the invalid pointer dereference you encountered.
>
> This should be tested by itself, without either of the first two patches.
>
> Alan Stern
>
>
>
> Index: usb-devel/drivers/usb/core/port.c
> ===================================================================
> --- usb-devel.orig/drivers/usb/core/port.c
> +++ usb-devel/drivers/usb/core/port.c
> @@ -51,13 +51,15 @@ static ssize_t disable_show(struct devic
>         struct usb_port *port_dev = to_usb_port(dev);
>         struct usb_device *hdev = to_usb_device(dev->parent->parent);
>         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
> -       struct usb_interface *intf = to_usb_interface(hub->intfdev);
> +       struct usb_interface *intf = to_usb_interface(dev->parent);
>         int port1 = port_dev->portnum;
>         u16 portstatus, unused;
>         bool disabled;
>         int rc;
>         struct kernfs_node *kn;
>
> +       if (!hub)
> +               return -ENODEV;
>         hub_get(hub);
>         rc = usb_autopm_get_interface(intf);
>         if (rc < 0)
> @@ -101,12 +103,14 @@ static ssize_t disable_store(struct devi
>         struct usb_port *port_dev = to_usb_port(dev);
>         struct usb_device *hdev = to_usb_device(dev->parent->parent);
>         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
> -       struct usb_interface *intf = to_usb_interface(hub->intfdev);
> +       struct usb_interface *intf = to_usb_interface(dev->parent);
>         int port1 = port_dev->portnum;
>         bool disabled;
>         int rc;
>         struct kernfs_node *kn;
>
> +       if (!hub)
> +               return -ENODEV;
>         rc = kstrtobool(buf, &disabled);
>         if (rc)
>                 return rc;
>

I applied this patch and it also can fix the warning. I am not sure
which one is better.

Best,
Yue

  reply	other threads:[~2024-04-17  7:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11  6:52 [Linux kernel bug] general protection fault in disable_store Sam Sun
2024-04-11  6:58 ` Greg KH
2024-04-11  7:19   ` Sam Sun
2024-04-11 15:24 ` Alan Stern
2024-04-12 13:08   ` Sam Sun
2024-04-12 14:40     ` Alan Stern
2024-04-12 16:26       ` Sam Sun
2024-04-12 18:11         ` Alan Stern
2024-04-12 18:32           ` Alan Stern
2024-04-13  5:08           ` Sam Sun
2024-04-15 14:47             ` Alan Stern
2024-04-16  9:05               ` Sam Sun
2024-04-16 16:35                 ` Alan Stern
2024-04-17  7:39                   ` Sam Sun [this message]
2024-04-17 14:23                     ` Alan Stern

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='CAEkJfYMPQJn+kYzxFwwix3fwKeu3aAdYKgp+Ksvq=o4CoTXEWQ@mail.gmail.com' \
    --to=samsun1006219@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=hkallweit1@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@linux.intel.com \
    --cc=ricardo@marliere.net \
    --cc=royluo@google.com \
    --cc=stern@rowland.harvard.edu \
    --cc=swboyd@chromium.org \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=xrivendell7@gmail.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.