Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] inet: reject multicast and broadcast bind addresses on SOCK_STREAM
@ 2026-11-24 18:06 Dominik Maier
  2026-11-28  0:55 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-24 18:06 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	David Ahern
  Cc: Ido Schimmel, Neal Cardwell, Kuniyuki Iwashima, Simon Horman,
	Joanne Koong, netdev, dmnk+artist

__inet6_bind() rejects native IPv6 multicast addresses on SOCK_STREAM
sockets with -EINVAL, while __inet_bind() and the IPV6_ADDR_MAPPED path
of __inet6_bind() still accept IPv4 multicast and broadcast addresses.

Binding a multicast or broadcast address stores that 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. A subsequent tcp_v4_connect() then sees !inet->inet_saddr and
calls inet_bhash2_update_saddr(), moving the active-open socket into the
unicast (port, saddr) bhash2 bucket without re-running
inet_csk_bind_conflict().

Reject multicast, limited broadcast, and FIB-confirmed subnet broadcast
addresses on SOCK_STREAM sockets with -EINVAL in __inet_bind() and in
the IPV6_ADDR_MAPPED branch of __inet6_bind(). Exempt INADDR_ANY
(ipv4_is_zeronet()) and verify non-lbcast RTN_BROADCAST results against
the FIB table so the missing-table RTN_BROADCAST fallback (such as in a
fresh network namespace with loopback still down or an empty VRF table)
does not reject early unicast or nonlocal binds.

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>
---
v2:
- Add inet_addr_is_mcast_or_bcast() to confirm non-lbcast RTN_BROADCAST
  against the FIB table so the missing-table RTN_BROADCAST fallback in a
  fresh netns (before lo is up) or an empty VRF table does not reject
  unicast or nonlocal binds.
- Move the testing note below ---.
- v1: https://lore.kernel.org/netdev/20261119192152.2643304-1-dmnk@google.com/

Found with AI-assisted code review; tested in QEMU across IPv4 and
v4-mapped IPv6 sockets, including fresh netns with loopback down, empty
VRF tables, IP_FREEBIND, multicast, limited broadcast, and subnet
directed broadcast.
 include/net/route.h     |  2 ++
 net/ipv4/af_inet.c      |  6 ++++++
 net/ipv4/fib_frontend.c | 24 ++++++++++++++++++++++++
 net/ipv6/af_inet6.c     |  7 +++++++
 4 files changed, 39 insertions(+)

diff --git a/include/net/route.h b/include/net/route.h
index 3fccd31eb74d..4fddeb902c5f 100644
--- a/include/net/route.h
+++ b/include/net/route.h
@@ -263,6 +263,8 @@ unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
 unsigned int inet_addr_type_dev_table(struct net *net,
 				      const struct net_device *dev,
 				      __be32 addr);
+bool inet_addr_is_mcast_or_bcast(struct net *net, u32 tb_id, __be32 addr,
+				 unsigned int addr_type);
 void ip_rt_multicast_event(struct in_device *);
 int ip_rt_ioctl(struct net *, unsigned int cmd, struct rtentry *rt);
 void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index 8822bae439f8..a7d4d78b4b38 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 &&
+	    inet_addr_is_mcast_or_bcast(net, tb_id, addr->sin_addr.s_addr,
+					chk_addr_ret))
+		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/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 8c123b111b3d..5968aa29f4ab 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -289,6 +289,30 @@ unsigned int inet_addr_type_dev_table(struct net *net,
 }
 EXPORT_SYMBOL(inet_addr_type_dev_table);
 
+bool inet_addr_is_mcast_or_bcast(struct net *net, u32 tb_id, __be32 addr,
+				 unsigned int addr_type)
+{
+	struct flowi4 fl4 = { .daddr = addr };
+	struct fib_result res;
+	bool is_bcast = false;
+	struct fib_table *tb;
+
+	if (addr_type == RTN_MULTICAST)
+		return true;
+	if (addr_type != RTN_BROADCAST || ipv4_is_zeronet(addr))
+		return false;
+	if (ipv4_is_lbcast(addr))
+		return true;
+
+	rcu_read_lock();
+	tb = fib_get_table(net, tb_id);
+	if (tb && !fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF))
+		is_bcast = res.type == RTN_BROADCAST;
+	rcu_read_unlock();
+	return is_bcast;
+}
+EXPORT_SYMBOL(inet_addr_is_mcast_or_bcast);
+
 __be32 fib_compute_spec_dst(struct sk_buff *skb)
 {
 	struct net_device *dev = skb->dev;
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index a9b8b4b42a1a..2b5d5ee9d0df 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -322,6 +322,13 @@ int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
 		/* Reproduce AF_INET checks to make the bindings consistent */
 		v4addr = addr->sin6_addr.s6_addr32[3];
 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
+		if (sk->sk_type == SOCK_STREAM &&
+		    inet_addr_is_mcast_or_bcast(net,
+						l3mdev_fib_table_rcu(dev) ? : RT_TABLE_LOCAL,
+						v4addr, chk_addr_ret)) {
+			err = -EINVAL;
+			goto out_unlock;
+		}
 		rcu_read_unlock();
 
 		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
-- 
2.56.1.831.g720f3f5937-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net v2] inet: reject multicast and broadcast bind addresses on SOCK_STREAM
  2026-11-24 18:06 [PATCH net v2] inet: reject multicast and broadcast bind addresses on SOCK_STREAM Dominik Maier
@ 2026-11-28  0:55 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28  0:55 UTC (permalink / raw)
  To: dmnk
  Cc: davem, edumazet, kuba, pabeni, dsahern, idosch, ncardwell, kuniyu,
	horms, joannelkoong, netdev, dmnk+artist

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · 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 leaves the bind-conflict bypass it targets reachable
  through the missing-FIB-table RTN_BROADCAST fallback.
- [Low] The commit message presents the IPV6_ADDR_MAPPED hunk in
  __inet6_bind() as part of the bhash2 bind-conflict fix, but the…

--

inet: reject multicast and broadcast bind addresses on SOCK_STREAM

This patch rejects IPv4 multicast, limited broadcast and FIB-confirmed
subnet broadcast bind addresses on SOCK_STREAM sockets, in __inet_bind()
and in the v4-mapped branch of __inet6_bind(). The aim is to stop
tcp_v4_connect() from moving a socket that has inet_rcv_saddr set and
inet_saddr cleared into a unicast bhash2 bucket without a bind conflict
check. A new helper, inet_addr_is_mcast_or_bcast(), re-checks
RTN_BROADCAST results against the FIB so that binds hitting the
missing-table fallback are not rejected.

> diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
> index 8c123b111b3d8..5968aa29f4abb 100644
> --- a/net/ipv4/fib_frontend.c
> +++ b/net/ipv4/fib_frontend.c
> @@ -289,6 +289,30 @@ unsigned int inet_addr_type_dev_table(struct net *net,
>  }
>  EXPORT_SYMBOL(inet_addr_type_dev_table);
>  
> +bool inet_addr_is_mcast_or_bcast(struct net *net, u32 tb_id, __be32 addr,
> +				 unsigned int addr_type)
> +{

[ ... ]

> +	rcu_read_lock();
> +	tb = fib_get_table(net, tb_id);
> +	if (tb && !fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF))
> +		is_bcast = res.type == RTN_BROADCAST;
> +	rcu_read_unlock();
> +	return is_bcast;
> +}

[Severity: High]
Can the bhash2 bypass still be reached through the missing-table
fallback?

With CONFIG_IP_MULTIPLE_TABLES, fib4_rules_init() only installs rules.
RT_TABLE_LOCAL is created later by fib_new_table() from fib_magic(), when
the first address or route is added (for example when lo comes up). Until
then, __inet_dev_addr_type() returns its default for any non-multicast
address:

net/ipv4/fib_frontend.c:__inet_dev_addr_type() {
	unsigned int ret = RTN_BROADCAST;
    ...
	table = fib_get_table(net, tb_id);
	if (table) {
		ret = RTN_UNICAST;
    ...
}

In that window this helper sees tb == NULL and returns false, so
__inet_bind() allows the bind. The v2 notes say this is intended.
inet_addr_valid_or_nonlocal() also accepts RTN_BROADCAST. That means any
address X can be bound, including a nonlocal one, without IP_FREEBIND or
ip_nonlocal_bind.

However, the later step in __inet_bind() still uses the raw chk_addr_ret:

	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
		inet->inet_saddr = 0;  /* Use device */

This leaves the TCP socket with inet_rcv_saddr = X and inet_saddr = 0.
That is the state the commit message names as the root cause.

Once networking is configured, another user can exclusively bind
127.0.0.1:PORT with no conflict, because X is a different address. A
later connect from the first socket then runs:

tcp_v4_connect()
    if (!inet->inet_saddr) {
        err = inet_bhash2_update_saddr(sk,  &fl4->saddr, AF_INET);
    __inet_bhash2_update_saddr()
        relinks sk into the (PORT, 127.0.0.1) bhash2 bucket
        without calling inet_csk_bind_conflict()

Is this the same port takeover the commit describes for 224.0.0.1?

There is also a narrower variant: a race between the two separate FIB
lookups. If a subnet broadcast route is removed between
inet_addr_type_table() and this helper, the helper returns false. The
stale chk_addr_ret still clears inet_saddr.

Could __inet_bind() use one classification for both decisions? For
example, on SOCK_STREAM it could clear inet_saddr only when the address is
confirmed multicast or broadcast, and treat the missing-table fallback as
unicast. Another option is to run a conflict check when connect rewrites
the source address.

> diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
> index a9b8b4b42a1af..2b5d5ee9d0dfe 100644
> --- a/net/ipv6/af_inet6.c
> +++ b/net/ipv6/af_inet6.c
> @@ -322,6 +322,13 @@ int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
>  		/* Reproduce AF_INET checks to make the bindings consistent */
>  		v4addr = addr->sin6_addr.s6_addr32[3];
>  		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
> +		if (sk->sk_type == SOCK_STREAM &&
> +		    inet_addr_is_mcast_or_bcast(net,
> +						l3mdev_fib_table_rcu(dev) ? : RT_TABLE_LOCAL,
> +						v4addr, chk_addr_ret)) {
> +			err = -EINVAL;
> +			goto out_unlock;
> +		}

[Severity: Low]
Is this hunk needed for the bhash2 fix described in the commit message?

On the v4-mapped path, __inet6_bind() later does:

	inet->inet_rcv_saddr = v4addr;
	inet->inet_saddr = v4addr;

It never clears inet_saddr for multicast or broadcast. When
tcp_v6_connect() passes a mapped destination to tcp_v4_connect(),
inet_saddr is non-zero. So tcp_v4_connect() takes the sk_rcv_saddr_set()
branch, not inet_bhash2_update_saddr().

The route lookup in ip_route_output_key_hash_rcu() also rejects such a
source:

	if (fl4->saddr) {
		if (ipv4_is_multicast(fl4->saddr) ||
		    ipv4_is_lbcast(fl4->saddr)) {
			rth = ERR_PTR(-EINVAL);
			goto out;

A non-local source such as a subnet broadcast address also fails there
without FLOWI_FLAG_ANYSRC.

So this change seems to only add a new -EINVAL for binding
::ffff:<mcast/bcast> on TCP, for consistency with the native IPv6 check.
Could the commit message say that? Right now it presents this hunk as part
of the bypass fix that goes to stable under the Fixes: tag.

>  		rcu_read_unlock();
>  
>  		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124180601.71719-1-dmnk%40google.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28  0:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:06 [PATCH net v2] inet: reject multicast and broadcast bind addresses on SOCK_STREAM Dominik Maier
2026-11-28  0:55 ` 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