Skip to content

Commit

Permalink
net: fix a data race when get vlan device
Browse files Browse the repository at this point in the history
We encountered a crash: in the packet receiving process, we got an
illegal VLAN device address, but the VLAN device address saved in vmcore
is correct. After checking the code, we found a possible data
competition:
CPU 0:                             CPU 1:
    (RCU read lock)                  (RTNL lock)
    vlan_do_receive()		       register_vlan_dev()
      vlan_find_dev()

        ->__vlan_group_get_device()	 ->vlan_group_prealloc_vid()

In vlan_group_prealloc_vid(), We need to make sure that memset()
in kzalloc() is executed before assigning  value to vlan devices array:
=================================
kzalloc()
    ->memset(object, 0, size)

smp_wmb()

vg->vlan_devices_arrays[pidx][vidx] = array;
==================================

Because __vlan_group_get_device() function depends on this order.
otherwise we may get a wrong address from the hardware cache on
another cpu.

So fix it by adding memory barrier instruction to ensure the order
of memory operations.

Signed-off-by: Di Zhu <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Di Zhu authored and davem330 committed Apr 19, 2021
1 parent 7ad18ff commit c1102e9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions net/8021q/vlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
if (array == NULL)
return -ENOBUFS;

/* paired with smp_rmb() in __vlan_group_get_device() */
smp_wmb();

vg->vlan_devices_arrays[pidx][vidx] = array;
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions net/8021q/vlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ static inline struct net_device *__vlan_group_get_device(struct vlan_group *vg,

array = vg->vlan_devices_arrays[pidx]
[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];

/* paired with smp_wmb() in vlan_group_prealloc_vid() */
smp_rmb();

return array ? array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] : NULL;
}

Expand Down

0 comments on commit c1102e9

Please sign in to comment.