A tagger that offloads TX forwarding reads dp->bridge from its xmit function, in an RCU read-side critical section with BH disabled, to find the bridge number of a frame that the bridge marked with offload_fwd_mark; the receive path reads it as well (dsa_tag_8021q_find_port_by_vbid()). dsa_port_bridge_destroy() clears dp->bridge under rtnl_lock and frees the structure with kfree() once the last port has left. A frame that was marked before the leave can still be on its way through the tagger at that point, and the tagger then dereferences freed memory. dsa_port_bridge_dev_get() and dsa_port_bridge_num_get() read the pointer twice, so a concurrent leave also turns their NULL test into a NULL dereference. Free the structure with kfree_rcu(), publish dp->bridge with smp_store_release() and clear it with WRITE_ONCE(), and read it with READ_ONCE() in the datapath: in both helpers, once each, and in dsa_find_designated_bridge_port_by_vid(), dsa_default_offload_fwd_mark() and dsa_tag_8021q_find_port_by_vbid(), which uses the bridge it read for the number as well. Found by an LLM review (Claude). Fixes: d3eed0e57d5d ("net: dsa: keep the bridge_dev and bridge_num as part of the same structure") Signed-off-by: Julius Bairaktaris --- include/net/dsa.h | 14 ++++++++++++-- net/dsa/port.c | 11 +++++++---- net/dsa/tag.h | 4 ++-- net/dsa/tag_8021q.c | 6 ++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/net/dsa.h b/include/net/dsa.h index 5d12191b6f6..1076439e838 100644 --- a/include/net/dsa.h +++ b/include/net/dsa.h @@ -242,6 +242,7 @@ struct dsa_bridge { unsigned int num; bool tx_fwd_offload; refcount_t refcount; + struct rcu_head rcu; }; struct dsa_port { @@ -302,6 +303,11 @@ struct dsa_port { struct device_node *dn; unsigned int ageing_time; + /* Written under rtnl_lock. The datapath reads it once, with + * READ_ONCE(), in an RCU read-side critical section: the port can + * leave its bridge while a frame is on its way through the tagger, + * and the structure is freed only after a grace period. + */ struct dsa_bridge *bridge; struct devlink_port devlink_port; struct phylink *pl; @@ -772,12 +778,16 @@ struct net_device *dsa_port_to_bridge_port(const struct dsa_port *dp) static inline struct net_device * dsa_port_bridge_dev_get(const struct dsa_port *dp) { - return dp->bridge ? dp->bridge->dev : NULL; + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + return bridge ? bridge->dev : NULL; } static inline unsigned int dsa_port_bridge_num_get(struct dsa_port *dp) { - return dp->bridge ? dp->bridge->num : 0; + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + return bridge ? bridge->num : 0; } static inline bool dsa_port_bridge_same(const struct dsa_port *a, diff --git a/net/dsa/port.c b/net/dsa/port.c index 1f5536c0dff..997e68cd215 100644 --- a/net/dsa/port.c +++ b/net/dsa/port.c @@ -427,7 +427,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp, bridge = dsa_tree_bridge_find(ds->dst, br); if (bridge) { refcount_inc(&bridge->refcount); - dp->bridge = bridge; + /* Pairs with the READ_ONCE() of the datapath */ + smp_store_release(&dp->bridge, bridge); return 0; } @@ -447,7 +448,8 @@ static int dsa_port_bridge_create(struct dsa_port *dp, return -EOPNOTSUPP; } - dp->bridge = bridge; + /* Pairs with the READ_ONCE() of the datapath */ + smp_store_release(&dp->bridge, bridge); return 0; } @@ -457,7 +459,7 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp, { struct dsa_bridge *bridge = dp->bridge; - dp->bridge = NULL; + WRITE_ONCE(dp->bridge, NULL); if (!refcount_dec_and_test(&bridge->refcount)) return; @@ -465,7 +467,8 @@ static void dsa_port_bridge_destroy(struct dsa_port *dp, if (bridge->num) dsa_bridge_num_put(br, bridge->num); - kfree(bridge); + /* The datapath may still hold the pointer it read from dp->bridge */ + kfree_rcu(bridge, rcu); } static bool dsa_port_supports_mst(struct dsa_port *dp) diff --git a/net/dsa/tag.h b/net/dsa/tag.h index cf52283fe9d..0e1be1da453 100644 --- a/net/dsa/tag.h +++ b/net/dsa/tag.h @@ -201,7 +201,7 @@ dsa_find_designated_bridge_port_by_vid(struct net_device *conduit, u16 vid) if (dp->type != DSA_PORT_TYPE_USER) continue; - if (!dp->bridge) + if (!READ_ONCE(dp->bridge)) continue; if (dp->stp_state != BR_STATE_LEARNING && @@ -238,7 +238,7 @@ static inline void dsa_default_offload_fwd_mark(struct sk_buff *skb) { struct dsa_port *dp = dsa_user_to_port(skb->dev); - skb->offload_fwd_mark = !!(dp->bridge); + skb->offload_fwd_mark = !!READ_ONCE(dp->bridge); } /* Helper for removing DSA header tags from packets in the RX path. diff --git a/net/dsa/tag_8021q.c b/net/dsa/tag_8021q.c index f91646e6024..ac9ba1ee04f 100644 --- a/net/dsa/tag_8021q.c +++ b/net/dsa/tag_8021q.c @@ -482,7 +482,9 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid) return NULL; dsa_tree_for_each_user_port(dp, dst) { - if (!dp->bridge) + const struct dsa_bridge *bridge = READ_ONCE(dp->bridge); + + if (!bridge) continue; if (dp->stp_state != BR_STATE_LEARNING && @@ -492,7 +494,7 @@ dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid) if (dp->cpu_dp != cpu_dp) continue; - if (dsa_port_bridge_num_get(dp) == vbid) + if (bridge->num == vbid) return dp->user; } -- 2.53.0