inet: reject multicast and broadcast bind addresses on SOCK_STREAM
Subsystems (3)
PATCH v3 1/1 inet: reject multicast and broadcast bind addresses on SOCK_STREAM
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.
---
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/[email protected]/
- v1: https://lore.kernel.org/netdev/[email protected]/
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(-)
include/net/route.h +2 -0
@@ -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);
net/ipv4/af_inet.c +8 -1
@@ -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. */
net/ipv4/fib_frontend.c +24 -0
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog