Netdev List
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	 Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>
Cc: Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	 Simon Horman <horms@kernel.org>,
	Joanne Koong <joannelkoong@gmail.com>,
	netdev@vger.kernel.org,  dmnk+artist@google.com,
	Dominik Maier <dmnk@google.com>
Subject: [PATCH net] inet: reject multicast and broadcast bind addresses on SOCK_STREAM
Date: Thu, 19 Nov 2026 20:21:52 +0100	[thread overview]
Message-ID: <20261119192152.2643304-1-dmnk@google.com> (raw)

__inet6_bind() already rejects native IPv6 multicast addresses on
SOCK_STREAM sockets with -EINVAL. However, __inet_bind() and the
IPV6_ADDR_MAPPED branch of __inet6_bind() allow binding SOCK_STREAM
sockets to IPv4 multicast and broadcast addresses.

When binding a non-INADDR_ANY RTN_MULTICAST or RTN_BROADCAST address,
__inet_bind() stores the non-unicast address in inet->inet_rcv_saddr
while clearing inet->inet_saddr to 0. Because bind conflict checks
compare receive addresses, binding 224.0.0.1:PORT does not conflict with
another user's exclusive unicast 127.0.0.1:PORT socket. On a subsequent
active tcp_v4_connect(), seeing !inet->inet_saddr causes the kernel to
call inet_bhash2_update_saddr(), relocating the active-open socket into
the unicast (port, saddr) bhash2 bucket alongside the exclusive socket
without re-evaluating inet_csk_bind_conflict() (note that this affects
active-open bhash2 rebinding and port occupancy rather than passive
lhash2 SYN delivery).

Reject non-INADDR_ANY RTN_MULTICAST and RTN_BROADCAST addresses on
SOCK_STREAM sockets with -EINVAL in __inet_bind() and in the
IPV6_ADDR_MAPPED path of __inet6_bind(), matching native IPv6 stream
socket validation while preserving INADDR_ANY (0.0.0.0) wildcard binds.

This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available upon maintainers' request. The fix was compile-tested with
-Werror and checked with checkpatch.pl --strict; an equivalent fix was
verified against the reproducer in QEMU/KVM.

Fixes: 28044fc1d495 ("net: Add a bhash2 table hashed by port and address")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
 net/ipv4/af_inet.c  | 6 ++++++
 net/ipv6/af_inet6.c | 8 ++++++++
 2 files changed, 14 insertions(+)

diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 8822bae..b1ea554 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -500,6 +500,12 @@ int __inet_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
 	tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
 	chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
 
+	err = -EINVAL;
+	if (sk->sk_type == SOCK_STREAM &&
+	    addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
+	    (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST))
+		goto out;
+
 	/* Not specified by any standard per-se, however it breaks too
 	 * many applications when removed.  It is unfortunate since
 	 * allowing applications to make a non-local bind solves
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index a9b8b4b..ab7da0c 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -324,6 +324,14 @@ int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
 		rcu_read_unlock();
 
+		if (sk->sk_type == SOCK_STREAM &&
+		    v4addr != htonl(INADDR_ANY) &&
+		    (chk_addr_ret == RTN_MULTICAST ||
+		     chk_addr_ret == RTN_BROADCAST)) {
+			err = -EINVAL;
+			goto out;
+		}
+
 		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
 						 chk_addr_ret)) {
 			err = -EADDRNOTAVAIL;
-- 
2.56.1.750.gf70dc682cf-goog


                 reply	other threads:[~2026-11-19 19:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119192152.2643304-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=davem@davemloft.net \
    --cc=dmnk+artist@google.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=joannelkoong@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox