Commit e51fb152 authored by Cong Wang's avatar Cong Wang Committed by David S. Miller

rtnetlink: fix a memory leak when ->newlink fails

It is possible that ->newlink() fails before registering
the device, in this case we should just free it, it's
safe to call free_netdev().

Fixes: commit 0e0eee24 (net: correct error path in rtnl_newlink())
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: default avatarCong Wang <cwang@twopensource.com>
Signed-off-by: default avatarCong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 9aaae044
...@@ -2019,11 +2019,15 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) ...@@ -2019,11 +2019,15 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
if (ops->newlink) { if (ops->newlink) {
err = ops->newlink(net, dev, tb, data); err = ops->newlink(net, dev, tb, data);
/* Drivers should call free_netdev() in ->destructor /* Drivers should call free_netdev() in ->destructor
* and unregister it on failure so that device could be * and unregister it on failure after registration
* finally freed in rtnl_unlock. * so that device could be finally freed in rtnl_unlock.
*/ */
if (err < 0) if (err < 0) {
/* If device is not registered at all, free it now */
if (dev->reg_state == NETREG_UNINITIALIZED)
free_netdev(dev);
goto out; goto out;
}
} else { } else {
err = register_netdevice(dev); err = register_netdevice(dev);
if (err < 0) { if (err < 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment