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 <[email protected]>
---
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
Hello,
> -----Original Message-----
> From: Jianglei Nie <[email protected]>
> Sent: 2022??3??3?? 9:30
> To: [email protected]; [email protected]; Cai,Huoqing
> Cc: [email protected]; [email protected]; 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 <[email protected]>
> ---
> 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
On Thu, 3 Mar 2022 09:30:22 +0800 Jianglei Nie wrote:
> 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 <[email protected]>
> ---
> 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);
Bus name is a constant please put it in a local variable:
const char *name = "Synopsys MII Bus";
...
bus->name = name;
and then you can use name in the error message without referring to bus.
> + mdiobus_free(bus);
> + return dev_err;
> }
>
> return 0;
> If resend a patch, you can use prefix "[PATCH v2]" in subject.
> e.g. git format-patch -1 -v2
Yes.
But please also read the netdev FAQ:
https://docs.kernel.org/networking/netdev-FAQ.html
Andrew
Hello,
> -----Original Message-----
> From: Andrew Lunn <[email protected]>
> Sent: 2022??3??8?? 21:43
> To: Jianglei Nie
> Cc: [email protected]; [email protected]; Cai,Huoqing;
> [email protected]; [email protected]
> Subject: Re: [PATCH] net: arc_emac: Fix use after free in arc_mdio_probe()
If resend a patch, you can use prefix "[PATCH v2]" in subject.
e.g. git format-patch -1 -v2
>
> On Tue, Mar 08, 2022 at 07:10:05PM +0800, Jianglei Nie wrote:
> > 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 putting the bus->name in a local variable and then use
> > the name in the error message without referring to bus to avoid the uaf.
> >
> > Signed-off-by: Jianglei Nie <[email protected]>
> > ---
> > 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..33fd63d227ef 100644
> > --- a/drivers/net/ethernet/arc/emac_mdio.c
> > +++ b/drivers/net/ethernet/arc/emac_mdio.c
> > @@ -134,6 +134,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> > struct device_node *np = priv->dev->of_node;
> > struct mii_bus *bus;
> > int error;
> > + const char *name = "Synopsys MII Bus";
>
> Netdev uses reverse christmass tree, meaning you need to sort
> variables longest to shortest.
>
> I'm also wondering about the lifetime of name. name itself is a stack
> variable, so it will disappear as soon as the function exits. The
> string itself is in the rodata section. But is a copy made onto the
> stack, or does bus->name point to the rodata?
>
> Andrew
>
> > bus = mdiobus_alloc();
> > if (!bus)
> > @@ -142,7 +143,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> > priv->bus = bus;
> > bus->priv = priv;
> > bus->parent = priv->dev;
> > - bus->name = "Synopsys MII Bus";
> > + bus->name = name;
> > bus->read = &arc_mdio_read;
> > bus->write = &arc_mdio_write;
> > bus->reset = &arc_mdio_reset;
> > @@ -167,7 +168,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> > if (error) {
> > mdiobus_free(bus);
> > return dev_err_probe(priv->dev, error,
> > - "cannot register MDIO bus %s\n", bus-
> >name);
> > + "cannot register MDIO bus %s\n", name);
> > }
> >
> > return 0;
> > --
> > 2.25.1
> >
On Tue, Mar 08, 2022 at 07:10:05PM +0800, Jianglei Nie wrote:
> 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 putting the bus->name in a local variable and then use
> the name in the error message without referring to bus to avoid the uaf.
>
> Signed-off-by: Jianglei Nie <[email protected]>
> ---
> 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..33fd63d227ef 100644
> --- a/drivers/net/ethernet/arc/emac_mdio.c
> +++ b/drivers/net/ethernet/arc/emac_mdio.c
> @@ -134,6 +134,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> struct device_node *np = priv->dev->of_node;
> struct mii_bus *bus;
> int error;
> + const char *name = "Synopsys MII Bus";
Netdev uses reverse christmass tree, meaning you need to sort
variables longest to shortest.
I'm also wondering about the lifetime of name. name itself is a stack
variable, so it will disappear as soon as the function exits. The
string itself is in the rodata section. But is a copy made onto the
stack, or does bus->name point to the rodata?
Andrew
> bus = mdiobus_alloc();
> if (!bus)
> @@ -142,7 +143,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> priv->bus = bus;
> bus->priv = priv;
> bus->parent = priv->dev;
> - bus->name = "Synopsys MII Bus";
> + bus->name = name;
> bus->read = &arc_mdio_read;
> bus->write = &arc_mdio_write;
> bus->reset = &arc_mdio_reset;
> @@ -167,7 +168,7 @@ int arc_mdio_probe(struct arc_emac_priv *priv)
> if (error) {
> mdiobus_free(bus);
> return dev_err_probe(priv->dev, error,
> - "cannot register MDIO bus %s\n", bus->name);
> + "cannot register MDIO bus %s\n", name);
> }
>
> return 0;
> --
> 2.25.1
>