In the context of phylink_pcs_change(), phylink instance is dereferenced from the pcs pointer present in the phylink_pcs struct. There is currently a fragility where this pcs pointer is never set NULL in phylink_destroy() and would reference invalid memory after phylink instance is freed. In this scenario, if a PCS driver calls phylink_pcs_change(), invalid memory can be dereferenced and phylink_link_changed() is called on freed memory. To address this, make the phylink instance saved in phylink_pcs struct RCU protected and access it under RCU lock. Also correctly reset the phylink instance pointer on phylink_destroy() and syncronyze RCU to catch an execution of phylink_pcs_change(). Signed-off-by: Christian Marangi --- drivers/net/phy/phylink.c | 17 ++++++++++++++--- include/linux/phylink.h | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 5d649903be9e..83620a120f18 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -1300,10 +1301,10 @@ static void phylink_major_config(struct phylink *pl, bool restart, phylink_pcs_disable(pl->pcs); if (pl->pcs) - pl->pcs->phylink = NULL; + rcu_assign_pointer(pcs->phylink, NULL); if (pcs) - pcs->phylink = pl; + rcu_assign_pointer(pcs->phylink, pl); pl->pcs = pcs; } @@ -2145,6 +2146,11 @@ void phylink_destroy(struct phylink *pl) if (pl->link_gpio) gpiod_put(pl->link_gpio); + if (pl->pcs) { + rcu_assign_pointer(pl->pcs->phylink, NULL); + synchronize_rcu(); + } + cancel_work_sync(&pl->resolve); kfree(pl); } @@ -2609,10 +2615,15 @@ EXPORT_SYMBOL_GPL(phylink_mac_change); */ void phylink_pcs_change(struct phylink_pcs *pcs, bool up) { - struct phylink *pl = pcs->phylink; + struct phylink *pl; + + rcu_read_lock(); + pl = rcu_dereference(pcs->phylink); if (pl) phylink_link_changed(pl, up, "pcs"); + + rcu_read_unlock(); } EXPORT_SYMBOL_GPL(phylink_pcs_change); diff --git a/include/linux/phylink.h b/include/linux/phylink.h index 3a88a69882a6..4a0dc6fa8010 100644 --- a/include/linux/phylink.h +++ b/include/linux/phylink.h @@ -494,7 +494,7 @@ struct phylink_pcs_ops; struct phylink_pcs { DECLARE_PHY_INTERFACE_MASK(supported_interfaces); const struct phylink_pcs_ops *ops; - struct phylink *phylink; + struct phylink __rcu *phylink; bool poll; bool rxc_always_on; }; -- 2.55.0