vlan: fix a use-after-free in vlan_device_event()

After refcnt reaches zero, vlan_vid_del() could free
dev->vlan_info via RCU:

RCU_INIT_POINTER(dev->vlan_info, NULL);
call_rcu(&vlan_info->rcu, vlan_info_rcu_free);

However, the pointer 'grp' still points to that memory
since it is set before vlan_vid_del():

vlan_info = rtnl_dereference(dev->vlan_info);
if (!vlan_info)
goto out;
grp = &vlan_info->grp;

Depends on when that RCU callback is scheduled, we could
trigger a use-after-free in vlan_group_for_each_dev()
right following this vlan_vid_del().

Fix it by moving vlan_vid_del() before setting grp. This
is also symmetric to the vlan_vid_add() we call in
vlan_device_event().

Reported-by: Fengguang Wu <fengguang.wu@intel.com>
Fixes: efc73f4bbc23 ("net: Fix memory leak - vlan_info struct")
Cc: Alexander Duyck <alexander.duyck@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Girish Moodalbail <girish.moodalbail@oracle.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Reviewed-by: Girish Moodalbail <girish.moodalbail@oracle.com>
Tested-by: Fengguang Wu <fengguang.wu@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by Cong Wang and committed by David S. Miller 052d41c0 2118df93

Changed files
+3 -3
net
8021q
+3 -3
net/8021q/vlan.c
··· 376 dev->name); 377 vlan_vid_add(dev, htons(ETH_P_8021Q), 0); 378 } 379 380 vlan_info = rtnl_dereference(dev->vlan_info); 381 if (!vlan_info) ··· 425 case NETDEV_DOWN: { 426 struct net_device *tmp; 427 LIST_HEAD(close_list); 428 - 429 - if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 430 - vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 431 432 /* Put all VLANs for this dev in the down state too. */ 433 vlan_group_for_each_dev(grp, i, vlandev) {
··· 376 dev->name); 377 vlan_vid_add(dev, htons(ETH_P_8021Q), 0); 378 } 379 + if (event == NETDEV_DOWN && 380 + (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) 381 + vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 382 383 vlan_info = rtnl_dereference(dev->vlan_info); 384 if (!vlan_info) ··· 422 case NETDEV_DOWN: { 423 struct net_device *tmp; 424 LIST_HEAD(close_list); 425 426 /* Put all VLANs for this dev in the down state too. */ 427 vlan_group_for_each_dev(grp, i, vlandev) {