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

From: Cai,Huoqing
Date: Wed Mar 02 2022 - 20:59:46 EST


Hello,

> -----Original Message-----
> From: Jianglei Nie <niejianglei2021@xxxxxxx>
> Sent: 2022年3月3日 9:30
> To: davem@xxxxxxxxxxxxx; kuba@xxxxxxxxxx; Cai,Huoqing
> Cc: netdev@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; Jianglei Nie
> Subject: [PATCH] net: arc_emac: Fix use after free in arc_mdio_probe()
>
> 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.
Add fix log here:)
Fixes: 95b5fc03c189 ("net: arc_emac: Make use of the helper function dev_err_probe()")
>
> 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