Skip to contentSkip to conversation

udp: Update reuse->has_conns under reuseport_lock.

PATCH Superseded Kuniyuki Iwashima 3 participants 7 messages Oct 12, 2022 – Oct 13, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 udp: Update reuse->has_conns under reuseport_lock.

When we call connect() for a UDP socket in a reuseport group, we have to update sk->sk_reuseport_cb->has_conns to 1. Otherwise, the kernel could select a unconnected socket wrongly for packets sent to the connected socket.

However, the current way to set has_conns is illegal and possible to trigger that problem. reuseport_has_conns() changes has_conns under rcu_read_lock(), which upgrades the RCU reader to the updater. Then, it must do the update under the updater's lock, reuseport_lock, but it doesn't for now.

For this reason, there is a race below where we fail to set has_conns resulting in the wrong socket selection. To avoid the race, let's split the reader and updater with proper locking.

cpu1 cpu2
+----+ +----+

__ip[46]_datagram_connect()        reuseport_grow()
.                                  .
|- reuseport_has_conns(sk, true)   |- more_reuse = __reuseport_alloc(more_socks_size)
|  .                               |
|  |- rcu_read_lock()
|  |- reuse = rcu_dereference(sk->sk_reuseport_cb)
|  |
|  |                               |  /* reuse->has_conns == 0 here */
|  |                               |- more_reuse->has_conns = reuse->has_conns
|  |- reuse->has_conns = 1         |  /* more_reuse->has_conns SHOULD BE 1 HERE */
|  |                               |
|  |                               |- rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
|  |                               |                     more_reuse)
|  `- rcu_read_unlock()            `- kfree_rcu(reuse, rcu)
|
|- sk->sk_state = TCP_ESTABLISHED
Fixes acdcecc61285 ("udp: correct reuseport selection with connected sockets")

---
v2:
* Fix build failure for CONFIG_IPV6=m
* Drop SO_INCOMING_CPU fix, which will be sent for net-next after the v6.1 merge window

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/sock_reuseport.h | 11 +++++------
 net/core/sock_reuseport.c    | 15 +++++++++++++++
 net/ipv4/datagram.c          |  2 +-
 net/ipv4/udp.c               |  2 +-
 net/ipv6/datagram.c          |  2 +-
 net/ipv6/udp.c               |  2 +-
 6 files changed, 24 insertions(+), 10 deletions(-)
include/net/sock_reuseport.h +5 -6
index 473b0b0fa4ab..efc9085c6892 100644
@@ -43,21 +43,20 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
 extern int reuseport_detach_prog(struct sock *sk);
 
-static inline bool reuseport_has_conns(struct sock *sk, bool set)
+static inline bool reuseport_has_conns(struct sock *sk)
 {
 	struct sock_reuseport *reuse;
 	bool ret = false;
 
 	rcu_read_lock();
 	reuse = rcu_dereference(sk->sk_reuseport_cb);
-	if (reuse) {
-		if (set)
-			reuse->has_conns = 1;
-		ret = reuse->has_conns;
-	}
+	if (reuse && reuse->has_conns)
+		ret = true;
 	rcu_read_unlock();
 
 	return ret;
 }
 
+void reuseport_has_conns_set(struct sock *sk);
+
 #endif  /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +15 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
 			       struct sock_reuseport *reuse, bool bind_inany);
 
+void reuseport_has_conns_set(struct sock *sk)
+{
+	struct sock_reuseport *reuse;
+
+	if (!rcu_access_pointer(sk->sk_reuseport_cb))
+		return;
+
+	spin_lock(&reuseport_lock);
+	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+					  lockdep_is_held(&reuseport_lock));
+	reuse->has_conns = 1;
+	spin_unlock(&reuseport_lock);
+}
+EXPORT_SYMBOL(reuseport_has_conns_set);
+
 static int reuseport_sock_index(struct sock *sk,
 				const struct sock_reuseport *reuse,
 				bool closed)
net/ipv4/datagram.c +1 -1
index 405a8c2aea64..5e66add7befa 100644
@@ -70,7 +70,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
 	}
 	inet->inet_daddr = fl4->daddr;
 	inet->inet_dport = usin->sin_port;
-	reuseport_has_conns(sk, true);
+	reuseport_has_conns_set(sk);
 	sk->sk_state = TCP_ESTABLISHED;
 	sk_set_txhash(sk);
 	inet->inet_id = prandom_u32();
net/ipv4/udp.c +1 -1
index d63118ce5900..29228231b058 100644
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
 			result = lookup_reuseport(net, sk, skb,
 						  saddr, sport, daddr, hnum);
 			/* Fall back to scoring if group has connections */
-			if (result && !reuseport_has_conns(sk, false))
+			if (result && !reuseport_has_conns(sk))
 				return result;
 
 			result = result ? : sk;
net/ipv6/datagram.c +1 -1
index df665d4e8f0f..5ecb56522f9d 100644
@@ -256,7 +256,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
 		goto out;
 	}
 
-	reuseport_has_conns(sk, true);
+	reuseport_has_conns_set(sk);
 	sk->sk_state = TCP_ESTABLISHED;
 	sk_set_txhash(sk);
 out:
net/ipv6/udp.c +1 -1
index 91e795bb9ade..56e4523a3004 100644
@@ -182,7 +182,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
 			result = lookup_reuseport(net, sk, skb,
 						  saddr, sport, daddr, hnum);
 			/* Fall back to scoring if group has connections */
-			if (result && !reuseport_has_conns(sk, false))
+			if (result && !reuseport_has_conns(sk))
 				return result;
 
 			result = result ? : sk;
Signature · 1 line
2.30.2
#1 lore raw
Quoted 93 lines · Kuniyuki Iwashima

When we call connect() for a UDP socket in a reuseport group, we have to update sk->sk_reuseport_cb->has_conns to 1. Otherwise, the kernel could select a unconnected socket wrongly for packets sent to the connected socket.

However, the current way to set has_conns is illegal and possible to trigger that problem. reuseport_has_conns() changes has_conns under rcu_read_lock(), which upgrades the RCU reader to the updater. Then, it must do the update under the updater's lock, reuseport_lock, but it doesn't for now.

For this reason, there is a race below where we fail to set has_conns resulting in the wrong socket selection. To avoid the race, let's split the reader and updater with proper locking.

cpu1 cpu2
+----+ +----+

__ip[46]_datagram_connect()        reuseport_grow()
.                                  .
|- reuseport_has_conns(sk, true)   |- more_reuse = __reuseport_alloc(more_socks_size)
|  .                               |
|  |- rcu_read_lock()
|  |- reuse = rcu_dereference(sk->sk_reuseport_cb)
|  |
|  |                               |  /* reuse->has_conns == 0 here */
|  |                               |- more_reuse->has_conns = reuse->has_conns
|  |- reuse->has_conns = 1         |  /* more_reuse->has_conns SHOULD BE 1 HERE */
|  |                               |
|  |                               |- rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
|  |                               |                     more_reuse)
|  `- rcu_read_unlock()            `- kfree_rcu(reuse, rcu)
|
|- sk->sk_state = TCP_ESTABLISHED
Fixes acdcecc61285 ("udp: correct reuseport selection with connected sockets")

---
v2:
* Fix build failure for CONFIG_IPV6=m
* Drop SO_INCOMING_CPU fix, which will be sent for net-next after the v6.1 merge window

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/sock_reuseport.h | 11 +++++------
 net/core/sock_reuseport.c    | 15 +++++++++++++++
 net/ipv4/datagram.c          |  2 +-
 net/ipv4/udp.c               |  2 +-
 net/ipv6/datagram.c          |  2 +-
 net/ipv6/udp.c               |  2 +-
 6 files changed, 24 insertions(+), 10 deletions(-)
include/net/sock_reuseport.h +5 -6
index 473b0b0fa4ab..efc9085c6892 100644
@@ -43,21 +43,20 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
 extern int reuseport_detach_prog(struct sock *sk);
 
-static inline bool reuseport_has_conns(struct sock *sk, bool set)
+static inline bool reuseport_has_conns(struct sock *sk)
 {
        struct sock_reuseport *reuse;
        bool ret = false;
 
        rcu_read_lock();
        reuse = rcu_dereference(sk->sk_reuseport_cb);
-       if (reuse) {
-               if (set)
-                       reuse->has_conns = 1;
-               ret = reuse->has_conns;
-       }
+       if (reuse && reuse->has_conns)
+               ret = true;
        rcu_read_unlock();
 
        return ret;
 }
 
+void reuseport_has_conns_set(struct sock *sk);
+
 #endif  /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                               struct sock_reuseport *reuse, bool bind_inany);
 

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 10 lines
net/core/sock_reuseport.c +10 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Previous test was performed without reuseport_lock being held.

Quoted 63 lines
net/core/sock_reuseport.c +5 -0
+       reuse->has_conns = 1;
+       spin_unlock(&reuseport_lock);
+}
+EXPORT_SYMBOL(reuseport_has_conns_set);
+
 static int reuseport_sock_index(struct sock *sk,
                                const struct sock_reuseport *reuse,
                                bool closed)
net/ipv4/datagram.c +1 -1
index 405a8c2aea64..5e66add7befa 100644
@@ -70,7 +70,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
        }
        inet->inet_daddr = fl4->daddr;
        inet->inet_dport = usin->sin_port;
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
        inet->inet_id = prandom_u32();
net/ipv4/udp.c +1 -1
index d63118ce5900..29228231b058 100644
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
net/ipv6/datagram.c +1 -1
index df665d4e8f0f..5ecb56522f9d 100644
@@ -256,7 +256,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
                goto out;
        }
 
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
 out:
net/ipv6/udp.c +1 -2
index 91e795bb9ade..56e4523a3004 100644
@@ -182,7 +182,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
--

2.30.2

#2 lore raw
From:   Eric Dumazet <[email protected]>
Date:   Wed, 12 Oct 2022 11:59:43 -0700
Quoted 107 lines · Kuniyuki Iwashima
Quoted 93 lines

When we call connect() for a UDP socket in a reuseport group, we have to update sk->sk_reuseport_cb->has_conns to 1. Otherwise, the kernel could select a unconnected socket wrongly for packets sent to the connected socket.

However, the current way to set has_conns is illegal and possible to trigger that problem. reuseport_has_conns() changes has_conns under rcu_read_lock(), which upgrades the RCU reader to the updater. Then, it must do the update under the updater's lock, reuseport_lock, but it doesn't for now.

For this reason, there is a race below where we fail to set has_conns resulting in the wrong socket selection. To avoid the race, let's split the reader and updater with proper locking.

cpu1 cpu2
+----+ +----+

__ip[46]_datagram_connect()        reuseport_grow()
.                                  .
|- reuseport_has_conns(sk, true)   |- more_reuse = __reuseport_alloc(more_socks_size)
|  .                               |
|  |- rcu_read_lock()
|  |- reuse = rcu_dereference(sk->sk_reuseport_cb)
|  |
|  |                               |  /* reuse->has_conns == 0 here */
|  |                               |- more_reuse->has_conns = reuse->has_conns
|  |- reuse->has_conns = 1         |  /* more_reuse->has_conns SHOULD BE 1 HERE */
|  |                               |
|  |                               |- rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
|  |                               |                     more_reuse)
|  `- rcu_read_unlock()            `- kfree_rcu(reuse, rcu)
|
|- sk->sk_state = TCP_ESTABLISHED
Fixes acdcecc61285 ("udp: correct reuseport selection with connected sockets")

---
v2:
* Fix build failure for CONFIG_IPV6=m
* Drop SO_INCOMING_CPU fix, which will be sent for net-next after the v6.1 merge window

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/sock_reuseport.h | 11 +++++------
 net/core/sock_reuseport.c    | 15 +++++++++++++++
 net/ipv4/datagram.c          |  2 +-
 net/ipv4/udp.c               |  2 +-
 net/ipv6/datagram.c          |  2 +-
 net/ipv6/udp.c               |  2 +-
 6 files changed, 24 insertions(+), 10 deletions(-)
include/net/sock_reuseport.h +5 -6
index 473b0b0fa4ab..efc9085c6892 100644
@@ -43,21 +43,20 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
 extern int reuseport_detach_prog(struct sock *sk);
 
-static inline bool reuseport_has_conns(struct sock *sk, bool set)
+static inline bool reuseport_has_conns(struct sock *sk)
 {
        struct sock_reuseport *reuse;
        bool ret = false;
 
        rcu_read_lock();
        reuse = rcu_dereference(sk->sk_reuseport_cb);
-       if (reuse) {
-               if (set)
-                       reuse->has_conns = 1;
-               ret = reuse->has_conns;
-       }
+       if (reuse && reuse->has_conns)
+               ret = true;
        rcu_read_unlock();
 
        return ret;
 }
 
+void reuseport_has_conns_set(struct sock *sk);
+
 #endif  /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                               struct sock_reuseport *reuse, bool bind_inany);
 

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 10 lines
net/core/sock_reuseport.c +10 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Quoted 1 line

Previous test was performed without reuseport_lock being held.

Usually, sk_reuseport_cb is changed under lock_sock().

The only exception is reuseport_grow() & TCP reqsk migration case.

1) shutdown() TCP listener, which is moved into the latter part of reuse->socks[] to migrate reqsk.

2) New listen() overflows reuse->socks[] and call reuseport_grow().

3) reuse->max_socks overflows u16 with the new listener.

4) reuseport_grow() pops the old shutdown()ed listener from the array and update its sk->sk_reuseport_cb as NULL without lock_sock().

shutdown()ed sk->sk_reuseport_cb can be changed without lock_sock().

But, reuseport_has_conns_set() is called only for UDP and under lock_sock(), so @reuse never be NULL in this case.

Quoted 62 lines
Quoted 62 lines
net/core/sock_reuseport.c +5 -0
+       reuse->has_conns = 1;
+       spin_unlock(&reuseport_lock);
+}
+EXPORT_SYMBOL(reuseport_has_conns_set);
+
 static int reuseport_sock_index(struct sock *sk,
                                const struct sock_reuseport *reuse,
                                bool closed)
net/ipv4/datagram.c +1 -1
index 405a8c2aea64..5e66add7befa 100644
@@ -70,7 +70,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
        }
        inet->inet_daddr = fl4->daddr;
        inet->inet_dport = usin->sin_port;
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
        inet->inet_id = prandom_u32();
net/ipv4/udp.c +1 -1
index d63118ce5900..29228231b058 100644
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
net/ipv6/datagram.c +1 -1
index df665d4e8f0f..5ecb56522f9d 100644
@@ -256,7 +256,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
                goto out;
        }
 
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
 out:
net/ipv6/udp.c +1 -2
index 91e795bb9ade..56e4523a3004 100644
@@ -182,7 +182,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
--

2.30.2

#3 lore raw
Quoted 123 lines · Kuniyuki Iwashima
From:   Eric Dumazet <[email protected]>
Date:   Wed, 12 Oct 2022 11:59:43 -0700
Quoted 107 lines · Kuniyuki Iwashima
Quoted 93 lines

When we call connect() for a UDP socket in a reuseport group, we have to update sk->sk_reuseport_cb->has_conns to 1. Otherwise, the kernel could select a unconnected socket wrongly for packets sent to the connected socket.

However, the current way to set has_conns is illegal and possible to trigger that problem. reuseport_has_conns() changes has_conns under rcu_read_lock(), which upgrades the RCU reader to the updater. Then, it must do the update under the updater's lock, reuseport_lock, but it doesn't for now.

For this reason, there is a race below where we fail to set has_conns resulting in the wrong socket selection. To avoid the race, let's split the reader and updater with proper locking.

cpu1 cpu2
+----+ +----+

__ip[46]_datagram_connect()        reuseport_grow()
.                                  .
|- reuseport_has_conns(sk, true)   |- more_reuse = __reuseport_alloc(more_socks_size)
|  .                               |
|  |- rcu_read_lock()
|  |- reuse = rcu_dereference(sk->sk_reuseport_cb)
|  |
|  |                               |  /* reuse->has_conns == 0 here */
|  |                               |- more_reuse->has_conns = reuse->has_conns
|  |- reuse->has_conns = 1         |  /* more_reuse->has_conns SHOULD BE 1 HERE */
|  |                               |
|  |                               |- rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
|  |                               |                     more_reuse)
|  `- rcu_read_unlock()            `- kfree_rcu(reuse, rcu)
|
|- sk->sk_state = TCP_ESTABLISHED
Fixes acdcecc61285 ("udp: correct reuseport selection with connected sockets")

---
v2:
* Fix build failure for CONFIG_IPV6=m
* Drop SO_INCOMING_CPU fix, which will be sent for net-next after the v6.1 merge window

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/sock_reuseport.h | 11 +++++------
 net/core/sock_reuseport.c    | 15 +++++++++++++++
 net/ipv4/datagram.c          |  2 +-
 net/ipv4/udp.c               |  2 +-
 net/ipv6/datagram.c          |  2 +-
 net/ipv6/udp.c               |  2 +-
 6 files changed, 24 insertions(+), 10 deletions(-)
include/net/sock_reuseport.h +5 -6
index 473b0b0fa4ab..efc9085c6892 100644
@@ -43,21 +43,20 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
 extern int reuseport_detach_prog(struct sock *sk);
 
-static inline bool reuseport_has_conns(struct sock *sk, bool set)
+static inline bool reuseport_has_conns(struct sock *sk)
 {
        struct sock_reuseport *reuse;
        bool ret = false;
 
        rcu_read_lock();
        reuse = rcu_dereference(sk->sk_reuseport_cb);
-       if (reuse) {
-               if (set)
-                       reuse->has_conns = 1;
-               ret = reuse->has_conns;
-       }
+       if (reuse && reuse->has_conns)
+               ret = true;
        rcu_read_unlock();
 
        return ret;
 }
 
+void reuseport_has_conns_set(struct sock *sk);
+
 #endif  /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                               struct sock_reuseport *reuse, bool bind_inany);
 

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 10 lines
net/core/sock_reuseport.c +10 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Quoted 1 line

Previous test was performed without reuseport_lock being held.

Usually, sk_reuseport_cb is changed under lock_sock().

The only exception is reuseport_grow() & TCP reqsk migration case.

1) shutdown() TCP listener, which is moved into the latter part of reuse->socks[] to migrate reqsk.

2) New listen() overflows reuse->socks[] and call reuseport_grow().

3) reuse->max_socks overflows u16 with the new listener.

Quoted 7 lines

4) reuseport_grow() pops the old shutdown()ed listener from the array and update its sk->sk_reuseport_cb as NULL without lock_sock().

shutdown()ed sk->sk_reuseport_cb can be changed without lock_sock().

But, reuseport_has_conns_set() is called only for UDP and under lock_sock(), so @reuse never be NULL in this case.

Given the complexity of this code and how much time is needed to review all possibilities, please add an additional

if (reuse)
reuse->has_conns = 1;

I doubt anyone will object to such safety measures.

Thanks.

Quoted 64 lines
Quoted 64 lines
Quoted 64 lines
net/core/sock_reuseport.c +5 -0
+       reuse->has_conns = 1;
+       spin_unlock(&reuseport_lock);
+}
+EXPORT_SYMBOL(reuseport_has_conns_set);
+
 static int reuseport_sock_index(struct sock *sk,
                                const struct sock_reuseport *reuse,
                                bool closed)
net/ipv4/datagram.c +1 -1
index 405a8c2aea64..5e66add7befa 100644
@@ -70,7 +70,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
        }
        inet->inet_daddr = fl4->daddr;
        inet->inet_dport = usin->sin_port;
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
        inet->inet_id = prandom_u32();
net/ipv4/udp.c +1 -1
index d63118ce5900..29228231b058 100644
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
net/ipv6/datagram.c +1 -1
index df665d4e8f0f..5ecb56522f9d 100644
@@ -256,7 +256,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
                goto out;
        }
 
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
 out:
net/ipv6/udp.c +1 -2
index 91e795bb9ade..56e4523a3004 100644
@@ -182,7 +182,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
--

2.30.2

#4 lore raw
From:   Eric Dumazet <[email protected]>
Date:   Thu, 13 Oct 2022 09:09:31 -0700
Quoted 132 lines · Kuniyuki Iwashima
Quoted 123 lines
From:   Eric Dumazet <[email protected]>
Date:   Wed, 12 Oct 2022 11:59:43 -0700
Quoted 107 lines · Kuniyuki Iwashima
Quoted 93 lines

When we call connect() for a UDP socket in a reuseport group, we have to update sk->sk_reuseport_cb->has_conns to 1. Otherwise, the kernel could select a unconnected socket wrongly for packets sent to the connected socket.

However, the current way to set has_conns is illegal and possible to trigger that problem. reuseport_has_conns() changes has_conns under rcu_read_lock(), which upgrades the RCU reader to the updater. Then, it must do the update under the updater's lock, reuseport_lock, but it doesn't for now.

For this reason, there is a race below where we fail to set has_conns resulting in the wrong socket selection. To avoid the race, let's split the reader and updater with proper locking.

cpu1 cpu2
+----+ +----+

__ip[46]_datagram_connect()        reuseport_grow()
.                                  .
|- reuseport_has_conns(sk, true)   |- more_reuse = __reuseport_alloc(more_socks_size)
|  .                               |
|  |- rcu_read_lock()
|  |- reuse = rcu_dereference(sk->sk_reuseport_cb)
|  |
|  |                               |  /* reuse->has_conns == 0 here */
|  |                               |- more_reuse->has_conns = reuse->has_conns
|  |- reuse->has_conns = 1         |  /* more_reuse->has_conns SHOULD BE 1 HERE */
|  |                               |
|  |                               |- rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
|  |                               |                     more_reuse)
|  `- rcu_read_unlock()            `- kfree_rcu(reuse, rcu)
|
|- sk->sk_state = TCP_ESTABLISHED
Fixes acdcecc61285 ("udp: correct reuseport selection with connected sockets")

---
v2:
* Fix build failure for CONFIG_IPV6=m
* Drop SO_INCOMING_CPU fix, which will be sent for net-next after the v6.1 merge window

v1: https://lore.kernel.org/netdev/[email protected]/

---
 include/net/sock_reuseport.h | 11 +++++------
 net/core/sock_reuseport.c    | 15 +++++++++++++++
 net/ipv4/datagram.c          |  2 +-
 net/ipv4/udp.c               |  2 +-
 net/ipv6/datagram.c          |  2 +-
 net/ipv6/udp.c               |  2 +-
 6 files changed, 24 insertions(+), 10 deletions(-)
include/net/sock_reuseport.h +5 -6
index 473b0b0fa4ab..efc9085c6892 100644
@@ -43,21 +43,20 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
 extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
 extern int reuseport_detach_prog(struct sock *sk);
 
-static inline bool reuseport_has_conns(struct sock *sk, bool set)
+static inline bool reuseport_has_conns(struct sock *sk)
 {
        struct sock_reuseport *reuse;
        bool ret = false;
 
        rcu_read_lock();
        reuse = rcu_dereference(sk->sk_reuseport_cb);
-       if (reuse) {
-               if (set)
-                       reuse->has_conns = 1;
-               ret = reuse->has_conns;
-       }
+       if (reuse && reuse->has_conns)
+               ret = true;
        rcu_read_unlock();
 
        return ret;
 }
 
+void reuseport_has_conns_set(struct sock *sk);
+
 #endif  /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
 static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                               struct sock_reuseport *reuse, bool bind_inany);
 

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 10 lines
net/core/sock_reuseport.c +10 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Quoted 1 line

Previous test was performed without reuseport_lock being held.

Usually, sk_reuseport_cb is changed under lock_sock().

The only exception is reuseport_grow() & TCP reqsk migration case.

1) shutdown() TCP listener, which is moved into the latter part of reuse->socks[] to migrate reqsk.

2) New listen() overflows reuse->socks[] and call reuseport_grow().

3) reuse->max_socks overflows u16 with the new listener.

Quoted 7 lines

4) reuseport_grow() pops the old shutdown()ed listener from the array and update its sk->sk_reuseport_cb as NULL without lock_sock().

shutdown()ed sk->sk_reuseport_cb can be changed without lock_sock().

But, reuseport_has_conns_set() is called only for UDP and under lock_sock(), so @reuse never be NULL in this case.

Quoted 7 lines

Given the complexity of this code and how much time is needed to review all possibilities, please add an additional

if (reuse)
reuse->has_conns = 1;

I doubt anyone will object to such safety measures.

I see.
If no one has any objections, I'll respin tomorrow with likely().

Thank you.

Quoted 67 lines

Thanks.

Quoted 64 lines
Quoted 64 lines
Quoted 64 lines
net/core/sock_reuseport.c +5 -0
+       reuse->has_conns = 1;
+       spin_unlock(&reuseport_lock);
+}
+EXPORT_SYMBOL(reuseport_has_conns_set);
+
 static int reuseport_sock_index(struct sock *sk,
                                const struct sock_reuseport *reuse,
                                bool closed)
net/ipv4/datagram.c +1 -1
index 405a8c2aea64..5e66add7befa 100644
@@ -70,7 +70,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
        }
        inet->inet_daddr = fl4->daddr;
        inet->inet_dport = usin->sin_port;
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
        inet->inet_id = prandom_u32();
net/ipv4/udp.c +1 -1
index d63118ce5900..29228231b058 100644
@@ -448,7 +448,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
net/ipv6/datagram.c +1 -1
index df665d4e8f0f..5ecb56522f9d 100644
@@ -256,7 +256,7 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
                goto out;
        }
 
-       reuseport_has_conns(sk, true);
+       reuseport_has_conns_set(sk);
        sk->sk_state = TCP_ESTABLISHED;
        sk_set_txhash(sk);
 out:
net/ipv6/udp.c +1 -2
index 91e795bb9ade..56e4523a3004 100644
@@ -182,7 +182,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                        result = lookup_reuseport(net, sk, skb,
                                                  saddr, sport, daddr, hnum);
                        /* Fall back to scoring if group has connections */
-                       if (result && !reuseport_has_conns(sk, false))
+                       if (result && !reuseport_has_conns(sk))
                                return result;
 
                        result = result ? : sk;
--

2.30.2

#5 lore raw
In reply to Eric Dumazet
Quoted 8 lines · Eric Dumazet
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
  static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                                struct sock_reuseport *reuse, bool bind_inany);
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 31

Quoted 8 lines
net/core/sock_reuseport.c +8 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);

It seems other paths are still using the spin_lock_bh(). It will be useful to have a few words here why _bh() is not needed.

Quoted 23 lines
Quoted 23 lines
Quoted 6 lines

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 2 lines
net/core/sock_reuseport.c +2 -0
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Previous test was performed without reuseport_lock being held.

Usually, sk_reuseport_cb is changed under lock_sock().

The only exception is reuseport_grow() & TCP reqsk migration case.

1) shutdown() TCP listener, which is moved into the latter part of

    reuse->socks[] to migrate reqsk.

2) New listen() overflows reuse->socks[] and call reuseport_grow().

3) reuse->max_socks overflows u16 with the new listener.

4) reuseport_grow() pops the old shutdown()ed listener from the array

    and update its sk->sk_reuseport_cb as NULL without lock_sock().

shutdown()ed sk->sk_reuseport_cb can be changed without lock_sock().

Quoted 8 lines
Quoted 2 lines

But, reuseport_has_conns_set() is called only for UDP and under lock_sock(), so @reuse never be NULL in this case.

Given the complexity of this code and how much time is needed to review all possibilities, please add an additional

if (reuse)

    reuse->has_conns = 1;

+1

#6 lore raw
From:   Martin KaFai Lau <[email protected]>
Date:   Thu, 13 Oct 2022 10:41:53 -0700
Quoted 9 lines · Eric Dumazet
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
net/core/sock_reuseport.c +0 -0
index 5daa1fa54249..abb414ed4aa7 100644
@@ -21,6 +21,21 @@ static DEFINE_IDA(reuseport_ida);
  static int reuseport_resurrect(struct sock *sk, struct sock_reuseport *old_reuse,
                                struct sock_reuseport *reuse, bool bind_inany);
 
Quoted 11 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 31

Quoted 8 lines
net/core/sock_reuseport.c +8 -0
+void reuseport_has_conns_set(struct sock *sk)
+{
+       struct sock_reuseport *reuse;
+
+       if (!rcu_access_pointer(sk->sk_reuseport_cb))
+               return;
+
+       spin_lock(&reuseport_lock);

It seems other paths are still using the spin_lock_bh(). It will be useful to have a few words here why _bh() is not needed.

I think I forgot to add _bh(), but I'm now wondering what is the hlist lock mentioned in reuseport_alloc()...

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 6 lines

net/core/sock_reuseport.c static DEFINE_IDA(reuseport_ida); · line 33

Quoted 2 lines
net/core/sock_reuseport.c +2 -0
+       reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
+                                         lockdep_is_held(&reuseport_lock));

Could @reuse be NULL at this point ?

Previous test was performed without reuseport_lock being held.

Usually, sk_reuseport_cb is changed under lock_sock().

The only exception is reuseport_grow() & TCP reqsk migration case.

1) shutdown() TCP listener, which is moved into the latter part of

    reuse->socks[] to migrate reqsk.

2) New listen() overflows reuse->socks[] and call reuseport_grow().

3) reuse->max_socks overflows u16 with the new listener.

4) reuseport_grow() pops the old shutdown()ed listener from the array

    and update its sk->sk_reuseport_cb as NULL without lock_sock().

shutdown()ed sk->sk_reuseport_cb can be changed without lock_sock().

But, reuseport_has_conns_set() is called only for UDP and under lock_sock(), so @reuse never be NULL in this case.

Quoted 7 lines
Quoted 5 lines

Given the complexity of this code and how much time is needed to review all possibilities, please add an additional

if (reuse)

    reuse->has_conns = 1;

+1

Acked.

Thank you.