On Fri, Jun 15, 2018 at 10:28 AM, Zhouyang Jia <jiazhouyang09@xxxxxxxxx> 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@xxxxxxxxx> > --- > 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 _______________________________________________ devel mailing list devel@xxxxxxxxxxxxxxxxxxxxxx http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel