When an ICMPv6 error embeds a non-first fragment, ipv6_find_hdr() returns at the Fragment header without updating *offset, so ip_vs_fill_iph_skb_icmp() leaves ciph.len equal to ciph.off, the start of the embedded IPv6 header. ip_vs_out_icmp_v6() and ip_vs_in_icmp_v6() pass embedded non-first TCP, UDP and SCTP fragments through before any lookup. AH and ESP set pp->dont_defrag, so their fragments continue. Their lookups read only the addresses and never use ciph.len, but handle_response_icmp() and ip_vs_icmp_xmit_v6() size skb_ensure_writable() from ciph.len, which then covers nothing of the embedded header. ip_vs_nat_icmp_v6() rewrites the embedded destination or source address, 16 bytes located 8 or 24 bytes past the end of the validated area. Include the embedded IPv6 header in ciph.len at both call sites when ipv6_find_hdr() stopped at a non-first fragment. Only the size of the writable check changes; the AH and ESP lookups do not use ciph.len. This fix does not depend on patch 2. Stable kernels without commit 342e24a339b9 ("ipvs: do not mangle ICMP replies for non-first fragments") also need that commit to handle incoming ICMPv6 errors quoting non-first ESP fragments. Fixes: 63dca2c0b0e7 ("ipvs: Fix faulty IPv6 extension header handling in IPVS") Cc: stable@vger.kernel.org Suggested-by: Julian Anastasov Signed-off-by: Axel Mierczuk --- net/netfilter/ipvs/ip_vs_core.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/net/netfilter/ipvs/ip_vs_core.c b/net/netfilter/ipvs/ip_vs_core.c index fd503f0efb57..d5da16ff9673 100644 --- a/net/netfilter/ipvs/ip_vs_core.c +++ b/net/netfilter/ipvs/ip_vs_core.c @@ -1206,6 +1206,16 @@ static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, true, &ciph)) return NF_ACCEPT; /* The packet looks wrong, ignore */ + /* ipv6_find_hdr() stops at the Fragment header of a non-first + * fragment and leaves ciph.len == ciph.off. Only AH and ESP + * (pp->dont_defrag) pass the fragoffs check below and their + * lookup reads only the addresses, but handle_response_icmp() + * sizes skb_ensure_writable() from ciph.len before + * ip_vs_nat_icmp_v6() rewrites the embedded header. Include it. + */ + if (ciph.len == ciph.off) + ciph.len += sizeof(struct ipv6hdr); + pp = ip_vs_proto_get(ciph.protocol); if (!pp) return NF_ACCEPT; @@ -2036,6 +2046,16 @@ static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph)) return NF_ACCEPT; + /* ipv6_find_hdr() stops at the Fragment header of a non-first + * fragment and leaves ciph.len == ciph.off. Only AH and ESP + * (pp->dont_defrag) pass the fragoffs check below and their + * lookup reads only the addresses, but ip_vs_icmp_xmit_v6() + * sizes skb_ensure_writable() from ciph.len before + * ip_vs_nat_icmp_v6() rewrites the embedded header. Include it. + */ + if (ciph.len == ciph.off) + ciph.len += sizeof(struct ipv6hdr); + pd = ip_vs_proto_data_get(ipvs, ciph.protocol); if (!pd) return NF_ACCEPT; -- 2.50.1 (Apple Git-155) The non-first fragment early return is the only successful exit of ipv6_find_hdr() that leaves *offset untouched, so a target < 0 caller gets back the offset it started with, the network header, instead of a position past the headers it asked to skip. IPVS relies on the offset being advanced. ip_vs_fill_iph_skb_off() stores it in iph->len, and handle_response() sizes skb_ensure_writable() from it before rewriting the IPv6 source address, so for a non-first ESP fragment matched through ah_esp_conn_out_get() the copy-on-write check covered none of the header. The ICMPv6 paths had the same problem with the embedded header. The previous patch works around it there so that it can be backported on its own. For non-first fragments with target < 0, set *offset immediately after the Fragment header, at the start of the fragment payload, and return -EBADMSG when the Fragment header is truncated, as the other exits already do for the header they skip. Callers must still account for the nonzero fragment offset. A NEXTHDR_FRAGMENT search continues to return the Fragment header's offset. nf_tables stores the offset in pkt->thoff whatever the fragment offset is, so nf_trace_fill_pkt_info() now dumps the network headers up to the fragment payload for non-first fragments instead of an empty network header and the IPv6 header as transport header. SYNPROXY uses the offset without checking the fragment offset as well and now reads the fragment payload instead of the IPv6 header; the -EBADMSG check keeps the offset within the packet. The other target < 0 users check the fragment offset first or only use the returned protocol. Drop the ciph.len workaround from the previous patch: every successful return now writes *offset, so ciph.len can no longer equal ciph.off. Fixes: b777e0ce7437 ("[NETFILTER]: make ipv6_find_hdr() find transport protocol header") Suggested-by: Julian Anastasov Signed-off-by: Axel Mierczuk --- net/ipv6/exthdrs_core.c | 9 ++++++++- net/netfilter/ipvs/ip_vs_core.c | 20 -------------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/net/ipv6/exthdrs_core.c b/net/ipv6/exthdrs_core.c index 4a9748338cf4..3112408ed2f3 100644 --- a/net/ipv6/exthdrs_core.c +++ b/net/ipv6/exthdrs_core.c @@ -179,7 +179,10 @@ EXPORT_SYMBOL_GPL(ipv6_find_tlv); * * Note that non-1st fragment is special case that "the protocol number * of last header" is "next header" field in Fragment header. In this case, - * *offset is meaningless and fragment offset is stored in *fragoff if fragoff + * for target < 0, *offset points immediately after the Fragment header, + * at the start of the fragment payload. Callers must still account for + * the nonzero fragment offset before interpreting the payload. The + * fragment offset is stored in *fragoff if fragoff * isn't NULL. * * if flags is not NULL and it's a fragment, then the frag flag @@ -259,8 +262,12 @@ int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, if (target < 0 && ((!ipv6_ext_hdr(hp->nexthdr)) || hp->nexthdr == NEXTHDR_NONE)) { + if (skb->len - start < + sizeof(struct frag_hdr)) + return -EBADMSG; if (fragoff) *fragoff = _frag_off; + *offset = start + sizeof(struct frag_hdr); return hp->nexthdr; } if (!found) diff --git a/net/netfilter/ipvs/ip_vs_core.c b/net/netfilter/ipvs/ip_vs_core.c index d5da16ff9673..fd503f0efb57 100644 --- a/net/netfilter/ipvs/ip_vs_core.c +++ b/net/netfilter/ipvs/ip_vs_core.c @@ -1206,16 +1206,6 @@ static int ip_vs_out_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, true, &ciph)) return NF_ACCEPT; /* The packet looks wrong, ignore */ - /* ipv6_find_hdr() stops at the Fragment header of a non-first - * fragment and leaves ciph.len == ciph.off. Only AH and ESP - * (pp->dont_defrag) pass the fragoffs check below and their - * lookup reads only the addresses, but handle_response_icmp() - * sizes skb_ensure_writable() from ciph.len before - * ip_vs_nat_icmp_v6() rewrites the embedded header. Include it. - */ - if (ciph.len == ciph.off) - ciph.len += sizeof(struct ipv6hdr); - pp = ip_vs_proto_get(ciph.protocol); if (!pp) return NF_ACCEPT; @@ -2046,16 +2036,6 @@ static int ip_vs_in_icmp_v6(struct netns_ipvs *ipvs, struct sk_buff *skb, if (!ip_vs_fill_iph_skb_icmp(AF_INET6, skb, offset, true, &ciph)) return NF_ACCEPT; - /* ipv6_find_hdr() stops at the Fragment header of a non-first - * fragment and leaves ciph.len == ciph.off. Only AH and ESP - * (pp->dont_defrag) pass the fragoffs check below and their - * lookup reads only the addresses, but ip_vs_icmp_xmit_v6() - * sizes skb_ensure_writable() from ciph.len before - * ip_vs_nat_icmp_v6() rewrites the embedded header. Include it. - */ - if (ciph.len == ciph.off) - ciph.len += sizeof(struct ipv6hdr); - pd = ip_vs_proto_data_get(ipvs, ciph.protocol); if (!pd) return NF_ACCEPT; -- 2.50.1 (Apple Git-155)