Skip to contentSkip to conversation

src: fix IPv6 header handling

PATCH In review Etan Kissling 2 participants 3 messages Jan 13, 2021 – Feb 17, 2021 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 src: fix IPv6 header handling

This corrects issues in IPv6 header handling that sometimes resulted in an endless loop.

---
 src/extra/ipv6.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/extra/ipv6.c +3 -2
index 42c5e25..1eb822f 100644
@@ -72,7 +72,8 @@ int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,
 		uint32_t hdrlen;
 
 		/* No more extensions, we're done. */
-		if (nexthdr == IPPROTO_NONE) {
+		if (nexthdr == IPPROTO_TCP || nexthdr == IPPROTO_UDP || nexthdr == IPPROTO_ESP ||
+		        nexthdr == IPPROTO_ICMPV6 || nexthdr == IPPROTO_NONE) {
 			cur = NULL;
 			break;
 		}
@@ -107,7 +108,7 @@ int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,
 		} else if (nexthdr == IPPROTO_AH)
 			hdrlen = (ip6_ext->ip6e_len + 2) << 2;
 		else
-			hdrlen = ip6_ext->ip6e_len;
+			hdrlen = (ip6_ext->ip6e_len + 1) << 3;
 
 		nexthdr = ip6_ext->ip6e_nxt;
 		cur += hdrlen;
Signature · 1 line
2.21.1 (Apple Git-122.3)
#1 lore raw

Hi Etan,

src/extra/ipv6.c int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,

Quoted 5 lines · Etan Kissling
src/extra/ipv6.c +0 -0
index 42c5e25..1eb822f 100644
@@ -72,7 +72,8 @@ int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,

Note: nfq_ip6_set_transport_header() is very much similar to ipv6_skip_exthdr() in the Linux kernel, see net/ipv6/exthdrs_core.c

Quoted 10 lines
src/extra/ipv6.c +2 -1
 		uint32_t hdrlen;
 
 		/* No more extensions, we're done. */
-		if (nexthdr == IPPROTO_NONE) {
+		if (nexthdr == IPPROTO_TCP || nexthdr == IPPROTO_UDP || nexthdr == IPPROTO_ESP ||
+		        nexthdr == IPPROTO_ICMPV6 || nexthdr == IPPROTO_NONE) {
 			cur = NULL;
 			break;
 		}

src/extra/ipv6.c int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h, · line 111

Quoted 5 lines
src/extra/ipv6.c +1 -1
@@ -107,7 +108,7 @@ int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,
 		} else if (nexthdr == IPPROTO_AH)
 			hdrlen = (ip6_ext->ip6e_len + 2) << 2;
 		else
-			hdrlen = ip6_ext->ip6e_len;
+			hdrlen = (ip6_ext->ip6e_len + 1) << 3;

This looks correct, IPv6 optlen is miscalculated.

The chunk above to stop the iteration, so I think the chunk that fixes optlen is sufficient to fix the bug.

Thanks.

#2 lore raw

On 09.02.21, 17:33, "Pablo Neira Ayuso" <[email protected]> wrote:

Thanks for looking into this.

Quoted 2 lines

Note: nfq_ip6_set_transport_header() is very much similar to ipv6_skip_exthdr() in the Linux kernel, see net/ipv6/exthdrs_core.c

I submitted a revised version that uses similar logic as in the kernel to exit the loop once extension headers are fully processed.

Quoted 18 lines
Quoted 10 lines
src/extra/ipv6.c +2 -1
 		uint32_t hdrlen;
 
 		/* No more extensions, we're done. */
-		if (nexthdr == IPPROTO_NONE) {
+		if (nexthdr == IPPROTO_TCP || nexthdr == IPPROTO_UDP || nexthdr == IPPROTO_ESP ||
+		        nexthdr == IPPROTO_ICMPV6 || nexthdr == IPPROTO_NONE) {
 			cur = NULL;
 			break;
 		}

src/extra/ipv6.c int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h, · line 111

Quoted 5 lines
src/extra/ipv6.c +1 -1
@@ -107,7 +108,7 @@ int nfq_ip6_set_transport_header(struct pkt_buff *pktb, struct ip6_hdr *ip6h,
 		} else if (nexthdr == IPPROTO_AH)
 			hdrlen = (ip6_ext->ip6e_len + 2) << 2;
 		else
-			hdrlen = ip6_ext->ip6e_len;
+			hdrlen = (ip6_ext->ip6e_len + 1) << 3;

This looks correct, IPv6 optlen is miscalculated.

Quoted 2 lines

The chunk above to stop the iteration, so I think the chunk that fixes optlen is sufficient to fix the bug.

The optlen fix is not sufficient when a non-existent 'target' is given. For example, if a UDP packet is passed with 'target' IPPROTO_TCP, the loop will go on and attempt to interpret the UDP packet body as another IP extension header. Instead, by exiting the loop also when all extension headers are processed, the function will now return 0 in that case, as documented in the header file.

Thanks

Etan