Skip to contentSkip to conversation

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

PATCH In review Cong Wang 5 participants 5 messages Nov 10, 2017 – Nov 11, 2017

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 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().

Fixes efc73f4bbc23 ("net: Fix memory leak - vlan_info struct")
Cc Alexander Duyck <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Girish Moodalbail <[email protected]>
---
 net/8021q/vlan.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/8021q/vlan.c +3 -3
index 9649579b5b9f..4a72ee4e2ae9 100644
@@ -376,6 +376,9 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
 			dev->name);
 		vlan_vid_add(dev, htons(ETH_P_8021Q), 0);
 	}
+	if (event == NETDEV_DOWN &&
+	    (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
+		vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
 
 	vlan_info = rtnl_dereference(dev->vlan_info);
 	if (!vlan_info)
@@ -423,9 +426,6 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
 		struct net_device *tmp;
 		LIST_HEAD(close_list);
 
-		if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
-			vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
-
 		/* Put all VLANs for this dev in the down state too.  */
 		vlan_group_for_each_dev(grp, i, vlandev) {
 			flgs = vlandev->flags;
Signature · 1 line
2.13.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Cong Wang

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().

Quoted 6 lines
Fixes efc73f4bbc23 ("net: Fix memory leak - vlan_info struct")
Cc Alexander Duyck <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Girish Moodalbail <[email protected]>

LGTM.

Thanks,
~Girish

Quoted 29 lines
---
  net/8021q/vlan.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
net/8021q/vlan.c +3 -3
index 9649579b5b9f..4a72ee4e2ae9 100644
@@ -376,6 +376,9 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
  			dev->name);
  		vlan_vid_add(dev, htons(ETH_P_8021Q), 0);
  	}
+	if (event == NETDEV_DOWN &&
+	    (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
+		vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
 
  	vlan_info = rtnl_dereference(dev->vlan_info);
  	if (!vlan_info)
@@ -423,9 +426,6 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
  		struct net_device *tmp;
  		LIST_HEAD(close_list);
 
-		if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
-			vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
-
  		/* Put all VLANs for this dev in the down state too.  */
  		vlan_group_for_each_dev(grp, i, vlandev) {
  			flgs = vlandev->flags;
maintainer
#4 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Thu, 9 Nov 2017 16:43:13 -0800

Quoted 22 lines

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().

Quoted 6 lines
Fixes efc73f4bbc23 ("net: Fix memory leak - vlan_info struct")
Cc Alexander Duyck <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Girish Moodalbail <[email protected]>

Applied and queued up for -stable, thanks Cong!