Skip to contentSkip to conversation

hdlcdrv: fix divide error bug if bitrate is 0

PATCH In review Firo Yang 2 participants 5 messages May 17, 2017 – May 18, 2017

Subsystems (2)
lore
Share & tools
lore

Firo Yang

author
#0 lore raw

PATCH 1/1 hdlcdrv: fix divide error bug if bitrate is 0

The divisor s->par.bitrate will always be 0 until initialized by ndo_open() and hdlcdrv_open().

In order to fix this divide zero error, check whether the netdevice was opened by ndo_open() before performing divide.

---
 drivers/net/hamradio/hdlcdrv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hamradio/hdlcdrv.c +1 -1
index 8c3633c..3c783fd 100644
@@ -574,7 +574,7 @@ static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 		break;		
 
 	case HDLCDRVCTL_CALIBRATE:
-		if(!capable(CAP_SYS_RAWIO))
+		if (!capable(CAP_SYS_RAWIO) || !netif_running(dev))
 			return -EPERM;
 		if (bi.data.calibrate > INT_MAX / s->par.bitrate)
 			return -EINVAL;
Signature · 1 line
2.9.4
#1 lore raw

Am 17.05.2017 14:35, schrieb Firo Yang:

Quoted 17 lines

The divisor s->par.bitrate will always be 0 until initialized by ndo_open() and hdlcdrv_open().

In order to fix this divide zero error, check whether the netdevice was opened by ndo_open() before performing divide.

---
 drivers/net/hamradio/hdlcdrv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hamradio/hdlcdrv.c +0 -0
index 8c3633c..3c783fd 100644

drivers/net/hamradio/hdlcdrv.c static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 580

Quoted 8 lines
drivers/net/hamradio/hdlcdrv.c +1 -1
@@ -574,7 +574,7 @@ static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 		break;		
 
 	case HDLCDRVCTL_CALIBRATE:
-		if(!capable(CAP_SYS_RAWIO))
+		if (!capable(CAP_SYS_RAWIO) || !netif_running(dev))
 			return -EPERM;
 		if (bi.data.calibrate > INT_MAX / s->par.bitrate)
 			return -EINVAL;

I would still check for s->par.bitrate > 0 later changes may affect the setting of it and it is much more obvious.

Also perhaps !netif_running(dev) should better return ENODEV.

just my 2 cents,
re,
wh

author
#2 lore raw
Quoted 22 lines · walter harms

Am 17.05.2017 14:35, schrieb Firo Yang:

Quoted 19 lines

The divisor s->par.bitrate will always be 0 until initialized by ndo_open() and hdlcdrv_open().

In order to fix this divide zero error, check whether the netdevice was opened by ndo_open() before performing divide.

---
 drivers/net/hamradio/hdlcdrv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hamradio/hdlcdrv.c +0 -0
index 8c3633c..3c783fd 100644
@@ -574,7 +574,7 @@ static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 		break;		
 
Quoted 9 lines

drivers/net/hamradio/hdlcdrv.c static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 580

Quoted 6 lines
drivers/net/hamradio/hdlcdrv.c +1 -1
 	case HDLCDRVCTL_CALIBRATE:
-		if(!capable(CAP_SYS_RAWIO))
+		if (!capable(CAP_SYS_RAWIO) || !netif_running(dev))
 			return -EPERM;
 		if (bi.data.calibrate > INT_MAX / s->par.bitrate)
 			return -EINVAL;

I would still check for s->par.bitrate > 0 later changes may affect the setting of it and it is much more obvious.

I think 0 is not valid value for bitrate, so we should check it in other places, like what ser12_open() did:
429 if (bc->baud < 300 || bc->baud > 4800) {
430 printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
431 "(300...4800)\n");
432 return -EINVAL;
433 }
...
440 bc->hdrv.par.bitrate = bc->baud;

Quoted 2 lines

Also perhaps !netif_running(dev) should better return ENODEV.

However, the 'dev' truly exists in this circumstance.

Thanks,
Firo

Quoted 6 lines

just my 2 cents,
re,
wh

#3 lore raw

Am 17.05.2017 15:42, schrieb Firo Yang:

Quoted 33 lines · walter harms
Quoted 22 lines

Am 17.05.2017 14:35, schrieb Firo Yang:

Quoted 19 lines

The divisor s->par.bitrate will always be 0 until initialized by ndo_open() and hdlcdrv_open().

In order to fix this divide zero error, check whether the netdevice was opened by ndo_open() before performing divide.

---
 drivers/net/hamradio/hdlcdrv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hamradio/hdlcdrv.c +0 -0
index 8c3633c..3c783fd 100644
@@ -574,7 +574,7 @@ static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 		break;		
 
Quoted 9 lines

drivers/net/hamradio/hdlcdrv.c static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 580

Quoted 6 lines
drivers/net/hamradio/hdlcdrv.c +1 -1
 	case HDLCDRVCTL_CALIBRATE:
-		if(!capable(CAP_SYS_RAWIO))
+		if (!capable(CAP_SYS_RAWIO) || !netif_running(dev))
 			return -EPERM;
 		if (bi.data.calibrate > INT_MAX / s->par.bitrate)
 			return -EINVAL;

I would still check for s->par.bitrate > 0 later changes may affect the setting of it and it is much more obvious.

Quoted 9 lines

I think 0 is not valid value for bitrate, so we should check it in other places, like what ser12_open() did:
429 if (bc->baud < 300 || bc->baud > 4800) {
430 printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
431 "(300...4800)\n");
432 return -EINVAL;
433 }
...
440 bc->hdrv.par.bitrate = bc->baud;

I do not want to say you change is not valid but i have learned that it is better to have an obvious check that to rely on hidden knowledge.

Quoted 6 lines
Quoted 3 lines

Also perhaps !netif_running(dev) should better return ENODEV.

However, the 'dev' truly exists in this circumstance.

yes and i do not feel good with that but "no permission" will lead any enduser into a search for user rights.

re,
wh

Quoted 9 lines

Thanks,
Firo

Quoted 6 lines

just my 2 cents,
re,
wh

author
#4 lore raw
Quoted 47 lines · walter harms

Am 17.05.2017 15:42, schrieb Firo Yang:

Quoted 33 lines · walter harms
Quoted 22 lines

Am 17.05.2017 14:35, schrieb Firo Yang:

Quoted 19 lines

The divisor s->par.bitrate will always be 0 until initialized by ndo_open() and hdlcdrv_open().

In order to fix this divide zero error, check whether the netdevice was opened by ndo_open() before performing divide.

---
 drivers/net/hamradio/hdlcdrv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hamradio/hdlcdrv.c +0 -0
index 8c3633c..3c783fd 100644
@@ -574,7 +574,7 @@ static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 		break;		
 
Quoted 9 lines

drivers/net/hamradio/hdlcdrv.c static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) · line 580

Quoted 6 lines
drivers/net/hamradio/hdlcdrv.c +1 -1
 	case HDLCDRVCTL_CALIBRATE:
-		if(!capable(CAP_SYS_RAWIO))
+		if (!capable(CAP_SYS_RAWIO) || !netif_running(dev))
 			return -EPERM;
 		if (bi.data.calibrate > INT_MAX / s->par.bitrate)
 			return -EINVAL;

I would still check for s->par.bitrate > 0 later changes may affect the setting of it and it is much more obvious.

Quoted 9 lines

I think 0 is not valid value for bitrate, so we should check it in other places, like what ser12_open() did:
429 if (bc->baud < 300 || bc->baud > 4800) {
430 printk(KERN_INFO "baycom_ser_fdx: invalid baudrate "
431 "(300...4800)\n");
432 return -EINVAL;
433 }
...
440 bc->hdrv.par.bitrate = bc->baud;

Quoted 2 lines

I do not want to say you change is not valid but i have learned that it is better to have an obvious check that to rely on hidden knowledge.

I agree with this.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

Also perhaps !netif_running(dev) should better return ENODEV.

However, the 'dev' truly exists in this circumstance.

yes and i do not feel good with that but "no permission" will lead any enduser into a search for user rights.

Indeed, ENODEV is more informative to enduser.
I will send a update patch.

Thanks,
Firo

Quoted 16 lines

re,
wh

Quoted 9 lines

Thanks,
Firo

Quoted 6 lines

just my 2 cents,
re,
wh