Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: nf_conntrack: fix explicit helper attachment and NAT

Explicit helper attachment via the CT target is broken with NAT
if non-standard ports are used. This problem was hidden behind
the automatic helper assignment routine. Thus, it becomes more
noticeable now that we can disable the automatic helper assignment
with Eric Leblond's:

9e8ac5a netfilter: nf_ct_helper: allow to disable automatic helper assignment

Basically, nf_conntrack_alter_reply asks for looking up the helper
up if NAT is enabled. Unfortunately, we don't have the conntrack
template at that point anymore.

Since we don't want to rely on the automatic helper assignment,
we can skip the second look-up and stick to the helper that was
attached by iptables. With the CT target, the user is in full
control of helper attachment, thus, the policy is to trust what
the user explicitly configures via iptables (no automatic magic
anymore).

Interestingly, this bug was hidden by the automatic helper look-up
code. But it can be easily trigger if you attach the helper in
a non-standard port, eg.

iptables -I PREROUTING -t raw -p tcp --dport 8888 \
-j CT --helper ftp

And you disabled the automatic helper assignment.

I added the IPS_HELPER_BIT that allows us to differenciate between
a helper that has been explicitly attached and those that have been
automatically assigned. I didn't come up with a better solution
(having backward compatibility in mind).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

+16 -1
+4
include/linux/netfilter/nf_conntrack_common.h
··· 83 83 /* Conntrack is a fake untracked entry */ 84 84 IPS_UNTRACKED_BIT = 12, 85 85 IPS_UNTRACKED = (1 << IPS_UNTRACKED_BIT), 86 + 87 + /* Conntrack got a helper explicitly attached via CT target. */ 88 + IPS_HELPER_BIT = 13, 89 + IPS_HELPER = (1 << IPS_HELPER_BIT), 86 90 }; 87 91 88 92 /* Connection tracking event types */
+12 -1
net/netfilter/nf_conntrack_helper.c
··· 182 182 struct net *net = nf_ct_net(ct); 183 183 int ret = 0; 184 184 185 + /* We already got a helper explicitly attached. The function 186 + * nf_conntrack_alter_reply - in case NAT is in use - asks for looking 187 + * the helper up again. Since now the user is in full control of 188 + * making consistent helper configurations, skip this automatic 189 + * re-lookup, otherwise we'll lose the helper. 190 + */ 191 + if (test_bit(IPS_HELPER_BIT, &ct->status)) 192 + return 0; 193 + 185 194 if (tmpl != NULL) { 186 195 help = nfct_help(tmpl); 187 - if (help != NULL) 196 + if (help != NULL) { 188 197 helper = help->helper; 198 + set_bit(IPS_HELPER_BIT, &ct->status); 199 + } 189 200 } 190 201 191 202 help = nfct_help(ct);