[PATCH] net: arc_emac: Fix use after free in arc_mdio_probe()

From: Jianglei Nie
Date: Wed Mar 02 2022 - 20:46:14 EST


If bus->state is equal to MDIOBUS_ALLOCATED, mdiobus_free(bus) will free
the "bus". But bus->name is still used in the next line, which will lead
to a use after free.

We can fix it by assigning dev_err_probe() to dev_err before the bus is
freed to avoid the uaf.

Signed-off-by: Jianglei Nie <niejianglei2021@xxxxxxx>
---
drivers/net/ethernet/arc/emac_mdio.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/arc/emac_mdio.c b/drivers/net/ethernet/arc/emac_mdio.c
index 9acf589b1178..795a25c5848a 100644
--- a/drivers/net/ethernet/arc/emac_mdio.c
+++ b/drivers/net/ethernet/arc/emac_mdio.c
@@ -165,9 +165,10 @@ int arc_mdio_probe(struct arc_emac_priv *priv)

error = of_mdiobus_register(bus, priv->dev->of_node);
if (error) {
- mdiobus_free(bus);
- return dev_err_probe(priv->dev, error,
+ int dev_err = dev_err_probe(priv->dev, error,
"cannot register MDIO bus %s\n", bus->name);
+ mdiobus_free(bus);
+ return dev_err;
}

return 0;
--
2.25.1