net: mscc: ocelot: fix error handling bugs in mscc_ocelot_init_ports()
PATCH 1/1 net: mscc: ocelot: fix error handling bugs in mscc_ocelot_init_ports()
There are several error handling bugs in mscc_ocelot_init_ports(). I went through the code, and carefully audited it and made fixes and cleanups.
1) The ocelot_probe_port() function didn't have a mirror release function so it was hard to follow. I created the ocelot_release_port() function.
2) In the ocelot_probe_port() function, if the register_netdev() call failed, then it lead to a double free_netdev(dev) bug. Fix this by setting "ocelot->ports[port] = NULL" on the error path.
3) I was concerned that the "port" which comes from of_property_read_u32() might be out of bounds so I added a check for that.
4) In the original code if ocelot_regmap_init() failed then the driver tried to continue but I think that should be a fatal error.
5) If ocelot_probe_port() failed then the most recent devlink was leaked. The fix for mostly came Vladimir Oltean. Get rid of "registered_ports" and just set a bit in "devlink_ports_registered" to say when the devlink port has been registered (and needs to be unregistered on error). There are fewer than 32 ports so a u32 is large enough for this purpose.
6) The error handling if the final ocelot_port_devlink_init() failed had two problems. The "while (port-- >= 0)" loop should have been "--port" pre-op instead of a post-op to avoid a buffer underflow. The "if (!registered_ports[port])" condition was reversed leading to resource leaks and double frees.
---
v2: Part of the commit was missing
v3: The first version introduced a bug which completely broke the
driver. I moved the "ocelot->ports[port] = ocelot_port;" assignment in ocelot_probe_port() and that was wrong. Thanks to Vladimir for catching this.
Also v3 has additional cleanups in mscc_ocelot_init_ports().
drivers/net/ethernet/mscc/ocelot.h | 1 + drivers/net/ethernet/mscc/ocelot_net.c | 14 +++++- drivers/net/ethernet/mscc/ocelot_vsc7514.c | 52 ++++++++-------------- 3 files changed, 33 insertions(+), 34 deletions(-)
drivers/net/ethernet/mscc/ocelot.h +1 -0
@@ -121,6 +121,7 @@ void ocelot_port_writel(struct ocelot_port *port, u32 val, u32 reg);int ocelot_probe_port (struct ocelot *ocelot,int port,struct regmap *target,struct phy_device *phy); +void ocelot_release_port (struct ocelot_port *ocelot_port);int ocelot_devlink_init (struct ocelot *ocelot);void ocelot_devlink_teardown (struct ocelot *ocelot); int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
drivers/net/ethernet/mscc/ocelot_net.c +13 -1
@@ -1283,7 +1283,19 @@ int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,if (err) {dev_err (ocelot->dev,"register_netdev failed\n" );free_netdev (dev); + ocelot->ports[port] =NULL ; +return err; } -return err; +return 0 ; +} + +void ocelot_release_port (struct ocelot_port *ocelot_port) +{ +struct ocelot_port_private *priv =container_of (ocelot_port, + struct ocelot_port_private, + port); + +unregister_netdev (priv->dev); +free_netdev (priv->dev); }
drivers/net/ethernet/mscc/ocelot_vsc7514.c +19 -33
120 lines not on this page · raw message
Signature · 1 line
2.30.0
PATCH v3 2/2 net: mscc: ocelot: fix error code in mscc_ocelot_probe()
Probe should return an error code if platform_get_irq_byname() fails but it returns success instead.
---
v3: rebase
drivers/net/ethernet/mscc/ocelot_vsc7514.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/ethernet/mscc/ocelot_vsc7514.c +3 -1
@@ -1300,8 +1300,10 @@ static int mscc_ocelot_probe(struct platform_device *pdev) goto out_free_devlink; irq_xtr =platform_get_irq_byname (pdev,"xtr" ); -if (irq_xtr <0 ) +if (irq_xtr <0 ) { + err = irq_xtr; goto out_free_devlink; + } err = devm_request_threaded_irq(&pdev->dev, irq_xtr, NULL, ocelot_xtr_irq_handler, IRQF_ONESHOT,
Signature · 1 line
2.30.0
This series was applied to netdev/net-next.git (refs/heads/master):
Hello:
This series was applied to netdev/net-next.git (refs/heads/master):
Quoted 26 lines · On Tue, 2 Feb 2021 12:12:38 +0300 you wrote:
There are several error handling bugs in
mscc_ocelot_init_ports(). I went through the code, and carefully audited it and made fixes and cleanups.1) The
ocelot_probe_port()function didn't have a mirror release function so it was hard to follow. I created theocelot_release_port()function.
2) In theocelot_probe_port()function, if theregister_netdev()call failed, then it lead to a doublefree_netdev(dev)bug. Fix this by setting "ocelot->ports[port] = NULL" on the error path.
3) I was concerned that the "port" which comes fromof_property_read_u32()might be out of bounds so I added a check for that.
4) In the original code ifocelot_regmap_init()failed then the driver tried to continue but I think that should be a fatal error.
5) Ifocelot_probe_port()failed then the most recent devlink was leaked. The fix for mostly came Vladimir Oltean. Get rid of "registered_ports" and just set a bit in "devlink_ports_registered" to say when the devlink port has been registered (and needs to be unregistered on error). There are fewer than 32 ports so a u32 is large enough for this purpose.
6) The error handling if the finalocelot_port_devlink_init()failed had two problems. The "while (port-- >= 0)" loop should have been "--port" pre-op instead of a post-op to avoid a buffer underflow. The "if (!registered_ports[port])" condition was reversed leading to resource leaks and double frees.
Quoted 1 line
[...]
Here is the summary with links:
- [net-next] net: mscc: ocelot: fix error handling bugs in mscc_ocelot_init_ports()
https://git.kernel.org/netdev/net-next/c/e0c16233577f - [v3,2/2,net-next] net: mscc: ocelot: fix error code in mscc_ocelot_probe() https://git.kernel.org/netdev/net-next/c/4160d9ec5b41
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html