Skip to contentSkip to conversation

igbvf: integer wrapping bug setting the mtu

PATCH Superseded Dan Carpenter 3 participants 6 messages Sep 13, 2013

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

PATCH 1/1 igbvf: integer wrapping bug setting the mtu

If new_mtu is very large then "new_mtu + ETH_HLEN + ETH_FCS_LEN" can wrap and the check on the next line can underflow. This is one of those bugs which can be triggered by the user if you have namespaces configured.

This is a static checker fix and I'm not sure what the impact is.

drivers/net/ethernet/intel/igbvf/netdev.c +1 -1
index 95d5430..24e3883 100644
@@ -2342,7 +2342,7 @@ static struct net_device_stats *igbvf_get_stats(struct net_device *netdev)
 static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
 {
 	struct igbvf_adapter *adapter = netdev_priv(netdev);
-	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
+	unsigned int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
 
 	if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
 		dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
#1 lore raw
Quoted 14 lines

If new_mtu is very large then "new_mtu + ETH_HLEN + ETH_FCS_LEN" can wrap and the check on the next line can underflow. This is one of those bugs which can be triggered by the user if you have namespaces configured.

This is a static checker fix and I'm not sure what the impact is.

drivers/net/ethernet/intel/igbvf/netdev.c +0 -0
index 95d5430..24e3883 100644

drivers/net/ethernet/intel/igbvf/netdev.c static struct net_device_stats *igbvf_get_stats(struct net_device *netdev) · line 2348

Quoted 8 lines
drivers/net/ethernet/intel/igbvf/netdev.c +1 -1
@@ -2342,7 +2342,7 @@ static struct net_device_stats *igbvf_get_stats(struct net_device *netdev)
 static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
 {
 	struct igbvf_adapter *adapter = netdev_priv(netdev);
-	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
+	unsigned int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
 
 	if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
 		dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");

It is safer to check:
if ((new_mtu < 68) || (new_mtu > MAX_JUMBO_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN)) {

David

#2 lore raw
Quoted 20 lines · David Laight
Quoted 20 lines

If new_mtu is very large then "new_mtu + ETH_HLEN + ETH_FCS_LEN" can wrap and the check on the next line can underflow. This is one of those bugs which can be triggered by the user if you have namespaces configured.

This is a static checker fix and I'm not sure what the impact is.

drivers/net/ethernet/intel/igbvf/netdev.c +1 -1
index 95d5430..24e3883 100644
@@ -2342,7 +2342,7 @@ static struct net_device_stats *igbvf_get_stats(struct net_device *netdev)
 static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
 {
 	struct igbvf_adapter *adapter = netdev_priv(netdev);
-	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
+	unsigned int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
 
Quoted 6 lines

drivers/net/ethernet/intel/igbvf/netdev.c static struct net_device_stats *igbvf_get_stats(struct net_device *netdev) · line 2348

Quoted 2 lines
drivers/net/ethernet/intel/igbvf/netdev.c +0 -0
 	if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
 		dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");

It is safer to check:
if ((new_mtu < 68) || (new_mtu > MAX_JUMBO_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN)) {

I believe my fix is already 100% safe... Where is the bug in my code?

Your fix harder to read because of the additional math and because it's checking "new_mtu" when we care about "max_frame".

regards,
dan carpenter

#3 lore raw
Quoted 13 lines
Quoted 6 lines
Quoted 6 lines

static int igbvf_change_mtu(struct net_device *netdev, int new_mtu) {

drivers/net/ethernet/intel/igbvf/netdev.c +1 -1
 	struct igbvf_adapter *adapter = netdev_priv(netdev);
-	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
+	unsigned int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
Quoted 6 lines

drivers/net/ethernet/intel/igbvf/netdev.c static struct net_device_stats *igbvf_get_stats(struct net_device *netdev) · line 2348

Quoted 2 lines
drivers/net/ethernet/intel/igbvf/netdev.c +0 -0
 	if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
 		dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");

It is safer to check:
if ((new_mtu < 68) || (new_mtu > MAX_JUMBO_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN)) {

Quoted 4 lines

I believe my fix is already 100% safe... Where is the bug in my code?

Your fix harder to read because of the additional math and because it's checking "new_mtu" when we care about "max_frame".

I don't like the window check on two variables.

However if ETH_HLEN and ETH_FCS_LEN are 'int' (not an unsigned type) and 'new_mtu' is just below MAX_INT then the signed arithmetic could overflow - generating an indeterminate value.
In which case 'max_frame' might not exceed MAX_JUMBO_FRAME_SIZE.

David

#4 lore raw

PATCH v2 1/1 igbvf: integer wrapping bug setting the mtu

If new_mtu is very large then "new_mtu + ETH_HLEN + ETH_FCS_LEN" can wrap and the check on the next line can underflow. This is one of those bugs which can be triggered by the user if you have namespaces configured.

Also since this is something the user can trigger then we don't want to have dev_err() message.

This is a static checker fix and I'm not sure what the impact is.
---
v2: reformat and also remove the dev_err()

drivers/net/ethernet/intel/igbvf/netdev.c +2 -3
index 95d5430..8cb7958 100644
@@ -2344,10 +2344,9 @@ static int igbvf_change_mtu(struct net_device *netdev, int new_mtu)
 	struct igbvf_adapter *adapter = netdev_priv(netdev);
 	int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
 
-	if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
-		dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
+	if (new_mtu < 68 || new_mtu > INT_MAX - ETH_HLEN - ETH_FCS_LEN ||
+	    max_frame > MAX_JUMBO_FRAME_SIZE)
 		return -EINVAL;
-	}
 
 #define MAX_STD_JUMBO_FRAME_SIZE 9234
 	if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
#5 lore raw
Quoted 10 lines · Dan Carpenter

If new_mtu is very large then "new_mtu + ETH_HLEN + ETH_FCS_LEN" can wrap and the check on the next line can underflow. This is one of those
bugs which can be triggered by the user if you have namespaces configured.

Also since this is something the user can trigger then we don't want to
have dev_err() message.

Quoted 5 lines

This is a static checker fix and I'm not sure what the impact is.
---
v2: reformat and also remove the dev_err()

Thanks Dan, I have added your patch to my queue.