netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 0/4] vrf: fix source address selection with route leak
@ 2024-06-24 13:07 Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 1/4] ipv4: " Nicolas Dichtel
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 13:07 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
  Cc: David Ahern, netdev

For patch 1 and 2, I didn't find the exact commit that introduced this bug, but
I suspect it has been here since the first version. I arbitrarily choose one.

 include/net/ip6_route.h                          | 19 +++++++-----
 net/ipv4/fib_semantics.c                         | 11 +++++--
 net/ipv6/addrconf.c                              |  3 +-
 net/ipv6/ip6_output.c                            |  1 +
 net/ipv6/route.c                                 |  2 +-
 tools/testing/selftests/net/vrf_route_leaking.sh | 38 ++++++++++++++++++++++--
 6 files changed, 61 insertions(+), 13 deletions(-)

Comments are welcome.

Regards,
Nicolas


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

* [PATCH net 1/4] ipv4: fix source address selection with route leak
  2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
@ 2024-06-24 13:07 ` Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 2/4] ipv6: " Nicolas Dichtel
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 13:07 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
  Cc: David Ahern, netdev, Nicolas Dichtel, stable

By default, an address assigned to the output interface is selected when
the source address is not specified. This is problematic when a route,
configured in a vrf, uses an interface from another vrf (aka route leak).
The original vrf does not own the selected source address.

Let's add a check against the output interface and call the appropriate
function to select the source address.

CC: stable@vger.kernel.org
Fixes: 8cbb512c923d ("net: Add source address lookup op for VRF")
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 net/ipv4/fib_semantics.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index f669da98d11d..459082f4936d 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -2270,6 +2270,13 @@ void fib_select_path(struct net *net, struct fib_result *res,
 		fib_select_default(fl4, res);
 
 check_saddr:
-	if (!fl4->saddr)
-		fl4->saddr = fib_result_prefsrc(net, res);
+	if (!fl4->saddr) {
+		struct net_device *l3mdev = dev_get_by_index_rcu(net, fl4->flowi4_l3mdev);
+
+		if (!l3mdev ||
+		    l3mdev_master_dev_rcu(FIB_RES_DEV(*res)) == l3mdev)
+			fl4->saddr = fib_result_prefsrc(net, res);
+		else
+			fl4->saddr = inet_select_addr(l3mdev, 0, RT_SCOPE_LINK);
+	}
 }
-- 
2.43.1


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

* [PATCH net 2/4] ipv6: fix source address selection with route leak
  2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 1/4] ipv4: " Nicolas Dichtel
@ 2024-06-24 13:07 ` Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 3/4] ipv6: take care of scope when choosing the src addr Nicolas Dichtel
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 13:07 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
  Cc: David Ahern, netdev, Nicolas Dichtel, stable

By default, an address assigned to the output interface is selected when
the source address is not specified. This is problematic when a route,
configured in a vrf, uses an interface from another vrf (aka route leak).
The original vrf does not own the selected source address.

Let's add a check against the output interface and call the appropriate
function to select the source address.

CC: stable@vger.kernel.org
Fixes: 0d240e7811c4 ("net: vrf: Implement get_saddr for IPv6")
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 include/net/ip6_route.h | 19 ++++++++++++-------
 net/ipv6/ip6_output.c   |  1 +
 net/ipv6/route.c        |  2 +-
 3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
index a18ed24fed94..62755fab1b52 100644
--- a/include/net/ip6_route.h
+++ b/include/net/ip6_route.h
@@ -127,18 +127,23 @@ void rt6_age_exceptions(struct fib6_info *f6i, struct fib6_gc_args *gc_args,
 
 static inline int ip6_route_get_saddr(struct net *net, struct fib6_info *f6i,
 				      const struct in6_addr *daddr,
-				      unsigned int prefs,
+				      unsigned int prefs, int l3mdev_index,
 				      struct in6_addr *saddr)
 {
+	struct net_device *l3mdev;
+	struct net_device *dev;
+	bool same_vrf;
 	int err = 0;
 
-	if (f6i && f6i->fib6_prefsrc.plen) {
+	rcu_read_lock();
+	l3mdev = dev_get_by_index_rcu(net, l3mdev_index);
+	dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
+	same_vrf = l3mdev && l3mdev_master_dev_rcu(dev) == l3mdev;
+	if (f6i && f6i->fib6_prefsrc.plen && same_vrf)
 		*saddr = f6i->fib6_prefsrc.addr;
-	} else {
-		struct net_device *dev = f6i ? fib6_info_nh_dev(f6i) : NULL;
-
-		err = ipv6_dev_get_saddr(net, dev, daddr, prefs, saddr);
-	}
+	else
+		err = ipv6_dev_get_saddr(net, same_vrf ? dev : l3mdev, daddr, prefs, saddr);
+	rcu_read_unlock();
 
 	return err;
 }
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 27d8725445e3..784424ac4147 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1124,6 +1124,7 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
 		from = rt ? rcu_dereference(rt->from) : NULL;
 		err = ip6_route_get_saddr(net, from, &fl6->daddr,
 					  sk ? READ_ONCE(inet6_sk(sk)->srcprefs) : 0,
+					  fl6->flowi6_l3mdev,
 					  &fl6->saddr);
 		rcu_read_unlock();
 
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 8d72ca0b086d..c9a9506b714d 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -5689,7 +5689,7 @@ static int rt6_fill_node(struct net *net, struct sk_buff *skb,
 				goto nla_put_failure;
 	} else if (dest) {
 		struct in6_addr saddr_buf;
-		if (ip6_route_get_saddr(net, rt, dest, 0, &saddr_buf) == 0 &&
+		if (ip6_route_get_saddr(net, rt, dest, 0, 0, &saddr_buf) == 0 &&
 		    nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
 			goto nla_put_failure;
 	}
-- 
2.43.1


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

* [PATCH net 3/4] ipv6: take care of scope when choosing the src addr
  2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 1/4] ipv4: " Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 2/4] ipv6: " Nicolas Dichtel
@ 2024-06-24 13:07 ` Nicolas Dichtel
  2024-06-24 13:07 ` [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test Nicolas Dichtel
  2024-06-24 20:30 ` [PATCH net 0/4] vrf: fix source address selection with route leak Jakub Kicinski
  4 siblings, 0 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 13:07 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
  Cc: David Ahern, netdev, Nicolas Dichtel, stable

When the source address is selected, the scope must be checked. For
example, if a loopback address is assigned to the vrf device, it must not
be chosen for packets sent outside.

CC: stable@vger.kernel.org
Fixes: afbac6010aec ("net: ipv6: Address selection needs to consider L3 domains")
Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 net/ipv6/addrconf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index 5c424a0e7232..4f2c5cc31015 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -1873,7 +1873,8 @@ int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
 							    master, &dst,
 							    scores, hiscore_idx);
 
-			if (scores[hiscore_idx].ifa)
+			if (scores[hiscore_idx].ifa &&
+			    scores[hiscore_idx].scopedist >= 0)
 				goto out;
 		}
 
-- 
2.43.1


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

* [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test
  2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
                   ` (2 preceding siblings ...)
  2024-06-24 13:07 ` [PATCH net 3/4] ipv6: take care of scope when choosing the src addr Nicolas Dichtel
@ 2024-06-24 13:07 ` Nicolas Dichtel
  2024-06-27 10:57   ` Simon Horman
  2024-06-24 20:30 ` [PATCH net 0/4] vrf: fix source address selection with route leak Jakub Kicinski
  4 siblings, 1 reply; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 13:07 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet
  Cc: David Ahern, netdev, Nicolas Dichtel

The goal is to check that the source address selected by the kernel is
routable when a leaking route is used.

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 .../selftests/net/vrf_route_leaking.sh        | 38 ++++++++++++++++++-
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/net/vrf_route_leaking.sh b/tools/testing/selftests/net/vrf_route_leaking.sh
index 2da32f4c479b..6c59e0bbbde3 100755
--- a/tools/testing/selftests/net/vrf_route_leaking.sh
+++ b/tools/testing/selftests/net/vrf_route_leaking.sh
@@ -533,6 +533,38 @@ ipv6_ping_frag_asym()
 	ipv6_ping_frag asym
 }
 
+ipv4_ping_local()
+{
+	local ttype="$1"
+
+	[ "x$ttype" = "x" ] && ttype="$DEFAULT_TTYPE"
+
+	log_section "IPv4 ($ttype route): VRF ICMP local error route lookup ping"
+
+	setup_"$ttype"
+
+	check_connectivity || return
+
+	run_cmd ip netns exec $r1 ip vrf exec blue ping -c1 -w1 ${H2_N2_IP}
+	log_test $? 0 "VRF ICMP local IPv4"
+}
+
+ipv6_ping_local()
+{
+	local ttype="$1"
+
+	[ "x$ttype" = "x" ] && ttype="$DEFAULT_TTYPE"
+
+	log_section "IPv6 ($ttype route): VRF ICMP local error route lookup ping"
+
+	setup_"$ttype"
+
+	check_connectivity6 || return
+
+	run_cmd ip netns exec $r1 ip vrf exec blue ${ping6} -c1 -w1 ${H2_N2_IP6}
+	log_test $? 0 "VRF ICMP local IPv6"
+}
+
 ################################################################################
 # usage
 
@@ -555,8 +587,8 @@ EOF
 # Some systems don't have a ping6 binary anymore
 command -v ping6 > /dev/null 2>&1 && ping6=$(command -v ping6) || ping6=$(command -v ping)
 
-TESTS_IPV4="ipv4_ping_ttl ipv4_traceroute ipv4_ping_frag ipv4_ping_ttl_asym ipv4_traceroute_asym"
-TESTS_IPV6="ipv6_ping_ttl ipv6_traceroute ipv6_ping_ttl_asym ipv6_traceroute_asym"
+TESTS_IPV4="ipv4_ping_ttl ipv4_traceroute ipv4_ping_frag ipv4_ping_local ipv4_ping_ttl_asym ipv4_traceroute_asym"
+TESTS_IPV6="ipv6_ping_ttl ipv6_traceroute ipv6_ping_local ipv6_ping_ttl_asym ipv6_traceroute_asym"
 
 ret=0
 nsuccess=0
@@ -594,12 +626,14 @@ do
 	ipv4_traceroute|traceroute)      ipv4_traceroute;;&
 	ipv4_traceroute_asym|traceroute) ipv4_traceroute_asym;;&
 	ipv4_ping_frag|ping)             ipv4_ping_frag;;&
+	ipv4_ping_local|ping)            ipv4_ping_local;;&
 
 	ipv6_ping_ttl|ping)              ipv6_ping_ttl;;&
 	ipv6_ping_ttl_asym|ping)         ipv6_ping_ttl_asym;;&
 	ipv6_traceroute|traceroute)      ipv6_traceroute;;&
 	ipv6_traceroute_asym|traceroute) ipv6_traceroute_asym;;&
 	ipv6_ping_frag|ping)             ipv6_ping_frag;;&
+	ipv6_ping_local|ping)            ipv6_ping_local;;&
 
 	# setup namespaces and config, but do not run any tests
 	setup_sym|setup)                 setup_sym; exit 0;;
-- 
2.43.1


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

* Re: [PATCH net 0/4] vrf: fix source address selection with route leak
  2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
                   ` (3 preceding siblings ...)
  2024-06-24 13:07 ` [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test Nicolas Dichtel
@ 2024-06-24 20:30 ` Jakub Kicinski
  2024-06-24 21:37   ` Nicolas Dichtel
  4 siblings, 1 reply; 9+ messages in thread
From: Jakub Kicinski @ 2024-06-24 20:30 UTC (permalink / raw)
  To: Nicolas Dichtel
  Cc: David S . Miller, Paolo Abeni, Eric Dumazet, David Ahern, netdev

On Mon, 24 Jun 2024 15:07:52 +0200 Nicolas Dichtel wrote:
> For patch 1 and 2, I didn't find the exact commit that introduced this bug, but
> I suspect it has been here since the first version. I arbitrarily choose one.

I think this breaks fcnal:

https://netdev-3.bots.linux.dev/vmksft-net/results/654201/1-fcnal-test-sh/stdout
-- 
pw-bot: cr

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

* Re: [PATCH net 0/4] vrf: fix source address selection with route leak
  2024-06-24 20:30 ` [PATCH net 0/4] vrf: fix source address selection with route leak Jakub Kicinski
@ 2024-06-24 21:37   ` Nicolas Dichtel
  0 siblings, 0 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-06-24 21:37 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: David S . Miller, Paolo Abeni, Eric Dumazet, David Ahern, netdev

Le 24/06/2024 à 22:30, Jakub Kicinski a écrit :
> On Mon, 24 Jun 2024 15:07:52 +0200 Nicolas Dichtel wrote:
>> For patch 1 and 2, I didn't find the exact commit that introduced this bug, but
>> I suspect it has been here since the first version. I arbitrarily choose one.
> 
> I think this breaks fcnal:
> 
> https://netdev-3.bots.linux.dev/vmksft-net/results/654201/1-fcnal-test-sh/stdout
Thanks, I will have a look.

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

* Re: [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test
  2024-06-24 13:07 ` [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test Nicolas Dichtel
@ 2024-06-27 10:57   ` Simon Horman
  2024-07-05 13:34     ` Nicolas Dichtel
  0 siblings, 1 reply; 9+ messages in thread
From: Simon Horman @ 2024-06-27 10:57 UTC (permalink / raw)
  To: Nicolas Dichtel
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet,
	David Ahern, netdev

On Mon, Jun 24, 2024 at 03:07:56PM +0200, Nicolas Dichtel wrote:
> The goal is to check that the source address selected by the kernel is
> routable when a leaking route is used.
> 
> Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
> ---
>  .../selftests/net/vrf_route_leaking.sh        | 38 ++++++++++++++++++-
>  1 file changed, 36 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/net/vrf_route_leaking.sh b/tools/testing/selftests/net/vrf_route_leaking.sh
> index 2da32f4c479b..6c59e0bbbde3 100755
> --- a/tools/testing/selftests/net/vrf_route_leaking.sh
> +++ b/tools/testing/selftests/net/vrf_route_leaking.sh
> @@ -533,6 +533,38 @@ ipv6_ping_frag_asym()
>  	ipv6_ping_frag asym
>  }
>  
> +ipv4_ping_local()
> +{
> +	local ttype="$1"
> +
> +	[ "x$ttype" = "x" ] && ttype="$DEFAULT_TTYPE"

Hi Nicolas,

I see this pattern already elsewhere in this file, but shellecheck flags that:

1. No arguments are passed to ipv4_ping_local
2. The condition can be more simply expressed as [ "$ttype" = "" ]
   (my 2c worth would be [ -z "$ttype" ])

Nit picking aside, I'm genuinely curious about 1, is it actually the case?

> +
> +	log_section "IPv4 ($ttype route): VRF ICMP local error route lookup ping"
> +
> +	setup_"$ttype"
> +
> +	check_connectivity || return
> +
> +	run_cmd ip netns exec $r1 ip vrf exec blue ping -c1 -w1 ${H2_N2_IP}
> +	log_test $? 0 "VRF ICMP local IPv4"
> +}

...

> @@ -594,12 +626,14 @@ do
>  	ipv4_traceroute|traceroute)      ipv4_traceroute;;&
>  	ipv4_traceroute_asym|traceroute) ipv4_traceroute_asym;;&
>  	ipv4_ping_frag|ping)             ipv4_ping_frag;;&
> +	ipv4_ping_local|ping)            ipv4_ping_local;;&
>  
>  	ipv6_ping_ttl|ping)              ipv6_ping_ttl;;&
>  	ipv6_ping_ttl_asym|ping)         ipv6_ping_ttl_asym;;&
>  	ipv6_traceroute|traceroute)      ipv6_traceroute;;&
>  	ipv6_traceroute_asym|traceroute) ipv6_traceroute_asym;;&
>  	ipv6_ping_frag|ping)             ipv6_ping_frag;;&
> +	ipv6_ping_local|ping)            ipv6_ping_local;;&
>  
>  	# setup namespaces and config, but do not run any tests
>  	setup_sym|setup)                 setup_sym; exit 0;;
> -- 
> 2.43.1
> 
> 

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

* Re: [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test
  2024-06-27 10:57   ` Simon Horman
@ 2024-07-05 13:34     ` Nicolas Dichtel
  0 siblings, 0 replies; 9+ messages in thread
From: Nicolas Dichtel @ 2024-07-05 13:34 UTC (permalink / raw)
  To: Simon Horman
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet,
	David Ahern, netdev

Le 27/06/2024 à 12:57, Simon Horman a écrit :
> On Mon, Jun 24, 2024 at 03:07:56PM +0200, Nicolas Dichtel wrote:
>> The goal is to check that the source address selected by the kernel is
>> routable when a leaking route is used.
>>
>> Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
>> ---
>>  .../selftests/net/vrf_route_leaking.sh        | 38 ++++++++++++++++++-
>>  1 file changed, 36 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/testing/selftests/net/vrf_route_leaking.sh b/tools/testing/selftests/net/vrf_route_leaking.sh
>> index 2da32f4c479b..6c59e0bbbde3 100755
>> --- a/tools/testing/selftests/net/vrf_route_leaking.sh
>> +++ b/tools/testing/selftests/net/vrf_route_leaking.sh
>> @@ -533,6 +533,38 @@ ipv6_ping_frag_asym()
>>  	ipv6_ping_frag asym
>>  }
>>  
>> +ipv4_ping_local()
>> +{
>> +	local ttype="$1"
>> +
>> +	[ "x$ttype" = "x" ] && ttype="$DEFAULT_TTYPE"
> 
> Hi Nicolas,
> 
> I see this pattern already elsewhere in this file, but shellecheck flags that:
> 
> 1. No arguments are passed to ipv4_ping_local
Yes, I don't add an asymmetric version of this test, I don't think that's
relevant. I wanted to keep it to be consistent with other tests.

> 2. The condition can be more simply expressed as [ "$ttype" = "" ]
>    (my 2c worth would be [ -z "$ttype" ])
Yeah, I asked myself also, but like for the previous point, I wanted to be
consistent with other tests.

I will remove this test.


Thank you,
Nicolas

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

end of thread, other threads:[~2024-07-05 13:34 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-24 13:07 [PATCH net 0/4] vrf: fix source address selection with route leak Nicolas Dichtel
2024-06-24 13:07 ` [PATCH net 1/4] ipv4: " Nicolas Dichtel
2024-06-24 13:07 ` [PATCH net 2/4] ipv6: " Nicolas Dichtel
2024-06-24 13:07 ` [PATCH net 3/4] ipv6: take care of scope when choosing the src addr Nicolas Dichtel
2024-06-24 13:07 ` [PATCH net 4/4] selftests: vrf_route_leaking: add local ping test Nicolas Dichtel
2024-06-27 10:57   ` Simon Horman
2024-07-05 13:34     ` Nicolas Dichtel
2024-06-24 20:30 ` [PATCH net 0/4] vrf: fix source address selection with route leak Jakub Kicinski
2024-06-24 21:37   ` Nicolas Dichtel

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