From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH nf 1/6] netfilter: nf_conntrack: validate skb->_nfct and packet headers
Date: Mon, 14 Sep 2026 18:58:37 +0200 [thread overview]
Message-ID: <20260914165842.4505-2-fw@strlen.de> (raw)
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.
Fixes: 9fb9cbb1082d ("[NETFILTER]: Add nf_conntrack subsystem.")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
net/netfilter/nf_conntrack_core.c | 75 +++++++++++++++++++++++++++----
1 file changed, 66 insertions(+), 9 deletions(-)
diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c
index d0d9e5ea84a0..57edb07e9b3b 100644
--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -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) {
--
2.55.0
next reply other threads:[~2026-09-14 16:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-14 16:58 Florian Westphal [this message]
2026-09-14 16:58 ` [PATCH nf 2/6] netfilter: nf_conntrack: refactor helper call logic in nf_confirm() Florian Westphal
2026-09-14 16:58 ` [PATCH nf 3/6] netfilter: nf_conntrack: verify L4 protocol before calling helper Florian Westphal
2026-09-14 16:58 ` [PATCH nf 4/6] netfilter: conntrack: replace open-coded helper invocation Florian Westphal
2026-09-14 16:58 ` [PATCH nf 5/6] netfilter: nf_conntrack: harden helper invocation with tuple revalidation Florian Westphal
2026-09-14 16:58 ` [PATCH nf 6/6] netfilter: nft_ct: validate timeout object protocol Florian Westphal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260914165842.4505-2-fw@strlen.de \
--to=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox