]> git.itanic.dy.fi Git - linux-stable/commitdiff
net: Find dst with sk's xfrm policy not ctl_sk
authorsewookseo <sewookseo@google.com>
Thu, 7 Jul 2022 10:01:39 +0000 (10:01 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 23 Sep 2022 12:14:06 +0000 (14:14 +0200)
commit e22aa14866684f77b4f6b6cae98539e520ddb731 upstream.

If we set XFRM security policy by calling setsockopt with option
IPV6_XFRM_POLICY, the policy will be stored in 'sock_policy' in 'sock'
struct. However tcp_v6_send_response doesn't look up dst_entry with the
actual socket but looks up with tcp control socket. This may cause a
problem that a RST packet is sent without ESP encryption & peer's TCP
socket can't receive it.
This patch will make the function look up dest_entry with actual socket,
if the socket has XFRM policy(sock_policy), so that the TCP response
packet via this function can be encrypted, & aligned on the encrypted
TCP socket.

Tested: We encountered this problem when a TCP socket which is encrypted
in ESP transport mode encryption, receives challenge ACK at SYN_SENT
state. After receiving challenge ACK, TCP needs to send RST to
establish the socket at next SYN try. But the RST was not encrypted &
peer TCP socket still remains on ESTABLISHED state.
So we verified this with test step as below.
[Test step]
1. Making a TCP state mismatch between client(IDLE) & server(ESTABLISHED).
2. Client tries a new connection on the same TCP ports(src & dst).
3. Server will return challenge ACK instead of SYN,ACK.
4. Client will send RST to server to clear the SOCKET.
5. Client will retransmit SYN to server on the same TCP ports.
[Expected result]
The TCP connection should be established.

Cc: Maciej Żenczykowski <maze@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Steffen Klassert <steffen.klassert@secunet.com>
Cc: Sehee Lee <seheele@google.com>
Signed-off-by: Sewook Seo <sewookseo@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/net/xfrm.h
net/ipv4/ip_output.c
net/ipv4/tcp_ipv4.c
net/ipv6/tcp_ipv6.c

index c39d910d4b4544f5faa25df9f8f2c5440c629de0..9ca397eed16389035676650380bee380ac3cf7ca 100644 (file)
@@ -1195,6 +1195,8 @@ int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
 
 static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
 {
+       if (!sk_fullsock(osk))
+               return 0;
        sk->sk_policy[0] = NULL;
        sk->sk_policy[1] = NULL;
        if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
index da8b3cc67234d891be8ec39b7db44c809a1a101c..028eb28c7882d0c4243d12c48710f72c41fac1c4 100644 (file)
@@ -1704,7 +1704,7 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
                           tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
                           arg->uid);
        security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
-       rt = ip_route_output_key(net, &fl4);
+       rt = ip_route_output_flow(net, &fl4, sk);
        if (IS_ERR(rt))
                return;
 
index 586c102ce152d01430e89d400d70f2573483b423..9fd92e263d0a30191adabe618099c3daf5cb9c4a 100644 (file)
@@ -819,6 +819,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
                ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ?
                                   inet_twsk(sk)->tw_priority : sk->sk_priority;
                transmit_time = tcp_transmit_time(sk);
+               xfrm_sk_clone_policy(ctl_sk, sk);
        }
        ip_send_unicast_reply(ctl_sk,
                              skb, &TCP_SKB_CB(skb)->header.h4.opt,
@@ -827,6 +828,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
                              transmit_time);
 
        ctl_sk->sk_mark = 0;
+       xfrm_sk_free_policy(ctl_sk);
        sock_net_set(ctl_sk, &init_net);
        __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
        __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
index be09941fe6d9a84029c02464406f4d7bd78968da..5eabe746cfa76a0555d530dc255ab056de2095e8 100644 (file)
@@ -952,7 +952,10 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
         * Underlying function will use this to retrieve the network
         * namespace
         */
-       dst = ip6_dst_lookup_flow(sock_net(ctl_sk), ctl_sk, &fl6, NULL);
+       if (sk && sk->sk_state != TCP_TIME_WAIT)
+               dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL); /*sk's xfrm_policy can be referred*/
+       else
+               dst = ip6_dst_lookup_flow(net, ctl_sk, &fl6, NULL);
        if (!IS_ERR(dst)) {
                skb_dst_set(buff, dst);
                ip6_xmit(ctl_sk, buff, &fl6, fl6.flowi6_mark, NULL,