Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796102955; b=HJHtZDoQqk5ZIQ+k4RZgiQ7uRSKEukPpUuUAL0LeY4m2Bz35pPGMJX6WSeCLb3R102dd 5baI6bS4UMHNuuflz0C/kuPljYyAsczxQkkUU1kydesV0cXhuaZAWvQ2bUjBXCgiq2pZ7 vPjtTlbJcM/EaIGG7KNOKFlc/FI8bqmw0SQ/2gULuCln/ABDjxaWvA41sAMSyjCddlPW0 a8kqDfUi5ewj5TXaW0AGkTcludG0h2GERoMhRrPmm3+D/tP+5LCz1Og04LFEZmK0s34HJ RTjDuio9OevvUK1j5XX1FWNDp1pjC/37anH0m1se9PmdHchc4A9U5xoL8eKOkQJyepw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796102955; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=X81TfrpTVKmBsg5l+xbyg74/99y0V+ivtirZZbtvlgA=; b=pBnm0VVnPXYXB9bo+cYQZR0g3//eYXP8O6Hkh26uutcf94fBypniZSOJp17deVXSDtKx UA5gkDWEU3rVZtuV8u4VJnwUK2VtFOFSt7TTfhN9rEXzTe6ibK84O9YOqiiWQNKFHClOM vs3CDfbO1HLRWM0lI+PKoxvxqJwmhXymA9rfP7b8904A7z67uMzDopaM3i+GvCCxG0kqI viKPalWAV/A2YofkSuZtDEFKh+QjsSsXHXhpl3mMdNWHFhYIUM0xS20YBrIh8WhFs1P3S wIArWUUTTu1HO+nlZZrfpduSnZFZ40yNwM+buYSmbTBP2BegHnCIYNN0Cvsr0b4XAPw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=W+6qDIXm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 028E4606A335; Tue, 1 Dec 2026 05:29:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=W+6qDIXm Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97A5F1F0089A; Tue, 1 Dec 2026 05:29:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796102954; bh=X81TfrpTVKmBsg5l+xbyg74/99y0V+ivtirZZbtvlgA=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=W+6qDIXm67QQEoHHb7JwQsfzBiDl5/qj1dqa3qiPMEN+kcRaN686n/Gzx/9HZdj2L h5OOfN7eIqKMqKCFXRxrpEesk5H4+MICG6TSe6zjW6MTQ2yHYVe5UKS9H1rhpMhDXN rC+/SuJ2Wjjp+TNweTwbYxp38l/kINQx1qa7eFqbkNtwUJ5spDjPp+jBAyEBnaabn0 EBvtmQI0B6rTBHJ8KsSenIk4xoM6tXatABXOra+AD2wVGJAr41/7DxXrfbU4nDErvz T1NWzlp0T5CImZJ3wW0yWavg+sGEmG46LtHSFEEeTR4/4wZHBZXKgK9dEU2du9CEjB jcsfW6bWBa0Yw== Subject: Re: [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check From: netdev-bot+sashiko@kernel.org To: juanagusnicco@gmail.com Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, kuniyu@google.com, willemb@google.com, willemdebruijn.kernel@gmail.com, dsahern@kernel.org, idosch@nvidia.com, shuah@kernel.org, tpluszz77@gmail.com, brauner@kernel.org, alice.kernel@fastmail.im, kylebot@openai.com, joe@dama.to, leitao@debian.org, xudu@redhat.com, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Date: Tue, 01 Dec 2026 05:29:13 +0000 Message-ID: <179610295315.3146.15580051983330492569@kernel.org> In-Reply-To: <20261130050729.893-3-juanagusnicco@gmail.com> References: <20261130050729.893-3-juanagusnicco@gmail.com> X-sashiko-severity: High Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 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