* [PATCH net v3] inet: reject multicast and broadcast bind addresses on SOCK_STREAM
@ 2026-11-30 16:04 Dominik Maier
2026-12-01 16:04 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-30 16:04 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
David Ahern
Cc: Ido Schimmel, Simon Horman, Kuniyuki Iwashima, Joanne Koong,
netdev, dmnk+artist
Binding a TCP socket to an IPv4 multicast or broadcast address lets a
later connect() land on another user's bound unicast port without a
conflict check.
Stream bind accepts multicast and broadcast addresses while clearing the
stored source address, so bind conflict checks see only the receive
address. When connect() later picks a real unicast source address, it
moves the socket into that unicast port bucket without rechecking bind
conflicts.
Reject multicast, limited broadcast, and FIB-confirmed subnet broadcast
addresses on SOCK_STREAM with -EINVAL, and never clear the bound source
address on SOCK_STREAM when a missing FIB table falls back to broadcast.
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>
---
v3:
- Never clear inet->inet_saddr on SOCK_STREAM so neither the
missing-FIB-table RTN_BROADCAST fallback nor a FIB broadcast-route
removal race can leave a SOCK_STREAM socket with inet_rcv_saddr != 0
and inet_saddr == 0 (Sashiko).
- Drop the __inet6_bind() IPV6_ADDR_MAPPED hunk, which never cleared
inet_saddr (Sashiko).
- v2: https://lore.kernel.org/netdev/20261124180601.71719-1-dmnk@google.com/
- v1: https://lore.kernel.org/netdev/20261119192152.2643304-1-dmnk@google.com/
Found with an AI-assisted code review tool (ARTIST) and verified in
QEMU/KVM across multicast, broadcast, fresh netns with lo down, empty
VRF tables, and IP_FREEBIND.
include/net/route.h | 2 ++
net/ipv4/af_inet.c | 9 ++++++++-
net/ipv4/fib_frontend.c | 24 ++++++++++++++++++++++++
3 files changed, 34 insertions(+), 1 deletion(-)
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 5d03a9149b40..020924100d9c 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
@@ -535,7 +541,8 @@ int __inet_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
goto out_release_sock;
inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
- if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
+ if (sk->sk_type != SOCK_STREAM &&
+ (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST))
inet->inet_saddr = 0; /* Use device */
/* Make sure we are allowed to bind here. */
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 8c123b111b3d..10edb607d99d 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 == htonl(INADDR_ANY))
+ return false;
+ if (addr_type == RTN_MULTICAST || ipv4_is_lbcast(addr))
+ return true;
+ if (addr_type != RTN_BROADCAST)
+ return false;
+
+ 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;
base-commit: e2fb9368a41662bc54624f9be87ea8989b109306
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH net v3] inet: reject multicast and broadcast bind addresses on SOCK_STREAM
2026-11-30 16:04 [PATCH net v3] inet: reject multicast and broadcast bind addresses on SOCK_STREAM Dominik Maier
@ 2026-12-01 16:04 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 16:04 UTC (permalink / raw)
To: dmnk
Cc: davem, edumazet, kuba, pabeni, dsahern, idosch, horms, kuniyu,
joannelkoong, netdev, dmnk+artist
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 4
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).
- [Medium] The fix is gated only on `sk->sk_type == SOCK_STREAM`.
- [Low] `__inet_dev_addr_type()` (net/ipv4/fib_frontend.c:236-237)
returns RTN_BROADCAST for every 0.0.0.0/8 address before any FIB…
- [Low] `inet_addr_is_mcast_or_bcast()` is exported with
`EXPORT_SYMBOL()` in net/ipv4/fib_frontend.c, but its only caller is…
- [Low] The v4-mapped branch of `__inet6_bind()`
(net/ipv6/af_inet6.c:322-331) has the comment "Reproduce AF_INET…
- [Low] The comment above `lock_sock()` in `__inet_bind()` says rcv_saddr
and saddr "are the same except where it would be illegal to use them…
--
inet: reject multicast and broadcast bind addresses on SOCK_STREAM
This patch makes __inet_bind() return -EINVAL for SOCK_STREAM sockets
bound to multicast, limited broadcast, or FIB-confirmed subnet broadcast
addresses, using a new inet_addr_is_mcast_or_bcast() helper. It also
stops clearing inet_saddr for SOCK_STREAM sockets. The goal is to stop
connect() from moving a socket into another user's bhash2 unicast bucket
without a bind-conflict check.
> diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
> index 5d03a9149b40f..020924100d9ce 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;
[Severity: Medium]
Does this fully close the bypass in the stable trees the patch is tagged
for?
Mainline no longer has DCCP, so SOCK_STREAM covers every bhash2 user
there. The patch carries Cc: stable and Fixes: 28044fc1d495 (v6.1),
though, and the 6.1, 6.6 and 6.12 trees still have DCCP. DCCP sockets
use .bind = inet_bind and reach __inet_bind() with sk_type ==
SOCK_DCCP.
On those trees, a DCCP socket bound to something like 224.0.0.1:P skips
the new -EINVAL check. inet_saddr is still cleared for it, because the
new sk_type != SOCK_STREAM condition further down is true for DCCP.
connect() then goes:
dccp_v4_connect() (or dccp_v6_connect())
if (inet->inet_saddr == 0)
inet_bhash2_update_saddr()
__inet_bhash2_update_saddr()
That relinks the socket into the (real_saddr, P) bucket with no
bind-conflict check. tcp_v4_connect() does the same move.
Should the backports cover SOCK_DCCP as well? Or could the commit
message at least warn backporters about this gap?
[Severity: Low]
Should the v4-mapped branch of __inet6_bind() in net/ipv6/af_inet6.c
get the same check? That branch says it reproduces the AF_INET checks:
/* 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);
rcu_read_unlock();
if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
chk_addr_ret)) {
inet_addr_valid_or_nonlocal() accepts RTN_MULTICAST and RTN_BROADCAST.
The earlier IPV6_ADDR_MULTICAST && SOCK_STREAM check does not catch
these addresses, because ipv6_addr_type() reports them as
IPV6_ADDR_MAPPED.
As a result, a dual-stack TCP socket can still bind to ::ffff:224.0.0.1,
::ffff:255.255.255.255 or a mapped subnet broadcast address. An AF_INET
TCP socket that asks for the same IPv4 address now gets -EINVAL.
The conflict bypass is not reachable this way.
__inet6_bind() sets inet->inet_saddr = v4addr and never clears it, so
connect() fails in ip_route_output_key_hash_rcu(). The two bind paths
still give different results, though, and the socket ends up bound to an
address it cannot use.
> [ ... ]
> @@ -535,7 +541,8 @@ int __inet_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
> goto out_release_sock;
>
> inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
> - if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
> + if (sk->sk_type != SOCK_STREAM &&
> + (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST))
> inet->inet_saddr = 0; /* Use device */
[Severity: Low]
This isn't a bug, but the comment just above lock_sock() in
__inet_bind() no longer matches what happens for SOCK_STREAM:
* In the BSD API these are the same except where it
* would be illegal to use them (multicast/broadcast) in
* which case the sending device address is used.
For SOCK_STREAM, some addresses are classified RTN_BROADCAST by
inet_addr_type_table() but pass the new helper. Examples are 0.0.0.0/8
and the missing-FIB-table fallback. These now keep the bound address in
inet_saddr.
Could a short comment explain why stream sockets are excluded here? It
would make it less likely that a later cleanup restores the clearing and
reopens the bhash2 bypass.
> diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
> index 8c123b111b3d8..10edb607d99db 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 == htonl(INADDR_ANY))
> + return false;
> + if (addr_type == RTN_MULTICAST || ipv4_is_lbcast(addr))
> + return true;
> + if (addr_type != RTN_BROADCAST)
> + return false;
> +
> + 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: Low]
How are 0.0.0.0/8 addresses meant to be handled here?
__inet_dev_addr_type() returns RTN_BROADCAST for them before any FIB
lookup:
if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
return RTN_BROADCAST;
A 0.x.y.z address normally has no RTN_BROADCAST route in tb_id, so this
helper returns false for it. inet_addr_valid_or_nonlocal() accepts
RTN_BROADCAST, so a TCP bind() to a non-local 0.1.2.3 still succeeds,
even without IP_FREEBIND.
Before this patch, __inet_bind() cleared inet_saddr for such a socket.
tcp_v4_connect() then moved it with inet_bhash2_update_saddr(), which is
the same bypass.
With this patch, only the "never clear inet_saddr on SOCK_STREAM" change
closes this case. inet_saddr stays 0.1.2.3, and connect() now returns
-ENETUNREACH, because __ip_dev_find() fails for a non-local saddr in
ip_route_output_key_hash_rcu(). Before the patch, connect() succeeded
with a routed source address.
The commit message describes the saddr change only as covering the case
"when a missing FIB table falls back to broadcast". Could it also say
that this change is what closes the 0.0.0.0/8 case? It could also note
that bind() to these addresses still succeeds while connect() now fails.
> +EXPORT_SYMBOL(inet_addr_is_mcast_or_bcast);
[Severity: Low]
Is this export needed? The only caller is __inet_bind() in
net/ipv4/af_inet.c. Both files are always-built-in IPv4 core, so no
module can use it.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130160424.2650506-1-dmnk%40google.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 16:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:04 [PATCH net v3] inet: reject multicast and broadcast bind addresses on SOCK_STREAM Dominik Maier
2026-12-01 16:04 ` 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