Skip to contentSkip to conversation

veth: fix memory leak in veth_newlink()

PATCH In review Rustam Kovhaev 3 participants 6 messages Aug 30, 2020 – Sep 1, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 veth: fix memory leak in veth_newlink()

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

---
 drivers/net/veth.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/veth.c +3 -1
index a475f48d43c4..e40ca62a046a 100644
@@ -1394,7 +1394,9 @@ static int veth_newlink(struct net *src_net, struct net_device *dev,
 	return 0;
 
 err_register_dev:
-	/* nothing to do */
+	priv = netdev_priv(dev);
+	if (priv->rq)
+		veth_dev_free(dev);
 err_configure_peer:
 	unregister_netdevice(peer);
 	return err;
Signature · 1 line
2.28.0
#1 lore raw
Quoted 4 lines · Rustam Kovhaev

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

AFAICS, register_netdevice() always goto err_uninit and calls priv_destructor() on failure after ndo_init() succeeded.
So I could not find such a code path.
Would you elaborate on it?

Thanks,
Toshiaki Makita

#2 lore raw
Quoted 10 lines · Toshiaki Makita
Quoted 4 lines · Rustam Kovhaev

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

AFAICS, register_netdevice() always goto err_uninit and calls priv_destructor() on failure after ndo_init() succeeded.
So I could not find such a code path.
Would you elaborate on it?

in net/core/dev.c:9863, where register_netdevice() calls rollback_registered(), which does not call priv_destructor(), then register_netdevice() returns error net/core/dev.c:9884

#3 lore raw
Quoted 7 lines · Rustam Kovhaev
Quoted 6 lines · Toshiaki Makita
Quoted 4 lines · Rustam Kovhaev

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

Quoted 8 lines
Quoted 4 lines

AFAICS, register_netdevice() always goto err_uninit and calls priv_destructor() on failure after ndo_init() succeeded.
So I could not find such a code path.
Would you elaborate on it?

in net/core/dev.c:9863, where register_netdevice() calls rollback_registered(), which does not call priv_destructor(), then register_netdevice() returns error net/core/dev.c:9884

Thank you, now I see the code path.
But then all devices which allocate something in ndo_init() and free them in priv_destructor() are affected? E.g. loopback and ifb seem to do such thing. Why not calling priv_destructor() after invocation of rollback_registered()? It looks weird that only that path does not call priv_destructor().

Toshiaki Makita

maintainer
#4 lore raw
In reply to Rustam Kovhaev

From: Rustam Kovhaev <[email protected]>
Date: Sun, 30 Aug 2020 06:13:36 -0700

Quoted 8 lines

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

I think I agree with Toshiaki here. There is no reason why the rollback_registered() path of register_netdevice() should behave differently from the normal control flow.

Any code path that invokes ->ndo_uninit() should probably also invoke the priv destructor.

The question is why does the err_uninit: label of register_netdevice behave differently from rollback_registered()? If there is a reason, it should be documented in a comment or similar. If it is wrong, it should be corrected.

#5 lore raw
Quoted 12 lines · David Miller

From: Rustam Kovhaev <[email protected]>
Date: Sun, 30 Aug 2020 06:13:36 -0700

Quoted 8 lines

when register_netdevice(dev) fails we should check whether struct veth_rq has been allocated via ndo_init callback and free it, because, depending on the code path, register_netdevice() might not call priv_destructor() callback

Quoted 6 lines

I think I agree with Toshiaki here. There is no reason why the rollback_registered() path of register_netdevice() should behave differently from the normal control flow.

Any code path that invokes ->ndo_uninit() should probably also invoke the priv destructor.

hi David, thank you for the review!

Quoted 5 lines

The question is why does the err_uninit: label of register_netdevice behave differently from rollback_registered()? If there is a reason, it should be documented in a comment or similar. If it is wrong, it should be corrected.

good question, that i do not know, i'll review it