2013-09-04 21:46:27

by Alexey Khoroshilov

[permalink] [raw]
Subject: [PATCH] can: pcan_usb_core: fix memory leak on failure paths in peak_usb_start()

Tx and rx urbs are not deallocated if something goes wrong in peak_usb_start().
The patch fixes error handling to deallocate all the resources.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov <[email protected]>
---
drivers/net/can/usb/peak_usb/pcan_usb_core.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
index a0f647f..0b7a4c3 100644
--- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
+++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
@@ -463,7 +463,7 @@ static int peak_usb_start(struct peak_usb_device *dev)
if (i < PCAN_USB_MAX_TX_URBS) {
if (i == 0) {
netdev_err(netdev, "couldn't setup any tx URB\n");
- return err;
+ goto err_tx;
}

netdev_warn(netdev, "tx performance may be slow\n");
@@ -472,7 +472,7 @@ static int peak_usb_start(struct peak_usb_device *dev)
if (dev->adapter->dev_start) {
err = dev->adapter->dev_start(dev);
if (err)
- goto failed;
+ goto err_adapter;
}

dev->state |= PCAN_USB_STATE_STARTED;
@@ -481,19 +481,26 @@ static int peak_usb_start(struct peak_usb_device *dev)
if (dev->adapter->dev_set_bus) {
err = dev->adapter->dev_set_bus(dev, 1);
if (err)
- goto failed;
+ goto err_adapter;
}

dev->can.state = CAN_STATE_ERROR_ACTIVE;

return 0;

-failed:
+err_adapter:
if (err == -ENODEV)
netif_device_detach(dev->netdev);

netdev_warn(netdev, "couldn't submit control: %d\n", err);

+ for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
+ usb_free_urb(dev->tx_contexts[i].urb);
+ dev->tx_contexts[i].urb = NULL;
+ }
+err_tx:
+ usb_kill_anchored_urbs(&dev->rx_submitted);
+
return err;
}

--
1.8.1.2


2013-09-05 09:34:23

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [PATCH] can: pcan_usb_core: fix memory leak on failure paths in peak_usb_start()

Added Stephane to Cc.

On 09/04/2013 11:46 PM, Alexey Khoroshilov wrote:
> Tx and rx urbs are not deallocated if something goes wrong in peak_usb_start().
> The patch fixes error handling to deallocate all the resources.
>
> Found by Linux Driver Verification project (linuxtesting.org).
>
> Signed-off-by: Alexey Khoroshilov <[email protected]>

Stephane, can you have a look at the patch and give your Acked-by.

Looks good to me.

tnx,
Marc
> ---
> drivers/net/can/usb/peak_usb/pcan_usb_core.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
> index a0f647f..0b7a4c3 100644
> --- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
> +++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
> @@ -463,7 +463,7 @@ static int peak_usb_start(struct peak_usb_device *dev)
> if (i < PCAN_USB_MAX_TX_URBS) {
> if (i == 0) {
> netdev_err(netdev, "couldn't setup any tx URB\n");
> - return err;
> + goto err_tx;
> }
>
> netdev_warn(netdev, "tx performance may be slow\n");
> @@ -472,7 +472,7 @@ static int peak_usb_start(struct peak_usb_device *dev)
> if (dev->adapter->dev_start) {
> err = dev->adapter->dev_start(dev);
> if (err)
> - goto failed;
> + goto err_adapter;
> }
>
> dev->state |= PCAN_USB_STATE_STARTED;
> @@ -481,19 +481,26 @@ static int peak_usb_start(struct peak_usb_device *dev)
> if (dev->adapter->dev_set_bus) {
> err = dev->adapter->dev_set_bus(dev, 1);
> if (err)
> - goto failed;
> + goto err_adapter;
> }
>
> dev->can.state = CAN_STATE_ERROR_ACTIVE;
>
> return 0;
>
> -failed:
> +err_adapter:
> if (err == -ENODEV)
> netif_device_detach(dev->netdev);
>
> netdev_warn(netdev, "couldn't submit control: %d\n", err);
>
> + for (i = 0; i < PCAN_USB_MAX_TX_URBS; i++) {
> + usb_free_urb(dev->tx_contexts[i].urb);
> + dev->tx_contexts[i].urb = NULL;
> + }
> +err_tx:
> + usb_kill_anchored_urbs(&dev->rx_submitted);
> +
> return err;
> }
>
>


--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |


Attachments:
signature.asc (259.00 B)
OpenPGP digital signature

2013-09-06 06:56:25

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [PATCH] can: pcan_usb_core: fix memory leak on failure paths in peak_usb_start()

On 09/06/2013 08:52 AM, Stephane Grosjean wrote:
> Tx and rx urbs are not deallocated if something goes wrong in peak_usb_start().
> The patch fixes error handling to deallocate all the resources.
>
> Found by Linux Driver Verification project (linuxtesting.org).
>
> Signed-off-by: Alexey Khoroshilov <[email protected]>
> Acked-by: Stephane Grosjean <[email protected]>

Tnx,
Marc

BTW: A simply reply to the original patch with your Acked-by is sufficient.

--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |


Attachments:
signature.asc (259.00 B)
OpenPGP digital signature

2013-09-06 06:58:48

by Stéphane Grosjean

[permalink] [raw]
Subject: Re: [PATCH] can: pcan_usb_core: fix memory leak on failure paths in peak_usb_start()


Le 06/09/2013 08:56, Marc Kleine-Budde a écrit :
> On 09/06/2013 08:52 AM, Stephane Grosjean wrote:
>> Tx and rx urbs are not deallocated if something goes wrong in peak_usb_start().
>> The patch fixes error handling to deallocate all the resources.
>>
>> Found by Linux Driver Verification project (linuxtesting.org).
>>
>> Signed-off-by: Alexey Khoroshilov <[email protected]>
>> Acked-by: Stephane Grosjean <[email protected]>
> Tnx,
> Marc
>
> BTW: A simply reply to the original patch with your Acked-by is sufficient.
>

Ok, thx Marc. I keep it in mind for the next time (if any ;-))

Stéphane
--
PEAK-System Technik GmbH, Otto-Roehm-Strasse 69, D-64293 Darmstadt
Geschaeftsleitung: A.Gach/U.Wilhelm,St.Nr.:007/241/13586 FA Darmstadt
HRB-9183 Darmstadt, Ust.IdNr.:DE 202220078, WEE-Reg.-Nr.: DE39305391
Tel.+49 (0)6151-817320 / Fax:+49 (0)6151-817329, [email protected]
---

2013-09-06 09:03:43

by Marc Kleine-Budde

[permalink] [raw]
Subject: Re: [PATCH] can: pcan_usb_core: fix memory leak on failure paths in peak_usb_start()

On 09/05/2013 11:33 AM, Marc Kleine-Budde wrote:
> Added Stephane to Cc.
>
> On 09/04/2013 11:46 PM, Alexey Khoroshilov wrote:
>> Tx and rx urbs are not deallocated if something goes wrong in peak_usb_start().
>> The patch fixes error handling to deallocate all the resources.
>>
>> Found by Linux Driver Verification project (linuxtesting.org).
>>
>> Signed-off-by: Alexey Khoroshilov <[email protected]>
>
> Stephane, can you have a look at the patch and give your Acked-by.
>
> Looks good to me.

Applied with Stephane's Acked-by to can/fixes-for-3.12.

Tnx,
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |


Attachments:
signature.asc (259.00 B)
OpenPGP digital signature