Skip to contentSkip to conversation

ipv6: Fix dangling pointer when ipv6 fragment

PATCH Superseded hujunwei 4 participants 7 messages Mar 30, 2019 – Apr 2, 2019

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

hujunwei

author
#0 lore raw

PATCH 1/1 ipv6: Fix dangling pointer when ipv6 fragment

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

Fixes 405c92f7a541 ("ipv6: add defensive check for CHECKSUM_PARTIAL skbs in ip_fragment")

---
 net/ipv6/ip6_output.c | 4 ++++

 1 file changed, 4 insertions(+)
net/ipv6/ip6_output.c +0 -0
index edbd12067170..6db3c60b3b66 100644
@@ -606,12 +606,14 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,

     __be32 frag_id;
     int ptr, offset = 0, err = 0;
     u8 *prevhdr, nexthdr = 0;
+    u8 nexthdr_offset;

     err = ip6_find_1stfragopt(skb, &prevhdr);
     if (err < 0)
         goto fail;
     hlen = err;
     nexthdr = *prevhdr;
+    nexthdr_offset = prevhdr - skb_network_header(skb);

     mtu = ip6_skb_dst_mtu(skb);

@@ -646,6 +648,8 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, (err = skb_checksum_help(skb)))
         goto fail;

+    prevhdr = skb_network_header(skb) + nexthdr_offset;
+
     hroom = LL_RESERVED_SPACE(rt->dst.dev);
     if (skb_has_frag_list(skb)) {
         unsigned int first_len = skb_pagelen(skb);

Signature · 1 line
2.19.1
#1 lore raw
Quoted 10 lines · hujunwei

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Quoted 7 lines

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

Fixes 405c92f7a541 ("ipv6: add defensive check for CHECKSUM_PARTIAL skbs in ip_fragment")

Interesting.

We got a syzbot report yesterday about this issue. (email thread : BUG: unable to handle kernel paging request in ip6_fragment)

<quote>
syzbot found the following crash on:

HEAD commit:    8c838f53 dpaa2-eth: fix race condition with bql frame acco..
git tree:       net
console output: https://syzkaller.appspot.com/x/log.txt?x=12b83a9b200000
kernel config:  https://syzkaller.appspot.com/x/.config?x=f05902bca21d8935
dashboard link: https://syzkaller.appspot.com/bug?extid=e8ce541d095e486074fc
compiler:       gcc (GCC) 9.0.0 20181231 (experimental)

Unfortunately, I don't have any reproducer for this crash yet.

IMPORTANT: if you fix the bug, please add the following tag to the commit:

Reported-by [email protected]

</quote>

Quoted 34 lines

---
 net/ipv6/ip6_output.c | 4 ++++

 1 file changed, 4 insertions(+)
net/ipv6/ip6_output.c +1 -0
index edbd12067170..6db3c60b3b66 100644
@@ -606,12 +606,14 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
      __be32 frag_id;
      int ptr, offset = 0, err = 0;
      u8 *prevhdr, nexthdr = 0;
+    u8 nexthdr_offset;


     err = ip6_find_1stfragopt(skb, &prevhdr); if (err < 0)
         goto fail;
     hlen = err;
     nexthdr = *prevhdr;
+    nexthdr_offset = prevhdr - skb_network_header(skb);
     mtu = ip6_skb_dst_mtu(skb);

net/ipv6/ip6_output.c +0 -0
@@ -646,6 +648,8 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
          (err = skb_checksum_help(skb)))
          goto fail;


+    prevhdr = skb_network_header(skb) + nexthdr_offset; +
     hroom = LL_RESERVED_SPACE(rt->dst.dev);
     if (skb_has_frag_list(skb)) {
         unsigned int first_len = skb_pagelen(skb);

#2 lore raw
Quoted 16 lines · Eric Dumazet
Quoted 13 lines · hujunwei

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

...

Quoted 9 lines

net/ipv6/ip6_output.c int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, · line 609

Quoted 9 lines
net/ipv6/ip6_output.c +1 -0
index edbd12067170..6db3c60b3b66 100644
@@ -606,12 +606,14 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
      __be32 frag_id;
      int ptr, offset = 0, err = 0;
      u8 *prevhdr, nexthdr = 0;
+    u8 nexthdr_offset;

Why u8 here ?

I would use "unsigned int" really.

Quoted 20 lines
Quoted 20 lines


     err = ip6_find_1stfragopt(skb, &prevhdr); if (err < 0)
         goto fail;
     hlen = err;
     nexthdr = *prevhdr;
+    nexthdr_offset = prevhdr - skb_network_header(skb);
     mtu = ip6_skb_dst_mtu(skb);

net/ipv6/ip6_output.c +0 -0
@@ -646,6 +648,8 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
          (err = skb_checksum_help(skb)))
          goto fail;


+    prevhdr = skb_network_header(skb) + nexthdr_offset; +
     hroom = LL_RESERVED_SPACE(rt->dst.dev);
     if (skb_has_frag_list(skb)) {
         unsigned int first_len = skb_pagelen(skb);

author
#3 lore raw

Hi Eri,

Thanks for your suggestion, u8 may not enough when the packet have a lot of exthdr.

I will update the patch in v2, by the way update Report-by tag.

Quoted 54 lines · Eric Dumazet
Quoted 15 lines · Eric Dumazet
Quoted 13 lines · hujunwei

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

...

Quoted 9 lines

net/ipv6/ip6_output.c int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, · line 609

Quoted 9 lines
net/ipv6/ip6_output.c +1 -0
index edbd12067170..6db3c60b3b66 100644
@@ -606,12 +606,14 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
      __be32 frag_id;
      int ptr, offset = 0, err = 0;
      u8 *prevhdr, nexthdr = 0;
+    u8 nexthdr_offset;

Why u8 here ?

I would use "unsigned int" really.

Quoted 12 lines
Quoted 12 lines


     err = ip6_find_1stfragopt(skb, &prevhdr); if (err < 0)
         goto fail;
     hlen = err;
     nexthdr = *prevhdr;
+    nexthdr_offset = prevhdr - skb_network_header(skb);
     mtu = ip6_skb_dst_mtu(skb);

net/ipv6/ip6_output.c +0 -0
@@ -646,6 +648,8 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
          (err = skb_checksum_help(skb)))
Quoted 8 lines
Quoted 8 lines
net/ipv6/ip6_output.c +0 -0
          goto fail;


+    prevhdr = skb_network_header(skb) + nexthdr_offset; +
     hroom = LL_RESERVED_SPACE(rt->dst.dev);
     if (skb_has_frag_list(skb)) {
         unsigned int first_len = skb_pagelen(skb);

.

author
#4 lore raw

PATCH v2 1/1 ipv6: Fix dangling pointer when ipv6 fragment

In reply to hujunwei

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

Fixes 405c92f7a541 ("ipv6: add defensive check for CHECKSUM_PARTIAL skbs in ip_fragment")
Reported-by [email protected]

---
V1->V2:
- change nexthdr_offset to unsigned int
- add Reported-by: [email protected]

 net/ipv6/ip6_output.c | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv6/ip6_output.c +0 -0
index edbd12067170..e51f3c648b09 100644
@@ -601,7 +601,7 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,

                 inet6_sk(skb->sk) : NULL;
     struct ipv6hdr *tmp_hdr;
     struct frag_hdr *fh;
-    unsigned int mtu, hlen, left, len;
+    unsigned int mtu, hlen, left, len, nexthdr_offset;
     int hroom, troom;
     __be32 frag_id;
     int ptr, offset = 0, err = 0;
@@ -612,6 +612,7 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, goto fail;
     hlen = err;
     nexthdr = *prevhdr;
+    nexthdr_offset = prevhdr - skb_network_header(skb);

     mtu = ip6_skb_dst_mtu(skb);

@@ -646,6 +647,7 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, (err = skb_checksum_help(skb)))
         goto fail;

+    prevhdr = skb_network_header(skb) + nexthdr_offset;
     hroom = LL_RESERVED_SPACE(rt->dst.dev);
     if (skb_has_frag_list(skb)) {
         unsigned int first_len = skb_pagelen(skb);

Signature · 1 line
2.19.1
Acked-by on v2 1/1
#5 lore raw
Quoted 10 lines · hujunwei

From: Junwei Hu <[email protected]>

At the beginning of ip6_fragment func, the prevhdr pointer is obtained in the ip6_find_1stfragopt func.
However, all the pointers pointing into skb header may change when calling skb_checksum_help func with skb->ip_summed = CHECKSUM_PARTIAL condition.
The prevhdr pointe will be dangling if it is not reloaded after calling __skb_linearize func in skb_checksum_help func.

Quoted 2 lines

Here, I add a variable, nexthdr_offset, to evaluate the offset, which does not changes even after calling __skb_linearize func.

maintainer
#6 lore raw
In reply to hujunwei

From: hujunwei <[email protected]>
Date: Sun, 31 Mar 2019 17:04:29 +0800

net/ipv6/ip6_output.c int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, · line 604

Quoted 9 lines
net/ipv6/ip6_output.c +0 -1
index edbd12067170..e51f3c648b09 100644
@@ -601,7 +601,7 @@ int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
                  inet6_sk(skb->sk) : NULL;
      struct ipv6hdr *tmp_hdr;
      struct frag_hdr *fh;
-    unsigned int mtu, hlen, left, len;

Your patch was corrupted by your email client.

Please fix this, email a test patch to yourself, and only when you can successfully apply that test patch should you repost this to the mailing list.

Thank you.