Skip to content

Commit b2662e7

Browse files
mehulraokuba-moo
authored andcommitted
net: nexthop: fix percpu use-after-free in remove_nh_grp_entry
When removing a nexthop from a group, remove_nh_grp_entry() publishes the new group via rcu_assign_pointer() then immediately frees the removed entry's percpu stats with free_percpu(). However, the synchronize_net() grace period in the caller remove_nexthop_from_groups() runs after the free. RCU readers that entered before the publish still see the old group and can dereference the freed stats via nh_grp_entry_stats_inc() -> get_cpu_ptr(nhge->stats), causing a use-after-free on percpu memory. Fix by deferring the free_percpu() until after synchronize_net() in the caller. Removed entries are chained via nh_list onto a local deferred free list. After the grace period completes and all RCU readers have finished, the percpu stats are safely freed. Fixes: f4676ea ("net: nexthop: Add nexthop group entry stats") Cc: [email protected] Signed-off-by: Mehul Rao <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 288598d commit b2662e7

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

net/ipv4/nexthop.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,8 @@ static void nh_hthr_group_rebalance(struct nh_group *nhg)
20022002
}
20032003

20042004
static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
2005-
struct nl_info *nlinfo)
2005+
struct nl_info *nlinfo,
2006+
struct list_head *deferred_free)
20062007
{
20072008
struct nh_grp_entry *nhges, *new_nhges;
20082009
struct nexthop *nhp = nhge->nh_parent;
@@ -2062,8 +2063,8 @@ static void remove_nh_grp_entry(struct net *net, struct nh_grp_entry *nhge,
20622063
rcu_assign_pointer(nhp->nh_grp, newg);
20632064

20642065
list_del(&nhge->nh_list);
2065-
free_percpu(nhge->stats);
20662066
nexthop_put(nhge->nh);
2067+
list_add(&nhge->nh_list, deferred_free);
20672068

20682069
/* Removal of a NH from a resilient group is notified through
20692070
* bucket notifications.
@@ -2083,6 +2084,7 @@ static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
20832084
struct nl_info *nlinfo)
20842085
{
20852086
struct nh_grp_entry *nhge, *tmp;
2087+
LIST_HEAD(deferred_free);
20862088

20872089
/* If there is nothing to do, let's avoid the costly call to
20882090
* synchronize_net()
@@ -2091,10 +2093,16 @@ static void remove_nexthop_from_groups(struct net *net, struct nexthop *nh,
20912093
return;
20922094

20932095
list_for_each_entry_safe(nhge, tmp, &nh->grp_list, nh_list)
2094-
remove_nh_grp_entry(net, nhge, nlinfo);
2096+
remove_nh_grp_entry(net, nhge, nlinfo, &deferred_free);
20952097

20962098
/* make sure all see the newly published array before releasing rtnl */
20972099
synchronize_net();
2100+
2101+
/* Now safe to free percpu stats — all RCU readers have finished */
2102+
list_for_each_entry_safe(nhge, tmp, &deferred_free, nh_list) {
2103+
list_del(&nhge->nh_list);
2104+
free_percpu(nhge->stats);
2105+
}
20982106
}
20992107

21002108
static void remove_nexthop_group(struct nexthop *nh, struct nl_info *nlinfo)

0 commit comments

Comments
 (0)