From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796015264; b=kEh/QGsQ9qaANkxGJawPeqstWLLpffsTtmh+uqPiPFma+lUw1XQUQbGvgkWkXPb4DcVM z2AqtgHZkP5H9xOEj+USiXA//lp312R5iHpYmBfimdwbntKzuEXOU5c81W6ys0vLszyBf 2vwdALuYEiIc1/FrFGLG9iVzxbsmE7MrddlEAiaq6zPmb/ULw8RRmPkf3OzDYGa+qQNms ts8eU2vDVlmgVlmaGG11GVnuc5J2gI07od6W9TgMSi5wm/LBjhLAh4ldvwX2paX5DGkO0 jERCMtz9lK4RI/BLOqG6itLpCVmf5fa6HK/HOSx6/KWU3Uh8LYMvrdWkSUMg9Xi+dVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796015264; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=zcbTTu75l5osXmPvjr4EZdQWusZL/7KPbijrDdhP9kY=; b=gsOAL0G8LWnL2f0FiMR/OfLBNFqQnvof9u2uOwJgz14+jmoBAjfBz+F7XPp7RiJ/dfCX xhDUnh1wNHZGzZP3HDq5TqdEUKj0lODojid0L1Q/NpTdT1oq6skwWLy/64u+85M8zs3+T 2iH0lvFzTqiDaUP6UxRicv9U7lESjfaY6gTGZFYGIH/p0ZS6TG6pZ9mCSgbKWsDI20qrd ptwQNDXnZgN3eVZyXlZ4WMyhS6Lm7XXdubzKoqG+YywDQ5Py7FTqzK5uThzSjGWuuYbnD 13wxkcvHFaa2OmvWISHd8OZTwaSKOL/yzw/xSMOPFMx9MCPFnypGhXHFiyeLhfoF0mQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=B3Or0Fu3; arc=none smtp.remote-ip=209.85.128.169 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3F9B606A685 for ; Mon, 30 Nov 2026 05:07:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A3F9B606A685 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=B3Or0Fu3 Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-8b2d14968c9so93347337b3.0 for ; Sun, 29 Nov 2026 21:07:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796015262; x=1796620062; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zcbTTu75l5osXmPvjr4EZdQWusZL/7KPbijrDdhP9kY=; b=B3Or0Fu3btEyKxW8MX699s8U4F6O7NfbN0CTUx5TmiDwhlp9ekxCAshS4hkZqNDUfG XerOru3HLzCdBaqMr+ii+iblg/XWCoeVTxCCDAFzCeLQu9R1XtRNWbIfPyYJ0iSND2xW XkU0bEH4QjmGpWwPd4DJ6+6YOL8fVPxLMmhRGQMceIm6Ndp6qTCy+dAPCF5GgX/f16uZ 2kwWalbw3Ss3JMhFskeSwTrG5jyu9PDra/2oXnb8HPUu7SysMQNRyAv16LE/6pPWgshv aQjva3BlhQvVP5qu24yuxkvPiqWh38IAyXeC8nnUzFJ1O5pFWFH1u5L6yQ6VAeNfjyLS PGbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796015262; x=1796620062; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zcbTTu75l5osXmPvjr4EZdQWusZL/7KPbijrDdhP9kY=; b=UaTTsOAGxRXKc1qShJ8DTxnajaodE7ovOuhVJxUoN7XGsMNBmcSE3g7JSQIbgHgVaX B6nnLdKnPcWQLRXm/Cf47fUMSiAWvZaR3hI0qF56dv1fsn1oAfcouUdCnZTldQEBZcVa aMSr3YoPMHGZQcwIAtbxiij7LHvU3W/PM45QyHfbrq3qXhwZVSzaDNb/QmcA5wLyqfmQ F3UKV4merTo79WJQRIy1j8CAnQ18E+JHMgkCWBt70CebwCUCmIpJIujk68iU7PxjHb23 gxrcXKfggdrbIHe2/WOhgBPNF3AoxDlRYQgEhd8oKO5SJ97NrMnfDRlVtE2ICU7pLgWt MNQA== X-Forwarded-Encrypted: i=1; AL+hErHaGUcfPsd2xR9xLmdoNzWsT6K461fSMOaUElyJF42kr66cVjAqe2s9DuLAsLm4IRwK8y5A900=@vger.kernel.org X-Gm-Message-State: APuU4Z9E8e1ddBEkHeRPOF/Vhq8Db2AZCSFoLDyA9fKtJufBj84AyO40 cC5fr8y7FU3DYIwIzlcBC5CTv1qzYivLYfApl/Izkp2s/4jSkZS4UFNR X-Gm-Gg: AZFdTZe3GptTBf039mr+QaJstCk+LYwUDqSyQUoXDxMvkjdoelmh0Zn+Lvzj0uCEIsE VRzLkYNbiYhhludVU6tkYwUsGHnjadb0Gm47L5kJQrfgWF398mI9WwP/FGViXOaMwRqcEuhtOMa 3FjvwsQuq2wMkv+dewU0lQ3YmCMOXN8GYbyzXDJqk6ZFdc7vxhRKEEyh7SJtLhCjOa1eKGDFa97 CZ0R5jh46pUk6xrRAqi29NP1aXnX8k3YdSU5rs5d3BPyBq2s3d183tkg1vPN/PJmzA1CWP4WKkM F2WhMjT9v0GNDTr2Dyq5iHKAmAxWnjxBuXthJnuvKHtK7k0p0mY+SmZje3Y/R2E49gR2z1U/XFv FK5tH73PIDtWCTJngYcMox56N3nc/A5frBToF8/gAwGaNT/qx5rCD0NFNDFZh0Khs5Vnirlg12a 7V/VjUZH7E11977HFDZIXXJISdNvp5J22W12yLNIZ+5ihKBdGm0RvhD1B2YRC42uKvTycfpnnMU axX23kQlLBckp9QCXSKMyh7DAOxiorLhAfKiL19sAF81Mw59GdkHIvOYVm0ifL7hsM2NLqZS86L f/S+477sL+M5ZzuiFso14nCW67gGaAazW9ZtXuTsGc2I384S9HIM X-Received: by 2002:a05:690c:68b:b0:8b5:9561:a327 with SMTP id 00721157ae682-8b59561a45fmr31130497b3.2.1796015262530; Sun, 29 Nov 2026 21:07:42 -0800 (PST) Received: from VPN-156-19.vpn.usf.edu ([2607:fe50:0:f802::4046]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b52028c7f5sm126540357b3.12.2026.11.29.21.07.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 21:07:41 -0800 (PST) From: Juan Agustin Niccolini To: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com Cc: 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 Subject: [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Date: Mon, 30 Nov 2026 00:07:28 -0500 Message-ID: <20261130050729.893-3-juanagusnicco@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261130050729.893-1-juanagusnicco@gmail.com> References: <20261130050729.893-1-juanagusnicco@gmail.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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