2020-07-29 03:03:01

by Lu Wei

[permalink] [raw]
Subject: [PATCH] net: ethernet: fix potential memory leak in gemini_ethernet_port_probe()

If some processes in gemini_ethernet_port_probe() fail,
free_netdev(dev) needs to be called to avoid a memory leak.

Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Reported-by: Hulk Robot <[email protected]>
Signed-off-by: Lu Wei <[email protected]>
---
drivers/net/ethernet/cortina/gemini.c | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 8d13ea370db1..5e93a1a570b6 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -2407,37 +2407,48 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
dmares = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!dmares) {
dev_err(dev, "no DMA resource\n");
+ free_netdev(netdev);
return -ENODEV;
}
port->dma_base = devm_ioremap_resource(dev, dmares);
- if (IS_ERR(port->dma_base))
+ if (IS_ERR(port->dma_base)) {
+ free_netdev(netdev);
return PTR_ERR(port->dma_base);
+ }

/* GMAC config memory */
gmacres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
if (!gmacres) {
dev_err(dev, "no GMAC resource\n");
+ free_netdev(netdev);
return -ENODEV;
}
port->gmac_base = devm_ioremap_resource(dev, gmacres);
- if (IS_ERR(port->gmac_base))
+ if (IS_ERR(port->gmac_base)) {
+ free_netdev(netdev);
return PTR_ERR(port->gmac_base);
+ }

/* Interrupt */
irq = platform_get_irq(pdev, 0);
- if (irq <= 0)
+ if (irq <= 0) {
+ free_netdev(netdev);
return irq ? irq : -ENODEV;
+ }
port->irq = irq;

/* Clock the port */
port->pclk = devm_clk_get(dev, "PCLK");
if (IS_ERR(port->pclk)) {
dev_err(dev, "no PCLK\n");
+ free_netdev(netdev);
return PTR_ERR(port->pclk);
}
ret = clk_prepare_enable(port->pclk);
- if (ret)
+ if (ret) {
+ free_netdev(netdev);
return ret;
+ }

/* Maybe there is a nice ethernet address we should use */
gemini_port_save_mac_addr(port);
@@ -2446,6 +2457,7 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
port->reset = devm_reset_control_get_exclusive(dev, NULL);
if (IS_ERR(port->reset)) {
dev_err(dev, "no reset\n");
+ free_netdev(netdev);
return PTR_ERR(port->reset);
}
reset_control_reset(port->reset);
@@ -2501,8 +2513,10 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
IRQF_SHARED,
port_names[port->id],
port);
- if (ret)
+ if (ret) {
+ free_netdev(netdev);
return ret;
+ }

ret = register_netdev(netdev);
if (!ret) {
--
2.17.1


2020-07-29 10:05:17

by Jisheng Zhang

[permalink] [raw]
Subject: Re: [PATCH] net: ethernet: fix potential memory leak in gemini_ethernet_port_probe()

On Wed, 29 Jul 2020 11:46:06 +0800 Lu Wei wrote:


>
>
> If some processes in gemini_ethernet_port_probe() fail,
> free_netdev(dev) needs to be called to avoid a memory leak.

Using devm_alloc_etherdev_mqs() would be much simpler

>
> Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
> Reported-by: Hulk Robot <[email protected]>
> Signed-off-by: Lu Wei <[email protected]>
> ---
> drivers/net/ethernet/cortina/gemini.c | 24 +++++++++++++++++++-----
> 1 file changed, 19 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
> index 8d13ea370db1..5e93a1a570b6 100644
> --- a/drivers/net/ethernet/cortina/gemini.c
> +++ b/drivers/net/ethernet/cortina/gemini.c
> @@ -2407,37 +2407,48 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
> dmares = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> if (!dmares) {
> dev_err(dev, "no DMA resource\n");
> + free_netdev(netdev);
> return -ENODEV;
> }
> port->dma_base = devm_ioremap_resource(dev, dmares);
> - if (IS_ERR(port->dma_base))
> + if (IS_ERR(port->dma_base)) {
> + free_netdev(netdev);
> return PTR_ERR(port->dma_base);
> + }
>
> /* GMAC config memory */
> gmacres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
> if (!gmacres) {
> dev_err(dev, "no GMAC resource\n");
> + free_netdev(netdev);
> return -ENODEV;
> }
> port->gmac_base = devm_ioremap_resource(dev, gmacres);
> - if (IS_ERR(port->gmac_base))
> + if (IS_ERR(port->gmac_base)) {
> + free_netdev(netdev);
> return PTR_ERR(port->gmac_base);
> + }
>
> /* Interrupt */
> irq = platform_get_irq(pdev, 0);
> - if (irq <= 0)
> + if (irq <= 0) {
> + free_netdev(netdev);
> return irq ? irq : -ENODEV;
> + }
> port->irq = irq;
>
> /* Clock the port */
> port->pclk = devm_clk_get(dev, "PCLK");
> if (IS_ERR(port->pclk)) {
> dev_err(dev, "no PCLK\n");
> + free_netdev(netdev);
> return PTR_ERR(port->pclk);
> }
> ret = clk_prepare_enable(port->pclk);
> - if (ret)
> + if (ret) {
> + free_netdev(netdev);
> return ret;
> + }
>
> /* Maybe there is a nice ethernet address we should use */
> gemini_port_save_mac_addr(port);
> @@ -2446,6 +2457,7 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
> port->reset = devm_reset_control_get_exclusive(dev, NULL);
> if (IS_ERR(port->reset)) {
> dev_err(dev, "no reset\n");
> + free_netdev(netdev);
> return PTR_ERR(port->reset);
> }
> reset_control_reset(port->reset);
> @@ -2501,8 +2513,10 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
> IRQF_SHARED,
> port_names[port->id],
> port);
> - if (ret)
> + if (ret) {
> + free_netdev(netdev);
> return ret;
> + }
>
> ret = register_netdev(netdev);
> if (!ret) {
> --
> 2.17.1
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.infradead.org_mailman_listinfo_linux-2Darm-2Dkernel&d=DwICAg&c=7dfBJ8cXbWjhc0BhImu8wQ&r=wlaKTGoVCDxOzHc2QUzpzGEf9oY3eidXlAe3OF1omvo&m=56ruRjtNY-BIzquRyoO0KSbrR7UBB81VfqotT_rfFus&s=Axiqv0SZYKFXgMc1zJLilZCk9wbRAt4LkKtW6VjKTgw&e=