Skip to contentSkip to conversation

sb1000: prevent a potential NULL pointer dereference in sb1000_dev_ioctl()

PATCH Domen Puncer 3 participants 6 messages Jul 29, 2007 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 8 lines · Jesper Juhl

Hi,

Here's a small patch, prompted by a find by the Coverity checker, that removes a potential NULL pointer dereference from drivers/net/sb1000.c::sb1000_dev_ioctl().
The checker spotted that we do a NULL test of 'dev', yet we dereference the pointer prior to that check.
This patch simply moves the dereference after the NULL test.

But... it can't be called without a valid 'dev', no? A quick 'grep do_ioctl net/' confirms that all calls are in the form of 'dev->do_ioctl(dev, ...'.

Domen

Quoted 16 lines
(quoted diff) +3 -1
@@ -991,11 +991,13 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	short PID[4];
 	int ioaddr[2], status, frequency;
 	unsigned int stats[5];
-	struct sb1000_private *lp = netdev_priv(dev);
+	struct sb1000_private *lp;
 
 	if (!(dev && dev->flags & IFF_UP))
 		return -ENODEV;
 
+	lp = netdev_priv(dev);
+
 	ioaddr[0] = dev->base_addr;
 	/* mem_start holds the second I/O address */
 	ioaddr[1] = dev->mem_start;
#2 lore raw
Quoted 10 lines · Domen Puncer
Quoted 8 lines · Jesper Juhl

Hi,

Here's a small patch, prompted by a find by the Coverity checker, that removes a potential NULL pointer dereference from drivers/net/sb1000.c::sb1000_dev_ioctl().
The checker spotted that we do a NULL test of 'dev', yet we dereference the pointer prior to that check.
This patch simply moves the dereference after the NULL test.

Quoted 3 lines

But... it can't be called without a valid 'dev', no? A quick 'grep do_ioctl net/' confirms that all calls are in the form of 'dev->do_ioctl(dev, ...'.

Yup, I think so too ...

Quoted 9 lines

Review static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 997

Quoted 9 lines
(quoted diff) +1 -1
@@ -991,11 +991,13 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	short PID[4];
 	int ioaddr[2], status, frequency;
 	unsigned int stats[5];
-	struct sb1000_private *lp = netdev_priv(dev);
+	struct sb1000_private *lp;
 
 	if (!(dev && dev->flags & IFF_UP))
 		return -ENODEV;

I think we could get rid of the !dev check itself. Actually, the IFF_UP check /also/ looks suspect to me for two reasons: (1) I remember Stephen Hemminger once telling me dev->flags is legacy and unsafe, and one of the netif_xxx() functions be used instead, and, (2) I wonder if we really require the interface to be up and *running* when we do this ioctl.

Satyam

#3 lore raw
Quoted 28 lines · Domen Puncer
Quoted 11 lines · Jesper Juhl
Quoted 8 lines

Hi,

Here's a small patch, prompted by a find by the Coverity checker, that removes a potential NULL pointer dereference from drivers/net/sb1000.c::sb1000_dev_ioctl().
The checker spotted that we do a NULL test of 'dev', yet we dereference the pointer prior to that check.
This patch simply moves the dereference after the NULL test.

Quoted 3 lines

But... it can't be called without a valid 'dev', no? A quick 'grep do_ioctl net/' confirms that all calls are in the form of 'dev->do_ioctl(dev, ...'.

Yup, I think so too ...

Quoted 7 lines
Quoted 7 lines
(quoted diff) +1 -1
@@ -991,11 +991,13 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	short PID[4];
 	int ioaddr[2], status, frequency;
 	unsigned int stats[5];
-	struct sb1000_private *lp = netdev_priv(dev);
+	struct sb1000_private *lp;
 
Quoted 8 lines
Quoted 2 lines

Review static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 997

Quoted 2 lines
(quoted diff) +0 -0
 	if (!(dev && dev->flags & IFF_UP))
 		return -ENODEV;

I think we could get rid of the !dev check itself. Actually, the IFF_UP check /also/ looks suspect to me for two reasons: (1) I remember Stephen Hemminger once telling me dev->flags is legacy and unsafe, and one of the netif_xxx() functions be used instead, and, (2) I wonder if we really require the interface to be up and *running* when we do this ioctl.

Updated patch below.

[PATCH] sb1000: Remove bogus checks

In net_device->do_ioctl() of the sb1000 driver (sb1000_dev_ioctl):

(1) !dev condition is always false -- this function cannot be called with NULL net_device.
(2) !(dev->flags & IFF_UP) is bogus because the functions of this ioctl can (and should) be allowed even when the interface is not up and running.

So let's remove these checks.

Cc Jesper Juhl <[email protected]>
Cc Domen Puncer <[email protected]>

---

 drivers/net/sb1000.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/net/sb1000.c +0 -3
index 1de3eec..f60fe98 100644
@@ -993,9 +993,6 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	unsigned int stats[5];
 	struct sb1000_private *lp = netdev_priv(dev);
 
-	if (!(dev && dev->flags & IFF_UP))
-		return -ENODEV;
-
 	ioaddr[0] = dev->base_addr;
 	/* mem_start holds the second I/O address */
 	ioaddr[1] = dev->mem_start;
#4 lore raw
Quoted 2 lines · On Sunday 29 July 2007 20:34:46 Satyam Sharma wrote:

(2) !(dev->flags & IFF_UP) is bogus because the functions of this ioctl can (and should) be allowed even when the interface is not up and running.

Are you _sure_? This function does poke with the device hardware. It might return crap or even machinecheck when not initialized. Hardware is probably powered down, if not IFF_UP. (I don't know if that's the case here, though).

Quoted 14 lines
 drivers/net/sb1000.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/net/sb1000.c +0 -3
index 1de3eec..f60fe98 100644
@@ -993,9 +993,6 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	unsigned int stats[5];
 	struct sb1000_private *lp = netdev_priv(dev);
 
-	if (!(dev && dev->flags & IFF_UP))
-		return -ENODEV;
-
Signature · 1 line
Greetings Michael.
#5 lore raw

Hi Michael,

Quoted 8 lines · Michael Buesch
Quoted 2 lines · On Sunday 29 July 2007 20:34:46 Satyam Sharma wrote:

(2) !(dev->flags & IFF_UP) is bogus because the functions of this ioctl can (and should) be allowed even when the interface is not up and running.

Are you _sure_? This function does poke with the device hardware. It might return crap or even machinecheck when not initialized. Hardware is probably powered down, if not IFF_UP. (I don't know if that's the case here, though).

IFF_UP checks if the _interface_ is up -- the hardware / card could still be powered up, but the interface down (ifconfing eth0 down or ip link set eth0 down).

Probably what we want here is netif_device_present()? -- I think that should return true only when the *device* itself is up (as in powered) but the interface itself could be down ...

Let's wait for comments from the netdev people Cc:'ed here, in that case.

Quoted 11 lines
Quoted 11 lines
 drivers/net/sb1000.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/net/sb1000.c +0 -0
index 1de3eec..f60fe98 100644
@@ -993,9 +993,6 @@ static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 	unsigned int stats[5];
 	struct sb1000_private *lp = netdev_priv(dev);
 
Quoted 3 lines

drivers/net/sb1000.c static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 998

Quoted 3 lines
drivers/net/sb1000.c +0 -3
-	if (!(dev && dev->flags & IFF_UP))
-		return -ENODEV;
-

Satyam

#6 lore raw
Quoted 9 lines · Satyam Sharma

Hi Michael,

Quoted 4 lines · Michael Buesch
Quoted 2 lines · On Sunday 29 July 2007 20:34:46 Satyam Sharma wrote:

(2) !(dev->flags & IFF_UP) is bogus because the functions of this ioctl can (and should) be allowed even when the interface is not up and running.

Quoted 8 lines
Quoted 4 lines

Are you _sure_? This function does poke with the device hardware. It might return crap or even machinecheck when not initialized. Hardware is probably powered down, if not IFF_UP. (I don't know if that's the case here, though).

IFF_UP checks if the _interface_ is up -- the hardware / card could still be powered up, but the interface down (ifconfing eth0 down or ip link set eth0 down).

Well, that is device/driver dependent and I don't know what's the case for this driver. It's encouraged to shutdown hardware completely (except the WOL parts) when the interface is down. Dunno if this driver does it. But _if_ it does it, it could cause problems to poke with the hardware while down.