veth: fix memory leak in veth_newlink()
Subsystems (3)
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
@@ -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
Quoted 4 lines · Rustam Kovhaev
when
register_netdevice(dev)fails we should check whether structveth_rqhas been allocated viando_initcallback and free it, because, depending on the code path,register_netdevice()might not callpriv_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
Quoted 10 lines · Toshiaki Makita
Quoted 4 lines · Rustam Kovhaev
when
register_netdevice(dev)fails we should check whether structveth_rqhas been allocated viando_initcallback and free it, because, depending on the code path,register_netdevice()might not callpriv_destructor()callbackAFAICS,
register_netdevice()always gotoerr_uninitand callspriv_destructor()on failure afterndo_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
Quoted 7 lines · Rustam Kovhaev
Quoted 6 lines · Toshiaki Makita
Quoted 4 lines · Rustam Kovhaev
when
register_netdevice(dev)fails we should check whether structveth_rqhas been allocated viando_initcallback and free it, because, depending on the code path,register_netdevice()might not callpriv_destructor()callback
Quoted 8 lines
Quoted 4 lines
AFAICS,
register_netdevice()always gotoerr_uninitand callspriv_destructor()on failure afterndo_init()succeeded.
So I could not find such a code path.
Would you elaborate on it?in
net/core/dev.c:9863, whereregister_netdevice()callsrollback_registered(), which does not callpriv_destructor(), thenregister_netdevice()returns errornet/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
David Miller
maintainerFrom: 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 structveth_rqhas been allocated viando_initcallback and free it, because, depending on the code path,register_netdevice()might not callpriv_destructor()callbackReported-and-tested-by [email protected]Signed-off-by Rustam Kovhaev <[email protected]>
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.
Quoted 12 lines · David Miller
From: Rustam Kovhaev <[email protected]>
Date: Sun, 30 Aug 2020 06:13:36 -0700Quoted 8 lines
when
register_netdevice(dev)fails we should check whether structveth_rqhas been allocated viando_initcallback and free it, because, depending on the code path,register_netdevice()might not callpriv_destructor()callbackReported-and-tested-by [email protected]Signed-off-by Rustam Kovhaev <[email protected]>
Quoted 6 lines
I think I agree with Toshiaki here. There is no reason why the
rollback_registered()path ofregister_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 ofregister_netdevicebehave differently fromrollback_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