netfilter: handle race w. module removal and nfqueue
Subsystems (2)
Florian Westphal
maintainerv2 cover netfilter: handle race w. module removal and nfqueue
There is a long-standing race that occurs with module removal (such as helpers) nfqueue, and unconfirmed (not in hash table) conntracks.
The main issue is that
a). unconfirmed conntracks can't safely be mangled from other cpu (we assume
exclusive access to grow/alter the extension area) and
b). nfqueued skbs leave RCU protection
This series address this by making the queue event similar to a confirm event:
Just as we do not commit 'dying' conntracks to the main table, refuse to queue dying and unconfirmed conntracks to userspace.
Combined with a 'drop queued skbs' when a module exit path calls the ct_iterate_destroy function this closes the hole, see patch #4 for details.
The only change since v1 is a build error that occured in patch 4 when nfqueue is enabled but conntrack is not, as reported by kbuild test robot.
Florian Westphal
maintainerPATCH v2 1/4 netfilter: expect: add and use nf_ct_expect_iterate helpers
We have several spots that open-code a expect walk, add a helper that is similar to nf_ct_iterate_destroy/nf_ct_iterate_cleanup.
---
No changes since v1
include/net/netfilter/nf_conntrack_expect.h | 5 +++ net/netfilter/nf_conntrack_expect.c | 54 +++++++++++++++++++++++++ net/netfilter/nf_conntrack_helper.c | 34 +++++++--------- net/netfilter/nf_conntrack_netlink.c | 63 ++++++++++------------------- 4 files changed, 95 insertions(+), 61 deletions(-)
include/net/netfilter/nf_conntrack_expect.h +5 -0
@@ -107,6 +107,11 @@ void nf_ct_remove_expectations(struct nf_conn *ct);void nf_ct_unexpect_related (struct nf_conntrack_expect *exp);bool nf_ct_remove_expect (struct nf_conntrack_expect *exp); +void nf_ct_expect_iterate_destroy (bool (*iter)(struct nf_conntrack_expect *e,void *data),void *data); +void nf_ct_expect_iterate_net (struct net *net, +bool (*iter)(struct nf_conntrack_expect *e,void *data), +void *data,u32 portid,int report); +/* Allocate space for an expectation: this is mandatory before calling nf_ct_expect_related. You will have to call put afterwards. */ struct nf_conntrack_expect *nf_ct_expect_alloc (struct nf_conn *me);
net/netfilter/nf_conntrack_expect.c +54 -0
60 lines not on this page · raw message
net/netfilter/nf_conntrack_helper.c +15 -19
@@ -437,12 +437,22 @@ int nf_conntrack_helper_register(struct nf_conntrack_helper *me) }EXPORT_SYMBOL_GPL (nf_conntrack_helper_register); -void nf_conntrack_helper_unregister (struct nf_conntrack_helper *me) +static bool expect_iter_me (struct nf_conntrack_expect *exp,void *data) { -struct nf_conntrack_expect *exp; -const struct hlist_node *next; -unsigned int i; +struct nf_conn_help *help =nfct_help (exp->master); +const struct nf_conntrack_helper *me = data; +const struct nf_conntrack_helper *this; + +if (exp->helper == me) +return true; + this =rcu_dereference_protected (help->helper, +lockdep_is_held (&nf_conntrack_expect_lock)); +return this == me; +} + +void nf_conntrack_helper_unregister (struct nf_conntrack_helper *me) +{mutex_lock (&nf_ct_helper_mutex);hlist_del_rcu (&me->hnode); nf_ct_helper_count--;
@@ -453,21 +463,7 @@ void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)*/ synchronize_rcu (); -/* Get rid of expectations */ -spin_lock_bh (&nf_conntrack_expect_lock); -for (i =0 ; i < nf_ct_expect_hsize; i++) { -hlist_for_each_entry_safe (exp, next, - &nf_ct_expect_hash[i], hnode) { -struct nf_conn_help *help =nfct_help (exp->master); -if ((rcu_dereference_protected ( - help->helper, -lockdep_is_held (&nf_conntrack_expect_lock) - ) == me || exp->helper == me)) -nf_ct_remove_expect (exp); - } - } -spin_unlock_bh (&nf_conntrack_expect_lock); - +nf_ct_expect_iterate_destroy (expect_iter_me,NULL );nf_ct_iterate_destroy (unhelp, me); }EXPORT_SYMBOL_GPL (nf_conntrack_helper_unregister);
net/netfilter/nf_conntrack_netlink.c +21 -42
86 lines not on this page · raw message
Signature · 1 line
2.13.0
Florian Westphal
maintainerPATCH v2 2/4 netfilter: add and use nf_ct_unconfirmed_destroy
In reply to Florian WestphalThis also removes __nf_ct_unconfirmed_destroy() call from nf_ct_iterate_cleanup_net, so that function can be used only when missing conntracks from unconfirmed list isn't a problem.
---
No changes since v1
include/net/netfilter/nf_conntrack.h | 3 +++ net/netfilter/nf_conntrack_core.c | 15 +++++++++++---- net/netfilter/nfnetlink_cttimeout.c | 1 + 3 files changed, 15 insertions(+), 4 deletions(-)
include/net/netfilter/nf_conntrack.h +3 -0
@@ -224,6 +224,9 @@ extern s32 (*nf_ct_nat_offset)(const struct nf_conn *ct,enum ip_conntrack_dir dir, u32 seq); +/* Set all unconfirmed conntrack as dying */ +void nf_ct_unconfirmed_destroy (struct net *); +/* Iterate over all conntracks: if iter returns true, it's deleted. */ void nf_ct_iterate_cleanup_net(struct net *net, int (*iter)(struct nf_conn *i, void *data),
net/netfilter/nf_conntrack_core.c +11 -4
@@ -1689,6 +1689,17 @@ __nf_ct_unconfirmed_destroy(struct net *net) } } +void nf_ct_unconfirmed_destroy (struct net *net) +{ +might_sleep (); + +if (atomic_read (&net->ct.count) >0 ) { +__nf_ct_unconfirmed_destroy (net); +synchronize_net (); + } +} +EXPORT_SYMBOL_GPL (nf_ct_unconfirmed_destroy); +void nf_ct_iterate_cleanup_net (struct net *net,int (*iter)(struct nf_conn *i,void *data),void *data,u32 portid,int report)
@@ -1700,14 +1711,10 @@ void nf_ct_iterate_cleanup_net(struct net *net,if (atomic_read (&net->ct.count) ==0 )return ; -__nf_ct_unconfirmed_destroy (net); - d.iter = iter; d.data = data; d.net = net; -synchronize_net (); -nf_ct_iterate_cleanup (iter_net_only, &d, portid, report); }EXPORT_SYMBOL_GPL (nf_ct_iterate_cleanup_net);
net/netfilter/nfnetlink_cttimeout.c +1 -0
@@ -572,6 +572,7 @@ static void __net_exit cttimeout_net_exit(struct net *net) {struct ctnl_timeout *cur, *tmp; +nf_ct_unconfirmed_destroy (net);ctnl_untimeout (net,NULL );list_for_each_entry_safe (cur, tmp, &net->nfct_timeout_list, head) {
Signature · 1 line
2.13.0
Florian Westphal
maintainerPATCH v2 3/4 netfilter: conntrack: destroy functions need to free queued packets
In reply to Florian Westphalqueued skbs might be using conntrack extensions that are being removed, such as timeout. This happens for skbs that have a skb->nfct in unconfirmed state (i.e., not in hash table yet).
This is destructive, but there are only two use cases:
- module removal (rare)
- netns cleanup (most likely no conntracks exist, and if they do, they are removed anyway later on).
---
No changes since v1
net/netfilter/nf_conntrack_core.c | 4 ++++ net/netfilter/nf_queue.c | 1 + 2 files changed, 5 insertions(+)
net/netfilter/nf_conntrack_core.c +4 -0
@@ -56,6 +56,8 @@#include <net/netfilter/nf_nat_helper.h> #include <net/netns/hash.h> +#include "nf_internals.h" +#define NF_CONNTRACK_VERSION "0.5.0" int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
@@ -1695,6 +1697,7 @@ void nf_ct_unconfirmed_destroy(struct net *net)if (atomic_read (&net->ct.count) >0 ) {__nf_ct_unconfirmed_destroy (net); +nf_queue_nf_hook_drop (net);synchronize_net (); } }
@@ -1740,6 +1743,7 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)if (atomic_read (&net->ct.count) ==0 )continue ;__nf_ct_unconfirmed_destroy (net); +nf_queue_nf_hook_drop (net); }rtnl_unlock ();
net/netfilter/nf_queue.c +1 -0
@@ -109,6 +109,7 @@ unsigned int nf_queue_nf_hook_drop(struct net *net)return count; } +EXPORT_SYMBOL_GPL (nf_queue_nf_hook_drop);static int __nf_queue (struct sk_buff *skb,const struct nf_hook_state *state,struct nf_hook_entry *hook_entry,unsigned int queuenum)
Signature · 1 line
2.13.0
Florian Westphal
maintainerPATCH v2 4/4 netfilter: nfnetlink_queue: don't queue dying conntracks to userspace
In reply to Florian WestphalWhen skb is queued to userspace it leaves softirq/rcu protection. skb->nfct (via conntrack extensions such as helper) could then reference modules that no longer exist if the conntrack was not yet confirmed.
nf_ct_iterate_destroy() will set the DYING bit for unconfirmed conntracks, we therefore solve this race as follows:
1. take the queue spinlock.
2. check if the conntrack is unconfirmed and has dying bit set. In this case, we must discard skb while we're still inside rcu read-side section.
3. If nf_ct_iterate_destroy() is called right after the packet is queued to userspace, it will be removed from the queue via nf_ct_iterate_destroy -> nf_queue_nf_hook_drop.
When userspace sends the verdict (nfnetlink takes rcu read lock), there are two cases to consider:
1. nf_ct_iterate_destroy() was called while packet was out. In this case, skb will have been removed from the queue already and no reinject takes place as we won't find a matching entry for the packet id.
2. nf_ct_iterate_destroy() gets called right after verdict callback found and removed the skb from queue list.
In this case, skb->nfct is marked as dying but it is still valid. The skb will be dropped either in nf_conntrack_confirm (we don't insert DYING conntracks into hash table) or when we try to queue the skb again, but either events don't occur before the rcu read lock is dropped.
---
Changes since v1:
- guard nf_conntrack.h inclusion with IS_ENABLED() check, else this fails to build if NF_QUEUE=y CONNTRACK=n.
net/netfilter/nfnetlink_queue.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
net/netfilter/nfnetlink_queue.c +19 -0
@@ -41,6 +41,10 @@#include "../bridge/br_private.h" #endif +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +#include <net/netfilter/nf_conntrack.h> +#endif +#define NFQNL_QMAX_DEFAULT 1024/* We're using struct nlattr which has 16bit nla_len. Note that nla_len
@@ -612,6 +616,18 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,return NULL ; } +static bool nf_ct_drop_unconfirmed (const struct nf_queue_entry *entry) +{ +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +static const unsigned long flags =IPS_CONFIRMED |IPS_DYING ; +const struct nf_conn *ct = (void *)skb_nfct (entry->skb); + +if (ct && ((ct->status & flags) ==IPS_DYING )) +return true; +#endif +return false; +} +static int __nfqnl_enqueue_packet (struct net *net,struct nfqnl_instance *queue,struct nf_queue_entry *entry)
@@ -628,6 +644,9 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue, }spin_lock_bh (&queue->lock); +if (nf_ct_drop_unconfirmed (entry)) + goto err_out_free_nskb; +if (queue->queue_total >= queue->queue_maxlen) {if (queue->flags &NFQA_CFG_F_FAIL_OPEN ) { failopen =1 ;
Signature · 1 line
2.13.0
Pablo Neira Ayuso
maintainerQuoted 11 lines · Florian Westphal
There is a long-standing race that occurs with module removal (such as helpers) nfqueue, and unconfirmed (not in hash table) conntracks.
The main issue is that
a). unconfirmed conntracks can't safely be mangled from other cpu (we assume
exclusive access to grow/alter the extension area) and
b). nfqueued skbs leave RCU protectionThis series address this by making the queue event similar to a confirm event:
Quoted 8 lines
Just as we do not commit 'dying' conntracks to the main table, refuse to queue dying and unconfirmed conntracks to userspace.
Combined with a 'drop queued skbs' when a module exit path calls the
ct_iterate_destroyfunction this closes the hole, see patch #4 for details.The only change since v1 is a build error that occured in patch 4 when nfqueue is enabled but conntrack is not, as reported by kbuild test robot.
Series applied.