Skip to content

Commit 5456262

Browse files
Martin KaFai Laukuba-moo
authored andcommitted
net: Fix incorrect address comparison when searching for a bind2 bucket
The v6_rcv_saddr and rcv_saddr are inside a union in the 'struct inet_bind2_bucket'. When searching a bucket by following the bhash2 hashtable chain, eg. inet_bind2_bucket_match, it is only using the sk->sk_family and there is no way to check if the inet_bind2_bucket has a v6 or v4 address in the union. This leads to an uninit-value KMSAN report in [0] and also potentially incorrect matches. This patch fixes it by adding a family member to the inet_bind2_bucket and then tests 'sk->sk_family != tb->family' before matching the sk's address to the tb's address. Cc: Joanne Koong <[email protected]> Fixes: 28044fc ("net: Add a bhash2 table hashed by port and address") Signed-off-by: Martin KaFai Lau <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Tested-by: Alexander Potapenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 30b172e commit 5456262

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

include/net/inet_hashtables.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ struct inet_bind2_bucket {
9595
possible_net_t ib_net;
9696
int l3mdev;
9797
unsigned short port;
98+
#if IS_ENABLED(CONFIG_IPV6)
99+
unsigned short family;
100+
#endif
98101
union {
99102
#if IS_ENABLED(CONFIG_IPV6)
100103
struct in6_addr v6_rcv_saddr;

net/ipv4/inet_hashtables.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static void inet_bind2_bucket_init(struct inet_bind2_bucket *tb,
109109
tb->l3mdev = l3mdev;
110110
tb->port = port;
111111
#if IS_ENABLED(CONFIG_IPV6)
112+
tb->family = sk->sk_family;
112113
if (sk->sk_family == AF_INET6)
113114
tb->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
114115
else
@@ -146,6 +147,9 @@ static bool inet_bind2_bucket_addr_match(const struct inet_bind2_bucket *tb2,
146147
const struct sock *sk)
147148
{
148149
#if IS_ENABLED(CONFIG_IPV6)
150+
if (sk->sk_family != tb2->family)
151+
return false;
152+
149153
if (sk->sk_family == AF_INET6)
150154
return ipv6_addr_equal(&tb2->v6_rcv_saddr,
151155
&sk->sk_v6_rcv_saddr);
@@ -791,6 +795,9 @@ static bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb,
791795
int l3mdev, const struct sock *sk)
792796
{
793797
#if IS_ENABLED(CONFIG_IPV6)
798+
if (sk->sk_family != tb->family)
799+
return false;
800+
794801
if (sk->sk_family == AF_INET6)
795802
return net_eq(ib2_net(tb), net) && tb->port == port &&
796803
tb->l3mdev == l3mdev &&
@@ -807,6 +814,9 @@ bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const
807814
#if IS_ENABLED(CONFIG_IPV6)
808815
struct in6_addr addr_any = {};
809816

817+
if (sk->sk_family != tb->family)
818+
return false;
819+
810820
if (sk->sk_family == AF_INET6)
811821
return net_eq(ib2_net(tb), net) && tb->port == port &&
812822
tb->l3mdev == l3mdev &&

0 commit comments

Comments
 (0)