netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [Patch net-next] ipv6: fix a potential NULL deref
@ 2012-10-29  3:50 Cong Wang
  2012-10-29  6:10 ` Eric Dumazet
  0 siblings, 1 reply; 12+ messages in thread
From: Cong Wang @ 2012-10-29  3:50 UTC (permalink / raw)
  To: netdev; +Cc: David S. Miller, 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.

Reported-by: Fengguang Wu <fengguang.wu@intel.com>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Cong Wang <amwang@redhat.com>

---
diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index 8f0b12a..c467dbb 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -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 */

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  3:50 [Patch net-next] ipv6: fix a potential NULL deref Cong Wang
@ 2012-10-29  6:10 ` Eric Dumazet
  2012-10-29  6:49   ` Cong Wang
  2012-10-29  8:16   ` [Patch net-next] ipv6: remove another useless NULL check Cong Wang
  0 siblings, 2 replies; 12+ messages in thread
From: Eric Dumazet @ 2012-10-29  6:10 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 11:50 +0800, Cong Wang wrote:
> 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.
> 
> Reported-by: Fengguang Wu <fengguang.wu@intel.com>
> Cc: David S. Miller <davem@davemloft.net>
> Signed-off-by: Cong Wang <amwang@redhat.com>
> 
> ---
> diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
> index 8f0b12a..c467dbb 100644
> --- a/net/ipv6/addrconf.c
> +++ b/net/ipv6/addrconf.c
> @@ -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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  6:10 ` Eric Dumazet
@ 2012-10-29  6:49   ` Cong Wang
  2012-10-29  7:05     ` Eric Dumazet
  2012-10-29 17:22     ` David Miller
  2012-10-29  8:16   ` [Patch net-next] ipv6: remove another useless NULL check Cong Wang
  1 sibling, 2 replies; 12+ messages in thread
From: Cong Wang @ 2012-10-29  6:49 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 07:10 +0100, Eric Dumazet wrote:
> > -		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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  6:49   ` Cong Wang
@ 2012-10-29  7:05     ` Eric Dumazet
  2012-10-29  7:25       ` Cong Wang
  2012-10-29 17:22     ` David Miller
  1 sibling, 1 reply; 12+ messages in thread
From: Eric Dumazet @ 2012-10-29  7:05 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 14:49 +0800, Cong Wang wrote:
> On Mon, 2012-10-29 at 07:10 +0100, Eric Dumazet wrote:
> > > -		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.
> 
> 

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

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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  7:05     ` Eric Dumazet
@ 2012-10-29  7:25       ` Cong Wang
  2012-10-29  8:25         ` Eric Dumazet
  2012-10-29 17:29         ` David Miller
  0 siblings, 2 replies; 12+ messages in thread
From: Cong Wang @ 2012-10-29  7:25 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 08:05 +0100, Eric Dumazet wrote:
> On Mon, 2012-10-29 at 14:49 +0800, Cong Wang wrote:
> > On Mon, 2012-10-29 at 07:10 +0100, Eric Dumazet wrote:
> > > > -		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.
> > 
> > 
> 
> &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?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Patch net-next] ipv6: remove another useless NULL check
  2012-10-29  6:10 ` Eric Dumazet
  2012-10-29  6:49   ` Cong Wang
@ 2012-10-29  8:16   ` Cong Wang
  2012-10-29  8:33     ` Eric Dumazet
  1 sibling, 1 reply; 12+ messages in thread
From: Cong Wang @ 2012-10-29  8:16 UTC (permalink / raw)
  To: netdev; +Cc: Eric Dumazet, David S. Miller, 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 <eric.dumazet@gmail.com>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Cong Wang <amwang@redhat.com>

---
diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index 8f0b12a..adeb479 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -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 */

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  7:25       ` Cong Wang
@ 2012-10-29  8:25         ` Eric Dumazet
  2012-10-29 17:29         ` David Miller
  1 sibling, 0 replies; 12+ messages in thread
From: Eric Dumazet @ 2012-10-29  8:25 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 15:25 +0800, Cong Wang wrote:

> 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.

> 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()

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: remove another useless NULL check
  2012-10-29  8:16   ` [Patch net-next] ipv6: remove another useless NULL check Cong Wang
@ 2012-10-29  8:33     ` Eric Dumazet
  2012-10-29  9:08       ` Cong Wang
  0 siblings, 1 reply; 12+ messages in thread
From: Eric Dumazet @ 2012-10-29  8:33 UTC (permalink / raw)
  To: Cong Wang; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 16:16 +0800, Cong Wang wrote:
> 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 <eric.dumazet@gmail.com>
> Cc: David S. Miller <davem@davemloft.net>
> Signed-off-by: Cong Wang <amwang@redhat.com>
> 
> ---
> diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
> index 8f0b12a..adeb479 100644
> --- a/net/ipv6/addrconf.c
> +++ b/net/ipv6/addrconf.c
> @@ -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

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: remove another useless NULL check
  2012-10-29  8:33     ` Eric Dumazet
@ 2012-10-29  9:08       ` Cong Wang
  0 siblings, 0 replies; 12+ messages in thread
From: Cong Wang @ 2012-10-29  9:08 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: netdev, David S. Miller

On Mon, 2012-10-29 at 09:33 +0100, Eric Dumazet wrote:
> On Mon, 2012-10-29 at 16:16 +0800, Cong Wang wrote:
> > 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 <eric.dumazet@gmail.com>
> > Cc: David S. Miller <davem@davemloft.net>
> > Signed-off-by: Cong Wang <amwang@redhat.com>
> > 
> > ---
> > diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
> > index 8f0b12a..adeb479 100644
> > --- a/net/ipv6/addrconf.c
> > +++ b/net/ipv6/addrconf.c
> > @@ -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.

Sure, that is better.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  6:49   ` Cong Wang
  2012-10-29  7:05     ` Eric Dumazet
@ 2012-10-29 17:22     ` David Miller
  1 sibling, 0 replies; 12+ messages in thread
From: David Miller @ 2012-10-29 17:22 UTC (permalink / raw)
  To: amwang; +Cc: eric.dumazet, netdev

From: Cong Wang <amwang@redhat.com>
Date: Mon, 29 Oct 2012 14:49:26 +0800

> On Mon, 2012-10-29 at 07:10 +0100, Eric Dumazet wrote:
>> > -		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.

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

You know what the difference is right?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29  7:25       ` Cong Wang
  2012-10-29  8:25         ` Eric Dumazet
@ 2012-10-29 17:29         ` David Miller
  2012-10-30  1:44           ` Cong Wang
  1 sibling, 1 reply; 12+ messages in thread
From: David Miller @ 2012-10-29 17:29 UTC (permalink / raw)
  To: amwang; +Cc: eric.dumazet, netdev

From: Cong Wang <amwang@redhat.com>
Date: Mon, 29 Oct 2012 15:25:23 +0800

> 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.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Patch net-next] ipv6: fix a potential NULL deref
  2012-10-29 17:29         ` David Miller
@ 2012-10-30  1:44           ` Cong Wang
  0 siblings, 0 replies; 12+ messages in thread
From: Cong Wang @ 2012-10-30  1:44 UTC (permalink / raw)
  To: David Miller; +Cc: eric.dumazet, netdev

On Mon, 2012-10-29 at 13:29 -0400, David Miller wrote:
> From: Cong Wang <amwang@redhat.com>
> Date: Mon, 29 Oct 2012 15:25:23 +0800
> 
> > 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

:)

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-10-30  1:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-29  3:50 [Patch net-next] ipv6: fix a potential NULL deref Cong Wang
2012-10-29  6:10 ` Eric Dumazet
2012-10-29  6:49   ` Cong Wang
2012-10-29  7:05     ` Eric Dumazet
2012-10-29  7:25       ` Cong Wang
2012-10-29  8:25         ` Eric Dumazet
2012-10-29 17:29         ` David Miller
2012-10-30  1:44           ` Cong Wang
2012-10-29 17:22     ` David Miller
2012-10-29  8:16   ` [Patch net-next] ipv6: remove another useless NULL check Cong Wang
2012-10-29  8:33     ` Eric Dumazet
2012-10-29  9:08       ` Cong Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).