Replace duplicated logic with the new nf_ct_call_helper() function. The skip of sequence adjustment for packets received via loopback only makes sense for the netfilter hooks, where such packets were already adjusted on the output path. OVS and act_ct do not run the netfilter hooks, so nf_ct_helper() always adjusted sequence numbers. Add a 'from_hook' argument to nf_ct_call_helper() so that nf_ct_helper() keeps doing so. One behavior change is intentional: if the helper's ->help callback is NULL (helper is being unregistered), nf_ct_helper() used to return without adjusting sequence numbers. Now it continues with sequence adjustment, like nf_confirm() always did. The connection may already have been mangled, so later packets need the adjustment. Check the helper's family against nf_ct_l3num(ct) instead of the caller-supplied protocol. nf_ct_call_helper() parses the packet according to nf_ct_l3num(ct), so the helper selection and the header parsing now always use the same family, even if act_ct or OVS see a packet whose family differs from the attached conntrack's family. The proto argument of nf_ct_helper() is therefore unused; remove it. This bug is ancient and always existed ever since conntrack was added to the kernel. Before unprivileged user and namespaces (5eaf563e5329, b33c77ef23dd) this required CAP_NET_ADMIN, so only privileged users could trigger it. Fixes: 9fb9cbb1082d ("[NETFILTER]: Add nf_conntrack subsystem.") Cc: stable@vger.kernel.org Signed-off-by: Florian Westphal --- include/net/netfilter/nf_conntrack_helper.h | 4 +- net/netfilter/nf_conntrack_ovs.c | 57 ++------------------- net/netfilter/nf_conntrack_proto.c | 18 +++++-- net/openvswitch/conntrack.c | 4 +- net/sched/act_ct.c | 2 +- 5 files changed, 22 insertions(+), 63 deletions(-) diff --git a/include/net/netfilter/nf_conntrack_helper.h b/include/net/netfilter/nf_conntrack_helper.h index 3279bef21014..191420feb9b9 100644 --- a/include/net/netfilter/nf_conntrack_helper.h +++ b/include/net/netfilter/nf_conntrack_helper.h @@ -123,13 +123,13 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl, gfp_t flags); int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, - enum ip_conntrack_info ctinfo, u16 proto); + enum ip_conntrack_info ctinfo); bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, struct nf_conn *ct, enum ip_conntrack_info ctinfo, unsigned int *protoffp, u8 *pnum); int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, - enum ip_conntrack_info ctinfo); + enum ip_conntrack_info ctinfo, bool from_hook); int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family, u8 proto, bool nat, struct nf_conntrack_helper **hp); diff --git a/net/netfilter/nf_conntrack_ovs.c b/net/netfilter/nf_conntrack_ovs.c index fe525b324af4..5e7b72149fa5 100644 --- a/net/netfilter/nf_conntrack_ovs.c +++ b/net/netfilter/nf_conntrack_ovs.c @@ -10,18 +10,10 @@ /* 'skb' should already be pulled to nh_ofs. */ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, - enum ip_conntrack_info ctinfo, u16 proto) + enum ip_conntrack_info ctinfo) { - int (*helper_cb)(struct sk_buff *skb, unsigned int protoff, - struct nf_conn *ct, - enum ip_conntrack_info conntrackinfo); const struct nf_conntrack_helper *helper; const struct nf_conn_help *help; - unsigned int protoff; - int err; - - if (ctinfo == IP_CT_RELATED_REPLY) - return NF_ACCEPT; help = nfct_help(ct); if (!help) @@ -32,53 +24,10 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, return NF_ACCEPT; if (helper->nfproto != NFPROTO_UNSPEC && - helper->nfproto != proto) - return NF_ACCEPT; - - switch (proto) { - case NFPROTO_IPV4: - protoff = ip_hdrlen(skb); - proto = ip_hdr(skb)->protocol; - break; - case NFPROTO_IPV6: { - u8 nexthdr = ipv6_hdr(skb)->nexthdr; - __be16 frag_off; - int ofs; - - ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, - &frag_off); - if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { - pr_debug("proto header not found\n"); - return NF_ACCEPT; - } - protoff = ofs; - proto = nexthdr; - break; - } - default: - WARN_ONCE(1, "helper invoked on non-IP family!"); - return NF_DROP; - } - - if (helper->l4proto != proto) - return NF_ACCEPT; - - helper_cb = rcu_dereference(helper->help); - if (!helper_cb) + helper->nfproto != nf_ct_l3num(ct)) return NF_ACCEPT; - err = helper_cb(skb, protoff, ct, ctinfo); - if (err != NF_ACCEPT) - return err; - - /* Adjust seqs after helper. This is needed due to some helpers (e.g., - * FTP with NAT) adusting the TCP payload size when mangling IP - * addresses and/or port numbers in the text-based control connection. - */ - if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && - !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) - return NF_DROP; - return NF_ACCEPT; + return nf_ct_call_helper(skb, ct, ctinfo, false); } EXPORT_SYMBOL_GPL(nf_ct_helper); diff --git a/net/netfilter/nf_conntrack_proto.c b/net/netfilter/nf_conntrack_proto.c index bd93d9a1f2b3..40f08f846b33 100644 --- a/net/netfilter/nf_conntrack_proto.c +++ b/net/netfilter/nf_conntrack_proto.c @@ -169,6 +169,11 @@ bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, * @skb: The socket buffer containing the packet to be processed. * @ct: The connection tracking entry associated with this packet. * @ctinfo: The current connection track information (state) of the packet. + * @from_hook: True when called from a netfilter hook (nf_confirm), false for + * act_ct and OVS, which do not run the netfilter hooks. + * + * The caller must hold rcu_read_lock(). The caller must have pulled + * @skb up to the network header (L3). * * This function calls the l4 connection tracking helper (e.g. ftp, sip...) if * one was assigned to the connection. @@ -183,7 +188,7 @@ bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, * Return: verdict (NF_ACCEPT, NF_DROP, ...) */ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, - enum ip_conntrack_info ctinfo) + enum ip_conntrack_info ctinfo, bool from_hook) { struct net *net = nf_ct_net(ct); const struct nf_conn_help *help; @@ -193,7 +198,13 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, help = nfct_help(ct); - seqadj_needed = test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && !nf_is_loopback_packet(skb); + seqadj_needed = test_bit(IPS_SEQ_ADJUST_BIT, &ct->status); + /* Packets looped back via lo already had their sequence numbers + * adjusted in the netfilter hooks on the output path. Callers outside + * of the netfilter hooks (act_ct, OVS) have no such earlier pass. + */ + if (from_hook && nf_is_loopback_packet(skb)) + seqadj_needed = false; if (!help && !seqadj_needed) return NF_ACCEPT; @@ -211,7 +222,6 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info conntrackinfo); int ret; - /* rcu_read_lock()ed by nf_hook */ helper = rcu_dereference(help->helper); if (helper) { if (helper->l4proto && helper->l4proto != pnum) @@ -264,7 +274,7 @@ unsigned int nf_confirm(void *priv, if (!ct || in_vrf_postrouting(state)) return NF_ACCEPT; - ret = nf_ct_call_helper(skb, ct, ctinfo); + ret = nf_ct_call_helper(skb, ct, ctinfo, true); if (unlikely(ret != NF_ACCEPT)) { if (ret == NF_DROP) NF_CT_STAT_INC_ATOMIC(state->net, drop); diff --git a/net/openvswitch/conntrack.c b/net/openvswitch/conntrack.c index d3326edcabf7..5768cfe8f594 100644 --- a/net/openvswitch/conntrack.c +++ b/net/openvswitch/conntrack.c @@ -825,7 +825,7 @@ static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, * before the call. */ if (nf_ct_is_confirmed(ct) && !cached) { - int err = nf_ct_helper(skb, ct, ctinfo, info->family); + int err = nf_ct_helper(skb, ct, ctinfo); err = verdict_to_errno(err); if (err) @@ -1032,7 +1032,7 @@ static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, /* Call the helpers now. We couldn't do this before as * all the extensions must be allocated before the call. */ - err = nf_ct_helper(skb, ct, ctinfo, info->family); + err = nf_ct_helper(skb, ct, ctinfo); err = verdict_to_errno(err); if (err) return err; diff --git a/net/sched/act_ct.c b/net/sched/act_ct.c index 7602733d7f6b..500aa2639a8c 100644 --- a/net/sched/act_ct.c +++ b/net/sched/act_ct.c @@ -1115,7 +1115,7 @@ TC_INDIRECT_SCOPE int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, * extensions are already added. */ if (nf_ct_is_confirmed(ct) ? (!cached && !skip_add) : commit) { - err = nf_ct_helper(skb, ct, ctinfo, family); + err = nf_ct_helper(skb, ct, ctinfo); if (err != NF_ACCEPT) goto nf_error; } -- 2.56.1