Skip to contentSkip to conversation

ipv6: Prevent overrun when parsing v6 header options

PATCH In review Young Xiao 3 participants 11 messages May 30, 2019 – Jun 4, 2019

Subsystems (2)
lore
Share & tools
lore

Young Xiao

author
#0 lore raw

PATCH 1/1 ipv6: Prevent overrun when parsing v6 header options

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +14 -10
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 			       u8 **nexthdr)
 {
 	u16 offset = sizeof(struct ipv6hdr);
-	struct ipv6_opt_hdr *exthdr =
-				   (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
 	const unsigned char *nh = skb_network_header(skb);
 	unsigned int packet_len = skb_tail_pointer(skb) -
 		skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
 	*nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-	while (offset + 1 <= packet_len) {
+	while (offset <= packet_len) {
+		struct ipv6_opt_hdr *exthdr;
 
 		switch (**nexthdr) {
 		case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 			return offset;
 		}
 
+		if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+			return -EINVAL;
+
+		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 		offset += ipv6_optlen(exthdr);
 		*nexthdr = &exthdr->nexthdr;
-		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 	}
 
-	return offset;
+	return -EINVAL;
 }
 
 static int mip6_destopt_init_state(struct xfrm_state *x)
@@ -399,8 +401,6 @@ static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
 			     u8 **nexthdr)
 {
 	u16 offset = sizeof(struct ipv6hdr);
-	struct ipv6_opt_hdr *exthdr =
-				   (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
 	const unsigned char *nh = skb_network_header(skb);
 	unsigned int packet_len = skb_tail_pointer(skb) -
 		skb_network_header(skb);
@@ -408,7 +408,8 @@ static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
 
 	*nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-	while (offset + 1 <= packet_len) {
+	while (offset <= packet_len) {
+		struct ipv6_opt_hdr *exthdr;
 
 		switch (**nexthdr) {
 		case NEXTHDR_HOP:
@@ -434,12 +435,15 @@ static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
 			return offset;
 		}
 
+		if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+			return -EINVAL;
+
+		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 		offset += ipv6_optlen(exthdr);
 		*nexthdr = &exthdr->nexthdr;
-		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 	}
 
-	return offset;
+	return -EINVAL;
 }
 
 static int mip6_rthdr_init_state(struct xfrm_state *x)
Signature · 1 line
2.7.4
#1 lore raw
Quoted 43 lines · Young Xiao

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +5 -3
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 			       u8 **nexthdr)
 {
 	u16 offset = sizeof(struct ipv6hdr);
-	struct ipv6_opt_hdr *exthdr =
-				   (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
 	const unsigned char *nh = skb_network_header(skb);
 	unsigned int packet_len = skb_tail_pointer(skb) -
 		skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
 	*nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-	while (offset + 1 <= packet_len) {
+	while (offset <= packet_len) {
+		struct ipv6_opt_hdr *exthdr;
 
 		switch (**nexthdr) {
 		case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 			return offset;
 		}
 
+		if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+			return -EINVAL;
+

net/ipv6/mip6.c static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, · line 311

Quoted 10 lines
net/ipv6/mip6.c +2 -2
+		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 		offset += ipv6_optlen(exthdr);
 		*nexthdr = &exthdr->nexthdr;
-		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 	}
 
-	return offset;
+	return -EINVAL;
 }
 

Ok, but have you checked that callers have been fixed ?

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

Thanks.

author
#2 lore raw
Quoted 53 lines · Eric Dumazet
Quoted 49 lines · Young Xiao

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +6 -4
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                             u8 **nexthdr)
 {
      u16 offset = sizeof(struct ipv6hdr);
-     struct ipv6_opt_hdr *exthdr =
-                                (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
      const unsigned char *nh = skb_network_header(skb);
      unsigned int packet_len = skb_tail_pointer(skb) -
              skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
      *nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-     while (offset + 1 <= packet_len) {
+     while (offset <= packet_len) {
+             struct ipv6_opt_hdr *exthdr;
 
              switch (**nexthdr) {
              case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                      return offset;
              }
 
+             if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+                     return -EINVAL;
+
+             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
              offset += ipv6_optlen(exthdr);
              *nexthdr = &exthdr->nexthdr;
-             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
      }
 
Quoted 7 lines

net/ipv6/mip6.c static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, · line 311

Quoted 4 lines
net/ipv6/mip6.c +1 -1
-     return offset;
+     return -EINVAL;
 }
 

Ok, but have you checked that callers have been fixed ?

I've checked the callers. There are two callers:
xfrm6_transport_output() and xfrm6_ro_output(). There are checks in both function.

------------------------------------------------------------------------------

        hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
        if (hdr_len < 0)
                return hdr_len;

------------------------------------------------------------------------------

Quoted 3 lines

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

I can not understand what you mean about this comment. Could you explain it in more detail.

Quoted 5 lines

Thanks.

#3 lore raw
Quoted 70 lines · Yang Xiao
Quoted 53 lines · Eric Dumazet
Quoted 49 lines · Young Xiao

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +6 -4
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                             u8 **nexthdr)
 {
      u16 offset = sizeof(struct ipv6hdr);
-     struct ipv6_opt_hdr *exthdr =
-                                (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
      const unsigned char *nh = skb_network_header(skb);
      unsigned int packet_len = skb_tail_pointer(skb) -
              skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
      *nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-     while (offset + 1 <= packet_len) {
+     while (offset <= packet_len) {
+             struct ipv6_opt_hdr *exthdr;
 
              switch (**nexthdr) {
              case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                      return offset;
              }
 
+             if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+                     return -EINVAL;
+
+             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
              offset += ipv6_optlen(exthdr);
              *nexthdr = &exthdr->nexthdr;
-             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
      }
 
Quoted 7 lines

net/ipv6/mip6.c static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, · line 311

Quoted 4 lines
net/ipv6/mip6.c +1 -1
-     return offset;
+     return -EINVAL;
 }
 

Ok, but have you checked that callers have been fixed ?

I've checked the callers. There are two callers:
xfrm6_transport_output() and xfrm6_ro_output(). There are checks in both function.

------------------------------------------------------------------------------

        hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
        if (hdr_len < 0)
                return hdr_len;
Quoted 6 lines

------------------------------------------------------------------------------

Quoted 3 lines

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

I can not understand what you mean about this comment. Could you explain it in more detail.

If we had a problem, then the memmove(ipv6_hdr(skb), iph, hdr_len);
in xfrm6_transport_output() would be buggy, since iph could also point to freed memory.

author
#4 lore raw

Sorry, I don't get your point. Why is xfrm6_transport_output() buggy? The point is that there would be out-of-bound access in mip6_destopt_offset() and mip6_destopt_offset(), since there is no sanity check for offset.

There is chance that offset + sizeof(struct ipv6_opt_hdr) > packet_len.

As described in CVE-2017-9074: "The IPv6 fragmentation implementation in the Linux kernel through 4.11.1 does not consider that the nexthdr field may be associated with an invalid option, which allows local users to cause a denial of service (out-of-bounds read and BUG)".

At the same time, there are bugs in mip6_destopt_offset() and mip6_destopt_offset(), which is similar to CVE-2017-7542.

Quoted 87 lines · Eric Dumazet
Quoted 70 lines · Yang Xiao
Quoted 53 lines · Eric Dumazet
Quoted 49 lines · Young Xiao

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +6 -4
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                             u8 **nexthdr)
 {
      u16 offset = sizeof(struct ipv6hdr);
-     struct ipv6_opt_hdr *exthdr =
-                                (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
      const unsigned char *nh = skb_network_header(skb);
      unsigned int packet_len = skb_tail_pointer(skb) -
              skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
      *nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-     while (offset + 1 <= packet_len) {
+     while (offset <= packet_len) {
+             struct ipv6_opt_hdr *exthdr;
 
              switch (**nexthdr) {
              case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                      return offset;
              }
 
+             if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+                     return -EINVAL;
+
+             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
              offset += ipv6_optlen(exthdr);
              *nexthdr = &exthdr->nexthdr;
-             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
      }
 
Quoted 7 lines

net/ipv6/mip6.c static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, · line 311

Quoted 4 lines
net/ipv6/mip6.c +1 -1
-     return offset;
+     return -EINVAL;
 }
 

Ok, but have you checked that callers have been fixed ?

I've checked the callers. There are two callers:
xfrm6_transport_output() and xfrm6_ro_output(). There are checks in both function.

------------------------------------------------------------------------------

        hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
        if (hdr_len < 0)
                return hdr_len;
Quoted 6 lines

------------------------------------------------------------------------------

Quoted 3 lines

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

I can not understand what you mean about this comment. Could you explain it in more detail.

If we had a problem, then the memmove(ipv6_hdr(skb), iph, hdr_len);
in xfrm6_transport_output() would be buggy, since iph could also point to freed memory.

Signature · 4 lines
Best regards!

Young
-----------------------------------------------------------
#5 lore raw
Quoted 7 lines · Yang Xiao

Sorry, I don't get your point. Why is xfrm6_transport_output() buggy? The point is that there would be out-of-bound access in mip6_destopt_offset() and mip6_destopt_offset(), since there is no sanity check for offset.

There is chance that offset + sizeof(struct ipv6_opt_hdr) > packet_len.

Quoted 8 lines

As described in CVE-2017-9074: "The IPv6 fragmentation implementation in the Linux kernel through 4.11.1 does not consider that the nexthdr field may be associated with an invalid option, which allows local users to cause a denial of service (out-of-bounds read and BUG)".

At the same time, there are bugs in mip6_destopt_offset() and mip6_destopt_offset(), which is similar to CVE-2017-7542.

I suggest that you stop the nonsense.

As explained by Herbert, your patch is not needed at all.

If this was needed, then we would have to fix the callers, which you did not.

Citing arbitrary CVE is of no use, we do not copy/paste patches or CVE.

Quoted 90 lines · Eric Dumazet
Quoted 87 lines · Yang Xiao
Quoted 70 lines · Eric Dumazet
Quoted 53 lines · Young Xiao
Quoted 49 lines

The fragmentation code tries to parse the header options in order to figure out where to insert the fragment option. Since nexthdr points to an invalid option, the calculation of the size of the network header can made to be much larger than the linear section of the skb and data is read outside of it.

This vulnerability is similar to CVE-2017-9074.

---
 net/ipv6/mip6.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
net/ipv6/mip6.c +6 -4
index 64f0f7b..30ed1c5 100644
@@ -263,8 +263,6 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                             u8 **nexthdr)
 {
      u16 offset = sizeof(struct ipv6hdr);
-     struct ipv6_opt_hdr *exthdr =
-                                (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
      const unsigned char *nh = skb_network_header(skb);
      unsigned int packet_len = skb_tail_pointer(skb) -
              skb_network_header(skb);
@@ -272,7 +270,8 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
 
      *nexthdr = &ipv6_hdr(skb)->nexthdr;
 
-     while (offset + 1 <= packet_len) {
+     while (offset <= packet_len) {
+             struct ipv6_opt_hdr *exthdr;
 
              switch (**nexthdr) {
              case NEXTHDR_HOP:
@@ -299,12 +298,15 @@ static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
                      return offset;
              }
 
+             if (offset + sizeof(struct ipv6_opt_hdr) > packet_len)
+                     return -EINVAL;
+
+             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
              offset += ipv6_optlen(exthdr);
              *nexthdr = &exthdr->nexthdr;
-             exthdr = (struct ipv6_opt_hdr *)(nh + offset);
      }
 
Quoted 7 lines

net/ipv6/mip6.c static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb, · line 311

Quoted 4 lines
net/ipv6/mip6.c +1 -1
-     return offset;
+     return -EINVAL;
 }
 

Ok, but have you checked that callers have been fixed ?

I've checked the callers. There are two callers:
xfrm6_transport_output() and xfrm6_ro_output(). There are checks in both function.

------------------------------------------------------------------------------

        hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
        if (hdr_len < 0)
                return hdr_len;
Quoted 6 lines

------------------------------------------------------------------------------

Quoted 3 lines

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

I can not understand what you mean about this comment. Could you explain it in more detail.

If we had a problem, then the memmove(ipv6_hdr(skb), iph, hdr_len);
in xfrm6_transport_output() would be buggy, since iph could also point to freed memory.

#6 lore raw
In reply to Eric Dumazet
Quoted 3 lines · Eric Dumazet

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

The headers that it's moving should be linearised. Is there something else I'm missing?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#7 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Eric Dumazet

xfrm6_transport_output() seems buggy as well,
unless the skbs are linearized before entering these functions ?

The headers that it's moving should be linearised. Is there something else I'm missing?

What do you mean by should ?

Are they currently already linearized before the function is called, or is it missing and a bug needs to be fixed ?

#8 lore raw
Quoted 5 lines · Eric Dumazet

What do you mean by should ?

Are they currently already linearized before the function is called, or is it missing and a bug needs to be fixed ?

AFAICS this is the code-path for locally generated outbound packets. Under what circumstances can the IPv6 header be not in the head?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#9 lore raw
Quoted 11 lines · Herbert Xu
Quoted 5 lines · Eric Dumazet

What do you mean by should ?

Are they currently already linearized before the function is called, or is it missing and a bug needs to be fixed ?

AFAICS this is the code-path for locally generated outbound packets. Under what circumstances can the IPv6 header be not in the head?

I guess this means we had yet another random submission from Young Xiao :/

Thanks.

author
#10 lore raw
Quoted 11 lines · Eric Dumazet
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Eric Dumazet

What do you mean by should ?

Are they currently already linearized before the function is called, or is it missing and a bug needs to be fixed ?

Quoted 6 lines
Quoted 4 lines

AFAICS this is the code-path for locally generated outbound packets. Under what circumstances can the IPv6 header be not in the head?

I guess this means we had yet another random submission from Young Xiao :/

Excuse me, what do you mean about random submission from Young? A month ago, I submitted the patch, and I was told that the format should be correct.
Then, I resubmitted again.

Quoted 3 lines

Thanks.