* [PATCH net v2 0/3] udp: Fix bind conflict check bypasses
@ 2026-11-30 5:07 Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-30 5:07 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni
Cc: horms, kuniyu, willemb, willemdebruijn.kernel, dsahern, idosch,
shuah, tpluszz77, brauner, alice.kernel, kylebot, joe, leitao,
xudu, netdev, linux-kselftest, linux-kernel
An unprivileged user can get a UDP socket bound to an address and port
that another user's socket holds, in a state bind() would have refused,
and receive that socket's datagrams. This series fixes two bugs that
allow that, both through the bound device, and adds a selftest.
These are sibling vulnerabilities of the TCP one fixed by my
commit f675070191f2 ("tcp: Fix the SO_REUSEPORT bind cache admitting a
conflicting socket"). Both predate git. They were discovered during an
LLM-based audit.
v2:
- Drop v1 patch 1, which commit d77700039d09 ("udp: only quit inuse
scan early on identical SO_REUSEPORT sockets") fixed, and rebase on
it (Willem).
- Patch 1: take the socket lock on IPv6 ping, raw and L2TP disconnect,
which now writes sk_userlocks (Willem).
- Patch 1: move the VRF behaviour change from the notes into the
commit message (Willem).
- Patch 2: read the device of the socket being bound directly and load
the other socket's device once (Willem).
- Patch 3: keep the SO_REUSEPORT tests and update the commit message
(Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-1-juanagusnicco@gmail.com/
Juan Agustin Niccolini (3):
udp: Keep the bound device on disconnect unless connect() set it
udp: Ignore a connect()-set device in the bind conflict check
selftests: net: Add UDP bind conflict tests
include/net/ipv6.h | 1 +
include/net/sock.h | 4 +
net/core/sock.c | 3 +
net/ipv4/udp.c | 55 ++++-
net/ipv6/datagram.c | 35 ++-
net/ipv6/ping.c | 2 +-
net/ipv6/raw.c | 2 +-
net/l2tp/l2tp_ip6.c | 2 +-
tools/testing/selftests/net/.gitignore | 1 +
tools/testing/selftests/net/Makefile | 1 +
.../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++
11 files changed, 317 insertions(+), 17 deletions(-)
create mode 100644 tools/testing/selftests/net/udp_bind_conflict.c
base-commit: fad0d57b12f6979bc0127f0a4f704c6d7177ca78
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it
2026-11-30 5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
@ 2026-11-30 5:07 ` Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2026-11-30 5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
2 siblings, 1 reply; 7+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-30 5:07 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni
Cc: horms, kuniyu, willemb, willemdebruijn.kernel, dsahern, idosch,
shuah, tpluszz77, brauner, alice.kernel, kylebot, joe, leitao,
xudu, netdev, linux-kselftest, linux-kernel
__udp_disconnect() clears sk_bound_dev_if unconditionally, without a
bind conflict check.
This is only safe for a device that connect() set, which
__ip6_datagram_connect() does for a link-local or multicast peer. The
device can also come from:
- The user, with SO_BINDTODEVICE, SO_BINDTOIFINDEX or a bind() to a
scoped IPv6 address.
- A cgroup program, as with ip vrf exec.
The bind conflict checks treat sockets bound to different devices as
not overlapping. As a result, an unprivileged user can bind an address
and port that another user's socket holds on another device, clear its
own device, and receive the other socket's datagrams:
victim: SO_BINDTODEVICE(lo), bind(0.0.0.0:P)
attacker: SO_BINDTODEVICE(eth0), bind(127.0.0.1:P)
attacker: connect(AF_UNSPEC) /* clears the device */
Record in sk_userlocks that connect() set the device of a socket that
had none, and only clear the device on disconnect in that case.
SO_BINDTODEVICE and SO_BINDTOIFINDEX drop the record.
Disconnect now writes sk_userlocks, which ping, raw and L2TP sockets
reach without the socket lock. Take it in their IPv6 disconnect
callbacks, as udp_disconnect() does. IPv4 sockets never get the record.
Note that this changes what disconnect does in two cases, for UDP, ping,
raw and L2TP sockets, which all use __udp_disconnect(). A program that
relied on disconnect to clear a device it set itself now finds the
device still there, and needs CAP_NET_RAW to change it, as on a socket
that never connected. And a VRF socket that connect() moved from the
master to one of its interfaces now keeps the interface after
disconnect, where it used to leave the VRF. Putting the master back is
not safe, because another socket may have bound the same port on the
master in the meantime.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
v2:
- Take the socket lock on IPv6 ping, raw and L2TP disconnect (Willem).
- Move the VRF note into the commit message (Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-3-juanagusnicco@gmail.com/
include/net/ipv6.h | 1 +
include/net/sock.h | 4 ++++
net/core/sock.c | 1 +
net/ipv4/udp.c | 13 +++++++++----
net/ipv6/datagram.c | 27 +++++++++++++++++++++++++--
net/ipv6/ping.c | 2 +-
net/ipv6/raw.c | 2 +-
net/l2tp/l2tp_ip6.c | 2 +-
8 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/include/net/ipv6.h b/include/net/ipv6.h
index 3de07e738538..b55b4dd73593 100644
--- a/include/net/ipv6.h
+++ b/include/net/ipv6.h
@@ -1149,6 +1149,7 @@ int ipv6_getsockopt(struct sock *sk, int level, int optname,
int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,
int addr_len);
int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr, int addr_len);
+int ip6_datagram_disconnect(struct sock *sk, int flags);
int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *addr,
int addr_len);
int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
diff --git a/include/net/sock.h b/include/net/sock.h
index 3144efad8c36..478c6366d054 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1534,6 +1534,10 @@ static inline int __sk_prot_rehash(struct sock *sk)
* define SOCK_CONNECT_BIND - &sock->sk_userlocks flag for auto-bind at connect() time
*/
#define SOCK_CONNECT_BIND 16
+/**
+ * define SOCK_CONNECT_DEV - &sock->sk_userlocks flag for a connect()-set device
+ */
+#define SOCK_CONNECT_DEV 32
struct socket_alloc {
struct socket socket;
diff --git a/net/core/sock.c b/net/core/sock.c
index 5675053625e6..8d5ed878fab7 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -650,6 +650,7 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
/* Paired with all READ_ONCE() done locklessly. */
WRITE_ONCE(sk->sk_bound_dev_if, ifindex);
+ WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
if (sk->sk_prot->rehash)
sk->sk_prot->rehash(sk);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index d784faf939c3..ff4f4845f2d0 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2196,6 +2196,7 @@ static int udp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
int __udp_disconnect(struct sock *sk, int flags)
{
+ u8 userlocks = READ_ONCE(sk->sk_userlocks);
struct inet_sock *inet = inet_sk(sk);
/*
* 1003.1g - break association.
@@ -2205,15 +2206,19 @@ int __udp_disconnect(struct sock *sk, int flags)
WRITE_ONCE(inet->inet_daddr, 0);
inet->inet_dport = 0;
sock_rps_reset_rxhash(sk);
- WRITE_ONCE(sk->sk_bound_dev_if, 0);
- if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
+ if (userlocks & SOCK_CONNECT_DEV) {
+ WRITE_ONCE(sk->sk_bound_dev_if, 0);
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+ }
+ if (!(userlocks & SOCK_BINDADDR_LOCK)) {
inet_reset_saddr(sk);
if (sk->sk_prot->rehash &&
- (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+ (userlocks & SOCK_BINDPORT_LOCK))
sk->sk_prot->rehash(sk);
}
- if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
+ if (!(userlocks & SOCK_BINDPORT_LOCK)) {
sk->sk_prot->unhash(sk);
inet->inet_sport = 0;
}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..9fbb78aa9d68 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -29,6 +29,7 @@
#include <net/tcp_states.h>
#include <net/dsfield.h>
#include <net/sock_reuseport.h>
+#include <net/udp.h>
#include <linux/errqueue.h>
#include <linux/uaccess.h>
@@ -139,6 +140,14 @@ void ip6_datagram_release_cb(struct sock *sk)
}
EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
+static void ip6_datagram_connect_dev(struct sock *sk, int old_dif, int dif)
+{
+ if (!old_dif && dif)
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks | SOCK_CONNECT_DEV);
+ WRITE_ONCE(sk->sk_bound_dev_if, dif);
+}
+
int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
int addr_len)
{
@@ -229,11 +238,13 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
err = -EINVAL;
goto out;
}
- WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id);
+ ip6_datagram_connect_dev(sk, old_bound_dev_if,
+ usin->sin6_scope_id);
}
if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
- WRITE_ONCE(sk->sk_bound_dev_if, READ_ONCE(np->mcast_oif));
+ ip6_datagram_connect_dev(sk, old_bound_dev_if,
+ READ_ONCE(np->mcast_oif));
/* Connect to link-local address requires an interface */
if (!sk->sk_bound_dev_if) {
@@ -265,6 +276,9 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
np->flow_label = old_fl6_flowlabel;
inet->inet_dport = old_dport;
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
+ if (!old_bound_dev_if)
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks & ~SOCK_CONNECT_DEV);
goto out;
}
@@ -287,6 +301,15 @@ int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int ad
}
EXPORT_SYMBOL_GPL(ip6_datagram_connect);
+int ip6_datagram_disconnect(struct sock *sk, int flags)
+{
+ lock_sock(sk);
+ __udp_disconnect(sk, flags);
+ release_sock(sk);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(ip6_datagram_disconnect);
+
int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *uaddr,
int addr_len)
{
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index 6e90d0bf9f3d..ac61d809de61 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -200,7 +200,7 @@ struct proto pingv6_prot = {
.close = ping_close,
.pre_connect = ping_v6_pre_connect,
.connect = ip6_datagram_connect_v6_only,
- .disconnect = __udp_disconnect,
+ .disconnect = ip6_datagram_disconnect,
.setsockopt = ipv6_setsockopt,
.getsockopt = ipv6_getsockopt,
.sendmsg = ping_v6_sendmsg,
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index 00d832c7cdae..10cb28d864d5 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -1214,7 +1214,7 @@ struct proto rawv6_prot = {
.close = rawv6_close,
.destroy = raw6_destroy,
.connect = ip6_datagram_connect_v6_only,
- .disconnect = __udp_disconnect,
+ .disconnect = ip6_datagram_disconnect,
.ioctl = rawv6_ioctl,
.init = rawv6_init_sk,
.setsockopt = rawv6_setsockopt,
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index bdaae1b64d25..36f79b098113 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -441,7 +441,7 @@ static int l2tp_ip6_disconnect(struct sock *sk, int flags)
if (sock_flag(sk, SOCK_ZAPPED))
return 0;
- return __udp_disconnect(sk, flags);
+ return ip6_datagram_disconnect(sk, flags);
}
static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check
2026-11-30 5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
@ 2026-11-30 5:07 ` Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2026-11-30 5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
2 siblings, 1 reply; 7+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-30 5:07 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni
Cc: horms, kuniyu, willemb, willemdebruijn.kernel, dsahern, idosch,
shuah, tpluszz77, brauner, alice.kernel, kylebot, joe, leitao,
xudu, netdev, linux-kselftest, linux-kernel
The bind conflict checks compare sk_bound_dev_if, which connect() also
writes, for a link-local or multicast IPv6 peer.
This is only safe if the device is the one that bind() reserved. A
socket bound with no device holds its port on every device, but while
it is connected to such a peer it looks bound to one device, so a bind
of the same port on another device is admitted.
As a result, an unprivileged user can bind a port that another user's
socket holds, and receive the datagrams that arrive on its device once
that socket disconnects:
victim: bind([::]:P), connect(fe80::1%eth0)
attacker: SO_BINDTODEVICE(eth1), bind([::]:P) /* should fail */
victim: connect(AF_UNSPEC)
Compare the device that bind() reserved instead: none for a socket
whose device connect() set, which SOCK_CONNECT_DEV records. A VRF
master that connect() narrowed to one of its interfaces does not get
the flag and is compared as before.
Note that the checks read the flag without the other socket's lock, so
it is ordered against the device with barriers: seeing the device
before the flag would trust a connect()-set device again.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
v2:
- Read the device of the socket being bound directly, and load the
other socket's device once and pass it to udp_sk_reuseport_match()
(Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-4-juanagusnicco@gmail.com/
Notes:
- A connect() that sets the device and is undone again, both between
the two reads of the flag, can still be seen as a bound device, so a
conflicting bind can be admitted. Not fixed here because closing it
needs a sequence count in struct sock, a larger change for a window
that the other socket's owner has to open within two loads.
- connect() also narrows the local address of a wildcard-bound
socket, which opens the same window through the address. It is not
fixed here: connect() moves the socket to the secondary hash slot of
the narrowed address, so a specific-address bind on a busy port never
scans it. Closing that needs either a full scan on every bind or no
rehash on connect, which slows down binds or lookups on busy ports,
so I leave it for a separate patch.
net/core/sock.c | 2 ++
net/ipv4/udp.c | 42 ++++++++++++++++++++++++++++++++++--------
net/ipv6/datagram.c | 12 ++++++++++--
3 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/net/core/sock.c b/net/core/sock.c
index 8d5ed878fab7..f55249f11da1 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -650,6 +650,8 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
/* Paired with all READ_ONCE() done locklessly. */
WRITE_ONCE(sk->sk_bound_dev_if, ifindex);
+ /* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
if (sk->sk_prot->rehash)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index ff4f4845f2d0..6807ce5f3e2e 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -131,11 +131,28 @@ EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc);
#define MAX_UDP_PORTS 65536
#define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET)
-static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2)
+static int udp_bound_dev_if(const struct sock *sk)
+{
+ int dif;
+
+ if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
+ return 0;
+ /* Pairs with the smp_wmb() before SOCK_CONNECT_DEV is cleared. */
+ smp_rmb();
+ dif = READ_ONCE(sk->sk_bound_dev_if);
+ /* Pairs with the smp_wmb() after SOCK_CONNECT_DEV is set. */
+ smp_rmb();
+ if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
+ return 0;
+ return dif;
+}
+
+static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2,
+ int dif2)
{
return sk2->sk_family == sk->sk_family &&
ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
- sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
+ dif2 == sk->sk_bound_dev_if &&
sk2->sk_reuse == sk->sk_reuse &&
inet_rcv_saddr_equal(sk, sk2, false);
}
@@ -157,14 +174,18 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
sk2 != sk &&
(bitmap || udp_sk(sk2)->udp_port_hash == num) &&
(bitmap || !sk2->sk_reuse || !sk->sk_reuse) &&
- (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
- sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
inet_rcv_saddr_equal(sk, sk2, true)) {
+ int dif2 = udp_bound_dev_if(sk2);
+
+ if (dif2 && sk->sk_bound_dev_if &&
+ dif2 != sk->sk_bound_dev_if)
+ continue;
if (!bitmap) {
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
uid_eq(uid, sk_uid(sk2))) {
- if (udp_sk_reuseport_match(sk, sk2))
+ if (udp_sk_reuseport_match(sk, sk2,
+ dif2))
return 0;
continue;
}
@@ -194,13 +215,16 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
sk2 != sk &&
(udp_sk(sk2)->udp_port_hash == num) &&
(!sk2->sk_reuse || !sk->sk_reuse) &&
- (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
- sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
inet_rcv_saddr_equal(sk, sk2, true)) {
+ int dif2 = udp_bound_dev_if(sk2);
+
+ if (dif2 && sk->sk_bound_dev_if &&
+ dif2 != sk->sk_bound_dev_if)
+ continue;
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
uid_eq(uid, sk_uid(sk2))) {
- if (udp_sk_reuseport_match(sk, sk2)) {
+ if (udp_sk_reuseport_match(sk, sk2, dif2)) {
res = 0;
break;
}
@@ -2208,6 +2232,8 @@ int __udp_disconnect(struct sock *sk, int flags)
sock_rps_reset_rxhash(sk);
if (userlocks & SOCK_CONNECT_DEV) {
WRITE_ONCE(sk->sk_bound_dev_if, 0);
+ /* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 9fbb78aa9d68..89870e142835 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -142,9 +142,12 @@ EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
static void ip6_datagram_connect_dev(struct sock *sk, int old_dif, int dif)
{
- if (!old_dif && dif)
+ if (!old_dif && dif) {
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks | SOCK_CONNECT_DEV);
+ /* Pairs with the second smp_rmb() in udp_bound_dev_if(). */
+ smp_wmb();
+ }
WRITE_ONCE(sk->sk_bound_dev_if, dif);
}
@@ -276,9 +279,14 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
np->flow_label = old_fl6_flowlabel;
inet->inet_dport = old_dport;
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
- if (!old_bound_dev_if)
+ if (!old_bound_dev_if) {
+ /* Pairs with the first smp_rmb() in
+ * udp_bound_dev_if().
+ */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+ }
goto out;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests
2026-11-30 5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
@ 2026-11-30 5:07 ` Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2 siblings, 1 reply; 7+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-30 5:07 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni
Cc: horms, kuniyu, willemb, willemdebruijn.kernel, dsahern, idosch,
shuah, tpluszz77, brauner, alice.kernel, kylebot, joe, leitao,
xudu, netdev, linux-kselftest, linux-kernel
Add tests for the two bind conflict fixes in this series and for the
SO_REUSEPORT one in commit d77700039d09 ("udp: only quit inuse scan
early on identical SO_REUSEPORT sockets"):
- reuseport_scan_{address,device,hash2}: a SO_REUSEPORT bind is refused
when a conflicting socket sits behind a compatible one, on the primary
and on the secondary hash.
- disconnect_keeps_{device,scoped_device}: connect(AF_UNSPEC) keeps a
device set with SO_BINDTOIFINDEX or with the scope id of bind().
- connect_device_ignored: a device set by connect() does not let a
socket on another device bind next to it.
reuseport_share and disconnect_clears_connect_device check that the
sharing and the clearing that should still happen still do.
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
v2:
- Keep the SO_REUSEPORT tests, now for commit d77700039d09, and update
the commit message (Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-5-juanagusnicco@gmail.com/
tools/testing/selftests/net/.gitignore | 1 +
tools/testing/selftests/net/Makefile | 1 +
.../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++
3 files changed, 230 insertions(+)
create mode 100644 tools/testing/selftests/net/udp_bind_conflict.c
diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore
index 27761239d069..ff75923ef369 100644
--- a/tools/testing/selftests/net/.gitignore
+++ b/tools/testing/selftests/net/.gitignore
@@ -58,6 +58,7 @@ tools
tun
txring_overwrite
txtimestamp
+udp_bind_conflict
udp_splice_checksum
udpgso
udpgso_bench_rx
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index baea3e7f0f8f..fc8fe8465038 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -206,6 +206,7 @@ TEST_GEN_PROGS := \
tap \
tcp_port_share \
tls \
+ udp_bind_conflict \
udp_splice_checksum \
# end of TEST_GEN_PROGS
diff --git a/tools/testing/selftests/net/udp_bind_conflict.c b/tools/testing/selftests/net/udp_bind_conflict.c
new file mode 100644
index 000000000000..9a67f58c348a
--- /dev/null
+++ b/tools/testing/selftests/net/udp_bind_conflict.c
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Check that bind() refuses a UDP socket that overlaps another one in
+ * the cases that SO_REUSEPORT, disconnect and connect() used to let
+ * through.
+ */
+
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define PORT 34567
+#define MAX_FDS 32
+
+FIXTURE(udp_bind_conflict)
+{
+ int fd[MAX_FDS];
+ int nr_fds;
+ int lo;
+ int d0;
+};
+
+FIXTURE_SETUP(udp_bind_conflict)
+{
+ ASSERT_EQ(unshare(CLONE_NEWNET), 0);
+ ASSERT_EQ(system("ip link set dev lo up"), 0);
+ ASSERT_EQ(system("ip link add d0 type dummy"), 0);
+ ASSERT_EQ(system("ip link set dev d0 up"), 0);
+ ASSERT_EQ(system("ip addr add fe80::1/64 dev d0 nodad"), 0);
+
+ self->lo = if_nametoindex("lo");
+ self->d0 = if_nametoindex("d0");
+ ASSERT_NE(self->lo, 0);
+ ASSERT_NE(self->d0, 0);
+ self->nr_fds = 0;
+}
+
+FIXTURE_TEARDOWN(udp_bind_conflict)
+{
+ while (self->nr_fds)
+ close(self->fd[--self->nr_fds]);
+}
+
+/* Bind a UDP socket to addr:PORT, on device @ifindex if not 0: through
+ * the scope id for a link-local address, SO_BINDTOIFINDEX otherwise.
+ * Returns the socket, or -errno.
+ */
+static int udp_bind(FIXTURE_DATA(udp_bind_conflict) *self, const char *addr,
+ int ifindex, bool reuseport)
+{
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in v4;
+ struct sockaddr_in6 v6;
+ } sa = {};
+ socklen_t len;
+ int one = 1;
+ int fd, err;
+
+ if (self->nr_fds == MAX_FDS)
+ return -ENOSPC;
+
+ if (inet_pton(AF_INET, addr, &sa.v4.sin_addr) == 1) {
+ sa.v4.sin_family = AF_INET;
+ sa.v4.sin_port = htons(PORT);
+ len = sizeof(sa.v4);
+ } else if (inet_pton(AF_INET6, addr, &sa.v6.sin6_addr) == 1) {
+ sa.v6.sin6_family = AF_INET6;
+ sa.v6.sin6_port = htons(PORT);
+ len = sizeof(sa.v6);
+ if (IN6_IS_ADDR_LINKLOCAL(&sa.v6.sin6_addr)) {
+ sa.v6.sin6_scope_id = ifindex;
+ ifindex = 0;
+ }
+ } else {
+ return -EINVAL;
+ }
+
+ fd = socket(sa.sa.sa_family, SOCK_DGRAM, 0);
+ if (fd < 0)
+ return -errno;
+
+ if ((reuseport &&
+ setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) ||
+ (ifindex &&
+ setsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX,
+ &ifindex, sizeof(ifindex))) ||
+ bind(fd, &sa.sa, len)) {
+ err = -errno;
+ close(fd);
+ return err;
+ }
+
+ self->fd[self->nr_fds++] = fd;
+ return fd;
+}
+
+static int disconnect(int fd)
+{
+ struct sockaddr sa = { .sa_family = AF_UNSPEC };
+
+ return connect(fd, &sa, sizeof(sa));
+}
+
+/* connect() to a link-local peer, which sets the device to @ifindex */
+static int connect_link_local(int fd, int ifindex)
+{
+ struct sockaddr_in6 sa = {
+ .sin6_family = AF_INET6,
+ .sin6_port = htons(PORT),
+ .sin6_scope_id = ifindex,
+ };
+
+ inet_pton(AF_INET6, "fe80::2", &sa.sin6_addr);
+ return connect(fd, (struct sockaddr *)&sa, sizeof(sa));
+}
+
+static int bound_ifindex(int fd)
+{
+ socklen_t len = sizeof(int);
+ int ifindex;
+
+ if (getsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX, &ifindex, &len))
+ return -errno;
+ return ifindex;
+}
+
+/* The conflict scan used to stop at the first SO_REUSEPORT socket of the
+ * same owner that overlapped the new one, before reaching 127.0.0.2.
+ */
+TEST_F(udp_bind_conflict, reuseport_scan_address)
+{
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", 0, false));
+ ASSERT_LE(0, udp_bind(self, "127.0.0.3", 0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true));
+}
+
+TEST_F(udp_bind_conflict, reuseport_scan_device)
+{
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->d0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true));
+}
+
+/* More than 10 sockets on the port: the secondary hash is scanned. */
+TEST_F(udp_bind_conflict, reuseport_scan_hash2)
+{
+ char addr[INET_ADDRSTRLEN];
+ int i;
+
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->lo, false));
+ for (i = 3; i < 14; i++) {
+ snprintf(addr, sizeof(addr), "127.0.0.%d", i);
+ ASSERT_LE(0, udp_bind(self, addr, 0, true));
+ }
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->d0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.2", 0, true));
+}
+
+/* Sockets of one owner that SO_REUSEPORT lets share a port still can. */
+TEST_F(udp_bind_conflict, reuseport_share)
+{
+ int i;
+
+ for (i = 0; i < 16; i++)
+ ASSERT_LE(0, udp_bind(self, "127.0.0.1", 0, true));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", 0, true));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, true));
+ ASSERT_LE(0, udp_bind(self, "127.0.0.1", self->d0, true));
+}
+
+/* Disconnect used to clear the device the socket was bound to, leaving it
+ * on top of 0.0.0.0%lo where bind() had refused it.
+ */
+TEST_F(udp_bind_conflict, disconnect_keeps_device)
+{
+ int fd;
+
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.1", 0, false));
+
+ fd = udp_bind(self, "127.0.0.1", self->d0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+}
+
+TEST_F(udp_bind_conflict, disconnect_keeps_scoped_device)
+{
+ int fd;
+
+ fd = udp_bind(self, "fe80::1", self->d0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+}
+
+/* A device that connect() set is still cleared by disconnect. */
+TEST_F(udp_bind_conflict, disconnect_clears_connect_device)
+{
+ int fd;
+
+ fd = udp_bind(self, "::", 0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, connect_link_local(fd, self->d0));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(0, bound_ifindex(fd));
+}
+
+/* A device that connect() set used to count in the conflict check, so a
+ * socket on another device could bind next to [::]:PORT and overlap it
+ * once it disconnected.
+ */
+TEST_F(udp_bind_conflict, connect_device_ignored)
+{
+ int fd;
+
+ fd = udp_bind(self, "::", 0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, connect_link_local(fd, self->d0));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "::", self->lo, false));
+}
+
+TEST_HARNESS_MAIN
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
@ 2026-12-01 5:29 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 5:29 UTC (permalink / raw)
To: juanagusnicco
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu, willemb,
willemdebruijn.kernel, dsahern, idosch, shuah, tpluszz77, brauner,
alice.kernel, kylebot, joe, leitao, xudu, netdev, linux-kselftest,
linux-kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] After the whole series, disconnect can still widen a
connect()-narrowed device back to 'all devices' with no conflict…
--
udp: Keep the bound device on disconnect unless connect() set it
When __ip6_datagram_connect() sets the device of a socket that had none,
this commit records that in sk_userlocks as SOCK_CONNECT_DEV.
__udp_disconnect() then clears sk_bound_dev_if only when that flag is set.
IPv6 ping, raw and L2TP sockets now use a new ip6_datagram_disconnect(),
which takes the socket lock around __udp_disconnect().
> diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
> index bdaae1b64d25a..36f79b098113b 100644
> --- a/net/l2tp/l2tp_ip6.c
> +++ b/net/l2tp/l2tp_ip6.c
> @@ -441,7 +441,7 @@ static int l2tp_ip6_disconnect(struct sock *sk, int flags)
> if (sock_flag(sk, SOCK_ZAPPED))
> return 0;
>
> - return __udp_disconnect(sk, flags);
> + return ip6_datagram_disconnect(sk, flags);
> }
[Severity: High]
Can L2TP/IPv6 sockets still be used for the takeover that the commit
message describes?
This path clears a device set by connect() without a conflict check. That
is only safe if every bind conflict check ignores devices set by connect().
__l2tp_ip6_bind_lookup() still compares against the live device:
net/l2tp/l2tp_ip6.c:__l2tp_ip6_bind_lookup() {
...
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
if (bound_dev_if && dif && bound_dev_if != dif)
continue;
...
}
IPPROTO_L2TP sockets are SOCK_DGRAM, so CAP_NET_RAW is not needed. Could
an unprivileged user run this sequence?
1) Attacker A binds L2TP/IPv6 conn_id C with no device.
2) A connects to fe80::x with scope_id=eth0. The call chain
l2tp_ip6_connect()->__ip6_datagram_connect()->ip6_datagram_connect_dev()
sets sk_bound_dev_if=eth0 and SOCK_CONNECT_DEV.
3) While A is connected, victim V calls SO_BINDTODEVICE(eth1) and binds
conn_id C. l2tp_ip6_bind() calls __l2tp_ip6_bind_lookup() with
dif=eth1. A is skipped because its live device is eth0, so V's bind
succeeds.
4) A calls connect(AF_UNSPEC). The call chain
l2tp_ip6_disconnect()->ip6_datagram_disconnect()->__udp_disconnect()
sees SOCK_CONNECT_DEV and sets sk_bound_dev_if=0.
l2tp_ip6_unhash() only touches sk_node, so A stays in
l2tp_ip6_bind_table. Without SOCK_BINDADDR_LOCK, inet_reset_saddr()
also turns A's local address into a wildcard.
5) A connects to a non-scoped peer P. No device is set, and A moves to the
head of the bind list. On receive, __l2tp_ip6_bind_lookup() then returns
A ahead of V for conn_id C traffic from P, on any device.
A later commit in this series, "udp: Ignore a connect()-set device in the
bind conflict check", fixes the UDP side. It changes udp_lib_lport_inuse()
and udp_lib_lport_inuse2() to use udp_bound_dev_if(), which returns 0 for
a SOCK_CONNECT_DEV socket.
I couldn't find a matching change to __l2tp_ip6_bind_lookup() anywhere in
the series. Should L2TP/IPv6 get the same treatment, given that this patch
moves it to the new disconnect behaviour?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130050729.893-1-juanagusnicco%40gmail.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check
2026-11-30 5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
@ 2026-12-01 5:29 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 5:29 UTC (permalink / raw)
To: juanagusnicco
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu, willemb,
willemdebruijn.kernel, dsahern, idosch, shuah, tpluszz77, brauner,
alice.kernel, kylebot, joe, leitao, xudu, netdev, linux-kselftest,
linux-kernel
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix covers UDP but not L2TP-over-IPv6, which has the same
bug.
- [Medium] The commit message presents the cross-user takeover as closed,
but the same victim sequence still allows an equivalent takeover…
- [Medium] The patch changed udp_sk_reuseport_match() to compare dif2 =
udp_bound_dev_if(sk2), the device bind() reserved, which is 0 for a…
- [Medium] udp_bound_dev_if() reads another socket's SOCK_CONNECT_DEV
flag, then its sk_bound_dev_if, then the flag again, without that…
- [Low] The patch adds lockless READ_ONCE(sk2->sk_userlocks) reads of
other, already-hashed sockets in udp_bound_dev_if(), called from…
--
udp: Ignore a connect()-set device in the bind conflict check
This patch makes the UDP bind conflict checks in udp_lib_lport_inuse() and
udp_lib_lport_inuse2() ignore a device that connect() set, which
SOCK_CONNECT_DEV records. A wildcard-device socket connected to a
link-local or multicast IPv6 peer therefore still conflicts with binds on
other devices. The flag and sk_bound_dev_if of the other socket are read
locklessly through a new udp_bound_dev_if() helper, ordered by
smp_rmb()/smp_wmb() pairs.
> As a result, an unprivileged user can bind a port that another user's
> socket holds, and receive the datagrams that arrive on its device once
> that socket disconnects:
>
> victim: bind([::]:P), connect(fe80::1%eth0)
> attacker: SO_BINDTODEVICE(eth1), bind([::]:P) /* should fail */
> victim: connect(AF_UNSPEC)
[Severity: Medium]
The commit message reads as if this takeover is now closed. Can the same
victim sequence still be used through the local address instead of the
device?
When the victim does bind([::]:P) and then connect(fe80::1%eth0),
__ip6_datagram_connect()->ip6_datagram_dst_update() narrows the wildcard
address:
net/ipv6/datagram.c:ip6_datagram_dst_update() {
...
if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
sk->sk_v6_rcv_saddr = fl6->saddr;
inet->inet_rcv_saddr = LOOPBACK4_IPV6;
...
}
Say the attacker skips SO_BINDTODEVICE and binds a specific address on
port P, such as eth1's global address. udp_lib_lport_inuse() then calls
inet_rcv_saddr_equal(sk, sk2, true) against the victim's narrowed address,
finds no match and reports no conflict. The new udp_bound_dev_if() check
doesn't come into play.
On connect(AF_UNSPEC), __udp_disconnect() restores the wildcard, because
SOCK_BINDADDR_LOCK is not set for a wildcard bind:
if (!(userlocks & SOCK_BINDADDR_LOCK)) {
inet_reset_saddr(sk);
After that the attacker's more specific socket wins for datagrams to that
address. IPv4 looks the same, since __ip4_datagram_connect() narrows
inet_rcv_saddr.
The notes below the --- line mention this. However, they won't reach git
history or the stable backporters that Cc: stable brings in. Could the
commit message say that the address-based variant is still open?
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index ff4f4845f2d0e..6807ce5f3e2e4 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -131,11 +131,28 @@ EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc);
> #define MAX_UDP_PORTS 65536
> #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET)
>
> -static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2)
> +static int udp_bound_dev_if(const struct sock *sk)
> +{
> + int dif;
> +
> + if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
> + return 0;
[Severity: Low]
This isn't a functional bug, but this adds lockless READ_ONCE() reads of
sk_userlocks on other sockets that are already hashed. Several writers of
that field still do plain read-modify-write stores under lock_sock only.
One example is __inet6_bind(), which runs after get_port() has hashed the
socket:
net/ipv6/af_inet6.c:__inet6_bind() {
...
sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
...
}
__inet_bind() and the SO_RCVBUF/SO_SNDBUF/SO_BUF_LOCK paths in
net/core/sock.c do the same. Would KCSAN report a concurrent bind of the
same port on another CPU as a marked-read versus plain-RMW data race?
The field is a u8, and SOCK_CONNECT_DEV only changes under lock_sock, so
there should be no visible runtime effect. inet_diag's
READ_ONCE(sk->sk_userlocks) already follows the same pattern.
> + /* Pairs with the smp_wmb() before SOCK_CONNECT_DEV is cleared. */
> + smp_rmb();
> + dif = READ_ONCE(sk->sk_bound_dev_if);
> + /* Pairs with the smp_wmb() after SOCK_CONNECT_DEV is set. */
> + smp_rmb();
> + if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
> + return 0;
> + return dif;
> +}
[Severity: Medium]
Can a set-then-clear of the device between the two flag loads still be
missed here? The writers hold only sk2's lock_sock, never the hslot->lock
that the reader holds:
CPU1 (attacker bind scan) CPU2 (victim connect)
udp_bound_dev_if(sk2)
flag clear
ip6_datagram_connect_dev()
set SOCK_CONNECT_DEV
sk_bound_dev_if = eth0
dif = eth0
ip6_datagram_dst_update() fails
sk_bound_dev_if = 0
clear SOCK_CONNECT_DEV
flag clear
return eth0
The caller in udp_lib_lport_inuse{,2}() then does:
if (dif2 && sk->sk_bound_dev_if &&
dif2 != sk->sk_bound_dev_if)
continue;
That admits the conflicting eth1 bind while the victim holds the port on
every device.
The whole set-then-clear fits inside one connect() call. It happens when
ip6_datagram_dst_update() fails before any rehash, because the error path
in __ip6_datagram_connect() never takes hslot->lock:
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
if (!old_bound_dev_if) {
smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
For a wildcard-bound victim whose connect succeeds, the rehash blocks on
the reader's hslot->lock. So this needs either the connect error path or
a victim bound to a specific address.
The reader's window can also be longer than two loads. On PREEMPT_RT,
spin_lock_bh(&hslot->lock) is preemptible, and elsewhere hardirqs, NMIs
or vCPU preemption can stretch it. The attacker can also retry its bind
freely.
The notes below --- describe this window as "within two loads", and they
are not part of the commit message. Would it take a sequence count, a
separate field for the bind-reserved device, or taking hslot->lock in the
writers to close it?
> +
> +static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2,
> + int dif2)
> {
> return sk2->sk_family == sk->sk_family &&
> ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
> - sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
> + dif2 == sk->sk_bound_dev_if &&
> sk2->sk_reuse == sk->sk_reuse &&
> inet_rcv_saddr_equal(sk, sk2, false);
> }
> @@ -157,14 +174,18 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
> sk2 != sk &&
> (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
> (bitmap || !sk2->sk_reuse || !sk->sk_reuse) &&
> - (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
> - sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
> inet_rcv_saddr_equal(sk, sk2, true)) {
> + int dif2 = udp_bound_dev_if(sk2);
> +
> + if (dif2 && sk->sk_bound_dev_if &&
> + dif2 != sk->sk_bound_dev_if)
> + continue;
> if (!bitmap) {
> if (sk2->sk_reuseport && sk->sk_reuseport &&
> !rcu_access_pointer(sk->sk_reuseport_cb) &&
> uid_eq(uid, sk_uid(sk2))) {
> - if (udp_sk_reuseport_match(sk, sk2))
> + if (udp_sk_reuseport_match(sk, sk2,
> + dif2))
> return 0;
[Severity: Medium]
Now that dif2 comes from udp_bound_dev_if(), can udp_sk_reuseport_match()
and udp_reuseport_add_sock() disagree about the same sk2?
udp_lib_get_port() calls udp_reuseport_add_sock() right after this scan,
and that function still compares the raw device:
net/ipv4/udp.c:udp_reuseport_add_sock() {
...
(sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
...
return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
}
Take sk2 as a same-uid SO_REUSEPORT socket bound to G:P with no device and
connected to fe80::1%eth0, so dif2 is 0. A new SO_REUSEPORT G:P bind with
no device matches it, and the scan returns 0 here. udp_reuseport_add_sock()
then sees eth0 != 0 and calls reuseport_alloc() for a separate group.
Because of the early return, the rest of the chain is never checked.
Consider a non-reuseport [::]:P socket further down the chain, bound
before sk2 and disconnected afterwards. Before this patch the scan would
continue past sk2, reach that socket and refuse the bind. With this
patch the bind is accepted. Building this across users seems to also need
the existing address-narrowing window.
Commit d77700039d09 ("udp: only quit inuse scan early on identical
SO_REUSEPORT sockets") limited this early exit to sockets that sk will
actually share a group with. Does this bring back an early exit on a
socket sk does not share a group with?
The reverse case looks possible too. If sk is bound to eth0 with
SO_BINDTODEVICE, the match fails (0 != eth0) and the scan continues. But
udp_reuseport_add_sock() then puts sk into sk2's group, which the check
had just treated as not matching.
> continue;
> }
[ ... ]
> diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
> index 9fbb78aa9d686..89870e1428359 100644
> --- a/net/ipv6/datagram.c
> +++ b/net/ipv6/datagram.c
> @@ -142,9 +142,12 @@ EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
>
> static void ip6_datagram_connect_dev(struct sock *sk, int old_dif, int dif)
> {
> - if (!old_dif && dif)
> + if (!old_dif && dif) {
> WRITE_ONCE(sk->sk_userlocks,
> sk->sk_userlocks | SOCK_CONNECT_DEV);
> + /* Pairs with the second smp_rmb() in udp_bound_dev_if(). */
> + smp_wmb();
> + }
> WRITE_ONCE(sk->sk_bound_dev_if, dif);
> }
[Severity: High]
Is L2TP over IPv6 still open to the same takeover?
l2tp_ip6_connect() calls __ip6_datagram_connect(), so this function sets
SOCK_CONNECT_DEV and the device for a link-local peer on L2TP/IPv6
sockets too. l2tp_ip6_disconnect() reaches __udp_disconnect() through
ip6_datagram_disconnect(), which the earlier patch in this series ("udp:
Keep the bound device on disconnect unless connect() set it") changed to
clear that device.
The L2TP bind conflict check still trusts the device that connect() set:
net/l2tp/l2tp_ip6.c:__l2tp_ip6_bind_lookup() {
...
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
if (bound_dev_if && dif && bound_dev_if != dif)
continue;
...
}
udp_bound_dev_if() is static to net/ipv4/udp.c, so only UDP gets the
fix. The sequence from the commit message seems to work with
IPPROTO_L2TP:
victim: bind(conn_id X, ::), connect(fe80::1%eth0)
attacker: SO_BINDTODEVICE(eth1), bind(conn_id X, ::)
victim: connect(AF_UNSPEC)
l2tp_ip6_bind() calls __l2tp_ip6_bind_lookup() with raddr NULL and dif set
to eth1. It skips the victim because eth0 != eth1 and accepts the bind.
After the disconnect, the victim's device goes back to 0. The attacker's
socket was inserted at the head of l2tp_ip6_bind_table after the victim's
connect() re-insert. l2tp_ip6_recv() takes the first match, so the
attacker would receive the L2TP control frames for conn_id X that arrive
on eth1.
An L2TP/IPv6 socket is SOCK_DGRAM and needs no capability, and the module
autoloads. The selftest patch later in the series only touches tools/, so
the kernel code is unchanged here at the end of the series.
Should __l2tp_ip6_bind_lookup() also ignore a SOCK_CONNECT_DEV device,
for example by sharing the helper?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130050729.893-1-juanagusnicco%40gmail.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests
2026-11-30 5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
@ 2026-12-01 5:29 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 5:29 UTC (permalink / raw)
To: juanagusnicco
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu, willemb,
willemdebruijn.kernel, dsahern, idosch, shuah, tpluszz77, brauner,
alice.kernel, kylebot, joe, leitao, xudu, netdev, linux-kselftest,
linux-kernel
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The new selftest tools/testing/selftests/net/udp_bind_conflict.c
does not run several kernel paths added by the two fixes it is meant…
--
selftests: net: Add UDP bind conflict tests
This adds a new kselftest, udp_bind_conflict, that checks how bind()
handles UDP conflicts in three cases: SO_REUSEPORT sockets, disconnect
via connect(AF_UNSPEC), and devices set by connect(). It also adds the
test to the net selftests Makefile and .gitignore.
> diff --git a/tools/testing/selftests/net/udp_bind_conflict.c b/tools/testing/selftests/net/udp_bind_conflict.c
> new file mode 100644
> index 0000000000000..9a67f58c348ab
> --- /dev/null
> +++ b/tools/testing/selftests/net/udp_bind_conflict.c
> @@ -0,0 +1,228 @@
[ ... ]
> +/* A device that connect() set used to count in the conflict check, so a
> + * socket on another device could bind next to [::]:PORT and overlap it
> + * once it disconnected.
> + */
> +TEST_F(udp_bind_conflict, connect_device_ignored)
> +{
> + int fd;
> +
> + fd = udp_bind(self, "::", 0, false);
> + ASSERT_LE(0, fd);
> + ASSERT_EQ(0, connect_link_local(fd, self->d0));
> + ASSERT_EQ(-EADDRINUSE, udp_bind(self, "::", self->lo, false));
> +}
[Severity: Low]
This isn't a bug, but does this test reach the secondary hash half of
the connect() device fix?
Both sockets are bound to the wildcard "::", and there are only two
sockets on the port. inet_use_hash2_on_bind() returns false for
in6addr_any, and hslot->count stays well below 10. That means
udp_lib_get_port() only goes through udp_lib_lport_inuse():
net/ipv4/udp.c:udp_lib_get_port() {
...
if (inet_use_hash2_on_bind(sk) && hslot->count > 10) {
...
}
So no socket whose device was set by connect() ever reaches the
udp_bound_dev_if() call in udp_lib_lport_inuse2(). The same goes for
the dif2 comparison in udp_sk_reuseport_match() on that path.
reuseport_scan_hash2 does scan hash2, but its devices are all set
through SO_BINDTOIFINDEX.
The clearing of SOCK_CONNECT_DEV in sock_bindtoindex_locked() doesn't
seem to be covered either:
net/core/sock.c:sock_bindtoindex_locked() {
...
WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
...
}
The series' disconnect commit describes this as "SO_BINDTODEVICE and
SO_BINDTOIFINDEX drop the record". The only sequence that runs it is
connect() to a link-local peer, then SO_BINDTOIFINDEX, then disconnect.
None of the tests does that:
- disconnect_keeps_device uses an IPv4 socket that never connects, so
it never has SOCK_CONNECT_DEV set.
- disconnect_keeps_scoped_device gets its device from bind().
- disconnect_clears_connect_device and connect_device_ignored never
call SO_BINDTOIFINDEX after connect_link_local().
If the line above were removed, wouldn't every test here still pass?
Two branches in __ip6_datagram_connect() are also not reached. The
first is the rollback when ip6_datagram_dst_update() fails:
err = ip6_datagram_dst_update(sk, true);
if (err) {
...
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
if (!old_bound_dev_if) {
...
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
}
The second is the multicast branch that takes the device from
mcast_oif:
if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
ip6_datagram_connect_dev(sk, old_bound_dev_if,
READ_ONCE(np->mcast_oif));
The commit message says these tests cover "the two bind conflict fixes
in this series". Could the test add cases for these paths? For example:
- a specific address with more than 10 sockets on the port, where one
socket's device was set by connect()
- connect(), then SO_BINDTOIFINDEX, then disconnect
- a connect() that fails
- a connect() to a multicast peer
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130050729.893-1-juanagusnicco%40gmail.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-01 5:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2026-11-30 5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2026-11-30 5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox