Netfilter fixes for net
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter fixes for net
Hi David,
The following patchset contains a rather large update with Netfilter fixes, specifically targeted to incorrect RCU usage in several spots and the userspace conntrack helper infrastructure (nfnetlink_cthelper), more specifically they are:
1) expect_class_max is incorrect set via cthelper, as in kernel semantics mandate that this represents the array of expectation classes minus 1. Patch from Liping Zhang.
2) Expectation policy updates via cthelper are currently broken for several
reasons: This code allows illegal changes in the policy such as changing the number of expeciation classes, it is leaking the updated policy and such update occurs with no RCU protection at all. Fix this by adding a new nfnl_cthelper_update_policy() that describes what is really legal on the update path.
3) Fix several memory leaks in cthelper, from Jeffy Chen.
4) synchronize_rcu() is missing in the removal path of several modules, this may lead to races since CPU may still be running on code that has just gone. Also from Liping Zhang.
5) Don't use the helper hashtable from cthelper, it is not safe to walk over those bits without the helper mutex. Fix this by introducing a new independent list for userspace helpers. From Liping Zhang.
6) nf_ct_extend_unregister() needs synchronize_rcu() to make sure no packets are walking on any conntrack extension that is gone after module removal, again from Liping.
7) nf_nat_snmp may crash if we fail to unregister the helper due to accidental leftover code, from Gao Feng.
8) Fix leak in nfnetlink_queue with secctx support, from Liping Zhang.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Thanks!
----------------------------------------------------------------
The following changes since commit db7f00b8dba6d687b6ab1f2e9309acfd214fcb4b:
tcp: tcp_get_info() should read tcp_time_stamp later (2017-03-16 21:37:13 -0700)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git HEAD
for you to fetch changes up to 77c1c03c5b8ef28e55bb0aff29b1e006037ca645:
netfilter: nfnetlink_queue: fix secctx memory leak (2017-03-29 12:20:50 +0200)
----------------------------------------------------------------
Gao Feng (1):
netfilter: nf_nat_snmp: Fix panic when snmp_trap_helper fails to register
Jeffy Chen (1):
netfilter: nfnl_cthelper: Fix memory leak
Liping Zhang (5):
netfilter: nfnl_cthelper: fix incorrect helper->expect_class_max
netfilter: invoke synchronize_rcu after set the _hook_ to NULL
netfilter: nfnl_cthelper: fix a race when walk the nf_ct_helper_hash table
netfilter: nf_ct_ext: fix possible panic after nf_ct_extend_unregister
netfilter: nfnetlink_queue: fix secctx memory leak
Pablo Neira Ayuso (1):
netfilter: nfnl_cthelper: fix runtime expectation policy updates
net/ipv4/netfilter/nf_nat_snmp_basic.c | 20 +-- net/netfilter/nf_conntrack_ecache.c | 2 + net/netfilter/nf_conntrack_extend.c | 13 +- net/netfilter/nf_conntrack_netlink.c | 1 + net/netfilter/nf_nat_core.c | 2 + net/netfilter/nfnetlink_cthelper.c | 287 +++++++++++++++++++++------------ net/netfilter/nfnetlink_cttimeout.c | 2 +- net/netfilter/nfnetlink_queue.c | 9 +- 8 files changed, 206 insertions(+), 130 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/8 netfilter: nfnl_cthelper: fix incorrect helper->expect_class_max
From: Liping Zhang <[email protected]>
The helper->expect_class_max must be set to the total number of expect_policy minus 1, since we will use the statement "if (class > helper->expect_class_max)" to validate the CTA_EXPECT_CLASS attr in ctnetlink_alloc_expect.
So for compatibility, set the helper->expect_class_max to the NFCTH_POLICY_SET_NUM attr's value minus 1.
Also: it's invalid when the NFCTH_POLICY_SET_NUM attr's value is zero.
1. this will result "expect_policy = kzalloc(0, GFP_KERNEL);";
2. we cannot set the helper->expect_class_max to a proper value.
So if nla_get_be32(tb[NFCTH_POLICY_SET_NUM]) is zero, report -EINVAL to the userspace.
--- net/netfilter/nfnetlink_cthelper.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
net/netfilter/nfnetlink_cthelper.c +11 -9
@@ -161,6 +161,7 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,int i, ret;struct nf_conntrack_expect_policy *expect_policy;struct nlattr *tb[NFCTH_POLICY_SET_MAX +1 ]; +unsigned int class_max; ret =nla_parse_nested (tb,NFCTH_POLICY_SET_MAX , attr, nfnl_cthelper_expect_policy_set);
@@ -170,19 +171,18 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,if (!tb[NFCTH_POLICY_SET_NUM ])return -EINVAL ; - helper->expect_class_max = -ntohl (nla_get_be32 (tb[NFCTH_POLICY_SET_NUM ])); - -if (helper->expect_class_max !=0 && - helper->expect_class_max >NF_CT_MAX_EXPECT_CLASSES ) + class_max =ntohl (nla_get_be32 (tb[NFCTH_POLICY_SET_NUM ])); +if (class_max ==0 ) +return -EINVAL ; +if (class_max >NF_CT_MAX_EXPECT_CLASSES )return -EOVERFLOW ; expect_policy =kzalloc (sizeof (struct nf_conntrack_expect_policy ) * - helper->expect_class_max,GFP_KERNEL ); + class_max,GFP_KERNEL );if (expect_policy ==NULL )return -ENOMEM ; -for (i=0 ; i<helper->expect_class_max; i++) { +for (i =0 ; i < class_max; i++) {if (!tb[NFCTH_POLICY_SET +i]) goto err;
@@ -191,6 +191,8 @@ nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,if (ret <0 ) goto err; } + + helper->expect_class_max = class_max -1 ; helper->expect_policy = expect_policy;return 0 ; err:
@@ -377,10 +379,10 @@ nfnl_cthelper_dump_policy(struct sk_buff *skb, goto nla_put_failure;if (nla_put_be32 (skb,NFCTH_POLICY_SET_NUM , -htonl (helper->expect_class_max))) +htonl (helper->expect_class_max +1 ))) goto nla_put_failure; -for (i=0 ; i<helper->expect_class_max; i++) { +for (i =0 ; i < helper->expect_class_max +1 ; i++) { nest_parms2 =nla_nest_start (skb, (NFCTH_POLICY_SET +i) |NLA_F_NESTED );if (nest_parms2 ==NULL )
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/8 netfilter: nfnl_cthelper: fix runtime expectation policy updates
In reply to Pablo Neira AyusoWe only allow runtime updates of expectation policies for timeout and maximum number of expectations, otherwise reject the update.
--- net/netfilter/nfnetlink_cthelper.c | 86 +++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-)
net/netfilter/nfnetlink_cthelper.c +84 -2
@@ -256,6 +256,89 @@ nfnl_cthelper_create(const struct nlattr * const tb[], }static int +nfnl_cthelper_update_policy_one (const struct nf_conntrack_expect_policy *policy, +struct nf_conntrack_expect_policy *new_policy, +const struct nlattr *attr) +{ +struct nlattr *tb[NFCTH_POLICY_MAX +1 ]; +int err; + + err =nla_parse_nested (tb,NFCTH_POLICY_MAX , attr, + nfnl_cthelper_expect_pol); +if (err <0 ) +return err; + +if (!tb[NFCTH_POLICY_NAME ] || + !tb[NFCTH_POLICY_EXPECT_MAX ] || + !tb[NFCTH_POLICY_EXPECT_TIMEOUT ]) +return -EINVAL ; + +if (nla_strcmp (tb[NFCTH_POLICY_NAME ], policy->name)) +return -EBUSY ; + + new_policy->max_expected = +ntohl (nla_get_be32 (tb[NFCTH_POLICY_EXPECT_MAX ])); + new_policy->timeout = +ntohl (nla_get_be32 (tb[NFCTH_POLICY_EXPECT_TIMEOUT ])); + +return 0 ; +} + +static int nfnl_cthelper_update_policy_all (struct nlattr *tb[], +struct nf_conntrack_helper *helper) +{ +struct nf_conntrack_expect_policy new_policy[helper->expect_class_max +1 ]; +struct nf_conntrack_expect_policy *policy; +int i, err; + +/* Check first that all policy attributes are well-formed, so we don't +* leave things in inconsistent state on errors. +*/ +for (i =0 ; i < helper->expect_class_max +1 ; i++) { + +if (!tb[NFCTH_POLICY_SET + i]) +return -EINVAL ; + + err =nfnl_cthelper_update_policy_one (&helper->expect_policy[i], + &new_policy[i], + tb[NFCTH_POLICY_SET + i]); +if (err <0 ) +return err; + } +/* Now we can safely update them. */ +for (i =0 ; i < helper->expect_class_max +1 ; i++) { + policy = (struct nf_conntrack_expect_policy *) + &helper->expect_policy[i]; + policy->max_expected = new_policy->max_expected; + policy->timeout = new_policy->timeout; + } + +return 0 ; +} + +static int nfnl_cthelper_update_policy (struct nf_conntrack_helper *helper, +const struct nlattr *attr) +{ +struct nlattr *tb[NFCTH_POLICY_SET_MAX +1 ]; +unsigned int class_max; +int err; + + err =nla_parse_nested (tb,NFCTH_POLICY_SET_MAX , attr, + nfnl_cthelper_expect_policy_set); +if (err <0 ) +return err; + +if (!tb[NFCTH_POLICY_SET_NUM ]) +return -EINVAL ; + + class_max =ntohl (nla_get_be32 (tb[NFCTH_POLICY_SET_NUM ])); +if (helper->expect_class_max +1 != class_max) +return -EBUSY ; + +return nfnl_cthelper_update_policy_all (tb, helper); +} + +static int nfnl_cthelper_update (const struct nlattr *const tb[],struct nf_conntrack_helper *helper) {
@@ -265,8 +348,7 @@ nfnl_cthelper_update(const struct nlattr * const tb[],return -EBUSY ;if (tb[NFCTH_POLICY ]) { - ret =nfnl_cthelper_parse_expect_policy (helper, - tb[NFCTH_POLICY ]); + ret =nfnl_cthelper_update_policy (helper, tb[NFCTH_POLICY ]);if (ret <0 )return ret; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/8 netfilter: nfnl_cthelper: Fix memory leak
In reply to Pablo Neira AyusoFrom: Jeffy Chen <[email protected]>
We have memory leaks of nf_conntrack_helper & expect_policy.
--- net/netfilter/nfnetlink_cthelper.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nfnetlink_cthelper.c +9 -3
@@ -216,7 +216,7 @@ nfnl_cthelper_create(const struct nlattr * const tb[], ret =nfnl_cthelper_parse_expect_policy (helper, tb[NFCTH_POLICY ]);if (ret <0 ) - goto err; + goto err1;strncpy (helper->name,nla_data (tb[NFCTH_NAME ]),NF_CT_HELPER_NAME_LEN ); helper->data_len =ntohl (nla_get_be32 (tb[NFCTH_PRIV_DATA_LEN ]));
@@ -247,10 +247,12 @@ nfnl_cthelper_create(const struct nlattr * const tb[], ret =nf_conntrack_helper_register (helper);if (ret <0 ) - goto err; + goto err2;return 0 ; -err: +err2: +kfree (helper->expect_policy); +err1:kfree (helper);return ret; }
@@ -696,6 +698,8 @@ static int nfnl_cthelper_del(struct net *net, struct sock *nfnl, found = true;nf_conntrack_helper_unregister (cur); +kfree (cur->expect_policy); +kfree (cur); } }/* Make sure we return success if we flush and there is no helpers */
@@ -759,6 +763,8 @@ static void __exit nfnl_cthelper_exit(void)continue ;nf_conntrack_helper_unregister (cur); +kfree (cur->expect_policy); +kfree (cur); } } }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/8 netfilter: invoke synchronize_rcu after set the _hook_ to NULL
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
Otherwise, another CPU may access the invalid pointer. For example:
CPU0 CPU1
- rcu_read_lock();
- pfunc = _hook_;
_hook_ = NULL; -
mod unload -
- pfunc(); // invalid, panic
- rcu_read_unlock();
So we must call synchronize_rcu() to wait the rcu reader to finish.
Also note, in nf_nat_snmp_basic_fini, synchronize_rcu() will be invoked by later nf_conntrack_helper_unregister, but I'm inclined to add a explicit synchronize_rcu after set the nf_nat_snmp_hook to NULL. Depend on such obscure assumptions is not a good idea.
Last, in nfnetlink_cttimeout, we use kfree_rcu to free the time object, so in cttimeout_exit, invoking rcu_barrier() is not necessary at all, remove it too.
--- net/ipv4/netfilter/nf_nat_snmp_basic.c | 1 + net/netfilter/nf_conntrack_ecache.c | 2 ++ net/netfilter/nf_conntrack_netlink.c | 1 + net/netfilter/nf_nat_core.c | 2 ++ net/netfilter/nfnetlink_cttimeout.c | 2 +- 5 files changed, 7 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +1 -0
@@ -1304,6 +1304,7 @@ static int __init nf_nat_snmp_basic_init(void) static void __exit nf_nat_snmp_basic_fini(void) {RCU_INIT_POINTER (nf_nat_snmp_hook,NULL ); +synchronize_rcu ();nf_conntrack_helper_unregister (&snmp_trap_helper); }
net/netfilter/nf_conntrack_ecache.c +2 -0
@@ -290,6 +290,7 @@ void nf_conntrack_unregister_notifier(struct net *net,BUG_ON (notify != new);RCU_INIT_POINTER (net->ct.nf_conntrack_event_cb,NULL );mutex_unlock (&nf_ct_ecache_mutex); +/* synchronize_rcu() is called from ctnetlink_exit. */ }EXPORT_SYMBOL_GPL (nf_conntrack_unregister_notifier);
@@ -326,6 +327,7 @@ void nf_ct_expect_unregister_notifier(struct net *net,BUG_ON (notify != new);RCU_INIT_POINTER (net->ct.nf_expect_event_cb,NULL );mutex_unlock (&nf_ct_ecache_mutex); +/* synchronize_rcu() is called from ctnetlink_exit. */ }EXPORT_SYMBOL_GPL (nf_ct_expect_unregister_notifier);
net/netfilter/nf_conntrack_netlink.c +1 -0
@@ -3442,6 +3442,7 @@ static void __exit ctnetlink_exit(void)#ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT RCU_INIT_POINTER (nfnl_ct_hook,NULL );#endif +synchronize_rcu (); }module_init (ctnetlink_init);
net/netfilter/nf_nat_core.c +2 -0
@@ -903,6 +903,8 @@ static void __exit nf_nat_cleanup(void)#ifdef CONFIG_XFRM RCU_INIT_POINTER (nf_nat_decode_session_hook,NULL );#endif +synchronize_rcu (); +for (i =0 ; i <NFPROTO_NUMPROTO ; i++)kfree (nf_nat_l4protos[i]);
net/netfilter/nfnetlink_cttimeout.c +1 -1
@@ -646,8 +646,8 @@ static void __exit cttimeout_exit(void)#ifdef CONFIG_NF_CONNTRACK_TIMEOUT RCU_INIT_POINTER (nf_ct_timeout_find_get_hook,NULL );RCU_INIT_POINTER (nf_ct_timeout_put_hook,NULL ); +synchronize_rcu ();#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ -rcu_barrier (); }module_init (cttimeout_init);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/8 netfilter: nfnl_cthelper: fix a race when walk the nf_ct_helper_hash table
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
The nf_ct_helper_hash table is protected by nf_ct_helper_mutex, while nfct_helper operation is protected by nfnl_lock(NFNL_SUBSYS_CTHELPER). So it's possible that one CPU is walking the nf_ct_helper_hash for cthelper add/get/del, another cpu is doing nf_conntrack_helpers_unregister at the same time. This is dangrous, and may cause use after free error.
Note, delete operation will flush all cthelpers added via nfnetlink, so using rcu to do protect is not easy.
Now introduce a dummy list to record all the cthelpers added via nfnetlink, then we can walk the dummy list instead of walking the nf_ct_helper_hash. Also, keep nfnl_cthelper_dump_table unchanged, it may be invoked without nfnl_lock(NFNL_SUBSYS_CTHELPER) held.
--- net/netfilter/nfnetlink_cthelper.c | 177 +++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 96 deletions(-)
net/netfilter/nfnetlink_cthelper.c +81 -96
@@ -32,6 +32,13 @@ MODULE_LICENSE("GPL");MODULE_AUTHOR ("Pablo Neira Ayuso <[email protected]>" );MODULE_DESCRIPTION ("nfnl_cthelper: User-space connection tracking helpers" ); +struct nfnl_cthelper { +struct list_head list; +struct nf_conntrack_helper helper; +}; + +static LIST_HEAD (nfnl_cthelper_list); +static int nfnl_userspace_cthelper (struct sk_buff *skb,unsigned int protoff,struct nf_conn *ct,enum ip_conntrack_info ctinfo)
@@ -205,14 +212,16 @@ nfnl_cthelper_create(const struct nlattr * const tb[], struct nf_conntrack_tuple *tuple) {struct nf_conntrack_helper *helper; +struct nfnl_cthelper *nfcth;int ret;if (!tb[NFCTH_TUPLE ] || !tb[NFCTH_POLICY ] || !tb[NFCTH_PRIV_DATA_LEN ])return -EINVAL ; - helper =kzalloc (sizeof (struct nf_conntrack_helper ),GFP_KERNEL ); -if (helper ==NULL ) + nfcth =kzalloc (sizeof (*nfcth),GFP_KERNEL ); +if (nfcth ==NULL )return -ENOMEM ; + helper = &nfcth->helper; ret =nfnl_cthelper_parse_expect_policy (helper, tb[NFCTH_POLICY ]);if (ret <0 )
@@ -249,11 +258,12 @@ nfnl_cthelper_create(const struct nlattr * const tb[],if (ret <0 ) goto err2; +list_add_tail (&nfcth->list, &nfnl_cthelper_list);return 0 ; err2:kfree (helper->expect_policy); err1: -kfree (helper); +kfree (nfcth);return ret; }
@@ -379,7 +389,8 @@ static int nfnl_cthelper_new(struct net *net, struct sock *nfnl,const char *helper_name;struct nf_conntrack_helper *cur, *helper =NULL ;struct nf_conntrack_tuple tuple; -int ret =0 , i; +struct nfnl_cthelper *nlcth; +int ret =0 ;if (!tb[NFCTH_NAME ] || !tb[NFCTH_TUPLE ])return -EINVAL ;
@@ -390,31 +401,22 @@ static int nfnl_cthelper_new(struct net *net, struct sock *nfnl,if (ret <0 )return ret; -rcu_read_lock (); -for (i =0 ; i < nf_ct_helper_hsize && !helper; i++) { -hlist_for_each_entry_rcu (cur, &nf_ct_helper_hash[i], hnode) { +list_for_each_entry (nlcth, &nfnl_cthelper_list, list) { + cur = &nlcth->helper; -/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) -continue ; +if (strncmp (cur->name, helper_name,NF_CT_HELPER_NAME_LEN )) +continue ; -if (strncmp (cur->name, helper_name, -NF_CT_HELPER_NAME_LEN ) !=0 ) -continue ; +if ((tuple.src.l3num != cur->tuple.src.l3num || + tuple.dst.protonum != cur->tuple.dst.protonum)) +continue ; -if ((tuple.src.l3num != cur->tuple.src.l3num || - tuple.dst.protonum != cur->tuple.dst.protonum)) -continue ; +if (nlh->nlmsg_flags &NLM_F_EXCL ) +return -EEXIST ; -if (nlh->nlmsg_flags &NLM_F_EXCL ) { - ret = -EEXIST ; - goto err; - } - helper = cur; -break ; - } + helper = cur; +break ; } -rcu_read_unlock ();if (helper ==NULL ) ret =nfnl_cthelper_create (tb, &tuple);
@@ -422,9 +424,6 @@ static int nfnl_cthelper_new(struct net *net, struct sock *nfnl, ret =nfnl_cthelper_update (tb, helper);return ret; -err: -rcu_read_unlock (); -return ret; } static int
@@ -588,11 +587,12 @@ static int nfnl_cthelper_get(struct net *net, struct sock *nfnl, struct sk_buff *skb, const struct nlmsghdr *nlh, const struct nlattr * const tb[]) { - int ret = -ENOENT, i; + int ret = -ENOENT;struct nf_conntrack_helper *cur;struct sk_buff *skb2;char *helper_name =NULL ;struct nf_conntrack_tuple tuple; +struct nfnl_cthelper *nlcth;bool tuple_set = false;if (nlh->nlmsg_flags &NLM_F_DUMP ) {
@@ -613,45 +613,39 @@ static int nfnl_cthelper_get(struct net *net, struct sock *nfnl, tuple_set = true; } -for (i =0 ; i < nf_ct_helper_hsize; i++) { -hlist_for_each_entry_rcu (cur, &nf_ct_helper_hash[i], hnode) { +list_for_each_entry (nlcth, &nfnl_cthelper_list, list) { + cur = &nlcth->helper; +if (helper_name && +strncmp (cur->name, helper_name,NF_CT_HELPER_NAME_LEN )) +continue ; -/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) -continue ; +if (tuple_set && + (tuple.src.l3num != cur->tuple.src.l3num || + tuple.dst.protonum != cur->tuple.dst.protonum)) +continue ; -if (helper_name &&strncmp (cur->name, helper_name, -NF_CT_HELPER_NAME_LEN ) !=0 ) { -continue ; - } -if (tuple_set && - (tuple.src.l3num != cur->tuple.src.l3num || - tuple.dst.protonum != cur->tuple.dst.protonum)) -continue ; - - skb2 =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); -if (skb2 ==NULL ) { - ret = -ENOMEM ; -break ; - } + skb2 =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL ); +if (skb2 ==NULL ) { + ret = -ENOMEM ; +break ; + } - ret =nfnl_cthelper_fill_info (skb2,NETLINK_CB (skb).portid, - nlh->nlmsg_seq, -NFNL_MSG_TYPE (nlh->nlmsg_type), -NFNL_MSG_CTHELPER_NEW , cur); -if (ret <=0 ) { -kfree_skb (skb2); -break ; - } + ret =nfnl_cthelper_fill_info (skb2,NETLINK_CB (skb).portid, + nlh->nlmsg_seq, +NFNL_MSG_TYPE (nlh->nlmsg_type), +NFNL_MSG_CTHELPER_NEW , cur); +if (ret <=0 ) { +kfree_skb (skb2); +break ; + } - ret =netlink_unicast (nfnl, skb2,NETLINK_CB (skb).portid, -MSG_DONTWAIT ); -if (ret >0 ) - ret =0 ; + ret =netlink_unicast (nfnl, skb2,NETLINK_CB (skb).portid, +MSG_DONTWAIT ); +if (ret >0 ) + ret =0 ; -/* this avoids a loop in nfnetlink. */ -return ret == -EAGAIN ? -ENOBUFS : ret; - } +/* this avoids a loop in nfnetlink. */ +return ret == -EAGAIN ? -ENOBUFS : ret; }return ret; }
@@ -662,10 +656,10 @@ static int nfnl_cthelper_del(struct net *net, struct sock *nfnl, {char *helper_name =NULL ;struct nf_conntrack_helper *cur; -struct hlist_node *tmp;struct nf_conntrack_tuple tuple;bool tuple_set = false, found = false; -int i, j =0 , ret; +struct nfnl_cthelper *nlcth, *n; +int j =0 , ret;if (tb[NFCTH_NAME ]) helper_name =nla_data (tb[NFCTH_NAME ]);
@@ -678,30 +672,27 @@ static int nfnl_cthelper_del(struct net *net, struct sock *nfnl, tuple_set = true; } -for (i =0 ; i < nf_ct_helper_hsize; i++) { -hlist_for_each_entry_safe (cur, tmp, &nf_ct_helper_hash[i], - hnode) { -/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) -continue ; +list_for_each_entry_safe (nlcth, n, &nfnl_cthelper_list, list) { + cur = &nlcth->helper; + j++; - j++; +if (helper_name && +strncmp (cur->name, helper_name,NF_CT_HELPER_NAME_LEN )) +continue ; -if (helper_name &&strncmp (cur->name, helper_name, -NF_CT_HELPER_NAME_LEN ) !=0 ) { -continue ; - } -if (tuple_set && - (tuple.src.l3num != cur->tuple.src.l3num || - tuple.dst.protonum != cur->tuple.dst.protonum)) -continue ; +if (tuple_set && + (tuple.src.l3num != cur->tuple.src.l3num || + tuple.dst.protonum != cur->tuple.dst.protonum)) +continue ; - found = true; -nf_conntrack_helper_unregister (cur); -kfree (cur->expect_policy); -kfree (cur); - } + found = true; +nf_conntrack_helper_unregister (cur); +kfree (cur->expect_policy); + +list_del (&nlcth->list); +kfree (nlcth); } +/* Make sure we return success if we flush and there is no helpers */ return (found || j ==0 ) ?0 : -ENOENT ; }
@@ -750,22 +741,16 @@ static int __init nfnl_cthelper_init(void) static void __exit nfnl_cthelper_exit(void) {struct nf_conntrack_helper *cur; -struct hlist_node *tmp; -int i; +struct nfnl_cthelper *nlcth, *n;nfnetlink_subsys_unregister (&nfnl_cthelper_subsys); -for (i=0 ; i<nf_ct_helper_hsize; i++) { -hlist_for_each_entry_safe (cur, tmp, &nf_ct_helper_hash[i], - hnode) { -/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) -continue ; +list_for_each_entry_safe (nlcth, n, &nfnl_cthelper_list, list) { + cur = &nlcth->helper; -nf_conntrack_helper_unregister (cur); -kfree (cur->expect_policy); -kfree (cur); - } +nf_conntrack_helper_unregister (cur); +kfree (cur->expect_policy); +kfree (nlcth); } }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/8 netfilter: nf_ct_ext: fix possible panic after nf_ct_extend_unregister
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
If one cpu is doing nf_ct_extend_unregister while another cpu is doing __nf_ct_ext_add_length, then we may hit BUG_ON(t == NULL). Moreover, there's no synchronize_rcu invocation after set nf_ct_ext_types[id] to NULL, so it's possible that we may access invalid pointer.
But actually, most of the ct extends are built-in, so the problem listed above will not happen. However, there are two exceptions: NF_CT_EXT_NAT and NF_CT_EXT_SYNPROXY.
For _EXT_NAT, the panic will not happen, since adding the nat extend and unregistering the nat extend are located in the same file(nf_nat_core.c), this means that after the nat module is removed, we cannot add the nat extend too.
For _EXT_SYNPROXY, synproxy extend may be added by init_conntrack, while synproxy extend unregister will be done by synproxy_core_exit. So after nf_synproxy_core.ko is removed, we may still try to add the synproxy extend, then kernel panic may happen.
I know it's very hard to reproduce this issue, but I can play a tricky game to make it happen very easily :)
Step 1. Enable SYNPROXY for tcp dport 1234 at FORWARD hook:
# iptables -I FORWARD -p tcp --dport 1234 -j SYNPROXY
Step 2. Queue the syn packet to the userspace at raw table OUTPUT hook.
Also note, in the userspace we only add a 20s' delay, then
reinject the syn packet to the kernel:
# iptables -t raw -I OUTPUT -p tcp --syn -j NFQUEUE --queue-num 1
Step 3. Using "nc 2.2.2.2 1234" to connect the server.
Step 4. Now remove the nf_synproxy_core.ko quickly:
# iptables -F FORWARD
# rmmod ipt_SYNPROXY
# rmmod nf_synproxy_core
Step 5. After 20s' delay, the syn packet is reinjected to the kernel.Now you will see the panic like this:
kernel BUG at net/netfilter/nf_conntrack_extend.c:91!
Call Trace: ? __nf_ct_ext_add_length+0x53/0x3c0 [nf_conntrack] init_conntrack+0x12b/0x600 [nf_conntrack] nf_conntrack_in+0x4cc/0x580 [nf_conntrack] ipv4_conntrack_local+0x48/0x50 [nf_conntrack_ipv4] nf_reinject+0x104/0x270 nfqnl_recv_verdict+0x3e1/0x5f9 [nfnetlink_queue] ? nfqnl_recv_verdict+0x5/0x5f9 [nfnetlink_queue] ? nla_parse+0xa0/0x100 nfnetlink_rcv_msg+0x175/0x6a9 [nfnetlink] [...]
One possible solution is to make NF_CT_EXT_SYNPROXY extend built-in, i.e. introduce nf_conntrack_synproxy.c and only do ct extend register and unregister in it, similar to nf_conntrack_timeout.c.
But having such a obscure restriction of nf_ct_extend_unregister is not a good idea, so we should invoke synchronize_rcu after set nf_ct_ext_types to NULL, and check the NULL pointer when do __nf_ct_ext_add_length. Then it will be easier if we add new ct extend in the future.
Last, we use kfree_rcu to free nf_ct_ext, so rcu_barrier() is unnecessary anymore, remove it too.
--- net/netfilter/nf_conntrack_extend.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_extend.c +10 -3
@@ -53,7 +53,11 @@ nf_ct_ext_create(struct nf_ct_ext **ext, enum nf_ct_ext_id id,rcu_read_lock (); t =rcu_dereference (nf_ct_ext_types[id]); -BUG_ON (t ==NULL ); +if (!t) { +rcu_read_unlock (); +return NULL ; + } + off =ALIGN (sizeof (struct nf_ct_ext ), t->align); len = off + t->len + var_alloc_len; alloc_size = t->alloc_size + var_alloc_len;
@@ -88,7 +92,10 @@ void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,rcu_read_lock (); t =rcu_dereference (nf_ct_ext_types[id]); -BUG_ON (t ==NULL ); +if (!t) { +rcu_read_unlock (); +return NULL ; + } newoff =ALIGN (old->len, t->align); newlen = newoff + t->len + var_alloc_len;
@@ -175,6 +182,6 @@ void nf_ct_extend_unregister(struct nf_ct_ext_type *type)RCU_INIT_POINTER (nf_ct_ext_types[type->id],NULL );update_alloc_size (type);mutex_unlock (&nf_ct_ext_type_mutex); -rcu_barrier ();/* Wait for completion of call_rcu()'s */ +synchronize_rcu (); }EXPORT_SYMBOL_GPL (nf_ct_extend_unregister);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/8 netfilter: nf_nat_snmp: Fix panic when snmp_trap_helper fails to register
In reply to Pablo Neira AyusoFrom: Gao Feng <[email protected]>
In the commit 93557f53e1fb ("netfilter: nf_conntrack: nf_conntrack snmp helper"), the snmp_helper is replaced by nf_nat_snmp_hook. So the snmp_helper is never registered. But it still tries to unregister the snmp_helper, it could cause the panic.
Now remove the useless snmp_helper and the unregister call in the error handler.
--- net/ipv4/netfilter/nf_nat_snmp_basic.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +1 -18
@@ -1260,16 +1260,6 @@ static const struct nf_conntrack_expect_policy snmp_exp_policy = { .timeout = 180, }; -static struct nf_conntrack_helper snmp_helper__read_mostly = { - .me =THIS_MODULE , - .help = help, - .expect_policy = &snmp_exp_policy, - .name ="snmp" , - .tuple.src.l3num =AF_INET , - .tuple.src.u.udp.port =cpu_to_be16 (SNMP_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , -}; -static struct nf_conntrack_helper snmp_trap_helper__read_mostly = { .me =THIS_MODULE , .help = help,
@@ -1288,17 +1278,10 @@ static struct nf_conntrack_helper snmp_trap_helper __read_mostly = {static int __init nf_nat_snmp_basic_init (void ) { -int ret =0 ; -BUG_ON (nf_nat_snmp_hook !=NULL );RCU_INIT_POINTER (nf_nat_snmp_hook, help); - ret =nf_conntrack_helper_register (&snmp_trap_helper); -if (ret <0 ) { -nf_conntrack_helper_unregister (&snmp_helper); -return ret; - } -return ret; +return nf_conntrack_helper_register (&snmp_trap_helper); } static void __exit nf_nat_snmp_basic_fini(void)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 8/8 netfilter: nfnetlink_queue: fix secctx memory leak
In reply to Pablo Neira AyusoFrom: Liping Zhang <[email protected]>
We must call security_release_secctx to free the memory returned by security_secid_to_secctx, otherwise memory may be leaked forever.
--- net/netfilter/nfnetlink_queue.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
net/netfilter/nfnetlink_queue.c +7 -2
@@ -443,7 +443,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, skb =alloc_skb (size,GFP_ATOMIC );if (!skb) {skb_tx_error (entskb); -return NULL ; + goto nlmsg_failure; } nlh = nlmsg_put(skb, 0, 0,
@@ -452,7 +452,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,if (!nlh) {skb_tx_error (entskb);kfree_skb (skb); -return NULL ; + goto nlmsg_failure; } nfmsg =nlmsg_data (nlh); nfmsg->nfgen_family = entry->state.pf;
@@ -598,12 +598,17 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, } nlh->nlmsg_len = skb->len; +if (seclen) +security_release_secctx (secdata, seclen);return skb; nla_put_failure:skb_tx_error (entskb);kfree_skb (skb);net_err_ratelimited ("nf_queue: error creating packet message\n" ); +nlmsg_failure: +if (seclen) +security_release_secctx (secdata, seclen);return NULL ; }
Signature · 1 line
2.1.4
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Wed, 29 Mar 2017 14:14:02 +0200
Quoted 6 lines
Hi David,
The following patchset contains a rather large update with Netfilter fixes, specifically targeted to incorrect RCU usage in several spots and the userspace conntrack helper infrastructure (
nfnetlink_cthelper), more specifically they are:
...
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf.git
Pulled, thanks Pablo.