Every counted IPv6 dst allocation runs ip6_dst_gc() once the number of live dst goes over gc_thresh (1024 by default). ip6_dst_gc() decays ip6_rt_gc_expire by gc_elasticity: the smaller gc_elasticity is, the smaller ip6_rt_gc_expire gets, i.e. the more aggressive the gc. ip6_dst_gc() reads ip6_rt_gc_expire into a u32, but gc_elasticity has no bounds at all, so writing >= 32 shifts a 32-bit value by >= 32 bits, and a negative value gives a negative shift count. Both are UB: val = atomic_read(&net->ipv6.ip6_rt_gc_expire); atomic_set(&net->ipv6.ip6_rt_gc_expire, val - (val >> rt_elasticity)); UBSAN: shift-out-of-bounds in net/ipv6/route.c:3369:53 shift exponent 32 is too large for 32-bit type 'unsigned int' CPU: 2 UID: 0 PID: 615 Comm: ping6 Not tainted 7.3.0-rc7+ #364 PREEMPT Call Trace: dump_stack_lvl+0xc1/0xf0 dump_stack+0x10/0x20 __ubsan_handle_shift_out_of_bounds+0x1d1/0x390 ip6_dst_gc.cold+0x15/0x1a dst_alloc+0x4e0/0x620 ip6_rt_cache_alloc+0x189/0x7e0 __ip6_rt_update_pmtu+0x68e/0x1160 ip6_update_pmtu+0x30c/0x3a0 icmpv6_err+0x1fa/0x330 icmpv6_notify+0x303/0x770 icmpv6_rcv+0xc12/0x1670 ip6_protocol_deliver_rcu+0xb4a/0x1520 ip6_input_finish+0x19c/0x400 ip6_input+0xd4/0x240 ipv6_rcv+0x1fd/0x330 __netif_receive_skb_one_core+0x118/0x1c0 __netif_receive_skb+0x21/0x150 process_backlog+0x37e/0x1200 __napi_poll.constprop.0+0xa5/0x5e0 net_rx_action+0x4c0/0xf70 handle_softirqs+0x204/0x9b0 __do_softirq+0x10/0x18 Make it a u8 and reject writes outside 0..31 with proc_dou8vec_minmax(). Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Reviewed-by: Fernando Fernandez Mancera Reviewed-by: Kuniyuki Iwashima Signed-off-by: Jiayuan Chen --- v2: switched to u8 and proc_dou8vec_minmax(), as suggested by Kuniyuki Iwashima. --- include/net/netns/ipv6.h | 2 +- net/ipv6/route.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/net/netns/ipv6.h b/include/net/netns/ipv6.h index ffda15318be59..aefce37c04a4f 100644 --- a/include/net/netns/ipv6.h +++ b/include/net/netns/ipv6.h @@ -25,11 +25,11 @@ struct netns_sysctl_ipv6 { int ip6_rt_gc_min_interval; int ip6_rt_gc_timeout; int ip6_rt_gc_interval; - int ip6_rt_gc_elasticity; int ip6_rt_mtu_expires; int ip6_rt_min_advmss; u32 multipath_hash_fields; u8 multipath_hash_policy; + u8 ip6_rt_gc_elasticity; __cacheline_group_begin(sysctl_ipv6_flowlabel); u8 flowlabel_consistency; diff --git a/net/ipv6/route.c b/net/ipv6/route.c index 94e5ef1c4f83e..f7d56dd3926bb 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -6579,6 +6579,8 @@ static int ipv6_sysctl_rtcache_flush(const struct ctl_table *ctl, int write, return 0; } +static unsigned int ip6_rt_gc_elasticity_max = 31; + static const struct ctl_table ipv6_route_table_template[] = { { .procname = "max_size", @@ -6625,9 +6627,10 @@ static const struct ctl_table ipv6_route_table_template[] = { { .procname = "gc_elasticity", .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, - .maxlen = sizeof(int), + .maxlen = sizeof(u8), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = proc_dou8vec_minmax, + .extra2 = &ip6_rt_gc_elasticity_max, }, { .procname = "mtu_expires", -- 2.43.0