netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: check for NULL net_device in FIB tables
@ 2016-07-04 12:47 Vegard Nossum
  2016-07-04 13:50 ` Vegard Nossum
  2016-07-04 19:45 ` Julian Anastasov
  0 siblings, 2 replies; 7+ messages in thread
From: Vegard Nossum @ 2016-07-04 12:47 UTC (permalink / raw)
  To: Andy Gospodarek, davem; +Cc: netdev, Vegard Nossum, Dinesh Dutt, Scott Feldman

struct fib_nh->nh_dev can be NULL, so we should check it before calling
__in_dev_get_rcu on it.

Multiple places seem to want this (and check the return value), so we can
add a convenience wrapper for this.

This fixes a crash in AF_NETLINK sendmsg().

Please double check that I caught all the callers that need the NULL
guard.

Fixes: 0eeb075fad73 ("net: ipv4 sysctl option to ignore routes when nexthop link is down")
Cc: Andy Gospodarek <gospo@cumulusnetworks.com>
Cc: Dinesh Dutt <ddutt@cumulusnetworks.com>
Cc: Scott Feldman <sfeldma@gmail.com>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
---
 include/linux/inetdevice.h | 7 +++++++
 net/ipv4/fib_semantics.c   | 8 ++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/include/linux/inetdevice.h b/include/linux/inetdevice.h
index ee971f3..2d8bea1 100644
--- a/include/linux/inetdevice.h
+++ b/include/linux/inetdevice.h
@@ -222,6 +222,13 @@ static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
 	return rtnl_dereference(dev->ip_ptr);
 }
 
+static inline struct in_device *in_dev_get_rtnl(const struct net_device *dev)
+{
+	if (!dev)
+		return NULL;
+	return __in_dev_get_rtnl(dev);
+}
+
 static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
 {
 	struct in_device *in_dev = __in_dev_get_rcu(dev);
diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index d09173b..8289799 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -545,7 +545,7 @@ static void fib_rebalance(struct fib_info *fi)
 		if (nh->nh_flags & RTNH_F_DEAD)
 			continue;
 
-		in_dev = __in_dev_get_rtnl(nh->nh_dev);
+		in_dev = in_dev_get_rtnl(nh->nh_dev);
 
 		if (in_dev &&
 		    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
@@ -559,7 +559,7 @@ static void fib_rebalance(struct fib_info *fi)
 	change_nexthops(fi) {
 		int upper_bound;
 
-		in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
+		in_dev = in_dev_get_rtnl(nexthop_nh->nh_dev);
 
 		if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
 			upper_bound = -1;
@@ -1261,7 +1261,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
 		    nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
 			goto nla_put_failure;
 		if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
-			in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
+			in_dev = in_dev_get_rtnl(fi->fib_nh->nh_dev);
 			if (in_dev &&
 			    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
 				rtm->rtm_flags |= RTNH_F_DEAD;
@@ -1292,7 +1292,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
 
 			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
 			if (nh->nh_flags & RTNH_F_LINKDOWN) {
-				in_dev = __in_dev_get_rtnl(nh->nh_dev);
+				in_dev = in_dev_get_rtnl(nh->nh_dev);
 				if (in_dev &&
 				    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
 					rtnh->rtnh_flags |= RTNH_F_DEAD;
-- 
1.9.1

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 12:47 [PATCH] net: check for NULL net_device in FIB tables Vegard Nossum
@ 2016-07-04 13:50 ` Vegard Nossum
  2016-07-04 19:45 ` Julian Anastasov
  1 sibling, 0 replies; 7+ messages in thread
From: Vegard Nossum @ 2016-07-04 13:50 UTC (permalink / raw)
  To: Andy Gospodarek, davem; +Cc: netdev, Dinesh Dutt, Scott Feldman

On 07/04/2016 02:47 PM, Vegard Nossum wrote:
> struct fib_nh->nh_dev can be NULL, so we should check it before calling
> __in_dev_get_rcu on it.

That should say __in_dev_get_rtnl(), obviously.

>
> Multiple places seem to want this (and check the return value), so we can
> add a convenience wrapper for this.
>
> This fixes a crash in AF_NETLINK sendmsg().
>
> Please double check that I caught all the callers that need the NULL
> guard.
>
> Fixes: 0eeb075fad73 ("net: ipv4 sysctl option to ignore routes when nexthop link is down")
> Cc: Andy Gospodarek <gospo@cumulusnetworks.com>
> Cc: Dinesh Dutt <ddutt@cumulusnetworks.com>
> Cc: Scott Feldman <sfeldma@gmail.com>
> Cc: David S. Miller <davem@davemloft.net>
> Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>

I guess we could also add:

Cc: stable@vger.kernel.org


Vegard

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 12:47 [PATCH] net: check for NULL net_device in FIB tables Vegard Nossum
  2016-07-04 13:50 ` Vegard Nossum
@ 2016-07-04 19:45 ` Julian Anastasov
  2016-07-04 19:56   ` Vegard Nossum
  1 sibling, 1 reply; 7+ messages in thread
From: Julian Anastasov @ 2016-07-04 19:45 UTC (permalink / raw)
  To: Vegard Nossum; +Cc: Andy Gospodarek, davem, netdev, Dinesh Dutt, Scott Feldman


	Hello,

On Mon, 4 Jul 2016, Vegard Nossum wrote:

> struct fib_nh->nh_dev can be NULL, so we should check it before calling
> __in_dev_get_rcu on it.
> 
> Multiple places seem to want this (and check the return value), so we can
> add a convenience wrapper for this.
> 
> This fixes a crash in AF_NETLINK sendmsg().
> 
> Please double check that I caught all the callers that need the NULL
> guard.

	What kind of configuration causes such crash?

> Fixes: 0eeb075fad73 ("net: ipv4 sysctl option to ignore routes when nexthop link is down")
> Cc: Andy Gospodarek <gospo@cumulusnetworks.com>
> Cc: Dinesh Dutt <ddutt@cumulusnetworks.com>
> Cc: Scott Feldman <sfeldma@gmail.com>
> Cc: David S. Miller <davem@davemloft.net>
> Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
> ---
>  include/linux/inetdevice.h | 7 +++++++
>  net/ipv4/fib_semantics.c   | 8 ++++----
>  2 files changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/include/linux/inetdevice.h b/include/linux/inetdevice.h
> index ee971f3..2d8bea1 100644
> --- a/include/linux/inetdevice.h
> +++ b/include/linux/inetdevice.h
> @@ -222,6 +222,13 @@ static inline struct in_device *__in_dev_get_rtnl(const struct net_device *dev)
>  	return rtnl_dereference(dev->ip_ptr);
>  }
>  
> +static inline struct in_device *in_dev_get_rtnl(const struct net_device *dev)
> +{
> +	if (!dev)
> +		return NULL;
> +	return __in_dev_get_rtnl(dev);
> +}
> +
>  static inline struct neigh_parms *__in_dev_arp_parms_get_rcu(const struct net_device *dev)
>  {
>  	struct in_device *in_dev = __in_dev_get_rcu(dev);
> diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
> index d09173b..8289799 100644
> --- a/net/ipv4/fib_semantics.c
> +++ b/net/ipv4/fib_semantics.c
> @@ -545,7 +545,7 @@ static void fib_rebalance(struct fib_info *fi)
>  		if (nh->nh_flags & RTNH_F_DEAD)
>  			continue;
>  
> -		in_dev = __in_dev_get_rtnl(nh->nh_dev);
> +		in_dev = in_dev_get_rtnl(nh->nh_dev);

	fib_rebalance can not crash because for multipath
routes (fib_nhs > 1) all nexthops should have valid nh_dev.

>  
>  		if (in_dev &&
>  		    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
> @@ -559,7 +559,7 @@ static void fib_rebalance(struct fib_info *fi)
>  	change_nexthops(fi) {
>  		int upper_bound;
>  
> -		in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
> +		in_dev = in_dev_get_rtnl(nexthop_nh->nh_dev);
>  
>  		if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
>  			upper_bound = -1;
> @@ -1261,7 +1261,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
>  		    nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
>  			goto nla_put_failure;
>  		if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
> -			in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
> +			in_dev = in_dev_get_rtnl(fi->fib_nh->nh_dev);

	Looks like this is the only place that can crash,
adding extra cycles to the other places is not good. And this
can happen only because fib_create_info() allows RTNH_F_LINKDOWN
to come for routes with error. May be fc_flags should be masked
there? Or there is another place that sets the flag when nh_dev
is NULL?

>  			if (in_dev &&
>  			    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
>  				rtm->rtm_flags |= RTNH_F_DEAD;
> @@ -1292,7 +1292,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
>  
>  			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
>  			if (nh->nh_flags & RTNH_F_LINKDOWN) {
> -				in_dev = __in_dev_get_rtnl(nh->nh_dev);
> +				in_dev = in_dev_get_rtnl(nh->nh_dev);

	Not needed because fib_nhs > 1

>  				if (in_dev &&
>  				    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
>  					rtnh->rtnh_flags |= RTNH_F_DEAD;
> -- 

Regards

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 19:45 ` Julian Anastasov
@ 2016-07-04 19:56   ` Vegard Nossum
  2016-07-04 21:24     ` Julian Anastasov
  0 siblings, 1 reply; 7+ messages in thread
From: Vegard Nossum @ 2016-07-04 19:56 UTC (permalink / raw)
  To: Julian Anastasov
  Cc: Andy Gospodarek, davem, netdev, Dinesh Dutt, Scott Feldman

On 07/04/2016 09:45 PM, Julian Anastasov wrote:
>
> 	Hello,
>
> On Mon, 4 Jul 2016, Vegard Nossum wrote:
>
>> struct fib_nh->nh_dev can be NULL, so we should check it before calling
>> __in_dev_get_rcu on it.
>>
>> Multiple places seem to want this (and check the return value), so we can
>> add a convenience wrapper for this.
>>
>> This fixes a crash in AF_NETLINK sendmsg().
>>
>> Please double check that I caught all the callers that need the NULL
>> guard.
>
> 	What kind of configuration causes such crash?

It's not very easy to tell, sorry, I was using a netlink fuzzer to find
the crash. But see below, you identified the right bit of code.

>> -		in_dev = __in_dev_get_rtnl(nh->nh_dev);
>> +		in_dev = in_dev_get_rtnl(nh->nh_dev);
>
> 	fib_rebalance can not crash because for multipath
> routes (fib_nhs > 1) all nexthops should have valid nh_dev.
>

Okay, cool.

>>
>>   		if (in_dev &&
>>   		    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
>> @@ -559,7 +559,7 @@ static void fib_rebalance(struct fib_info *fi)
>>   	change_nexthops(fi) {
>>   		int upper_bound;
>>
>> -		in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
>> +		in_dev = in_dev_get_rtnl(nexthop_nh->nh_dev);
>>
>>   		if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
>>   			upper_bound = -1;
>> @@ -1261,7 +1261,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
>>   		    nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
>>   			goto nla_put_failure;
>>   		if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
>> -			in_dev = __in_dev_get_rtnl(fi->fib_nh->nh_dev);
>> +			in_dev = in_dev_get_rtnl(fi->fib_nh->nh_dev);
>
> 	Looks like this is the only place that can crash,
> adding extra cycles to the other places is not good. And this
> can happen only because fib_create_info() allows RTNH_F_LINKDOWN
> to come for routes with error. May be fc_flags should be masked
> there? Or there is another place that sets the flag when nh_dev
> is NULL?

Indeed, this is the (only) place that actually crashed for me.

>
>>   			if (in_dev &&
>>   			    IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
>>   				rtm->rtm_flags |= RTNH_F_DEAD;
>> @@ -1292,7 +1292,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
>>
>>   			rtnh->rtnh_flags = nh->nh_flags & 0xFF;
>>   			if (nh->nh_flags & RTNH_F_LINKDOWN) {
>> -				in_dev = __in_dev_get_rtnl(nh->nh_dev);
>> +				in_dev = in_dev_get_rtnl(nh->nh_dev);
>
> 	Not needed because fib_nhs > 1
>

Alright.

Thanks for the review! I can submit a new patch to only check the one
place above that actually crashed. Otherwise, if you think it's better
to go with your fc_flags suggestion, feel free to send a patch for that.
As you can tell, I am not very familiar with this code :-) If you do
send a patch, I can test it easily.


Vegard

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 19:56   ` Vegard Nossum
@ 2016-07-04 21:24     ` Julian Anastasov
  2016-07-04 22:02       ` Vegard Nossum
  0 siblings, 1 reply; 7+ messages in thread
From: Julian Anastasov @ 2016-07-04 21:24 UTC (permalink / raw)
  To: Vegard Nossum; +Cc: Andy Gospodarek, davem, netdev, Dinesh Dutt, Scott Feldman


	Hello,

On Mon, 4 Jul 2016, Vegard Nossum wrote:

> Alright.
> 
> Thanks for the review! I can submit a new patch to only check the one
> place above that actually crashed. Otherwise, if you think it's better
> to go with your fc_flags suggestion, feel free to send a patch for that.
> As you can tell, I am not very familiar with this code :-) If you do
> send a patch, I can test it easily.

	Of course, here is untested version. May be I'll do some
testing this weekend and will submit with proper commit
message...

[PATCH RFC] ipv4: reject RTNH_F_LINKDOWN for incompatible routes

The RTNH_F_LINKDOWN flag is used only for link routes.
Reject it for error routes and local routes.

Signed-off-by: Julian Anastasov <ja@ssi.bg>
---
 net/ipv4/fib_semantics.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index 2b68418..1f10c20 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -1113,7 +1113,8 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
 	}
 
 	if (fib_props[cfg->fc_type].error) {
-		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
+		if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp ||
+		    (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN))
 			goto err_inval;
 		goto link_it;
 	} else {
@@ -1136,7 +1137,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
 		struct fib_nh *nh = fi->fib_nh;
 
 		/* Local address is added. */
-		if (nhs != 1 || nh->nh_gw)
+		if (nhs != 1 || nh->nh_gw || (nh->nh_flags & RTNH_F_LINKDOWN))
 			goto err_inval;
 		nh->nh_scope = RT_SCOPE_NOWHERE;
 		nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
-- 
1.9.3

Regards

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 21:24     ` Julian Anastasov
@ 2016-07-04 22:02       ` Vegard Nossum
  2016-07-04 22:15         ` Julian Anastasov
  0 siblings, 1 reply; 7+ messages in thread
From: Vegard Nossum @ 2016-07-04 22:02 UTC (permalink / raw)
  To: Julian Anastasov
  Cc: Andy Gospodarek, davem, netdev, Dinesh Dutt, Scott Feldman

On 07/04/2016 11:24 PM, Julian Anastasov wrote:
>
> 	Hello,
>
> On Mon, 4 Jul 2016, Vegard Nossum wrote:
>
>> Alright.
>>
>> Thanks for the review! I can submit a new patch to only check the one
>> place above that actually crashed. Otherwise, if you think it's better
>> to go with your fc_flags suggestion, feel free to send a patch for that.
>> As you can tell, I am not very familiar with this code :-) If you do
>> send a patch, I can test it easily.
>
> 	Of course, here is untested version. May be I'll do some
> testing this weekend and will submit with proper commit
> message...
>
> [PATCH RFC] ipv4: reject RTNH_F_LINKDOWN for incompatible routes
>
> The RTNH_F_LINKDOWN flag is used only for link routes.
> Reject it for error routes and local routes.
>
> Signed-off-by: Julian Anastasov <ja@ssi.bg>

Tested your patch, it fixes the problem for me. I haven't tested if it
breaks anything else :-)


Vegard

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

* Re: [PATCH] net: check for NULL net_device in FIB tables
  2016-07-04 22:02       ` Vegard Nossum
@ 2016-07-04 22:15         ` Julian Anastasov
  0 siblings, 0 replies; 7+ messages in thread
From: Julian Anastasov @ 2016-07-04 22:15 UTC (permalink / raw)
  To: Vegard Nossum; +Cc: Andy Gospodarek, davem, netdev, Dinesh Dutt, Scott Feldman


	Hello,

On Tue, 5 Jul 2016, Vegard Nossum wrote:

> > [PATCH RFC] ipv4: reject RTNH_F_LINKDOWN for incompatible routes
> >
> > The RTNH_F_LINKDOWN flag is used only for link routes.
> > Reject it for error routes and local routes.
> >
> > Signed-off-by: Julian Anastasov <ja@ssi.bg>
> 
> Tested your patch, it fixes the problem for me. I haven't tested if it
> breaks anything else :-)

	Thanks!

Regards

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

end of thread, other threads:[~2016-07-04 22:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-04 12:47 [PATCH] net: check for NULL net_device in FIB tables Vegard Nossum
2016-07-04 13:50 ` Vegard Nossum
2016-07-04 19:45 ` Julian Anastasov
2016-07-04 19:56   ` Vegard Nossum
2016-07-04 21:24     ` Julian Anastasov
2016-07-04 22:02       ` Vegard Nossum
2016-07-04 22:15         ` Julian Anastasov

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