netfilter: nft_reject: prevent unbounded recursion in output hooks
Subsystems (3)
PATCH v3 1/1 netfilter: nft_reject: prevent unbounded recursion in output hooks
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Note that the dev_queue_xmit() path used when CONFIG_BRIDGE_NETFILTER is enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In addition nf_send_unreach() and nf_send_unreach6() are safe as the ICMP transmit functions are protected against recursion by the icmp_xmit_lock() and icmpv6_xmit_lock() guards.
---
v3: fixed build robot errors due to the struct member being guarded by CONFIG_NF_DUP_NETDEV instead of CONFIG_NETFILTER and renamed the struct member too.
--- include/linux/netdevice_xmit.h | 4 +-- include/net/netfilter/nf_dup_netdev.h | 35 ---------------------- include/net/netfilter/nf_recursion.h | 42 +++++++++++++++++++++++++++ net/ipv4/netfilter/nf_reject_ipv4.c | 12 ++++++++ net/ipv6/netfilter/nf_reject_ipv6.c | 13 +++++++++ net/netfilter/nf_dup_netdev.c | 1 + net/netfilter/nft_fwd_netdev.c | 1 + 7 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 include/net/netfilter/nf_recursion.h
include/linux/netdevice_xmit.h +2 -2
@@ -18,8 +18,8 @@ struct netdev_xmit {u8 sched_mirred_nest;struct net_device *sched_mirred_dev[MIRRED_NEST_LIMIT ];#endif -#if IS_ENABLED (CONFIG_NF_DUP_NETDEV ) -u8 nf_dup_skb_recursion; +#if IS_ENABLED (CONFIG_NETFILTER ) +u8 nf_xmit_skb_recursion;#endif };
include/net/netfilter/nf_dup_netdev.h +0 -35
@@ -9,41 +9,6 @@void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif);void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif); -#define NF_RECURSION_LIMIT 2 - -#ifndef CONFIG_PREEMPT_RT -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (__this_cpu_read (softnet_data.xmit.nf_dup_skb_recursion) > -NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ -__this_cpu_inc (softnet_data.xmit.nf_dup_skb_recursion); -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ -__this_cpu_dec (softnet_data.xmit.nf_dup_skb_recursion); -} -#else -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (current->net_xmit.nf_dup_skb_recursion >NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ - current->net_xmit.nf_dup_skb_recursion++; -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ - current->net_xmit.nf_dup_skb_recursion--; -} -#endif -struct nft_offload_ctx ;struct nft_flow_rule ;
include/net/netfilter/nf_recursion.h new file +42 -0
42 lines not on this page · raw message
net/ipv4/netfilter/nf_reject_ipv4.c +12 -0
@@ -10,6 +10,7 @@#include <net/dst.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv4/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv4.h> #include <linux/netfilter_bridge.h>
@@ -336,7 +337,18 @@ void nf_send_reset(struct net *net, struct sock *sk, struct sk_buff *oldskb,dev_queue_xmit (nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); + goto free_nskb; + } + +nf_dev_xmit_recursion_inc ();ip_local_out (net, nskb->sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + }return ;
net/ipv6/netfilter/nf_reject_ipv6.c +13 -0
@@ -10,6 +10,7 @@#include <net/ip6_checksum.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv6/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv6.h> #include <linux/netfilter_bridge.h>
@@ -409,7 +410,19 @@ void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb, dev_queue_xmit(nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); +kfree_skb (nskb); +return ; + } + +nf_dev_xmit_recursion_inc ();ip6_local_out (net, sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + } }EXPORT_SYMBOL_GPL (nf_send_reset6);
net/netfilter/nf_dup_netdev.c +1 -0
@@ -12,6 +12,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev,enum nf_dev_hooks hook)
net/netfilter/nft_fwd_netdev.c +1 -0
@@ -14,6 +14,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> #include <net/neighbour.h> #include <net/ip.h>
Signature · 1 line
2.55.0
Quoted 19 lines · Fernando Fernandez Mancera
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Note that the
dev_queue_xmit()path used whenCONFIG_BRIDGE_NETFILTERis enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In additionnf_send_unreach()andnf_send_unreach6()are safe as the ICMP transmit functions are protected against recursion by theicmp_xmit_lock()andicmpv6_xmit_lock()guards.
Quoted 8 lines
Reported-by Yu Junzhe <[email protected]>Fixes bee11dc78fc8 ("netfilter: nft_reject: support for IPv6 and TCP reset")Signed-off-by Fernando Fernandez Mancera <[email protected]>---
v3: fixed build robot errors due to the struct member being guarded byCONFIG_NF_DUP_NETDEVinstead ofCONFIG_NETFILTERand renamed the struct member too.
Is there something missing for this patch? It would be nice if we can get this in the next nf.git since it was discover by a real user (or it looks like so).
Quoted 207 lines
--- include/linux/netdevice_xmit.h | 4 +-- include/net/netfilter/nf_dup_netdev.h | 35 ---------------------- include/net/netfilter/nf_recursion.h | 42 +++++++++++++++++++++++++++ net/ipv4/netfilter/nf_reject_ipv4.c | 12 ++++++++ net/ipv6/netfilter/nf_reject_ipv6.c | 13 +++++++++ net/netfilter/nf_dup_netdev.c | 1 + net/netfilter/nft_fwd_netdev.c | 1 + 7 files changed, 71 insertions(+), 37 deletions(-)create mode 100644
include/net/netfilter/nf_recursion.hinclude/linux/netdevice_xmit.h +2
-2@@ -18,8 +18,8 @@ struct netdev_xmit {u8 sched_mirred_nest;struct net_device *sched_mirred_dev[MIRRED_NEST_LIMIT ];#endif -#if IS_ENABLED (CONFIG_NF_DUP_NETDEV ) -u8 nf_dup_skb_recursion; +#if IS_ENABLED (CONFIG_NETFILTER ) +u8 nf_xmit_skb_recursion;#endif };include/net/netfilter/nf_dup_netdev.h +0
-35@@ -9,41 +9,6 @@void nf_dup_netdev_egress (const struct nft_pktinfo *pkt,int oif);void nf_fwd_netdev_egress (const struct nft_pktinfo *pkt,int oif); -#define NF_RECURSION_LIMIT 2 - -#ifndef CONFIG_PREEMPT_RT -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (__this_cpu_read (softnet_data.xmit.nf_dup_skb_recursion) > -NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ -__this_cpu_inc (softnet_data.xmit.nf_dup_skb_recursion); -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ -__this_cpu_dec (softnet_data.xmit.nf_dup_skb_recursion); -} -#else -static inline bool nf_dev_xmit_recursion (void ) -{ -return unlikely (current->net_xmit.nf_dup_skb_recursion >NF_RECURSION_LIMIT ); -} - -static inline void nf_dev_xmit_recursion_inc (void ) -{ - current->net_xmit.nf_dup_skb_recursion++; -} - -static inline void nf_dev_xmit_recursion_dec (void ) -{ - current->net_xmit.nf_dup_skb_recursion--; -} -#endif -struct nft_offload_ctx ;struct nft_flow_rule ;include/net/netfilter/nf_recursion.h new file +42
-0@@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _NF_RECURSION_H_ +#define _NF_RECURSION_H_ + +#include <linux/netdevice.h> + +#define NF_RECURSION_LIMIT 2 + +#ifndef CONFIG_PREEMPT_RT +static inline bool nf_dev_xmit_recursion (void ) +{ +return unlikely (__this_cpu_read (softnet_data.xmit.nf_xmit_skb_recursion) > +NF_RECURSION_LIMIT ); +} + +static inline void nf_dev_xmit_recursion_inc (void ) +{ +__this_cpu_inc (softnet_data.xmit.nf_xmit_skb_recursion); +} + +static inline void nf_dev_xmit_recursion_dec (void ) +{ +__this_cpu_dec (softnet_data.xmit.nf_xmit_skb_recursion); +} +#else +static inline bool nf_dev_xmit_recursion (void ) +{ +return unlikely (current->net_xmit.nf_xmit_skb_recursion >NF_RECURSION_LIMIT ); +} + +static inline void nf_dev_xmit_recursion_inc (void ) +{ + current->net_xmit.nf_xmit_skb_recursion++; +} + +static inline void nf_dev_xmit_recursion_dec (void ) +{ + current->net_xmit.nf_xmit_skb_recursion--; +} +#endif + +#endif /* _NF_RECURSION_H_ */ net/ipv4/netfilter/nf_reject_ipv4.c +12
-0@@ -10,6 +10,7 @@#include <net/dst.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv4/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv4.h> #include <linux/netfilter_bridge.h> @@ -336,7 +337,18 @@ void nf_send_reset(struct net *net, struct sock *sk, struct sk_buff *oldskb,dev_queue_xmit (nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); + goto free_nskb; + } + +nf_dev_xmit_recursion_inc ();ip_local_out (net, nskb->sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + }return ;net/ipv6/netfilter/nf_reject_ipv6.c +13
-0@@ -10,6 +10,7 @@#include <net/ip6_checksum.h> #include <net/dst_metadata.h> #include <net/netfilter/ipv6/nf_reject.h> +#include <net/netfilter/nf_recursion.h> #include <linux/netfilter_ipv6.h> #include <linux/netfilter_bridge.h> @@ -409,7 +410,19 @@ void nf_send_reset6(struct net *net, struct sock *sk, struct sk_buff *oldskb, dev_queue_xmit(nskb); } else#endif + { +local_bh_disable (); +if (nf_dev_xmit_recursion ()) { +local_bh_enable (); +kfree_skb (nskb); +return ; + } + +nf_dev_xmit_recursion_inc ();ip6_local_out (net, sk, nskb); +nf_dev_xmit_recursion_dec (); +local_bh_enable (); + } }EXPORT_SYMBOL_GPL (nf_send_reset6);net/netfilter/nf_dup_netdev.c +1
-0@@ -12,6 +12,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> static void nf_do_netdev_egress (struct sk_buff *skb,struct net_device *dev,enum nf_dev_hooks hook)net/netfilter/nft_fwd_netdev.c +1
-0@@ -14,6 +14,7 @@#include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_offload.h> #include <net/netfilter/nf_dup_netdev.h> +#include <net/netfilter/nf_recursion.h> #include <net/neighbour.h> #include <net/ip.h>
Pablo Neira Ayuso
maintainerQuoted 29 lines · Fernando Fernandez Mancera
Quoted 19 lines · Fernando Fernandez Mancera
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Note that the
dev_queue_xmit()path used whenCONFIG_BRIDGE_NETFILTERis enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In additionnf_send_unreach()andnf_send_unreach6()are safe as the ICMP transmit functions are protected against recursion by theicmp_xmit_lock()andicmpv6_xmit_lock()guards.Quoted 8 lines
Reported-by Yu Junzhe <[email protected]>Fixes bee11dc78fc8 ("netfilter: nft_reject: support for IPv6 and TCP reset")Signed-off-by Fernando Fernandez Mancera <[email protected]>---
v3: fixed build robot errors due to the struct member being guarded byCONFIG_NF_DUP_NETDEVinstead ofCONFIG_NETFILTERand renamed the struct member too.
Quoted 3 lines
Is there something missing for this patch? It would be nice if we can get this in the next nf.git since it was discover by a real user (or it looks like so).
I will include this in the next nf.git batch.
Pablo Neira Ayuso
maintainerQuoted 11 lines · Fernando Fernandez Mancera
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Quoted 7 lines
Note that the
dev_queue_xmit()path used whenCONFIG_BRIDGE_NETFILTERis enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In additionnf_send_unreach()andnf_send_unreach6()are safe as the ICMP transmit functions are protected against recursion by theicmp_xmit_lock()andicmpv6_xmit_lock()guards.
Unrelated to this patch, but should ip_local_out() call in synproxy_send_tcp() be tracked by the recursion counter too?
Quoted 20 lines · Pablo Neira Ayuso
Quoted 11 lines · Fernando Fernandez Mancera
nftables payload-mangling rules can rewrite packet contents (such as TCP flags) before they are evaluated by reject targets. This defeats the content-based loop guards, allowing unbounded hook recursion that leads to kernel stack exhaustion and panics.
Fix this by extracting the per-CPU xmit recursion limit previously used by the netdev duplication/forwarding paths into a shared header, and apply it to the inet and inet6 reject transmit functions. It disables BH while transmitting the skb to address a possible migration to different CPU leading to imbalanced decrementation of the recursion counters.
Quoted 7 lines
Note that the
dev_queue_xmit()path used whenCONFIG_BRIDGE_NETFILTERis enabled does not require this recursion guard. In that scenario, the generated reset packet is transmitted directly at Layer 2. Because the packet egresses from a different hook, it cannot synchronously loop. In additionnf_send_unreach()andnf_send_unreach6()are safe as the ICMP transmit functions are protected against recursion by theicmp_xmit_lock()andicmpv6_xmit_lock()guards.
Quoted 2 lines
Unrelated to this patch, but should
ip_local_out()call insynproxy_send_tcp()be tracked by the recursion counter too?
Hi Pablo, you are right. I have reproduced the same on synproxy. I am sending a patch to nf tree.
Thanks for pointing it out Pablo!