Skip to contentSkip to conversation

fix potential NULL pointer dereference in yam

PATCH In review Jesper Juhl 3 participants 5 messages May 14, 2006 – May 16, 2006 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix potential NULL pointer dereference in yam

Testing a pointer for NULL after it has already been dereferenced is not very safe.
Patch below to rework yam_open() so that `dev' is not dereferenced until after it has been tested for NULL.

Found by coverity checker as #787

(please Cc me on replies)

---

 drivers/net/hamradio/yam.c |   18 ++++++++++++++----
 1 files changed, 14 insertions(+), 4 deletions(-)
drivers/net/hamradio/yam.c +14 -4
@@ -845,15 +845,25 @@ static struct net_device_stats *yam_get_
 
 static int yam_open(struct net_device *dev)
 {
-	struct yam_port *yp = netdev_priv(dev);
+	struct yam_port *yp;
 	enum uart u;
 	int i;
-	int ret=0;
+	int ret = 0;
 
-	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n", dev->name, dev->base_addr, dev->irq);
+	if (!dev) {
+		printk(KERN_ERR "yam_open() called without device\n");
+		return -ENXIO;
+	}
 
-	if (!dev || !yp->bitrate)
+	yp = netdev_priv(dev);
+	if (!yp->bitrate) {
+		printk(KERN_ERR "%s: no bitrate\n", dev->name);
 		return -ENXIO;
+	}
+
+	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n",
+		dev->name, dev->base_addr, dev->irq);
+
 	if (!dev->base_addr || dev->base_addr > 0x1000 - YAM_EXTENT ||
 		dev->irq < 2 || dev->irq > 15) {
 		return -ENXIO;
#1 lore raw
Quoted 10 lines · Jesper Juhl

Testing a pointer for NULL after it has already been dereferenced is not very safe.
Patch below to rework yam_open() so that `dev' is not dereferenced until after it has been tested for NULL.

Found by coverity checker as #787

drivers/net/hamradio/yam.c static struct net_device_stats *yam_get_ · line 851

Quoted 9 lines
drivers/net/hamradio/yam.c +2 -2
@@ -845,15 +845,25 @@ static struct net_device_stats *yam_get_
 
 static int yam_open(struct net_device *dev)
 {
-	struct yam_port *yp = netdev_priv(dev);
+	struct yam_port *yp;
 	enum uart u;
 	int i;
-	int ret=0;
+	int ret = 0;

Please, don't make unrelated changes.

drivers/net/hamradio/yam.c static struct net_device_stats *yam_get_ · line 856

Quoted 5 lines
drivers/net/hamradio/yam.c +4 -1
-	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n", dev->name, dev->base_addr, dev->irq);
+	if (!dev) {
+		printk(KERN_ERR "yam_open() called without device\n");
+		return -ENXIO;
+	}

How can it be NULL here? The whole array of valid net_devices was allocated at module init time.

Quoted 13 lines
drivers/net/hamradio/yam.c +8 -1
-	if (!dev || !yp->bitrate)
+	yp = netdev_priv(dev);
+	if (!yp->bitrate) {
+		printk(KERN_ERR "%s: no bitrate\n", dev->name);
 		return -ENXIO;
+	}
+
+	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n",
+		dev->name, dev->base_addr, dev->irq);
+
 	if (!dev->base_addr || dev->base_addr > 0x1000 - YAM_EXTENT ||
 		dev->irq < 2 || dev->irq > 15) {
 		return -ENXIO;
#2 lore raw
Quoted 12 lines · Alexey Dobriyan
Quoted 11 lines · Jesper Juhl

Testing a pointer for NULL after it has already been dereferenced is not very safe.
Patch below to rework yam_open() so that `dev' is not dereferenced until after it has been tested for NULL.

Found by coverity checker as #787

drivers/net/hamradio/yam.c +0 -0
@@ -845,15 +845,25 @@ static struct net_device_stats *yam_get_
 
Quoted 11 lines

drivers/net/hamradio/yam.c static struct net_device_stats *yam_get_ · line 851

Quoted 8 lines
drivers/net/hamradio/yam.c +2 -2
 static int yam_open(struct net_device *dev)
 {
-	struct yam_port *yp = netdev_priv(dev);
+	struct yam_port *yp;
 	enum uart u;
 	int i;
-	int ret=0;
+	int ret = 0;

Please, don't make unrelated changes.

Sorry.

Quoted 9 lines

drivers/net/hamradio/yam.c static struct net_device_stats *yam_get_ · line 855

Quoted 5 lines
drivers/net/hamradio/yam.c +4 -1
-	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n", dev->name, dev->base_addr, dev->irq);
+	if (!dev) {
+		printk(KERN_ERR "yam_open() called without device\n");
+		return -ENXIO;
+	}

How can it be NULL here? The whole array of valid net_devices was allocated at module init time.

It cannot. You are right, I'm wrong.
I guess removing the check makes sense then ?

---

drivers/net/hamradio/yam.c +1 -1
@@ -852,7 +852,7 @@ static int yam_open(struct net_device *d
 
 	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n", dev->name, dev->base_addr, dev->irq);
 
-	if (!dev || !yp->bitrate)
+	if (!yp->bitrate)
 		return -ENXIO;
 	if (!dev->base_addr || dev->base_addr > 0x1000 - YAM_EXTENT ||
 		dev->irq < 2 || dev->irq > 15) {
Acked-by
#3 lore raw
Quoted 6 lines · Jesper Juhl
Quoted 3 lines

How can it be NULL here? The whole array of valid net_devices was allocated at module init time.

It cannot. You are right, I'm wrong.
I guess removing the check makes sense then ?

Yes.

Quoted 7 lines

---

drivers/net/hamradio/yam.c static int yam_open(struct net_device *d · line 858

Quoted 8 lines
drivers/net/hamradio/yam.c +1 -1
@@ -852,7 +852,7 @@ static int yam_open(struct net_device *d
 
 	printk(KERN_INFO "Trying %s at iobase 0x%lx irq %u\n", dev->name, dev->base_addr, dev->irq);
 
-	if (!dev || !yp->bitrate)
+	if (!yp->bitrate)
 		return -ENXIO;
 	if (!dev->base_addr || dev->base_addr > 0x1000 - YAM_EXTENT ||
 		dev->irq < 2 || dev->irq > 15) {

73 de DL5RB op Ralf

--
Loc. JN47BS / CQ 14 / ITU 28 / DOK A21

#4 lore raw
In reply to Jesper Juhl
Quoted 4 lines · Jesper Juhl

Testing a pointer for NULL after it has already been dereferenced is not very safe.
Patch below to rework yam_open() so that `dev' is not dereferenced until after it has been tested for NULL.

It may not obvious and that itself is some sort of bug bug netdev_priv() assumes that the private part of struct netdev has been allocated immediately following struct netdev, so the macro will compile into just pointer arithmetic. So no possible NULL pointer dereference here.

Ralf