Skip to contentSkip to conversation

net: ipv6: Fix a possible null-pointer dereference in ip6_xmit()

PATCH In review Jia-Ju Bai 2 participants 2 messages Jul 26, 2019

Subsystems (3)
lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/2 net: ipv6: Fix a possible null-pointer dereference in ip6_xmit()

In ip6_xmit(), there is an if statement on line 245 to check whether np is NULL:

    if (np)

When np is NULL, it is used on line 251:

    ip6_autoflowlabel(net, np)
        if (!np->autoflowlabel_set)

Thus, a possible null-pointer dereference may occur.

To fix this bug, np is checked before calling
ip6_autoflowlabel(net,np).

This bug is found by a static analysis tool STCheck written by us.

---
 net/ipv6/ip6_output.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/ipv6/ip6_output.c +4 -2
index 8e49fd62eea9..07db5ab6e970 100644
@@ -247,8 +247,10 @@ int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
 	if (hlimit < 0)
 		hlimit = ip6_dst_hoplimit(dst);
 
-	ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
-				ip6_autoflowlabel(net, np), fl6));
+	if (np) {
+		ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
+					ip6_autoflowlabel(net, np), fl6));
+	}
 
 	hdr->payload_len = htons(seg_len);
 	hdr->nexthdr = proto;
Signature · 1 line
2.17.0
#1 lore raw
Quoted 26 lines · Jia-Ju Bai

In ip6_xmit(), there is an if statement on line 245 to check whether np is NULL:

    if (np)

When np is NULL, it is used on line 251:

    ip6_autoflowlabel(net, np)
        if (!np->autoflowlabel_set)

Thus, a possible null-pointer dereference may occur.

To fix this bug, np is checked before calling
ip6_autoflowlabel(net,np).

This bug is found by a static analysis tool STCheck written by us.

---
 net/ipv6/ip6_output.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/ipv6/ip6_output.c +0 -0
index 8e49fd62eea9..07db5ab6e970 100644

net/ipv6/ip6_output.c int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6, · line 253

Quoted 9 lines
net/ipv6/ip6_output.c +4 -2
@@ -247,8 +247,10 @@ int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
        if (hlimit < 0)
                hlimit = ip6_dst_hoplimit(dst);
 
-       ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
-                               ip6_autoflowlabel(net, np), fl6));
+       if (np) {
+               ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
+                                       ip6_autoflowlabel(net, np), fl6));
+       }

I don't know when np can be NULL in ip6_xmit. But if so, must still setup the ipv6 header.

A more narrow change would be in ip6_autoflowlabel

- if (!np->autoflowlabel_set)
+ if (!np || !np->autoflowlabel_set)