From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.245.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796313531; b=jxc26nHkH5irLEqJnPnnFmvdAHQaKHbamK59XuuKGcpYQWVQpKuwgqby0xy5c7wL7ood r+o92JNY24srU2CFezwAMKv0RAkEAApS6WvQsLvORyLOVk+ZJCVFNk30dvBKDOf/gOZX1 PwZZx4gXsR1cA2BUA6qz1ALjiU9LtSSGoOxeZrAzWDcbCMDbMAdSbeYFLSA7j6arbV9cS zRjrjmhnNCwe7lm8lju5wKBYUaVGUxCo1/Ok3NBRs6J66X4Aet/xOemqZya2RrRnDxhAC VhuPW4rjK+tFJfjRU/VSvCcJBD84H2z4+iBoceimzS1nD7Ml+RpPRGY1UQ2Ae+E85EQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796313531; h=DMARC-Filter:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=x8SAG5++dSyoM8WkCStmRQ6rX4Wu+sI4nvM/I2Uiv18=; b=udITn3mtwvbMc//DIIrf1HO9PWzzeJNaDzimCVPHkcl00AzF2GOhL5ix4K8J+opKuOhd Zo3caFyp23niNtYe+I1MmbczOp4J2ebWIiUk072aGmJuEQ1iIijIWU35AADgUIUO8SKVo x70WJivAEgQOunaI4gjKQQpoe3qREPhAATdVqPSLGbDhSeiSRQz6q4tiOdbQXrMnjchpt x272X6FwTr8Pl8ZA6TTGBeVu6yK3rwVClRXGdWgt/NAXfRwBVJFfxh5XFZV+ztI9nXgZP OVkh6cCzRqc3d7HWwBMKAXv/JF002p60pFL36f9iKneBezLq7BzlOGz8ow9GIFGUBIg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=strlen.de; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc; arc=none smtp.remote-ip=91.216.245.30 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [91.216.245.30]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A81BB606A322; Thu, 3 Dec 2026 15:58:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A81BB606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=strlen.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc Received: by Chamillionaire.breakpoint.cc (Postfix, from userid 1003) id 1AFB660678; Thu, 03 Dec 2026 16:58:49 +0100 (CET) From: Florian Westphal To: Cc: Florian Westphal , 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 Message-ID: <20261203155812.8873-7-fw@strlen.de> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203155812.8873-1-fw@strlen.de> References: <20261203155812.8873-1-fw@strlen.de> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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