Skip to contentSkip to conversation

net: dsa: fix dereference on ds->dev before null check error

PATCH In review Colin King 4 participants 4 messages Oct 24, 2019

Subsystems (4)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 net: dsa: fix dereference on ds->dev before null check error

From: Colin Ian King <[email protected]>

Currently ds->dev is dereferenced on the assignments of pdata and np before ds->dev is null checked, hence there is a potential null pointer dereference on ds->dev. Fix this by assigning pdata and np after the ds->dev null pointer sanity check.

Addresses-Coverity: ("Dereference before null check")

Fixes 7e99e3470172 ("net: dsa: remove dsa_switch_alloc helper")
---
 net/dsa/dsa2.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/dsa/dsa2.c +5 -2
index 1e3ac9b56c89..214dd703b0cc 100644
@@ -842,13 +842,16 @@ static int dsa_switch_add(struct dsa_switch *ds)
 
 static int dsa_switch_probe(struct dsa_switch *ds)
 {
-	struct dsa_chip_data *pdata = ds->dev->platform_data;
-	struct device_node *np = ds->dev->of_node;
+	struct dsa_chip_data *pdata;
+	struct device_node *np;
 	int err;
 
 	if (!ds->dev)
 		return -ENODEV;
 
+	pdata = ds->dev->platform_data;
+	np = ds->dev->of_node;
+
 	if (!ds->num_ports)
 		return -EINVAL;
 
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

Currently ds->dev is dereferenced on the assignments of pdata and np before ds->dev is null checked, hence there is a potential null pointer dereference on ds->dev. Fix this by assigning pdata and np after the ds->dev null pointer sanity check.

Addresses-Coverity: ("Dereference before null check")

Fixes 7e99e3470172 ("net: dsa: remove dsa_switch_alloc helper")

Andrew

Reviewed-by on v1 1/1
#2 lore raw
In reply to Colin King
Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

Currently ds->dev is dereferenced on the assignments of pdata and np before ds->dev is null checked, hence there is a potential null pointer dereference on ds->dev. Fix this by assigning pdata and np after the ds->dev null pointer sanity check.

Addresses-Coverity: ("Dereference before null check")

Fixes 7e99e3470172 ("net: dsa: remove dsa_switch_alloc helper")

and while we are at it:

[linux-next:master 5983/6376] net/dsa/dsa2.c:849 dsa_switch_probe()
warn: variable dereferenced before check 'ds->dev' (see line 845)

Quoted 28 lines
---
 net/dsa/dsa2.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/dsa/dsa2.c +5 -2
index 1e3ac9b56c89..214dd703b0cc 100644
@@ -842,13 +842,16 @@ static int dsa_switch_add(struct dsa_switch *ds)
 
 static int dsa_switch_probe(struct dsa_switch *ds)
 {
-	struct dsa_chip_data *pdata = ds->dev->platform_data;
-	struct device_node *np = ds->dev->of_node;
+	struct dsa_chip_data *pdata;
+	struct device_node *np;
 	int err;
 
 	if (!ds->dev)
 		return -ENODEV;
 
+	pdata = ds->dev->platform_data;
+	np = ds->dev->of_node;
+
 	if (!ds->num_ports)
 		return -EINVAL;
 
Signature · 1 line
Florian
Reviewed-by on v1 1/1
#3 lore raw
In reply to Colin King
Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

Currently ds->dev is dereferenced on the assignments of pdata and np before ds->dev is null checked, hence there is a potential null pointer dereference on ds->dev. Fix this by assigning pdata and np after the ds->dev null pointer sanity check.

Addresses-Coverity: ("Dereference before null check")

Fixes 7e99e3470172 ("net: dsa: remove dsa_switch_alloc helper")