diff mbox series

[net] tcp: fix race in tcp_v6_syn_recv_sock()

Message ID 20240606154652.360331-1-edumazet@google.com (mailing list archive)
State Accepted
Commit d37fe4255abe8e7b419b90c5847e8ec2b8debb08
Delegated to: Netdev Maintainers
Headers show
Series [net] tcp: fix race in tcp_v6_syn_recv_sock() | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 902 this patch: 902
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: dsahern@kernel.org
netdev/build_clang success Errors and warnings before: 904 this patch: 904
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 906 this patch: 906
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 15 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-06-07--09-00 (tests: 1041)

Commit Message

Eric Dumazet June 6, 2024, 3:46 p.m. UTC
tcp_v6_syn_recv_sock() calls ip6_dst_store() before
inet_sk(newsk)->pinet6 has been set up.

This means ip6_dst_store() writes over the parent (listener)
np->dst_cookie.

This is racy because multiple threads could share the same
parent and their final np->dst_cookie could be wrong.

Move ip6_dst_store() call after inet_sk(newsk)->pinet6
has been changed and after the copy of parent ipv6_pinfo.

Fixes: e994b2f0fb92 ("tcp: do not lock listener to process SYN packets")
Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 net/ipv6/tcp_ipv6.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Simon Horman June 8, 2024, 1:25 p.m. UTC | #1
+ David Ahern

On Thu, Jun 06, 2024 at 03:46:51PM +0000, Eric Dumazet wrote:
> tcp_v6_syn_recv_sock() calls ip6_dst_store() before
> inet_sk(newsk)->pinet6 has been set up.
> 
> This means ip6_dst_store() writes over the parent (listener)
> np->dst_cookie.
> 
> This is racy because multiple threads could share the same
> parent and their final np->dst_cookie could be wrong.
> 
> Move ip6_dst_store() call after inet_sk(newsk)->pinet6
> has been changed and after the copy of parent ipv6_pinfo.
> 
> Fixes: e994b2f0fb92 ("tcp: do not lock listener to process SYN packets")
> Signed-off-by: Eric Dumazet <edumazet@google.com>

Reviewed-by: Simon Horman <horms@kernel.org>

> ---
>  net/ipv6/tcp_ipv6.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
> index 8c577b651bfcd2f94b45e339ed4a2b47e93ff17a..729faf8bd366ad25d093a4ae931fb46ebd45b79c 100644
> --- a/net/ipv6/tcp_ipv6.c
> +++ b/net/ipv6/tcp_ipv6.c
> @@ -1439,7 +1439,6 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
>  	 */
>  
>  	newsk->sk_gso_type = SKB_GSO_TCPV6;
> -	ip6_dst_store(newsk, dst, NULL, NULL);
>  	inet6_sk_rx_dst_set(newsk, skb);
>  
>  	inet_sk(newsk)->pinet6 = tcp_inet6_sk(newsk);
> @@ -1450,6 +1449,8 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
>  
>  	memcpy(newnp, np, sizeof(struct ipv6_pinfo));
>  
> +	ip6_dst_store(newsk, dst, NULL, NULL);
> +
>  	newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
>  	newnp->saddr = ireq->ir_v6_loc_addr;
>  	newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
> -- 
> 2.45.1.467.gbab1589fc0-goog
> 
>
patchwork-bot+netdevbpf@kernel.org June 10, 2024, 12:20 p.m. UTC | #2
Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <davem@davemloft.net>:

On Thu,  6 Jun 2024 15:46:51 +0000 you wrote:
> tcp_v6_syn_recv_sock() calls ip6_dst_store() before
> inet_sk(newsk)->pinet6 has been set up.
> 
> This means ip6_dst_store() writes over the parent (listener)
> np->dst_cookie.
> 
> This is racy because multiple threads could share the same
> parent and their final np->dst_cookie could be wrong.
> 
> [...]

Here is the summary with links:
  - [net] tcp: fix race in tcp_v6_syn_recv_sock()
    https://git.kernel.org/netdev/net/c/d37fe4255abe

You are awesome, thank you!
David Ahern June 10, 2024, 3:30 p.m. UTC | #3
On 6/8/24 7:25 AM, Simon Horman wrote:
> + David Ahern
> 
> On Thu, Jun 06, 2024 at 03:46:51PM +0000, Eric Dumazet wrote:
>> tcp_v6_syn_recv_sock() calls ip6_dst_store() before
>> inet_sk(newsk)->pinet6 has been set up.
>>
>> This means ip6_dst_store() writes over the parent (listener)
>> np->dst_cookie.
>>
>> This is racy because multiple threads could share the same
>> parent and their final np->dst_cookie could be wrong.
>>
>> Move ip6_dst_store() call after inet_sk(newsk)->pinet6
>> has been changed and after the copy of parent ipv6_pinfo.
>>
>> Fixes: e994b2f0fb92 ("tcp: do not lock listener to process SYN packets")
>> Signed-off-by: Eric Dumazet <edumazet@google.com>
> 
> Reviewed-by: Simon Horman <horms@kernel.org>
> 
>> ---
>>  net/ipv6/tcp_ipv6.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>

Reviewed-by: David Ahern <dsahern@kernel.org>
diff mbox series

Patch

diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 8c577b651bfcd2f94b45e339ed4a2b47e93ff17a..729faf8bd366ad25d093a4ae931fb46ebd45b79c 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1439,7 +1439,6 @@  static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
 	 */
 
 	newsk->sk_gso_type = SKB_GSO_TCPV6;
-	ip6_dst_store(newsk, dst, NULL, NULL);
 	inet6_sk_rx_dst_set(newsk, skb);
 
 	inet_sk(newsk)->pinet6 = tcp_inet6_sk(newsk);
@@ -1450,6 +1449,8 @@  static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
 
 	memcpy(newnp, np, sizeof(struct ipv6_pinfo));
 
+	ip6_dst_store(newsk, dst, NULL, NULL);
+
 	newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
 	newnp->saddr = ireq->ir_v6_loc_addr;
 	newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;