linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Jim Lin <jilin@nvidia.com>
Cc: Felipe Balbi <balbi@kernel.org>,
	linux-usb@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
	Greg KH <gregkh@linuxfoundation.org>,
	Steve Beattie <steve.beattie@canonical.com>
Subject: Re: [PATCH v2] usb: gadget: configfs: Fix KASAN use-after-free
Date: Fri, 1 Sep 2017 10:25:32 -0700	[thread overview]
Message-ID: <CAGXu5jL8zwxUbymeA4cByaJZ8Ju6=Bv+-dbdfc=5Cu4oRwwoJA@mail.gmail.com> (raw)
In-Reply-To: <1484645392-29819-1-git-send-email-jilin@nvidia.com>

On Tue, Jan 17, 2017 at 1:29 AM, Jim Lin <jilin@nvidia.com> wrote:
> When gadget is disconnected, running sequence is like this.
> . composite_disconnect
> . Call trace:
>   usb_string_copy+0xd0/0x128
>   gadget_config_name_configuration_store+0x4
>   gadget_config_name_attr_store+0x40/0x50
>   configfs_write_file+0x198/0x1f4
>   vfs_write+0x100/0x220
>   SyS_write+0x58/0xa8
> . configfs_composite_unbind
> . configfs_composite_bind
>
> In configfs_composite_bind, it has
> "cn->strings.s = cn->configuration;"
>
> When usb_string_copy is invoked. it would
> allocate memory, copy input string, release previous pointed memory space,
> and use new allocated memory.
>
> When gadget is connected, host sends down request to get information.
> Call trace:
>   usb_gadget_get_string+0xec/0x168
>   lookup_string+0x64/0x98
>   composite_setup+0xa34/0x1ee8
>   android_setup+0xb4/0x140
>
> If gadget is disconnected and connected quickly, in the failed case,
> cn->configuration memory has been released by usb_string_copy kfree but
> configfs_composite_bind hasn't been run in time to assign new allocated
> "cn->configuration" pointer to "cn->strings.s".
>
> When "strlen(s->s) of usb_gadget_get_string is being executed, the dangling
> memory is accessed, "BUG: KASAN: use-after-free" error occurs.
>
> Signed-off-by: Jim Lin <jilin@nvidia.com>

Hi! What's the current state of this patch?

> ---
> Changes in v2:
>  Rephrase commit description
>
>  drivers/usb/gadget/configfs.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/usb/gadget/configfs.c b/drivers/usb/gadget/configfs.c
> index 78c4497..39fea62 100644
> --- a/drivers/usb/gadget/configfs.c
> +++ b/drivers/usb/gadget/configfs.c
> @@ -106,6 +106,9 @@ struct gadget_config_name {
>         struct list_head list;
>  };
>
> +#define MAX_USB_STRING_LEN     126
> +#define MAX_USB_STRING_WITH_NULL_LEN   (MAX_USB_STRING_LEN+1)
> +
>  static int usb_string_copy(const char *s, char **s_copy)
>  {
>         int ret;
> @@ -115,12 +118,16 @@ static int usb_string_copy(const char *s, char **s_copy)
>         if (ret > 126)

This should be MAX_USB_STRING_LEN, yes?

>                 return -EOVERFLOW;
>
> -       str = kstrdup(s, GFP_KERNEL);
> -       if (!str)
> -               return -ENOMEM;
> +       if (copy) {
> +               str = copy;
> +       } else {
> +               str = kmalloc(MAX_USB_STRING_WITH_NULL_LEN, GFP_KERNEL);
> +               if (!str)
> +                       return -ENOMEM;
> +       }
> +       strcpy(str, s);
>         if (str[ret - 1] == '\n')
>                 str[ret - 1] = '\0';
> -       kfree(copy);
>         *s_copy = str;
>         return 0;
>  }
> --
> 2.7.4
>

-Kees


-- 
Kees Cook
Pixel Security

  reply	other threads:[~2017-09-01 17:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-17  9:29 [PATCH v2] usb: gadget: configfs: Fix KASAN use-after-free Jim Lin
2017-09-01 17:25 ` Kees Cook [this message]
     [not found]   ` <CACCg+XODNWZ5FMYLxQaLpspjDYKB_3Uzx1zC-q0dAo2g8xpOYw@mail.gmail.com>
2020-06-11  4:24     ` Macpaul Lin

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='CAGXu5jL8zwxUbymeA4cByaJZ8Ju6=Bv+-dbdfc=5Cu4oRwwoJA@mail.gmail.com' \
    --to=keescook@chromium.org \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jilin@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=steve.beattie@canonical.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 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).