net: dsa: mv88e6xxx: fix use-after-free in mv88e6xxx_mdios_unregister
authorVladimir Oltean <vladimir.oltean@nxp.com>
Thu, 10 Feb 2022 17:40:17 +0000 (19:40 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 16 Feb 2022 11:56:32 +0000 (12:56 +0100)
commitf916181692cb5da7050ec0efbf96c915d8ebbbbc
treea2e165b56f78025975156a6e236458a069838075
parentd98ba26a4ba9b835887e379193be6cee814c7a52
net: dsa: mv88e6xxx: fix use-after-free in mv88e6xxx_mdios_unregister

[ Upstream commit 51a04ebf21122d5c76a716ecd9bfc33ea44b2b39 ]

Since struct mv88e6xxx_mdio_bus *mdio_bus is the bus->priv of something
allocated with mdiobus_alloc_size(), this means that mdiobus_free(bus)
will free the memory backing the mdio_bus as well. Therefore, the
mdio_bus->list element is freed memory, but we continue to iterate
through the list of MDIO buses using that list element.

To fix this, use the proper list iterator that handles element deletion
by keeping a copy of the list element next pointer.

Fixes: f53a2ce893b2 ("net: dsa: mv88e6xxx: don't use devres for mdiobus")
Reported-by: Rafael Richter <rafael.richter@gin.de>
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Link: https://lore.kernel.org/r/20220210174017.3271099-1-vladimir.oltean@nxp.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/dsa/mv88e6xxx/chip.c