Skip to contentSkip to conversation

net: axienet: fix a signedness bug in probe

PATCH Applied Dan Carpenter 5 participants 8 messages Sep 25, 2019 – Sep 27, 2019

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

PATCH 1/1 net: axienet: fix a signedness bug in probe

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

Fixes ee06b1728b95 ("net: axienet: add support for standard phy-mode binding")
---
 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
index 4fc627fb4d11..676006f32f91 100644
@@ -1762,7 +1762,7 @@ static int axienet_probe(struct platform_device *pdev)
 		}
 	} else {
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {
 			ret = -EINVAL;
 			goto free_netdev;
 		}
Signature · 1 line
2.20.1
#1 lore raw

Hi, Dan

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 1765

Quoted 6 lines · Dan Carpenter

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {

This (almost) exact code appears in a lot of different drivers too, so maybe it'd be nice to review them all and apply the same cast if needed?

Best regards

Signature · 1 line
Alvaro G. M.
maintainer
#2 lore raw

From: "Alvaro G. M" <[email protected]>
Date: Wed, 25 Sep 2019 13:05:43 +0200

Quoted 6 lines

Hi, Dan

Quoted 3 lines · Dan Carpenter

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

Quoted 6 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 1765

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {

This (almost) exact code appears in a lot of different drivers too, so maybe it'd be nice to review them all and apply the same cast if needed?

Or make the thing an int if negative values are never valid 32-bit phy_mode values anyways.

#3 lore raw
Quoted 16 lines · David Miller

From: "Alvaro G. M" <[email protected]>
Date: Wed, 25 Sep 2019 13:05:43 +0200

Quoted 6 lines

Hi, Dan

Quoted 3 lines · Dan Carpenter

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

Quoted 6 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 1765

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {

This (almost) exact code appears in a lot of different drivers too, so maybe it'd be nice to review them all and apply the same cast if needed?

Quoted 2 lines

Or make the thing an int if negative values are never valid 32-bit phy_mode values anyways.

Maybe we should change the API

int of_get_phy_mode(struct device_node *np, phy_interface_t *phy_mode);

Separate the error from the value we are getting.

Andrew

#4 lore raw
In reply to Alvaro G. M
Quoted 6 lines · Alvaro G. M

Hi, Dan

Quoted 3 lines · Dan Carpenter

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

Quoted 7 lines

drivers/net/ethernet/xilinx/xilinx_axienet_main.c static int axienet_probe(struct platform_device *pdev) · line 1765

Quoted 3 lines
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {

This (almost) exact code appears in a lot of different drivers too, so maybe it'd be nice to review them all and apply the same cast if needed?

This is a new warning in Smatch. I did send patches for the whole kernel. We won't get these bugs in the future because people run Smatch on the kernel and will find the bugs. All the bugs were from 2017 or later which suggests that someone cleared these out two years ago but soon the 0-day bot will warn about issues so they will get fixed quicker.

I'm sort of out of it today...

The get_phy_mode() function seem like they lend themselves to creating these bugs. The ->phy_mode variables tend to be declared in the driver so it would require quite a few patches to make them all int and I'm not sure that's more beautiful. Andrew Lunn's idea to update the API would probably be a good idea.

I'm going back to bed for now and I'll think about this some more.

regards,
dan carpenter

#5 lore raw
Quoted 5 lines

The get_phy_mode() function seem like they lend themselves to creating these bugs. The ->phy_mode variables tend to be declared in the driver so it would require quite a few patches to make them all int and I'm not sure that's more beautiful. Andrew Lunn's idea to update the API would probably be a good idea.

Hi Dan

I started on it. Once net-next has opened, and 0-day has compile tested my changes, i will post the code for review.

Andrew

Reviewed-by on v1 1/1
#6 lore raw
In reply to Dan Carpenter
Quoted 10 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: Wednesday, September 25, 2019 4:29 PM
To: Radhey Shyam Pandey <[email protected]>; Alvaro G. M <[email protected]>
Cc: David S. Miller <[email protected]>; Michal Simek <[email protected]>; Russell King <[email protected]>; [email protected]; [email protected]
Subject: [PATCH net] net: axienet: fix a signedness bug in probe

Quoted 6 lines

The "lp->phy_mode" is an enum but in this context GCC treats it as an unsigned int so the error handling is never triggered.

Fixes ee06b1728b95 ("net: axienet: add support for standard phy-mode

binding")

Quoted 21 lines
---
 drivers/net/ethernet/xilinx/xilinx_axienet_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/xilinx/xilinx_axienet_main.c +1 -1
index 4fc627fb4d11..676006f32f91 100644
@@ -1762,7 +1762,7 @@ static int axienet_probe(struct platform_device
 *pdev)
 		}
 	} else {
 		lp->phy_mode = of_get_phy_mode(pdev->dev.of_node);
-		if (lp->phy_mode < 0) {
+		if ((int)lp->phy_mode < 0) {
 			ret = -EINVAL;
 			goto free_netdev;

}
--
2.20.1