Skip to content

Commit

Permalink
rtnetlink: fix a memory leak when ->newlink fails
Browse files Browse the repository at this point in the history
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 0e0eee2 (net: correct error path in rtnl_newlink())
Cc: David S. Miller <davem@davemloft.net>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Cong Wang <cwang@twopensource.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Cong Wang authored and David S. Miller committed Jun 4, 2014
1 parent 9aaae04 commit e51fb15
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -2019,11 +2019,15 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
if (ops->newlink) {
err = ops->newlink(net, dev, tb, data);
/* Drivers should call free_netdev() in ->destructor
* and unregister it on failure so that device could be
* finally freed in rtnl_unlock.
* and unregister it on failure after registration
* 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;
}
} else {
err = register_netdevice(dev);
if (err < 0) {
Expand Down

0 comments on commit e51fb15

Please sign in to comment.