This commit ensures that a conntrack helper is only invoked if its configured L4 protocol matches the actual protocol of the packet. Currently, nf_ct_call_helper() invokes a helper based on the connection state, but it does not explicitly verify the protocol number against the helper's expected protocol. This could potentially lead to helpers processing packets they were not designed for. This can happen with related ICMP(v6) errors in the original direction or when conntrack got attached via TC/act_ct, then was later munged via pedit or bpf to alter the ip protocol. Extend the introduced helper functions: extract the L4 protocol number, then check it matches helper->l4proto. Also change ipv4 to explicitly skip fragments, this shouldn't happen for normal netfilter-only code path, because conntrack depends on defrag module, but this may not be the case for other users (ovs, tc). For ipv6, treat any non-zero frag_off as a fragment, matching the ipv4 check. This also skips first fragments (offset 0, M bit set): a helper only sees a truncated payload of those, and non-first fragments carry no l4 header, so helpers cannot work on fragmented packets. conntrack's defrag normally prevents such packets from reaching helpers. Also skip the helper invocation if the packet contains no payload (protoff == skb->len). helper->l4proto == 0 means the helper is protocol agnostic and is exempt from the check. This is needed for the internal, unregistered helper that nft_ct allocates for expectation objects: it can be attached to any master connection and ct_expect_help() does not parse l4 payload. 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_core.c | 4 +++- net/netfilter/nf_conntrack_proto.c | 23 ++++++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/net/netfilter/nf_conntrack_helper.h b/include/net/netfilter/nf_conntrack_helper.h index 676373fb1143..3279bef21014 100644 --- a/include/net/netfilter/nf_conntrack_helper.h +++ b/include/net/netfilter/nf_conntrack_helper.h @@ -44,7 +44,7 @@ struct nf_conntrack_helper { refcount_t ct_refcnt; u8 nfproto; /* NFPROTO_*, can be NFPROTO_UNSPEC */ - u8 l4proto; /* IPPROTO_UDP/TCP */ + u8 l4proto; /* IPPROTO_*, 0: any (internal, unregistered helpers) */ /* Function to call when data passes; return verdict */ int __rcu (*help)(struct sk_buff *skb, unsigned int protoff, @@ -127,7 +127,7 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, struct nf_conn *ct, enum ip_conntrack_info ctinfo, - unsigned int *protoffp); + unsigned int *protoffp, u8 *pnum); int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info ctinfo); int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family, diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c index 5506c4ab1f26..2ceb198c70ac 100644 --- a/net/netfilter/nf_conntrack_core.c +++ b/net/netfilter/nf_conntrack_core.c @@ -2219,6 +2219,7 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct, const struct nf_conn_help *help; unsigned int helper_flags; unsigned int protoff; + u8 pnum; help = nfct_help(ct); if (!help) @@ -2232,7 +2233,8 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct, if (!(helper_flags & NF_CT_HELPER_F_USERSPACE)) return NF_ACCEPT; - if (!nf_confirm_get_protoff(skb, nf_ct_net(ct), ct, ctinfo, &protoff)) + if (!nf_confirm_get_protoff(skb, nf_ct_net(ct), ct, ctinfo, &protoff, + &pnum)) return NF_ACCEPT; if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && diff --git a/net/netfilter/nf_conntrack_proto.c b/net/netfilter/nf_conntrack_proto.c index 9c74bce8bf25..bd93d9a1f2b3 100644 --- a/net/netfilter/nf_conntrack_proto.c +++ b/net/netfilter/nf_conntrack_proto.c @@ -134,21 +134,23 @@ static bool in_vrf_postrouting(const struct nf_hook_state *state) bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, struct nf_conn *ct, enum ip_conntrack_info ctinfo, - unsigned int *protoffp) + unsigned int *protoffp, u8 *pnum) { unsigned int protoff; __be16 frag_off; int start; - u8 pnum; switch (nf_ct_l3num(ct)) { case NFPROTO_IPV4: + if (ip_is_fragment(ip_hdr(skb))) + return false; protoff = skb_network_offset(skb) + ip_hdrlen(skb); + *pnum = ip_hdr(skb)->protocol; break; case NFPROTO_IPV6: - pnum = ipv6_hdr(skb)->nexthdr; - start = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum, &frag_off); - if (start < 0 || (frag_off & htons(~0x7)) != 0) + *pnum = ipv6_hdr(skb)->nexthdr; + start = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), pnum, &frag_off); + if (start < 0 || frag_off) return false; protoff = start; @@ -170,6 +172,9 @@ bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net, * * This function calls the l4 connection tracking helper (e.g. ftp, sip...) if * one was assigned to the connection. + * This re-derives the l4 protocol number: act_ct can associate the skb with + * a connection/protocol and later pedit/bpf function can munge the packet + * afterwards. * * It also performs TCP sequence number adjustment if the connection has * IPS_SEQ_ADJUST set (e.g. after a helper or NAT changed the payload length). @@ -184,6 +189,7 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, const struct nf_conn_help *help; bool seqadj_needed; unsigned int protoff; + u8 pnum = 0; help = nfct_help(ct); @@ -195,10 +201,10 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, if (ctinfo == IP_CT_RELATED_REPLY) return NF_ACCEPT; - if (!nf_confirm_get_protoff(skb, net, ct, ctinfo, &protoff)) + if (!nf_confirm_get_protoff(skb, net, ct, ctinfo, &protoff, &pnum)) return NF_ACCEPT; - if (help) { + if (help && protoff < skb->len) { const struct nf_conntrack_helper *helper; int (*helper_cb)(struct sk_buff *skb, unsigned int protoff, struct nf_conn *ct, @@ -208,6 +214,9 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct, /* rcu_read_lock()ed by nf_hook */ helper = rcu_dereference(help->helper); if (helper) { + if (helper->l4proto && helper->l4proto != pnum) + return NF_ACCEPT; + helper_cb = rcu_dereference(helper->help); if (helper_cb) { ret = helper_cb(skb, protoff, -- 2.56.1