Skip to content

Commit 6f1a914

Browse files
winminPaolo Abeni
authored andcommitted
net: add xmit recursion limit to tunnel xmit functions
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> Fixes: 745e20f ("net: add a recursion limit in xmit path") Reported-by: Xiang Mei <xmei5@asu.edu> Signed-off-by: Weiming Shi <bestswngs@gmail.com> Link: https://patch.msgid.link/20260306160133.3852900-2-bestswngs@gmail.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 3228835 commit 6f1a914

5 files changed

Lines changed: 64 additions & 35 deletions

File tree

include/linux/netdevice.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,17 +3576,49 @@ struct page_pool_bh {
35763576
};
35773577
DECLARE_PER_CPU(struct page_pool_bh, system_page_pool);
35783578

3579+
#define XMIT_RECURSION_LIMIT 8
3580+
35793581
#ifndef CONFIG_PREEMPT_RT
35803582
static inline int dev_recursion_level(void)
35813583
{
35823584
return this_cpu_read(softnet_data.xmit.recursion);
35833585
}
3586+
3587+
static inline bool dev_xmit_recursion(void)
3588+
{
3589+
return unlikely(__this_cpu_read(softnet_data.xmit.recursion) >
3590+
XMIT_RECURSION_LIMIT);
3591+
}
3592+
3593+
static inline void dev_xmit_recursion_inc(void)
3594+
{
3595+
__this_cpu_inc(softnet_data.xmit.recursion);
3596+
}
3597+
3598+
static inline void dev_xmit_recursion_dec(void)
3599+
{
3600+
__this_cpu_dec(softnet_data.xmit.recursion);
3601+
}
35843602
#else
35853603
static inline int dev_recursion_level(void)
35863604
{
35873605
return current->net_xmit.recursion;
35883606
}
35893607

3608+
static inline bool dev_xmit_recursion(void)
3609+
{
3610+
return unlikely(current->net_xmit.recursion > XMIT_RECURSION_LIMIT);
3611+
}
3612+
3613+
static inline void dev_xmit_recursion_inc(void)
3614+
{
3615+
current->net_xmit.recursion++;
3616+
}
3617+
3618+
static inline void dev_xmit_recursion_dec(void)
3619+
{
3620+
current->net_xmit.recursion--;
3621+
}
35903622
#endif
35913623

35923624
void __netif_schedule(struct Qdisc *q);

include/net/ip6_tunnel.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
156156
{
157157
int pkt_len, err;
158158

159+
if (dev_recursion_level() > IP_TUNNEL_RECURSION_LIMIT) {
160+
net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
161+
dev->name);
162+
DEV_STATS_INC(dev, tx_errors);
163+
kfree_skb(skb);
164+
return;
165+
}
166+
167+
dev_xmit_recursion_inc();
168+
159169
memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
160170
IP6CB(skb)->flags = ip6cb_flags;
161171
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,
166176
pkt_len = -1;
167177
iptunnel_xmit_stats(dev, pkt_len);
168178
}
179+
180+
dev_xmit_recursion_dec();
169181
}
170182
#endif
171183
#endif

include/net/ip_tunnels.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
#include <net/ip6_route.h>
2828
#endif
2929

30+
/* Recursion limit for tunnel xmit to detect routing loops.
31+
* Unlike XMIT_RECURSION_LIMIT (8) used in the no-qdisc path, tunnel
32+
* recursion involves route lookups and full IP output, consuming much
33+
* more stack per level, so a lower limit is needed.
34+
*/
35+
#define IP_TUNNEL_RECURSION_LIMIT 4
36+
3037
/* Keep error state on tunnel for 30 sec */
3138
#define IPTUNNEL_ERR_TIMEO (30*HZ)
3239

net/core/dev.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -366,41 +366,6 @@ static inline void napi_assert_will_not_race(const struct napi_struct *napi)
366366

367367
void kick_defer_list_purge(unsigned int cpu);
368368

369-
#define XMIT_RECURSION_LIMIT 8
370-
371-
#ifndef CONFIG_PREEMPT_RT
372-
static inline bool dev_xmit_recursion(void)
373-
{
374-
return unlikely(__this_cpu_read(softnet_data.xmit.recursion) >
375-
XMIT_RECURSION_LIMIT);
376-
}
377-
378-
static inline void dev_xmit_recursion_inc(void)
379-
{
380-
__this_cpu_inc(softnet_data.xmit.recursion);
381-
}
382-
383-
static inline void dev_xmit_recursion_dec(void)
384-
{
385-
__this_cpu_dec(softnet_data.xmit.recursion);
386-
}
387-
#else
388-
static inline bool dev_xmit_recursion(void)
389-
{
390-
return unlikely(current->net_xmit.recursion > XMIT_RECURSION_LIMIT);
391-
}
392-
393-
static inline void dev_xmit_recursion_inc(void)
394-
{
395-
current->net_xmit.recursion++;
396-
}
397-
398-
static inline void dev_xmit_recursion_dec(void)
399-
{
400-
current->net_xmit.recursion--;
401-
}
402-
#endif
403-
404369
int dev_set_hwtstamp_phylib(struct net_device *dev,
405370
struct kernel_hwtstamp_config *cfg,
406371
struct netlink_ext_ack *extack);

net/ipv4/ip_tunnel_core.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
5858
struct iphdr *iph;
5959
int err;
6060

61+
if (dev_recursion_level() > IP_TUNNEL_RECURSION_LIMIT) {
62+
net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
63+
dev->name);
64+
DEV_STATS_INC(dev, tx_errors);
65+
ip_rt_put(rt);
66+
kfree_skb(skb);
67+
return;
68+
}
69+
70+
dev_xmit_recursion_inc();
71+
6172
skb_scrub_packet(skb, xnet);
6273

6374
skb_clear_hash_if_not_l4(skb);
@@ -88,6 +99,8 @@ void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
8899
pkt_len = 0;
89100
iptunnel_xmit_stats(dev, pkt_len);
90101
}
102+
103+
dev_xmit_recursion_dec();
91104
}
92105
EXPORT_SYMBOL_GPL(iptunnel_xmit);
93106

0 commit comments

Comments
 (0)