From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Bui Quang Minh <minhquangbui99@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org
Subject: Re: [BUG REPORT] usb: usb-skeleton: Race condition between skel_open and skel_disconnect
Date: Mon, 24 May 2021 11:50:17 -0700 [thread overview]
Message-ID: <YKv1aYqdGWopI2fp@google.com> (raw)
In-Reply-To: <2f3f0176-244c-d76b-3d7d-15b332c87041@gmail.com>
Hi,
On Mon, May 17, 2021 at 09:31:57PM +0700, Bui Quang Minh wrote:
> Hi,
>
> I spotted this bug through code review and I don't know how to make a Proof
> of Concept for this bug so maybe I'm wrong.
>
> Between skel_open() and skel_disconnect(), this scenario can happen
>
> skel_open() skel_disconnect()
> dev = usb_get_intfdata(interface);
> usb_set_intfdata(interface, NULL);
> kref_put(&dev->kref, skel_delete);
> kref_get(&dev->kref);
>
> In case dev's refcount is 1 before these events, kref_put() in
> skel_disconnect() will call the skel_delete to free dev. As a result, a UAF
> will happen when we try to access dev->kref in skel_open(). I can see this
> pattern in other USB drivers as well such as usblcd.c, yurex.c, ...
>
> Please correct me if I am wrong.
I think it is mostly OK. As far as I can see the minor_rwsem in
drivers/usb/core/file.c makes sure that usb_open()/skel_open() either
complete if they happen before call to usb_deregister_dev() in
skel_disconnect() or usb_open() errors out and not call into skel_open()
if usb_deregister_dev() already completed. So there is no issue with
bumping refcount while the structure is being deleted.
This only leaves usb_get_intfdata() returning NULL because we set it to
NULL too early in skel_disconnect(). I'd recommend moving
"usb_set_intfdata(interface, NULL)" to happen after call to
usb_deregister_dev() in skel_disconnect().
Thanks.
--
Dmitry
prev parent reply other threads:[~2021-05-24 18:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-17 14:31 [BUG REPORT] usb: usb-skeleton: Race condition between skel_open and skel_disconnect Bui Quang Minh
2021-05-24 18:50 ` Dmitry Torokhov [this message]
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=YKv1aYqdGWopI2fp@google.com \
--to=dmitry.torokhov@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-usb@vger.kernel.org \
--cc=minhquangbui99@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.