net: add xmit recursion limit to tunnel xmit functions
Subsystems (3)
PATCH v3 1/1 net: add xmit recursion limit to tunnel xmit functions
From: Weiming Shi <[email protected]>
Tunnel xmit functions (iptunnel_xmit, ip6tunnel_xmit) lack their own recursion limit. When a bond device in broadcast mode has GRE tap interfaces as slaves, and those GRE tunnels route back through the bond, multicast/broadcast traffic triggers infinite recursion between bond_xmit_broadcast() and ip_tunnel_xmit()/ip6_tnl_xmit(), causing kernel stack overflow.
The existing XMIT_RECURSION_LIMIT (8) in the no-qdisc path is not sufficient because tunnel recursion involves route lookups and full IP output, consuming much more stack per level. Use a lower limit of 4 (IP_TUNNEL_RECURSION_LIMIT) to prevent overflow.
Add recursion detection using dev_xmit_recursion helpers directly in iptunnel_xmit() and ip6tunnel_xmit() to cover all IPv4/IPv6 tunnel paths including UDP encapsulated tunnels (VXLAN, Geneve, etc.).
Move dev_xmit_recursion helpers from net/core/dev.h to public header include/linux/netdevice.h so they can be used by tunnel code.
BUG: KASAN: stack-out-of-bounds in blake2s.constprop.0+0xe7/0x160 Write of size 32 at addr ffff88810033fed0 by task kworker/0:1/11 Workqueue: mld mld_ifc_work Call Trace: <TASK> __build_flow_key.constprop.0 (net/ipv4/route.c:515) ip_rt_update_pmtu (net/ipv4/route.c:1073) iptunnel_xmit (net/ipv4/ip_tunnel_core.c:84) ip_tunnel_xmit (net/ipv4/ip_tunnel.c:847) gre_tap_xmit (net/ipv4/ip_gre.c:779) dev_hard_start_xmit (net/core/dev.c:3887) sch_direct_xmit (net/sched/sch_generic.c:347) __dev_queue_xmit (net/core/dev.c:4802) bond_dev_queue_xmit (drivers/net/bonding/bond_main.c:312) bond_xmit_broadcast (drivers/net/bonding/bond_main.c:5279) bond_start_xmit (drivers/net/bonding/bond_main.c:5530) dev_hard_start_xmit (net/core/dev.c:3887) __dev_queue_xmit (net/core/dev.c:4841) ip_finish_output2 (net/ipv4/ip_output.c:237) ip_output (net/ipv4/ip_output.c:438) iptunnel_xmit (net/ipv4/ip_tunnel_core.c:86) gre_tap_xmit (net/ipv4/ip_gre.c:779) dev_hard_start_xmit (net/core/dev.c:3887) sch_direct_xmit (net/sched/sch_generic.c:347) __dev_queue_xmit (net/core/dev.c:4802) bond_dev_queue_xmit (drivers/net/bonding/bond_main.c:312) bond_xmit_broadcast (drivers/net/bonding/bond_main.c:5279) bond_start_xmit (drivers/net/bonding/bond_main.c:5530) dev_hard_start_xmit (net/core/dev.c:3887) __dev_queue_xmit (net/core/dev.c:4841) ip_finish_output2 (net/ipv4/ip_output.c:237) ip_output (net/ipv4/ip_output.c:438) iptunnel_xmit (net/ipv4/ip_tunnel_core.c:86) ip_tunnel_xmit (net/ipv4/ip_tunnel.c:847) gre_tap_xmit (net/ipv4/ip_gre.c:779) dev_hard_start_xmit (net/core/dev.c:3887) sch_direct_xmit (net/sched/sch_generic.c:347) __dev_queue_xmit (net/core/dev.c:4802) bond_dev_queue_xmit (drivers/net/bonding/bond_main.c:312) bond_xmit_broadcast (drivers/net/bonding/bond_main.c:5279) bond_start_xmit (drivers/net/bonding/bond_main.c:5530) dev_hard_start_xmit (net/core/dev.c:3887) __dev_queue_xmit (net/core/dev.c:4841) mld_sendpack mld_ifc_work process_one_work worker_thread </TASK>
---
v3:
- Move recursion check from ip_tunnel_xmit()/ip6_tnl_xmit() to iptunnel_xmit()/ip6tunnel_xmit() to also cover UDP encapsulated tunnels (VXLAN, Geneve, etc.) (Paolo Abeni)
v2:
- Use IP_TUNNEL_RECURSION_LIMIT (4) instead of MAX_NEST_DEV (8) because tunnel recursion consumes more stack per level (Eric Dumazet)
- Add recursion detection with dev_xmit_recursion helpers
--- include/linux/netdevice.h | 32 ++++++++++++++++++++++++++++++++ include/net/ip6_tunnel.h | 12 ++++++++++++ include/net/ip_tunnels.h | 7 +++++++ net/core/dev.h | 35 ----------------------------------- net/ipv4/ip_tunnel_core.c | 13 +++++++++++++ 5 files changed, 64 insertions(+), 35 deletions(-)
include/linux/netdevice.h +32 -0
@@ -3576,17 +3576,49 @@ struct page_pool_bh { }; DECLARE_PER_CPU(struct page_pool_bh, system_page_pool); +#define XMIT_RECURSION_LIMIT 8 +#ifndef CONFIG_PREEMPT_RT static inline int dev_recursion_level (void ) {return this_cpu_read (softnet_data.xmit.recursion); } + +static inline bool dev_xmit_recursion (void ) +{ +return unlikely (__this_cpu_read (softnet_data.xmit.recursion) > +XMIT_RECURSION_LIMIT ); +} + +static inline void dev_xmit_recursion_inc (void ) +{ +__this_cpu_inc (softnet_data.xmit.recursion); +} + +static inline void dev_xmit_recursion_dec (void ) +{ +__this_cpu_dec (softnet_data.xmit.recursion); +}#else static inline int dev_recursion_level (void ) {return current->net_xmit.recursion; } +static inline bool dev_xmit_recursion (void ) +{ +return unlikely (current->net_xmit.recursion >XMIT_RECURSION_LIMIT ); +} + +static inline void dev_xmit_recursion_inc (void ) +{ + current->net_xmit.recursion++; +} + +static inline void dev_xmit_recursion_dec (void ) +{ + current->net_xmit.recursion--; +}#endif void __netif_schedule (struct Qdisc *q);
include/net/ip6_tunnel.h +12 -0
@@ -156,6 +156,16 @@ static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb, {int pkt_len, err; +if (dev_recursion_level () >IP_TUNNEL_RECURSION_LIMIT ) { +net_crit_ratelimited ("Dead loop on virtual device %s, fix it urgently!\n" , + dev->name); +DEV_STATS_INC (dev, tx_errors); +kfree_skb (skb); +return ; + } + +dev_xmit_recursion_inc (); +memset (skb->cb,0 ,sizeof (struct inet6_skb_parm ));IP6CB (skb)->flags = ip6cb_flags; pkt_len = skb->len -skb_inner_network_offset (skb);
@@ -166,6 +176,8 @@ static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb, pkt_len =-1 ;iptunnel_xmit_stats (dev, pkt_len); } + +dev_xmit_recursion_dec (); }#endif #endif
include/net/ip_tunnels.h +7 -0
@@ -27,6 +27,13 @@#include <net/ip6_route.h> #endif +/* Recursion limit for tunnel xmit to detect routing loops. +* Unlike XMIT_RECURSION_LIMIT (8) used in the no-qdisc path, tunnel +* recursion involves route lookups and full IP output, consuming much +* more stack per level, so a lower limit is needed. +*/ +#define IP_TUNNEL_RECURSION_LIMIT 4 +/* Keep error state on tunnel for 30 sec */ #define IPTUNNEL_ERR_TIMEO (30*HZ)
net/core/dev.h +0 -35
@@ -366,41 +366,6 @@ static inline void napi_assert_will_not_race(const struct napi_struct *napi)void kick_defer_list_purge (unsigned int cpu); -#define XMIT_RECURSION_LIMIT 8 - -#ifndef CONFIG_PREEMPT_RT -static inline bool dev_xmit_recursion (void ) -{ -return unlikely (__this_cpu_read (softnet_data.xmit.recursion) > -XMIT_RECURSION_LIMIT ); -} - -static inline void dev_xmit_recursion_inc (void ) -{ -__this_cpu_inc (softnet_data.xmit.recursion); -} - -static inline void dev_xmit_recursion_dec (void ) -{ -__this_cpu_dec (softnet_data.xmit.recursion); -} -#else -static inline bool dev_xmit_recursion (void ) -{ -return unlikely (current->net_xmit.recursion >XMIT_RECURSION_LIMIT ); -} - -static inline void dev_xmit_recursion_inc (void ) -{ - current->net_xmit.recursion++; -} - -static inline void dev_xmit_recursion_dec (void ) -{ - current->net_xmit.recursion--; -} -#endif -int dev_set_hwtstamp_phylib (struct net_device *dev,struct kernel_hwtstamp_config *cfg,struct netlink_ext_ack *extack);
net/ipv4/ip_tunnel_core.c +13 -0
@@ -58,6 +58,17 @@ void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,struct iphdr *iph;int err; +if (dev_recursion_level () >IP_TUNNEL_RECURSION_LIMIT ) { +net_crit_ratelimited ("Dead loop on virtual device %s, fix it urgently!\n" , + dev->name); +DEV_STATS_INC (dev, tx_errors); +ip_rt_put (rt); +kfree_skb (skb); +return ; + } + +dev_xmit_recursion_inc (); +skb_scrub_packet (skb, xnet);skb_clear_hash_if_not_l4 (skb);
@@ -88,6 +99,8 @@ void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, pkt_len =0 ;iptunnel_xmit_stats (dev, pkt_len); } + +dev_xmit_recursion_dec (); }EXPORT_SYMBOL_GPL (iptunnel_xmit);
Signature · 1 line
2.43.0