udp: Fix bind conflict check bypasses
Subsystems (5)
cover udp: Fix bind conflict check bypasses
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 three bugs that allow that 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"). The one in patch 1 has the same kind of cause: a SO_REUSEPORT shortcut that lets one matching socket stand for every socket on the port. The other two predate git. All were discovered during an LLM-based audit.
Juan Agustin Niccolini (4):
udp: Fix bind conflict check stopping at the first reuseport socket
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/sock.h | 4 + net/core/sock.c | 3 + net/ipv4/udp.c | 69 +++++- net/ipv6/datagram.c | 25 +- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + .../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++ 7 files changed, 317 insertions(+), 14 deletions(-)
base-commit: 19a227903e65c7a9c179156485660a4e017575fe
Signature · 1 line
2.43.0
PATCH 1/4 udp: Fix bind conflict check stopping at the first reuseport socket
Since commit df560056d960 ("udp: inuse checks can quit early for reuseport"), udp_lib_lport_inuse() and udp_lib_lport_inuse2() report the port as free as soon as they find one overlapping socket that has SO_REUSEPORT and the same owner, without looking at the rest of the chain.
This is only safe if overlap is transitive, and it is not:
- A wildcard address overlaps specific addresses that do not overlap each other.
- A socket with no bound device overlaps sockets bound to different devices.
As a result, an unprivileged user can bind an address and port that another user's socket holds, and receive its datagrams:
victim: bind(127.0.0.2:P)
attacker: SO_REUSEPORT, bind(127.0.0.3:P)
attacker: SO_REUSEPORT, bind(0.0.0.0:P) /* should fail */
attacker: SO_REUSEPORT, bind(127.0.0.2:P) /* should fail */
Only stop at a socket that matches the new one in family, IPV6_V6ONLY, bound device, SO_REUSEADDR and local address, and keep scanning past the other compatible sockets. Identical SO_REUSEPORT sockets, the workload the early stop was added for, still stop at the first one (binding 16384 of them on a KASAN build: 223 ms before, 222 ms after, 3727 ms with the full scan every time).
--- net/ipv4/udp.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/ipv4/udp.c +16 -2
@@ -131,6 +131,15 @@ 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_lport_same_bind (const struct sock *sk,const struct sock *sk2) +{ +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 && + sk2->sk_reuse == sk->sk_reuse && +inet_rcv_saddr_equal (sk, sk2, false); +} + static int udp_lib_lport_inuse(struct net *net, __u16 num, const struct udp_hslot *hslot, unsigned long *bitmap,
@@ -154,8 +163,11 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,if (!bitmap) {if (sk2->sk_reuseport && sk->sk_reuseport && !rcu_access_pointer (sk->sk_reuseport_cb) && -uid_eq (uid,sk_uid (sk2))) -return 0 ; +uid_eq (uid,sk_uid (sk2))) { +if (udp_lport_same_bind (sk, sk2)) +return 0 ; +continue ; + }return 1 ; }__set_bit (udp_sk (sk2)->udp_port_hash >> log, bitmap);
@@ -188,6 +200,8 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,if (sk2->sk_reuseport && sk->sk_reuseport && !rcu_access_pointer (sk->sk_reuseport_cb) &&uid_eq (uid,sk_uid (sk2))) { +if (!udp_lport_same_bind (sk, sk2)) +continue ; res =0 ; }else { res =1 ;
Signature · 1 line
2.43.0
PATCH 2/4 udp: Keep the bound device on disconnect unless connect() set it
In reply to Juan Agustin Niccolini__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.
This changes connect(AF_UNSPEC) for UDP, ping, raw and L2TP sockets, which all use __udp_disconnect(): a device that connect() did not set is now kept, so changing it afterwards needs CAP_NET_RAW, as on a socket that never disconnected.
---
Notes:
- Within a VRF, connect() can replace the master with one of its interfaces, which is now kept on disconnect, so binding back to the master needs CAP_NET_RAW. Before, disconnect took the socket out of its VRF. Restoring the master is not safe: the bind conflict checks treat a master and its interfaces as different devices, so another socket may hold the master by then. Fixing that is a separate, VRF-wide change.
- Ping, raw and L2TP sockets call __udp_disconnect() without the socket lock, so the sk_userlocks update is unlocked there. That predates this change: commit 286c72deabaa ("udp: must lock the socket in udp_disconnect()") only added the lock for UDP. Fixing it means changing how ping, raw and L2TP handle disconnect, which is separate from this fix, and a lost update cannot cause a bind overlap anyway.
include/net/sock.h | 4 ++++ net/core/sock.c | 1 + net/ipv4/udp.c | 13 +++++++++---- net/ipv6/datagram.c | 17 +++++++++++++++-- 4 files changed, 29 insertions(+), 6 deletions(-)
include/net/sock.h +4 -0
@@ -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 32struct socket_alloc {struct socket socket;
net/core/sock.c +1 -0
@@ -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);
net/ipv4/udp.c +9 -4
@@ -2195,6 +2195,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.
@@ -2204,15 +2205,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 ; }
net/ipv6/datagram.c +15 -2
@@ -139,6 +139,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 +237,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 +275,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; }
Signature · 1 line
2.43.0
PATCH 3/4 udp: Ignore a connect()-set device in the bind conflict check
In reply to Juan Agustin NiccoliniThe 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.
---
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 | 46 +++++++++++++++++++++++++++++++++++---------- net/ipv6/datagram.c | 12 ++++++++++-- 3 files changed, 48 insertions(+), 12 deletions(-)
net/core/sock.c +2 -0
@@ -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)
net/ipv4/udp.c +36 -10
92 lines not on this page · raw message
net/ipv6/datagram.c +10 -2
@@ -141,9 +141,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); }
@@ -275,9 +278,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; }
Signature · 1 line
2.43.0
PATCH 4/4 selftests: net: Add UDP bind conflict tests
In reply to Juan Agustin NiccoliniAdd tests for the three bind conflict fixes in this series:
- 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.
--- 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
tools/testing/selftests/net/.gitignore +1 -0
@@ -56,6 +56,7 @@ tools tun txring_overwrite txtimestamp +udp_bind_conflict udp_splice_checksum udpgso udpgso_bench_rx
tools/testing/selftests/net/Makefile +1 -0
@@ -202,6 +202,7 @@ TEST_GEN_PROGS := \ tap \ tcp_port_share \ tls \ + udp_bind_conflict \ udp_splice_checksum \ # end of TEST_GEN_PROGS
tools/testing/selftests/net/udp_bind_conflict.c new file +228 -0
228 lines not on this page · raw message
Signature · 1 line
2.43.0