Skip to contentSkip to conversation

ipv6: fix a potential NULL deref

PATCH In review Cong Wang 3 participants 12 messages Oct 29, 2012 – Oct 30, 2012

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 ipv6: fix a potential NULL deref

In ipv6_del_addr():

                if (rt != net->ipv6.ip6_null_entry &&
                    addrconf_is_prefix_route(rt)) {
                        if (onlink == 0) {
                                ip6_del_rt(rt);
                                rt = NULL;
                        } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
                                rt6_set_expires(rt, expires);
                        }
                }
                dst_release(&rt->dst);

obviously rt could be NULL'd before dst_release(), so we have to check if rt is NULL before calling it.

---

net/ipv6/addrconf.c +2 -1
index 8f0b12a..c467dbb 100644
@@ -951,7 +951,8 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
 				rt6_set_expires(rt, expires);
 			}
 		}
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}
 
 	/* clean up prefsrc entries */
#1 lore raw
Quoted 26 lines · Cong Wang

In ipv6_del_addr():

                if (rt != net->ipv6.ip6_null_entry &&
                    addrconf_is_prefix_route(rt)) {
                        if (onlink == 0) {
                                ip6_del_rt(rt);
                                rt = NULL;
                        } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
                                rt6_set_expires(rt, expires);
                        }
                }
                dst_release(&rt->dst);

obviously rt could be NULL'd before dst_release(), so we have to check if rt is NULL before calling it.

---

net/ipv6/addrconf.c +0 -0
index 8f0b12a..c467dbb 100644

net/ipv6/addrconf.c static void ipv6_del_addr(struct inet6_ifaddr *ifp) · line 957

Quoted 8 lines
net/ipv6/addrconf.c +2 -1
@@ -951,7 +951,8 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
 				rt6_set_expires(rt, expires);
 			}
 		}
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}
 

dst_release() is like kfree(), it accepts a NULL argument.

author
#2 lore raw
Quoted 8 lines · Eric Dumazet

net/ipv6/addrconf.c static void ipv6_del_addr(struct inet6_ifaddr *ifp) · line 956

Quoted 5 lines
net/ipv6/addrconf.c +2 -1
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}

dst_release() is like kfree(), it accepts a NULL argument.

'rt->dst' already dereferences 'rt', no matter dst_release() accepts NULL or not.

#3 lore raw
Quoted 10 lines · Cong Wang
Quoted 8 lines · Eric Dumazet

net/ipv6/addrconf.c static void ipv6_del_addr(struct inet6_ifaddr *ifp) · line 956

Quoted 5 lines
net/ipv6/addrconf.c +2 -1
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}

dst_release() is like kfree(), it accepts a NULL argument.

Quoted 4 lines

'rt->dst' already dereferences 'rt', no matter dst_release() accepts NULL or not.

&rt->dst doesnt dereference rt, you are quite mistaken.

if rt is NULL, &rt->dst is also NULL

author
#4 lore raw
Quoted 11 lines · Eric Dumazet
Quoted 10 lines · Cong Wang
Quoted 8 lines · Eric Dumazet

net/ipv6/addrconf.c static void ipv6_del_addr(struct inet6_ifaddr *ifp) · line 956

Quoted 5 lines
net/ipv6/addrconf.c +2 -1
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}

dst_release() is like kfree(), it accepts a NULL argument.

Quoted 9 lines
Quoted 4 lines

'rt->dst' already dereferences 'rt', no matter dst_release() accepts NULL or not.

&rt->dst doesnt dereference rt, you are quite mistaken.

if rt is NULL, &rt->dst is also NULL

Oh, yeah, gcc should be smart enough to do calculation without deref it given it has the offset and the address. And dst happens to be first field of rt, so offset is 0, &rt->dst should be NULL too if rt is NULL.

But this will be a problem if someone moved dst inside rt, as there is no comment saying dst has to be the first one?

author
#9 lore raw

PATCH 1/1 ipv6: remove another useless NULL check

In reply to Eric Dumazet

When 'rt' is NULL, '&rt->dst' is NULL too because >dst is always the first field of 'rt'. And dst_release accepts NULL.

Cc Eric Dumazet <[email protected]>
Cc David S. Miller <[email protected]>

---

net/ipv6/addrconf.c +1 -2
index 8f0b12a..adeb479 100644
@@ -2027,8 +2027,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
 					      dev, expires, flags);
 		}
-		if (rt)
-			dst_release(&rt->dst);
+		dst_release(&rt->dst);
 	}
 
 	/* Try to figure out our local address for this prefix */
#5 lore raw
In reply to Cong Wang
Quoted 4 lines · Cong Wang

Oh, yeah, gcc should be smart enough to do calculation without deref it given it has the offset and the address. And dst happens to be first field of rt, so offset is 0, &rt->dst should be NULL too if rt is NULL.

There is no dereference, even if gcc was dumb, since dst is an embedded struct, not a pointer to a struct.

This wont change in a near future.

Quoted 2 lines

But this will be a problem if someone moved dst inside rt, as there is no comment saying dst has to be the first one?

I dont think this placement will change in a near future, it would break lot of things.

I guess we can use a BUILD_BUG_ON() instead of a comment that could be ignored.

Note we could move dst in rtable, if we change the NULL test in dst_release with some if ((unsigned long)dst < 4000) condition

I'll send a patch against ip_rt_put()

#10 lore raw

Re: [Patch net-next] ipv6: remove another useless NULL check

In reply to Cong Wang
Quoted 14 lines · Cong Wang

When 'rt' is NULL, '&rt->dst' is NULL too because >dst is always the first field of 'rt'. And dst_release accepts NULL.

Cc Eric Dumazet <[email protected]>
Cc David S. Miller <[email protected]>

---

net/ipv6/addrconf.c +0 -0
index 8f0b12a..adeb479 100644

net/ipv6/addrconf.c void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) · line 2033

Quoted 9 lines
net/ipv6/addrconf.c +1 -2
@@ -2027,8 +2027,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
 					      dev, expires, flags);
 		}
-		if (rt)
-			dst_release(&rt->dst);
+		dst_release(&rt->dst);
 	}
 
 	/* Try to figure out our local address for this prefix */

Could you instead introduce a helper for that, like we have in IPv4.

(ip_rt_put() -> ip6_rt_put() or something...)

See my followup patch for ipv4

author
#11 lore raw

Re: [Patch net-next] ipv6: remove another useless NULL check

Quoted 23 lines · Eric Dumazet
Quoted 22 lines · Cong Wang

When 'rt' is NULL, '&rt->dst' is NULL too because >dst is always the first field of 'rt'. And dst_release accepts NULL.

Cc Eric Dumazet <[email protected]>
Cc David S. Miller <[email protected]>

---

net/ipv6/addrconf.c +1 -2
index 8f0b12a..adeb479 100644
@@ -2027,8 +2027,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
 					      dev, expires, flags);
 		}
-		if (rt)
-			dst_release(&rt->dst);
+		dst_release(&rt->dst);
 	}
 
Quoted 5 lines

net/ipv6/addrconf.c void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) · line 2033

Quoted 1 line
net/ipv6/addrconf.c +0 -0
 	/* Try to figure out our local address for this prefix */

Could you instead introduce a helper for that, like we have in IPv4.

Sure, that is better.

maintainer
#8 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 29 Oct 2012 14:49:26 +0800

Quoted 10 lines · Eric Dumazet
Quoted 8 lines

net/ipv6/addrconf.c static void ipv6_del_addr(struct inet6_ifaddr *ifp) · line 956

Quoted 5 lines
net/ipv6/addrconf.c +2 -1
-		dst_release(&rt->dst);
+		if (rt)
+			dst_release(&rt->dst);
 	}

dst_release() is like kfree(), it accepts a NULL argument.

Quoted 2 lines

'rt->dst' already dereferences 'rt', no matter dst_release() accepts NULL or not.

It's taking the address of a struct member, it's not a dereference.

You know what the difference is right?

maintainer
#6 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 29 Oct 2012 15:25:23 +0800

Quoted 2 lines

But this will be a problem if someone moved dst inside rt, as there is no comment saying dst has to be the first one?

If we move the initial dst member, many things that depend upon it being first will have to change.

Your change was unnecessary and inappropriate, and let's just leave it at that.

Thanks.

author
#7 lore raw
Quoted 11 lines · David Miller

From: Cong Wang <[email protected]>
Date: Mon, 29 Oct 2012 15:25:23 +0800

Quoted 2 lines

But this will be a problem if someone moved dst inside rt, as there is no comment saying dst has to be the first one?

If we move the initial dst member, many things that depend upon it being first will have to change.

Your change was unnecessary and inappropriate, and let's just leave it at that.

Yeah, that is why I sent:
http://marc.info/?l=linux-netdev&m=135150561014916&w=2

:)