lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAGXu5jJDk+w7yW-gg9MqeTfmBTPNWcLBYGZ1GmpBP073Hu4GMw@mail.gmail.com>
Date:   Fri, 15 Jun 2018 16:17:24 -0700
From:   Kees Cook <keescook@...omium.org>
To:     Zhouyang Jia <jiazhouyang09@...il.com>
Cc:     Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
        Christophe JAILLET <christophe.jaillet@...adoo.fr>,
        Colin Ian King <colin.king@...onical.com>,
        Jia-Ju Bai <baijiaju1990@...il.com>,
        Shreeya Patel <shreeya.patel23498@...il.com>,
        devel@...verdev.osuosl.org, LKML <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v3] staging: rtl8192u: add error handling for usb_alloc_urb

On Fri, Jun 15, 2018 at 10:28 AM, Zhouyang Jia <jiazhouyang09@...il.com> wrote:
> When usb_alloc_urb fails, the lack of error-handling code may
> cause unexpected results.
>
> This patch adds error-handling code after calling usb_alloc_urb.
>
> Signed-off-by: Zhouyang Jia <jiazhouyang09@...il.com>
> ---
> v1->v2:
> - Fix memory leak.
> v2->v3:
> - Release memory in error path.
> ---
>  drivers/staging/rtl8192u/r8192U_core.c | 24 +++++++++++++++---------
>  1 file changed, 15 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/staging/rtl8192u/r8192U_core.c b/drivers/staging/rtl8192u/r8192U_core.c
> index 7a0dbc0..1c980e9 100644
> --- a/drivers/staging/rtl8192u/r8192U_core.c
> +++ b/drivers/staging/rtl8192u/r8192U_core.c
> @@ -1649,12 +1649,12 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
>         for (i = 0; i < (MAX_RX_URB + 1); i++) {
>                 priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
>                 if (!priv->rx_urb[i])
> -                       return -ENOMEM;
> +                       goto out_release_mem;

You need to use kcalloc() above here for priv->rx_urb, otherwise you
may free random garbage. :)

-Kees

>
>                 priv->rx_urb[i]->transfer_buffer =
>                         kmalloc(RX_URB_SIZE, GFP_KERNEL);
>                 if (!priv->rx_urb[i]->transfer_buffer)
> -                       return -ENOMEM;
> +                       goto out_release_mem;
>
>                 priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
>         }
> @@ -1666,9 +1666,13 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
>                 void *oldaddr, *newaddr;
>
>                 priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
> +               if (!priv->rx_urb[16])
> +                       goto out_release_mem;
> +
>                 priv->oldaddr = kmalloc(16, GFP_KERNEL);
>                 if (!priv->oldaddr)
> -                       return -ENOMEM;
> +                       goto out_release_mem;
> +
>                 oldaddr = priv->oldaddr;
>                 align = ((long)oldaddr) & 3;
>                 if (align) {
> @@ -1686,17 +1690,19 @@ static short rtl8192_usb_initendpoints(struct net_device *dev)
>         priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
>                                  GFP_KERNEL);
>         if (!priv->pp_rxskb) {
> -               kfree(priv->rx_urb);
> -
> -               priv->pp_rxskb = NULL;
> -               priv->rx_urb = NULL;
> -
>                 DMESGE("Endpoint Alloc Failure");
> -               return -ENOMEM;
> +               goto out_release_mem;
>         }
>
>         netdev_dbg(dev, "End of initendpoints\n");
>         return 0;
> +
> +out_release_mem:
> +       for (i = 0; i < (MAX_RX_URB + 1); i++)
> +               kfree(priv->rx_urb[i]);
> +       kfree(priv->rx_urb);
> +       priv->rx_urb = NULL;
> +       return -ENOMEM;
>  }
>
>  #ifdef THOMAS_BEACON
> --
> 2.7.4
>



-- 
Kees Cook
Pixel Security

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ