From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>, stable@vger.kernel.org
Subject: [PATCH nf-next v4 6/9] netfilter: conntrack: harden helper invocation with tuple revalidation
Date: Thu, 3 Dec 2026 16:58:09 +0100 [thread overview]
Message-ID: <20261203155812.8873-7-fw@strlen.de> (raw)
In-Reply-To: <20261203155812.8873-1-fw@strlen.de>
Complete 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, either as-is
(not yet NATed: act_ct and OVS may run the helper before a later
ct action with nat translates the packet) or inverted against the
other direction (already NATed, as on the netfilter hook path).
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. L4 Header Sanity: For TCP, ensure a full and sane header is present
(doff). For UDP, ensure the full header is present and that uh->len is
neither smaller than the header nor larger than the remaining packet,
as helpers compute the payload length from it. UDP packets larger than
64KiB have uh->len == 0 and are rejected for helpers on purpose: helpers
deal with small signalling messages, and some (snmp, nat payload
mangling) cannot handle uh->len == 0. The packet is still tracked,
only the helper invocation is skipped.
Also adds missing 'nhoff' to ipv6_skip_exthdr(), this would be required
for conntrack over bridges.
Fixes: 9fb9cbb1082d ("[NETFILTER]: Add nf_conntrack subsystem.")
Cc: stable@vger.kernel.org
Signed-off-by: Florian Westphal <fw@strlen.de>
---
net/netfilter/nf_conntrack_proto.c | 115 ++++++++++++++++++++++++++++-
1 file changed, 112 insertions(+), 3 deletions(-)
diff --git a/net/netfilter/nf_conntrack_proto.c b/net/netfilter/nf_conntrack_proto.c
index 40f08f846b33..97dd02ec59dc 100644
--- a/net/netfilter/nf_conntrack_proto.c
+++ b/net/netfilter/nf_conntrack_proto.c
@@ -131,11 +131,88 @@ static bool in_vrf_postrouting(const struct nf_hook_state *state)
return false;
}
+/* Helpers and seqadj assume that a full, sane L4 header is present at
+ * protoff. The tuple lookup only pulls the port numbers, so validate the rest.
+ */
+static bool nf_confirm_l4_hdr_ok(const struct sk_buff *skb,
+ unsigned int protoff, u8 protonum)
+{
+ unsigned int l4len = skb->len - protoff;
+
+ switch (protonum) {
+ case IPPROTO_TCP: {
+ const struct tcphdr *th;
+ struct tcphdr _tcph;
+
+ th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
+ if (!th)
+ return false;
+
+ return th->doff * 4 >= sizeof(*th) && l4len >= th->doff * 4;
+ }
+ case IPPROTO_UDP: {
+ const struct udphdr *uh;
+ unsigned int udplen;
+ struct udphdr _uh;
+
+ /* Mirrors udp_error(): helpers compute the payload length
+ * as uh->len - sizeof(struct udphdr).
+ *
+ * uh->len is 0 for UDP packets larger than 64KiB (ipv6 jumbogram,
+ * big GSO skb). Helpers do not support those: they handle small
+ * signalling messages only, and some of them (snmp, nat payload
+ * mangling) cannot cope with uh->len == 0. Rejecting them here is
+ * intentional: the helper is skipped, the packet is still tracked.
+ */
+ uh = skb_header_pointer(skb, protoff, sizeof(_uh), &_uh);
+ if (!uh)
+ return false;
+
+ udplen = ntohs(uh->len);
+ return udplen >= sizeof(*uh) && udplen <= l4len;
+ }
+ }
+
+ return true;
+}
+
+/**
+ * nf_confirm_get_protoff() - Validate skb and get L4 header offset for helpers
+ * @skb: The socket buffer; must have a valid network header, pulled to L3.
+ * @net: The network namespace of the connection tracking entry.
+ * @ct: The connection tracking entry associated with the packet.
+ * @ctinfo: The current connection track information (state) of the packet.
+ * @protoffp: Offset of the L4 header, written only on success.
+ * @pnum: L4 protocol number, written only on success.
+ *
+ * The caller must hold rcu_read_lock().
+ *
+ * Re-extracts the tuple from the packet and verifies that it matches the
+ * connection tracking entry, either as-is (packet not NAT-translated yet,
+ * e.g. tc/OVS before a later ct action with nat) or inverted against the
+ * other direction (NAT already applied, as on the netfilter hook path).
+ * This prevents helpers and seqadj from running on packets that were
+ * modified after the connection lookup, e.g. via act_ct -> pedit, and on
+ * ICMP(v6) error packets related to the connection.
+ *
+ * For TCP and UDP, also verifies that a full and sane L4 header is present
+ * at the L4 offset (tcp data offset, udp length field).
+ *
+ * A false return covers both packets that cannot be processed (fragment,
+ * truncated or malformed header) and packets whose tuple does not match
+ * the connection tracking entry.
+ *
+ * Return: true on success, false if the packet must not be passed to
+ * helpers or seqadj.
+ */
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)
{
+ unsigned int nhoff = skb_network_offset(skb);
+ struct nf_conntrack_tuple tuple, invert;
+ enum ip_conntrack_dir dir;
unsigned int protoff;
__be16 frag_off;
int start;
@@ -144,12 +221,13 @@ bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net,
case NFPROTO_IPV4:
if (ip_is_fragment(ip_hdr(skb)))
return false;
- protoff = skb_network_offset(skb) + ip_hdrlen(skb);
+ protoff = nhoff + 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);
+ start = ipv6_skip_exthdr(skb, nhoff + sizeof(struct ipv6hdr),
+ pnum, &frag_off);
if (start < 0 || frag_off)
return false;
@@ -160,6 +238,37 @@ bool nf_confirm_get_protoff(struct sk_buff *skb, struct net *net,
return false;
}
+ if (protoff > skb->len)
+ return false;
+
+ if (!nf_ct_get_tuplepr(skb, nhoff, nf_ct_l3num(ct), net, &tuple))
+ return false;
+
+ dir = CTINFO2DIR(ctinfo);
+
+ /* Either the packet has not been NAT-translated yet. This is the case
+ * for tc and OVS: a ct action without nat may look up the connection
+ * and the helper may run before a later ct action with nat translates
+ * the packet. The packet tuple then matches the conntrack tuple in
+ * this direction.
+ */
+ if (!nf_ct_tuple_equal(&tuple, nf_ct_tuple(ct, dir))) {
+ /* Or NAT has been applied already (netfilter hooks, or a
+ * previous tc/OVS ct action with 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_invert_tuple(&invert, &tuple))
+ return false;
+
+ if (!nf_ct_tuple_equal(&invert, nf_ct_tuple(ct, !dir)))
+ return false;
+ }
+
+ if (!nf_confirm_l4_hdr_ok(skb, protoff, tuple.dst.protonum))
+ return false;
+
*protoffp = protoff;
return true;
}
@@ -203,7 +312,7 @@ int nf_ct_call_helper(struct sk_buff *skb, struct nf_conn *ct,
* 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))
+ if (seqadj_needed && from_hook && nf_is_loopback_packet(skb))
seqadj_needed = false;
if (!help && !seqadj_needed)
return NF_ACCEPT;
--
2.56.1
next prev parent reply other threads:[~2026-12-03 15:58 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261203155812.8873-1-fw@strlen.de>
2026-12-03 15:58 ` [PATCH nf-next v4 2/9] netfilter: nf_conntrack_h323: register H.245 helper as TCP Florian Westphal
2026-12-03 15:58 ` [PATCH nf-next v4 3/9] netfilter: conntrack: refactor helper call logic in nf_confirm() Florian Westphal
2026-12-03 15:58 ` [PATCH nf-next v4 4/9] netfilter: conntrack: verify L4 protocol before calling helper Florian Westphal
2026-12-03 15:58 ` [PATCH nf-next v4 5/9] netfilter: conntrack: replace open-coded helper invocation Florian Westphal
2026-12-03 15:58 ` Florian Westphal [this message]
2026-12-03 15:58 ` [PATCH nf-next v4 7/9] netfilter: conntrack: confirm ct when helper validation fails 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=20261203155812.8873-7-fw@strlen.de \
--to=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
--cc=stable@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