Re: POSSIBLE BUG: selftests/net/fcnal-test.sh: [FAIL] in vrf "bind - ns-B IPv6 LLA" test

From: Mirsad Todorovac
Date: Tue Jun 06 2023 - 10:28:48 EST


On 6/6/23 16:11, Guillaume Nault wrote:
On Tue, Jun 06, 2023 at 03:57:35PM +0200, Mirsad Todorovac wrote:
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index c4835dbdfcff..c1d81c49b775 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -73,6 +73,10 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
struct rt6_info *rt;
struct pingfakehdr pfh;
struct ipcm6_cookie ipc6;
+ struct net *net = sock_net(sk);
+ struct net_device *dev = NULL;
+ struct net_device *mdev = NULL;
+ struct net_device *bdev = NULL;

err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
sizeof(user_icmph));
@@ -111,10 +115,26 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
else if (!oif)
oif = np->ucast_oif;

+ if (oif) {
+ rcu_read_lock();
+ dev = dev_get_by_index_rcu(net, oif);
+ rcu_read_unlock();

You can't assume '*dev' is still valid after rcu_read_unlock() unless
you hold a reference on it.

+ rtnl_lock();
+ mdev = netdev_master_upper_dev_get(dev);
+ rtnl_unlock();

Because of that, 'dev' might have already disappeared at the time
netdev_master_upper_dev_get() is called. So it may dereference an
invalid pointer here.

Good point, thanks. I didn't expect those to change.

This can be fixed, provided that RCU and RTNL locks can be nested:

rcu_read_lock();
if (oif) {
dev = dev_get_by_index_rcu(net, oif);
rtnl_lock();
mdev = netdev_master_upper_dev_get(dev);
rtnl_unlock();
}

if (sk->sk_bound_dev_if) {
bdev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
}

addr_type = ipv6_addr_type(daddr);
if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) ||
(addr_type & IPV6_ADDR_MAPPED) ||
(oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if &&
!(mdev && sk->sk_bound_dev_if && bdev && mdev == bdev))) {
rcu_read_unlock();
return -EINVAL;
}
rcu_read_unlock();

But again this is still probably not race-free (bdev might also disappear before
the mdev == bdev test), even if it passed fcnal-test.sh, there is much duplication
of code, so your one-line solution is obviously by far better. :-)

Much obliged.

Best regards,
Mirsad

+ }
+
+ if (sk->sk_bound_dev_if) {
+ rcu_read_lock();
+ bdev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
+ rcu_read_unlock();
+ }
+
addr_type = ipv6_addr_type(daddr);
if ((__ipv6_addr_needs_scope_id(addr_type) && !oif) ||
(addr_type & IPV6_ADDR_MAPPED) ||
- (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if))
+ (oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if &&
+ !(mdev && sk->sk_bound_dev_if && bdev && mdev == bdev)))
return -EINVAL;

ipcm6_init_sk(&ipc6, np);

However, this works by the test (888 passed) but your two liner is obviously
better :-)

:)

--
Mirsad Goran Todorovac
Sistem inženjer
Grafički fakultet | Akademija likovnih umjetnosti
Sveučilište u Zagrebu

System engineer
Faculty of Graphic Arts | Academy of Fine Arts
University of Zagreb, Republic of Croatia