alistair23-linux/include/net/netns
Pablo Neira Ayuso 0838aa7fcf netfilter: fix netns dependencies with conntrack templates
Quoting Daniel Borkmann:

"When adding connection tracking template rules to a netns, f.e. to
configure netfilter zones, the kernel will endlessly busy-loop as soon
as we try to delete the given netns in case there's at least one
template present, which is problematic i.e. if there is such bravery that
the priviledged user inside the netns is assumed untrusted.

Minimal example:

  ip netns add foo
  ip netns exec foo iptables -t raw -A PREROUTING -d 1.2.3.4 -j CT --zone 1
  ip netns del foo

What happens is that when nf_ct_iterate_cleanup() is being called from
nf_conntrack_cleanup_net_list() for a provided netns, we always end up
with a net->ct.count > 0 and thus jump back to i_see_dead_people. We
don't get a soft-lockup as we still have a schedule() point, but the
serving CPU spins on 100% from that point onwards.

Since templates are normally allocated with nf_conntrack_alloc(), we
also bump net->ct.count. The issue why they are not yet nf_ct_put() is
because the per netns .exit() handler from x_tables (which would eventually
invoke xt_CT's xt_ct_tg_destroy() that drops reference on info->ct) is
called in the dependency chain at a *later* point in time than the per
netns .exit() handler for the connection tracker.

This is clearly a chicken'n'egg problem: after the connection tracker
.exit() handler, we've teared down all the connection tracking
infrastructure already, so rightfully, xt_ct_tg_destroy() cannot be
invoked at a later point in time during the netns cleanup, as that would
lead to a use-after-free. At the same time, we cannot make x_tables depend
on the connection tracker module, so that the xt_ct_tg_destroy() would
be invoked earlier in the cleanup chain."

Daniel confirms this has to do with the order in which modules are loaded or
having compiled nf_conntrack as modules while x_tables built-in. So we have no
guarantees regarding the order in which netns callbacks are executed.

Fix this by allocating the templates through kmalloc() from the respective
SYNPROXY and CT targets, so they don't depend on the conntrack kmem cache.
Then, release then via nf_ct_tmpl_free() from destroy_conntrack(). This branch
is marked as unlikely since conntrack templates are rarely allocated and only
from the configuration plane path.

Note that templates are not kept in any list to avoid further dependencies with
nf_conntrack anymore, thus, the tmpl larval list is removed.

Reported-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Tested-by: Daniel Borkmann <daniel@iogearbox.net>
2015-07-20 14:58:19 +02:00
..
conntrack.h netfilter: fix netns dependencies with conntrack templates 2015-07-20 14:58:19 +02:00
core.h percpu: add __percpu sparse annotations to net 2010-02-16 23:05:38 -08:00
dccp.h [NETNS][DCCPV6]: Move the dccp_v6_ctl_sk on the struct net. 2008-04-13 22:32:25 -07:00
generic.h netns: remove BUG_ONs from net_generic() 2015-04-17 15:21:48 -04:00
hash.h netns: constify net_hash_mix() and various callers 2015-03-18 22:00:34 -04:00
ieee802154_6lowpan.h ieee802154: 6lowpan: ensure MTU of 1280 for 6lowpan 2014-08-19 19:17:42 +02:00
ipv4.h tcp/dccp: warn user for preferred ip_local_port_range 2015-05-27 14:35:36 -04:00
ipv6.h ipv6: Flow label state ranges 2015-05-03 21:58:01 -04:00
mib.h net: use IS_ENABLED(CONFIG_IPV6) 2011-12-11 18:25:16 -05:00
mpls.h mpls: Add a sysctl to control the size of the mpls label table 2015-03-04 00:26:06 -05:00
netfilter.h netfilter: don't pull include/linux/netfilter.h from netns headers 2015-06-18 21:14:31 +02:00
nftables.h netfilter: nf_tables: add netdev table to filter from ingress 2015-05-26 18:41:23 +02:00
packet.h packet: fix broken build. 2012-08-23 09:29:45 -07:00
sctp.h sctp: fix ASCONF list handling 2015-06-14 12:55:49 -07:00
unix.h [NETNS]: struct net content re-work (v3) 2008-01-28 14:57:14 -08:00
x_tables.h netfilter: don't pull include/linux/netfilter.h from netns headers 2015-06-18 21:14:31 +02:00
xfrm.h xfrm: Do not hash socket policies 2014-11-13 11:25:03 +01:00