Skip to contentSkip to conversation

tcp: take care of SYN_RECV sockets in tcp_v4_send_ack() and tcp_v6_send_response()

PATCH In review Eric Dumazet 3 participants 6 messages Jun 10, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: take care of SYN_RECV sockets in tcp_v4_send_ack() and tcp_v6_send_response()

TCP can send ACK packets on behalf of SYN_RECV sockets.

tcp_v4_send_ack() and tcp_v6_send_response() incorrectly dereference sk->sk_mark for non TIME_WAIT sockets.

This field is not defined for SYN_RECV sockets.

Using sk_to_full_sk() should get back to the listener socket.

Note that this also provides a socket pointer to sock_net_uid() calls.

Fixes 00483690552c ("tcp: Add mark for TIMEWAIT sockets")
Cc Jon Maxwell <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 6 ++++--
 net/ipv6/tcp_ipv6.c | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +4 -2
index f059fbd81a84314ae6fef37f600b0cf28bd2ad30..2bb27d5eae78efdff52a741904d7526a234595d8 100644
@@ -856,12 +856,14 @@ static void tcp_v4_send_ack(const struct sock *sk,
 	if (oif)
 		arg.bound_dev_if = oif;
 	arg.tos = tos;
-	arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
 	local_bh_disable();
 	ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
-	if (sk)
+	if (sk) {
+		sk = sk_to_full_sk(sk);
 		ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
 				   inet_twsk(sk)->tw_mark : sk->sk_mark;
+	}
+	arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
 	ip_send_unicast_reply(ctl_sk,
 			      skb, &TCP_SKB_CB(skb)->header.h4.opt,
 			      ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
net/ipv6/tcp_ipv6.c +1 -0
index ad7039137a20f9ad8581d9ca01347c67aa8a8433..ea4dd988bc7f9a90e0d95283e10db5a517a59027 100644
@@ -884,6 +884,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
 	}
 
 	if (sk) {
+		sk = sk_to_full_sk(sk);
 		if (sk->sk_state == TCP_TIME_WAIT) {
 			mark = inet_twsk(sk)->tw_mark;
 			/* autoflowlabel relies on buff->hash */
Signature · 1 line
2.22.0.rc2.383.gf4fbbf30c2-goog
maintainer
#1 lore raw

From: Eric Dumazet <[email protected]>
Date: Mon, 10 Jun 2019 14:45:43 -0700

Quoted 1 line

Using sk_to_full_sk() should get back to the listener socket.

net/ipv6/tcp_ipv6.c: In function ‘tcp_v6_send_response’:
net/ipv6/tcp_ipv6.c:887:22: warning: passing argument 1 of ‘sk_to_full_sk’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

#2 lore raw
Quoted 8 lines · David Miller

From: Eric Dumazet <[email protected]>
Date: Mon, 10 Jun 2019 14:45:43 -0700

Quoted 1 line

Using sk_to_full_sk() should get back to the listener socket.

net/ipv6/tcp_ipv6.c: In function ‘tcp_v6_send_response’:
net/ipv6/tcp_ipv6.c:887:22: warning: passing argument 1 of ‘sk_to_full_sk’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

I was about to ask you to drop this patch.

Apparently the only case we can have SYN_RECV sockets here are for fastopen, and in this case sk->sk_mark defined.

This only leaves the chance to get better sock_net_uid() reach, and my upcoming patch adding per-tcp-socket arbitrary tx delay.

#3 lore raw
In reply to Eric Dumazet

Thanks for fixing that Eric.

Quoted 56 lines · Eric Dumazet

TCP can send ACK packets on behalf of SYN_RECV sockets.

tcp_v4_send_ack() and tcp_v6_send_response() incorrectly dereference sk->sk_mark for non TIME_WAIT sockets.

This field is not defined for SYN_RECV sockets.

Using sk_to_full_sk() should get back to the listener socket.

Note that this also provides a socket pointer to sock_net_uid() calls.

Fixes 00483690552c ("tcp: Add mark for TIMEWAIT sockets")
Cc Jon Maxwell <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 6 ++++--
 net/ipv6/tcp_ipv6.c | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +4 -2
index f059fbd81a84314ae6fef37f600b0cf28bd2ad30..2bb27d5eae78efdff52a741904d7526a234595d8 100644
@@ -856,12 +856,14 @@ static void tcp_v4_send_ack(const struct sock *sk,
        if (oif)
                arg.bound_dev_if = oif;
        arg.tos = tos;
-       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        local_bh_disable();
        ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
-       if (sk)
+       if (sk) {
+               sk = sk_to_full_sk(sk);
                ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
                                   inet_twsk(sk)->tw_mark : sk->sk_mark;
+       }
+       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        ip_send_unicast_reply(ctl_sk,
                              skb, &TCP_SKB_CB(skb)->header.h4.opt,
                              ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
net/ipv6/tcp_ipv6.c +1 -1
index ad7039137a20f9ad8581d9ca01347c67aa8a8433..ea4dd988bc7f9a90e0d95283e10db5a517a59027 100644
@@ -884,6 +884,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
        }
 
        if (sk) {
+               sk = sk_to_full_sk(sk);
                if (sk->sk_state == TCP_TIME_WAIT) {
                        mark = inet_twsk(sk)->tw_mark;
                        /* autoflowlabel relies on buff->hash */
--

2.22.0.rc2.383.gf4fbbf30c2-goog

#4 lore raw
Quoted 2 lines · Jonathan Maxwell

Thanks for fixing that Eric.

Hi Jonathan.

There is no bug actually, at least not on sk->sk_mark part.

I will send a simpler patch so that sock_net_uid() correctly fetches sk->sk_uid for FASTOPEN socket.

Thanks !

Quoted 58 lines · Eric Dumazet
Quoted 56 lines

TCP can send ACK packets on behalf of SYN_RECV sockets.

tcp_v4_send_ack() and tcp_v6_send_response() incorrectly dereference sk->sk_mark for non TIME_WAIT sockets.

This field is not defined for SYN_RECV sockets.

Using sk_to_full_sk() should get back to the listener socket.

Note that this also provides a socket pointer to sock_net_uid() calls.

Fixes 00483690552c ("tcp: Add mark for TIMEWAIT sockets")
Cc Jon Maxwell <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 6 ++++--
 net/ipv6/tcp_ipv6.c | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +4 -2
index f059fbd81a84314ae6fef37f600b0cf28bd2ad30..2bb27d5eae78efdff52a741904d7526a234595d8 100644
@@ -856,12 +856,14 @@ static void tcp_v4_send_ack(const struct sock *sk,
        if (oif)
                arg.bound_dev_if = oif;
        arg.tos = tos;
-       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        local_bh_disable();
        ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
-       if (sk)
+       if (sk) {
+               sk = sk_to_full_sk(sk);
                ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
                                   inet_twsk(sk)->tw_mark : sk->sk_mark;
+       }
+       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        ip_send_unicast_reply(ctl_sk,
                              skb, &TCP_SKB_CB(skb)->header.h4.opt,
                              ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
net/ipv6/tcp_ipv6.c +1 -1
index ad7039137a20f9ad8581d9ca01347c67aa8a8433..ea4dd988bc7f9a90e0d95283e10db5a517a59027 100644
@@ -884,6 +884,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
        }
 
        if (sk) {
+               sk = sk_to_full_sk(sk);
                if (sk->sk_state == TCP_TIME_WAIT) {
                        mark = inet_twsk(sk)->tw_mark;
                        /* autoflowlabel relies on buff->hash */
--

2.22.0.rc2.383.gf4fbbf30c2-goog

#5 lore raw

Sounds good Eric. Thanks

Quoted 74 lines · Eric Dumazet
Quoted 2 lines · Jonathan Maxwell

Thanks for fixing that Eric.

Hi Jonathan.

There is no bug actually, at least not on sk->sk_mark part.

I will send a simpler patch so that sock_net_uid() correctly fetches sk->sk_uid for FASTOPEN socket.

Thanks !

Quoted 58 lines · Eric Dumazet
Quoted 56 lines

TCP can send ACK packets on behalf of SYN_RECV sockets.

tcp_v4_send_ack() and tcp_v6_send_response() incorrectly dereference sk->sk_mark for non TIME_WAIT sockets.

This field is not defined for SYN_RECV sockets.

Using sk_to_full_sk() should get back to the listener socket.

Note that this also provides a socket pointer to sock_net_uid() calls.

Fixes 00483690552c ("tcp: Add mark for TIMEWAIT sockets")
Cc Jon Maxwell <[email protected]>
---
 net/ipv4/tcp_ipv4.c | 6 ++++--
 net/ipv6/tcp_ipv6.c | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
net/ipv4/tcp_ipv4.c +4 -2
index f059fbd81a84314ae6fef37f600b0cf28bd2ad30..2bb27d5eae78efdff52a741904d7526a234595d8 100644
@@ -856,12 +856,14 @@ static void tcp_v4_send_ack(const struct sock *sk,
        if (oif)
                arg.bound_dev_if = oif;
        arg.tos = tos;
-       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        local_bh_disable();
        ctl_sk = this_cpu_read(*net->ipv4.tcp_sk);
-       if (sk)
+       if (sk) {
+               sk = sk_to_full_sk(sk);
                ctl_sk->sk_mark = (sk->sk_state == TCP_TIME_WAIT) ?
                                   inet_twsk(sk)->tw_mark : sk->sk_mark;
+       }
+       arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
        ip_send_unicast_reply(ctl_sk,
                              skb, &TCP_SKB_CB(skb)->header.h4.opt,
                              ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
net/ipv6/tcp_ipv6.c +1 -1
index ad7039137a20f9ad8581d9ca01347c67aa8a8433..ea4dd988bc7f9a90e0d95283e10db5a517a59027 100644
@@ -884,6 +884,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
        }
 
        if (sk) {
+               sk = sk_to_full_sk(sk);
                if (sk->sk_state == TCP_TIME_WAIT) {
                        mark = inet_twsk(sk)->tw_mark;
                        /* autoflowlabel relies on buff->hash */
--

2.22.0.rc2.383.gf4fbbf30c2-goog