netfilter: nf_conntrack: validate skb->_nfct and packet headers
Subsystems (2)
Florian Westphal
maintainerPATCH 1/6 netfilter: nf_conntrack: validate skb->_nfct and packet headers
skbs coming from loopback already have skb->_nfct attached. act_ct may also attach a conntrack to the skb.
Facilities like clsact or pedit can alter headers which may then result in e.g. UDP packet with TCP nf_conn.
Revalidate that this skb matches the skb l3/l4 header. If not, drop the stale reference and let nf_conntrack_in perform a re-lookup.
For conntrack helpers, more checks may be required, e.g. tcph->doff revalidation. This is handled in a followup patch.
Note that the Fixes tag is bogus, back then this was perfectly fine.
namespaces and especially unprivileged user namespaces did not exist and all crash-configs were in the "don't do that, then" department.
--- net/netfilter/nf_conntrack_core.c | 75 +++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 9 deletions(-)
net/netfilter/nf_conntrack_core.c +66 -9
@@ -2000,6 +2000,63 @@ static int nf_conntrack_handle_packet(struct nf_conn *ct,return generic_packet (ct, skb, ctinfo); } +/** +* nf_ct_get_careful - Get connection tracking entry with protocol revalidation +* +* @skb: Socket buffer whose conntrack entry is to be retrieved +* @state: Netfilter hook state +* @dataoff: Offset to the layer-4 header within @skb +* @protonum: protocol number (e.g., IPPROTO_TCP) +* @ctinfo: Pointer to store ip_conntrack_info +* +* This function retrieves the connection tracking entry associated with @skb. +* Performs revalidation of packet header, unlike nf_ct_get(). +* +* If earlier in-kernel mangling (e.g., via TC pedit or clsact) altered packet +* contents (e.g. changing TCP to UDP), this function will drop the conntrack +* reference and returns NULL. skbs coming in via NF_INET_LOCAL_OUT are +* trusted and never revalidated. +* +* Returns: +* %NULL - No valid conntrack entry exists or revalidation failed. +* Pointer to &struct nf_conn associated with skb, may be a template. +*/ +static struct nf_conn * +nf_ct_get_careful (struct sk_buff *skb, +const struct nf_hook_state *state, +unsigned int dataoff,u8 protonum, +enum ip_conntrack_info *ctinfo) +{ +struct nf_conn *tmpl =nf_ct_get (skb, ctinfo); +struct nf_conntrack_tuple tuple; + +/* LOCAL_OUT is trusted: in case stack sends ICMP error, skb gets +* the conntrack assigned via nf_ct_attach(). +* +* Such conntrack may not even be in hashtable yet, so +* nf_conntrack_handle_icmp() cannot find a connection matching +* the inner header. +*/ +if (state->hook ==NF_INET_LOCAL_OUT ) +return tmpl; + +if (!tmpl ||nf_ct_is_template (tmpl)) +return tmpl; + +if (state->pf !=nf_ct_l3num (tmpl)) + goto error; + +if (nf_ct_get_tuple (skb,skb_network_offset (skb), + dataoff, state->pf, protonum, state->net, + &tuple) && +nf_ct_tuple_equal (&tuple,nf_ct_tuple (tmpl,CTINFO2DIR (*ctinfo)))) +return tmpl; + +error: +nf_reset_ct (skb); +return NULL ; +} +unsigned int nf_conntrack_in (struct sk_buff *skb,const struct nf_hook_state *state) {
@@ -2008,21 +2065,21 @@ nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state)u_int8_t protonum;int dataoff, ret; - tmpl =nf_ct_get (skb, &ctinfo); +/* rcu_read_lock()ed by nf_hook_thresh */ + dataoff =get_l4proto (skb,skb_network_offset (skb), state->pf, &protonum); +if (dataoff <=0 ) { +NF_CT_STAT_INC_ATOMIC (state->net, invalid); +return NF_ACCEPT ; + } + + tmpl =nf_ct_get_careful (skb, state, dataoff, protonum, &ctinfo);if (tmpl || ctinfo ==IP_CT_UNTRACKED ) {/* Previously seen (loopback or untracked)? Ignore. */ if ((tmpl && !nf_ct_is_template (tmpl)) || ctinfo ==IP_CT_UNTRACKED )return NF_ACCEPT ; - skb->_nfct =0 ; - } -/* rcu_read_lock()ed by nf_hook_thresh */ - dataoff =get_l4proto (skb,skb_network_offset (skb), state->pf, &protonum); -if (dataoff <=0 ) { -NF_CT_STAT_INC_ATOMIC (state->net, invalid); - ret =NF_ACCEPT ; - goto out; + skb->_nfct =0 ; }if (protonum ==IPPROTO_ICMP || protonum ==IPPROTO_ICMPV6 ) {
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 2/6 netfilter: nf_conntrack: refactor helper call logic in nf_confirm()
The current implementation of nf_confirm() handles several distinct
tasks: retrieving the connection, calculating protocol offsets for different network families, and executing the associated helpers or sequence adjustments.
Move functionality to extra functions:
1. nf_confirm_get_protoff(): Isolates the logic for determining the protocol offset (protoff) for IPv4 and IPv6 packets.
2. nf_ct_call_helper(): Encapsulates the decision-making process of whether to call a connection tracking helper or perform sequence adjustment, utilizing nf_confirm_get_protoff() to find the correct offset.
NF_CT_STAT_INC_ATOMIC() is now incremented for any NF_DROP, earlier this was elided in some cases, IMO that was a bug all along.
Followup patches will extend these new functions with stricter checks.
--- include/net/netfilter/nf_conntrack_helper.h | 2 + net/netfilter/nf_conntrack_proto.c | 113 +++++++++++++++----- 2 files changed, 87 insertions(+), 28 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +2 -0
@@ -124,6 +124,8 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,int nf_ct_helper (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo,u16 proto); +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,u8 proto,bool nat,struct nf_conntrack_helper **hp);
net/netfilter/nf_conntrack_proto.c +85 -28
146 lines not on this page · raw message
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 3/6 netfilter: nf_conntrack: verify L4 protocol before calling helper
In reply to Florian WestphalThis 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 act_pipe 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 frasgments, 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).
Also skip the helper invocation if the packet contains no payload (protoff == skb->len).
nft_ct.c needs to set l4proto in the new helper it allocates to avoid tripping the new helper->l4proto != pnum guard.
--- net/netfilter/nf_conntrack_proto.c | 23 ++++++++++++++++------- net/netfilter/nft_ct.c | 1 + 2 files changed, 17 insertions(+), 7 deletions(-)
net/netfilter/nf_conntrack_proto.c +16 -7
@@ -134,21 +134,23 @@ static bool in_vrf_postrouting(const struct nf_hook_state *state)static 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 @@ static 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. * * Return: verdict (NF_ACCEPT, NF_DROP, ...) */
@@ -180,6 +185,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);
@@ -191,10 +197,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,
@@ -204,6 +210,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 != pnum) +return NF_ACCEPT ; + helper_cb =rcu_dereference (helper->help);if (helper_cb) { ret = helper_cb(skb, protoff,
net/netfilter/nft_ct.c +1 -0
@@ -1306,6 +1306,7 @@ static int nft_ct_expect_helper_alloc(struct nft_ct_expect_obj *priv) "nft_ct_expect"); ct_expect_helper->me =THIS_MODULE ; ct_expect_helper->expect_policy[NF_CT_EXPECT_CLASS_DEFAULT ].max_expected = priv->size; + ct_expect_helper->l4proto = priv->l4proto;rcu_assign_pointer (ct_expect_helper->help, ct_expect_help);refcount_set (&ct_expect_helper->ct_refcnt,1 );
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 4/6 netfilter: conntrack: replace open-coded helper invocation
In reply to Florian WestphalReplace duplicated logic with the new nf_ct_call_helper() helper. No functional changes intended, except the additional checks to ensure the helper can process the given packet.
--- net/netfilter/nf_conntrack_ovs.c | 50 +------------------------------- 1 file changed, 1 insertion(+), 49 deletions(-)
net/netfilter/nf_conntrack_ovs.c +1 -49
@@ -12,13 +12,8 @@int nf_ct_helper (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo,u16 proto) { -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 ;
@@ -35,50 +30,7 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, 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) -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); }EXPORT_SYMBOL_GPL (nf_ct_helper);
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 5/6 netfilter: nf_conntrack: harden helper invocation with tuple revalidation
In reply to Florian WestphalComplete hardening of the connection tracking helper invocation sequence initiated in the previous refactoring commits.
1. Tuple Verification: Extracts the *current* tuple from the packet and verify it matches the connection tracking entry's tuple. This prevents helpers from being invoked on packets that may have been modified after the initial connection lookup, e.g. via act_ct -> pedit. This also prevents the helper from operating on ICMP(v6) PMTU errors.
2. TCP Header Sanity: For TCP traffic, the patch introduces a check to ensure a full and sane TCP header is present.
--- net/netfilter/nf_conntrack_proto.c | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
net/netfilter/nf_conntrack_proto.c +34 -0
@@ -136,6 +136,8 @@ static bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net,enum ip_conntrack_info ctinfo, unsigned int *protoffp, u8 *pnum) { + struct nf_conntrack_tuple tuple, invert; +enum ip_conntrack_dir dir;unsigned int protoff;__be16 frag_off;int start;
@@ -160,6 +162,38 @@ static bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net,return false; } +if (!nf_ct_get_tuplepr (skb,skb_network_offset (skb),nf_ct_l3num (ct), + net, &tuple)) +return false; + + dir =CTINFO2DIR (ctinfo); +nf_ct_invert_tuple (&invert, &tuple); + +/* This is called after L3/L4 headers have been mangled by NAT: +* Packet in original direction has been subject to SNAT, i.e. +* inverted reply dir. +* Packet in reply direction has been subject to DNAT, i.e. +* inverted original direction. +*/ +if (!nf_ct_tuple_equal (&invert,nf_ct_tuple (ct, !dir))) +return false; + +/* Validate that a full, sane TCP header (including options) is +* present at protoff before helpers/seqadj are allowed to touch it. +*/ +if (tuple.dst.protonum ==IPPROTO_TCP ) { +unsigned int tcplen = skb->len - protoff; +const struct tcphdr *th; +struct tcphdr _tcph; + + th =skb_header_pointer (skb, protoff,sizeof (_tcph), &_tcph); +if (!th) +return false; + +if (th->doff *4 <sizeof (*th) || tcplen < th->doff *4 ) +return false; + } + *protoffp = protoff;return true; }
Signature · 1 line
2.55.0
Florian Westphal
maintainerPATCH 6/6 netfilter: nft_ct: validate timeout object protocol
In reply to Florian Westphalnft_ct_timeout_obj_eval() only compares the timeout object protocol with packet metadata. A packet header can be changed after conntrack attaches an entry, so this metadata does not necessarily describe the entry.
Timeout objects contain protocol-specific arrays. Attaching an object for a protocol with fewer timeout states to an entry for one with more states lets the conntrack tracker read beyond the object.
Require the object protocol to match the conntrack tuple protocol before attaching it. This mirrors validation by named timeout policies and nftables conntrack helper objects.
Based on original patch from Kyle Zheng, who also authored this commit message.
--- net/netfilter/nft_ct.c | 52 ++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 12 deletions(-)
net/netfilter/nft_ct.c +40 -12
@@ -839,6 +839,38 @@ static struct nft_expr_type nft_notrack_type __read_mostly = { .owner = THIS_MODULE, }; +/** +* nft_ct_get_safe() - Return nf_conn with extra checks +* @pkt: nftables packet information structure +* @l4proto: The expected Layer 4 protocol +* @ctinfop: packet ip_conntrack_info storage +* +* Returns the conntrack entry only if it is unconfirmed, non-template and +* matches the expected L4 protocol. +* +* Return: Pointer to the &struct nf_conn if all checks pass; NULL otherwise. +*/ +static struct nf_conn *nft_ct_get_safe (const struct nft_pktinfo *pkt, +u8 l4proto,enum ip_conntrack_info *ctinfop) +{ +enum ip_conntrack_info ctinfo; +struct nf_conn *ct; + + ct =nf_ct_get (pkt->skb, &ctinfo); +if (!ct || l4proto != pkt->tprot) +return NULL ; + +if (l4proto !=nf_ct_protonum (ct)) +return NULL ; + +if (READ_ONCE (ct->status) & (IPS_TEMPLATE |IPS_CONFIRMED )) +return NULL ; + +if (ctinfop) + *ctinfop = ctinfo; +return ct; +} +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT static int nft_ct_timeout_parse_policy(void *timeouts,
@@ -878,14 +910,12 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj, const struct nft_pktinfo *pkt) {const struct nft_ct_timeout_obj *priv =nft_obj_data (obj); -struct nf_conn *ct = (struct nf_conn *)skb_nfct (pkt->skb);struct nf_conn_timeout *timeout;const unsigned int *values; +struct nf_conn *ct; -if (priv->l4proto != pkt->tprot) -return ; - -if (!ct ||nf_ct_is_template (ct) ||nf_ct_is_confirmed (ct)) + ct =nft_ct_get_safe (pkt, priv->l4proto,NULL ); +if (!ct)return ; timeout =nf_ct_timeout_find (ct);
@@ -1114,14 +1144,12 @@ static void nft_ct_helper_obj_eval(struct nft_object *obj, const struct nft_pktinfo *pkt) {const struct nft_ct_helper_obj *priv =nft_obj_data (obj); -struct nf_conn *ct = (struct nf_conn *)skb_nfct (pkt->skb);struct nf_conntrack_helper *to_assign =NULL ;struct nf_conn_help *help; +struct nf_conn *ct; -if (!ct || -nf_ct_is_confirmed (ct) || -nf_ct_is_template (ct) || - priv->l4proto !=nf_ct_protonum (ct)) + ct =nft_ct_get_safe (pkt, priv->l4proto,NULL ); +if (!ct)return ;switch (nf_ct_l3num (ct)) {
@@ -1417,8 +1445,8 @@ static void nft_ct_expect_obj_eval(struct nft_object *obj,struct nf_conn_help *help;struct nf_conn *ct; - ct =nf_ct_get (pkt->skb, &ctinfo); -if (!ct ||nf_ct_is_confirmed (ct) ||nf_ct_is_template (ct)) { + ct =nft_ct_get_safe (pkt, priv->l4proto, &ctinfo); +if (!ct) { regs->verdict.code =NFT_BREAK ;return ; }
Signature · 1 line
2.55.0