Skip to contentSkip to conversation

net: vlan: Avoid using BUG() in vlan_proto_idx()

PATCH Applied Florian Fainelli 4 participants 5 messages Sep 25, 2020 – Sep 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: vlan: Avoid using BUG() in vlan_proto_idx()

While we should always make sure that we specify a valid VLAN protocol to vlan_proto_idx(), killing the machine when an invalid value is specified is too harsh and not helpful for debugging. All callers are capable of dealing with an error returned by vlan_proto_idx() so check the index value and propagate it accordingly.

---
Changes in v2:
- changed signature to return int
- changed message to use ntohs()
- renamed an index variable to 'pidx' instead of 'pdix'

 net/8021q/vlan.c |  3 +++
 net/8021q/vlan.h | 19 +++++++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)
net/8021q/vlan.c +3 -0
index d4bcfd8f95bf..6c08de1116c1 100644
@@ -57,6 +57,9 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
 	ASSERT_RTNL();
 
 	pidx  = vlan_proto_idx(vlan_proto);
+	if (pidx < 0)
+		return -EINVAL;
+
 	vidx  = vlan_id / VLAN_GROUP_ARRAY_PART_LEN;
 	array = vg->vlan_devices_arrays[pidx][vidx];
 	if (array != NULL)
net/8021q/vlan.h +13 -6
index bb7ec1a3915d..953405362795 100644
@@ -36,7 +36,7 @@ struct vlan_info {
 	struct rcu_head		rcu;
 };
 
-static inline unsigned int vlan_proto_idx(__be16 proto)
+static inline int vlan_proto_idx(__be16 proto)
 {
 	switch (proto) {
 	case htons(ETH_P_8021Q):
@@ -44,8 +44,8 @@ static inline unsigned int vlan_proto_idx(__be16 proto)
 	case htons(ETH_P_8021AD):
 		return VLAN_PROTO_8021AD;
 	default:
-		BUG();
-		return 0;
+		WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto));
+		return -EINVAL;
 	}
 }
 
@@ -64,17 +64,24 @@ static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
 						       __be16 vlan_proto,
 						       u16 vlan_id)
 {
-	return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
+	int pidx = vlan_proto_idx(vlan_proto);
+
+	if (pidx < 0)
+		return NULL;
+
+	return __vlan_group_get_device(vg, pidx, vlan_id);
 }
 
 static inline void vlan_group_set_device(struct vlan_group *vg,
 					 __be16 vlan_proto, u16 vlan_id,
 					 struct net_device *dev)
 {
+	int pidx = vlan_proto_idx(vlan_proto);
 	struct net_device **array;
-	if (!vg)
+
+	if (!vg || pidx < 0)
 		return;
-	array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
+	array = vg->vlan_devices_arrays[pidx]
 				       [vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
 	array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
 }
Signature · 1 line
2.25.1
#1 lore raw
I love your patch! Perhaps something to improve:

Hi Florian,

I love your patch! Perhaps something to improve:

[auto build test WARNING on net-next/master]

url:    https://github.com/0day-ci/linux/commits/Florian-Fainelli/net-vlan-Avoid-using-BUG-in-vlan_proto_idx/20200925-082933
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git 3fc826f121d89c5aa4afd7b3408b07e0ff59466b
config: x86_64-randconfig-m001-20200925 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate

smatch warnings:
net/8021q/vlan.c:60 vlan_group_prealloc_vid() warn: unsigned 'pidx' is never less than zero.

vim +/pidx +60 net/8021q/vlan.c

    49	
    50	static int vlan_group_prealloc_vid(struct vlan_group *vg,
    51					   __be16 vlan_proto, u16 vlan_id)
    52	{
    53		struct net_device **array;
    54		unsigned int pidx, vidx;
    55		unsigned int size;
    56	
    57		ASSERT_RTNL();
    58	
    59		pidx  = vlan_proto_idx(vlan_proto);
  > 60		if (pidx < 0)
    61			return -EINVAL;
    62	
    63		vidx  = vlan_id / VLAN_GROUP_ARRAY_PART_LEN;
    64		array = vg->vlan_devices_arrays[pidx][vidx];
    65		if (array != NULL)
    66			return 0;
    67	
    68		size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
    69		array = kzalloc(size, GFP_KERNEL);
    70		if (array == NULL)
    71			return -ENOBUFS;
    72	
    73		vg->vlan_devices_arrays[pidx][vidx] = array;
    74		return 0;
    75	}
    76	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer Applied
#2 lore raw
In reply to Florian Fainelli

From: Florian Fainelli <[email protected]>
Date: Thu, 24 Sep 2020 17:27:44 -0700

Quoted 6 lines

While we should always make sure that we specify a valid VLAN protocol to vlan_proto_idx(), killing the machine when an invalid value is specified is too harsh and not helpful for debugging. All callers are capable of dealing with an error returned by vlan_proto_idx() so check the index value and propagate it accordingly.

Quoted 6 lines

---
Changes in v2:
- changed signature to return int
- changed message to use ntohs()
- renamed an index variable to 'pidx' instead of 'pdix'

Applied, thanks Florian.

#3 lore raw
Quoted 4 lines · David Miller

From: Florian Fainelli <[email protected]>
Date: Thu, 24 Sep 2020 17:27:44 -0700

Applied, thanks Florian.

Uh-oh, that 'negative value stored in unsigned variable' issue that the build bot reported was on v2, wasn't it?

#4 lore raw
Quoted 8 lines · Vladimir Oltean
Quoted 4 lines · David Miller

From: Florian Fainelli <[email protected]>
Date: Thu, 24 Sep 2020 17:27:44 -0700

Applied, thanks Florian.

Uh-oh, that 'negative value stored in unsigned variable' issue that the build bot reported was on v2, wasn't it?

Yes, I will be sending a fixup now.

Signature · 1 line
Florian