]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-6.1/tcp-fix-bind-regression-for-v6-only-wildcard-and-v4-.patch
6.1-stable patches
[thirdparty/kernel/stable-queue.git] / queue-6.1 / tcp-fix-bind-regression-for-v6-only-wildcard-and-v4-.patch
1 From 961f27c6b980bd03433c02a3b405cc917c093f0f Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Tue, 26 Mar 2024 13:42:45 -0700
4 Subject: tcp: Fix bind() regression for v6-only wildcard and v4(-mapped-v6)
5 non-wildcard addresses.
6
7 From: Kuniyuki Iwashima <kuniyu@amazon.com>
8
9 [ Upstream commit d91ef1e1b55f730bee8ce286b02b7bdccbc42973 ]
10
11 Jianguo Wu reported another bind() regression introduced by bhash2.
12
13 Calling bind() for the following 3 addresses on the same port, the
14 3rd one should fail but now succeeds.
15
16 1. 0.0.0.0 or ::ffff:0.0.0.0
17 2. [::] w/ IPV6_V6ONLY
18 3. IPv4 non-wildcard address or v4-mapped-v6 non-wildcard address
19
20 The first two bind() create tb2 like this:
21
22 bhash2 -> tb2(:: w/ IPV6_V6ONLY) -> tb2(0.0.0.0)
23
24 The 3rd bind() will match with the IPv6 only wildcard address bucket
25 in inet_bind2_bucket_match_addr_any(), however, no conflicting socket
26 exists in the bucket. So, inet_bhash2_conflict() will returns false,
27 and thus, inet_bhash2_addr_any_conflict() returns false consequently.
28
29 As a result, the 3rd bind() bypasses conflict check, which should be
30 done against the IPv4 wildcard address bucket.
31
32 So, in inet_bhash2_addr_any_conflict(), we must iterate over all buckets.
33
34 Note that we cannot add ipv6_only flag for inet_bind2_bucket as it
35 would confuse the following patetrn.
36
37 1. [::] w/ SO_REUSE{ADDR,PORT} and IPV6_V6ONLY
38 2. [::] w/ SO_REUSE{ADDR,PORT}
39 3. IPv4 non-wildcard address or v4-mapped-v6 non-wildcard address
40
41 The first bind() would create a bucket with ipv6_only flag true,
42 the second bind() would add the [::] socket into the same bucket,
43 and the third bind() could succeed based on the wrong assumption
44 that ipv6_only bucket would not conflict with v4(-mapped-v6) address.
45
46 Fixes: 28044fc1d495 ("net: Add a bhash2 table hashed by port and address")
47 Diagnosed-by: Jianguo Wu <wujianguo106@163.com>
48 Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
49 Link: https://lore.kernel.org/r/20240326204251.51301-3-kuniyu@amazon.com
50 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
51 Signed-off-by: Sasha Levin <sashal@kernel.org>
52 ---
53 net/ipv4/inet_connection_sock.c | 19 +++++++++++--------
54 1 file changed, 11 insertions(+), 8 deletions(-)
55
56 diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
57 index f7832d4253820..8407098a59391 100644
58 --- a/net/ipv4/inet_connection_sock.c
59 +++ b/net/ipv4/inet_connection_sock.c
60 @@ -289,6 +289,7 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l
61 struct sock_reuseport *reuseport_cb;
62 struct inet_bind_hashbucket *head2;
63 struct inet_bind2_bucket *tb2;
64 + bool conflict = false;
65 bool reuseport_cb_ok;
66
67 rcu_read_lock();
68 @@ -301,18 +302,20 @@ static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l
69
70 spin_lock(&head2->lock);
71
72 - inet_bind_bucket_for_each(tb2, &head2->chain)
73 - if (inet_bind2_bucket_match_addr_any(tb2, net, port, l3mdev, sk))
74 - break;
75 + inet_bind_bucket_for_each(tb2, &head2->chain) {
76 + if (!inet_bind2_bucket_match_addr_any(tb2, net, port, l3mdev, sk))
77 + continue;
78
79 - if (tb2 && inet_bhash2_conflict(sk, tb2, uid, relax, reuseport_cb_ok,
80 - reuseport_ok)) {
81 - spin_unlock(&head2->lock);
82 - return true;
83 + if (!inet_bhash2_conflict(sk, tb2, uid, relax, reuseport_cb_ok, reuseport_ok))
84 + continue;
85 +
86 + conflict = true;
87 + break;
88 }
89
90 spin_unlock(&head2->lock);
91 - return false;
92 +
93 + return conflict;
94 }
95
96 /*
97 --
98 2.43.0
99