Skip to content

Commit d3843fe

Browse files
tracywwnjdavem330
authored andcommitted
ipv6: replace dst_hold() with dst_hold_safe() in routing code
With rwlock, it is safe to call dst_hold() in the read thread because read thread is guaranteed to be separated from write thread. However, after we replace rwlock with rcu, it is no longer safe to use dst_hold(). A dst might already have been deleted but is waiting for the rcu grace period to pass before freeing the memory when a read thread is trying to do dst_hold(). This could potentially cause double free issue. So this commit replaces all dst_hold() with dst_hold_safe() in all read thread to avoid this double free issue. And in order to make the code more compact, a new function ip6_hold_safe() is introduced. It calls dst_hold_safe() first, and if that fails, it will either fall back to hold and return net->ipv6.ip6_null_entry or set rt to NULL according to the caller's need. Signed-off-by: Wei Wang <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 51e398e commit d3843fe

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

net/ipv6/addrconf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,8 @@ static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
23332333
continue;
23342334
if ((rt->rt6i_flags & noflags) != 0)
23352335
continue;
2336-
dst_hold(&rt->dst);
2336+
if (!dst_hold_safe(&rt->dst))
2337+
rt = NULL;
23372338
break;
23382339
}
23392340
out:

net/ipv6/route.c

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,23 @@ static struct fib6_node* fib6_backtrack(struct fib6_node *fn,
874874
}
875875
}
876876

877+
static bool ip6_hold_safe(struct net *net, struct rt6_info **prt,
878+
bool null_fallback)
879+
{
880+
struct rt6_info *rt = *prt;
881+
882+
if (dst_hold_safe(&rt->dst))
883+
return true;
884+
if (null_fallback) {
885+
rt = net->ipv6.ip6_null_entry;
886+
dst_hold(&rt->dst);
887+
} else {
888+
rt = NULL;
889+
}
890+
*prt = rt;
891+
return false;
892+
}
893+
877894
static struct rt6_info *ip6_pol_route_lookup(struct net *net,
878895
struct fib6_table *table,
879896
struct flowi6 *fl6, int flags)
@@ -898,7 +915,9 @@ static struct rt6_info *ip6_pol_route_lookup(struct net *net,
898915
if (rt_cache)
899916
rt = rt_cache;
900917

901-
dst_use(&rt->dst, jiffies);
918+
if (ip6_hold_safe(net, &rt, true))
919+
dst_use_noref(&rt->dst, jiffies);
920+
902921
read_unlock_bh(&table->tb6_lock);
903922

904923
trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
@@ -1061,10 +1080,9 @@ static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt)
10611080
p = this_cpu_ptr(rt->rt6i_pcpu);
10621081
pcpu_rt = *p;
10631082

1064-
if (pcpu_rt) {
1065-
dst_hold(&pcpu_rt->dst);
1083+
if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false))
10661084
rt6_dst_from_metrics_check(pcpu_rt);
1067-
}
1085+
10681086
return pcpu_rt;
10691087
}
10701088

@@ -1625,12 +1643,17 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16251643
if (rt_cache)
16261644
rt = rt_cache;
16271645

1628-
if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) {
1629-
dst_use(&rt->dst, jiffies);
1646+
if (rt == net->ipv6.ip6_null_entry) {
1647+
read_unlock_bh(&table->tb6_lock);
1648+
dst_hold(&rt->dst);
1649+
trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
1650+
return rt;
1651+
} else if (rt->rt6i_flags & RTF_CACHE) {
1652+
if (ip6_hold_safe(net, &rt, true)) {
1653+
dst_use_noref(&rt->dst, jiffies);
1654+
rt6_dst_from_metrics_check(rt);
1655+
}
16301656
read_unlock_bh(&table->tb6_lock);
1631-
1632-
rt6_dst_from_metrics_check(rt);
1633-
16341657
trace_fib6_table_lookup(net, rt, table->tb6_id, fl6);
16351658
return rt;
16361659
} else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) &&
@@ -1643,7 +1666,13 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16431666

16441667
struct rt6_info *uncached_rt;
16451668

1646-
dst_use(&rt->dst, jiffies);
1669+
if (ip6_hold_safe(net, &rt, true)) {
1670+
dst_use_noref(&rt->dst, jiffies);
1671+
} else {
1672+
read_unlock_bh(&table->tb6_lock);
1673+
uncached_rt = rt;
1674+
goto uncached_rt_out;
1675+
}
16471676
read_unlock_bh(&table->tb6_lock);
16481677

16491678
uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL);
@@ -1659,6 +1688,7 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16591688
dst_hold(&uncached_rt->dst);
16601689
}
16611690

1691+
uncached_rt_out:
16621692
trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6);
16631693
return uncached_rt;
16641694

@@ -1667,8 +1697,7 @@ struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table,
16671697

16681698
struct rt6_info *pcpu_rt;
16691699

1670-
rt->dst.lastuse = jiffies;
1671-
rt->dst.__use++;
1700+
dst_use_noref(&rt->dst, jiffies);
16721701
pcpu_rt = rt6_get_pcpu_route(rt);
16731702

16741703
if (pcpu_rt) {
@@ -2130,7 +2159,7 @@ static struct rt6_info *__ip6_route_redirect(struct net *net,
21302159
}
21312160

21322161
out:
2133-
dst_hold(&rt->dst);
2162+
ip6_hold_safe(net, &rt, true);
21342163

21352164
read_unlock_bh(&table->tb6_lock);
21362165

@@ -2841,7 +2870,8 @@ static int ip6_route_del(struct fib6_config *cfg,
28412870
continue;
28422871
if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol)
28432872
continue;
2844-
dst_hold(&rt->dst);
2873+
if (!dst_hold_safe(&rt->dst))
2874+
break;
28452875
read_unlock_bh(&table->tb6_lock);
28462876

28472877
/* if gateway was specified only delete the one hop */
@@ -3038,7 +3068,7 @@ static struct rt6_info *rt6_get_route_info(struct net *net,
30383068
continue;
30393069
if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
30403070
continue;
3041-
dst_hold(&rt->dst);
3071+
ip6_hold_safe(NULL, &rt, false);
30423072
break;
30433073
}
30443074
out:
@@ -3096,7 +3126,7 @@ struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_dev
30963126
break;
30973127
}
30983128
if (rt)
3099-
dst_hold(&rt->dst);
3129+
ip6_hold_safe(NULL, &rt, false);
31003130
read_unlock_bh(&table->tb6_lock);
31013131
return rt;
31023132
}
@@ -3139,9 +3169,12 @@ static void __rt6_purge_dflt_routers(struct fib6_table *table)
31393169
for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
31403170
if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) &&
31413171
(!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) {
3142-
dst_hold(&rt->dst);
3143-
read_unlock_bh(&table->tb6_lock);
3144-
ip6_del_rt(rt);
3172+
if (dst_hold_safe(&rt->dst)) {
3173+
read_unlock_bh(&table->tb6_lock);
3174+
ip6_del_rt(rt);
3175+
} else {
3176+
read_unlock_bh(&table->tb6_lock);
3177+
}
31453178
goto restart;
31463179
}
31473180
}

0 commit comments

Comments
 (0)