Skip to content

Commit e22aa14

Browse files
sewookseodavem330
authored andcommitted
net: Find dst with sk's xfrm policy not ctl_sk
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 <[email protected]> Cc: Eric Dumazet <[email protected]> Cc: Steffen Klassert <[email protected]> Cc: Sehee Lee <[email protected]> Signed-off-by: Sewook Seo <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 0076cad commit e22aa14

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

include/net/xfrm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
11951195

11961196
static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
11971197
{
1198+
if (!sk_fullsock(osk))
1199+
return 0;
11981200
sk->sk_policy[0] = NULL;
11991201
sk->sk_policy[1] = NULL;
12001202
if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))

net/ipv4/ip_output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
17001700
tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
17011701
arg->uid);
17021702
security_skb_classify_flow(skb, flowi4_to_flowi_common(&fl4));
1703-
rt = ip_route_output_key(net, &fl4);
1703+
rt = ip_route_output_flow(net, &fl4, sk);
17041704
if (IS_ERR(rt))
17051705
return;
17061706

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
819819
ctl_sk->sk_priority = (sk->sk_state == TCP_TIME_WAIT) ?
820820
inet_twsk(sk)->tw_priority : sk->sk_priority;
821821
transmit_time = tcp_transmit_time(sk);
822+
xfrm_sk_clone_policy(ctl_sk, sk);
822823
}
823824
ip_send_unicast_reply(ctl_sk,
824825
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)
827828
transmit_time);
828829

829830
ctl_sk->sk_mark = 0;
831+
xfrm_sk_free_policy(ctl_sk);
830832
sock_net_set(ctl_sk, &init_net);
831833
__TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
832834
__TCP_INC_STATS(net, TCP_MIB_OUTRSTS);

net/ipv6/tcp_ipv6.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,10 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
952952
* Underlying function will use this to retrieve the network
953953
* namespace
954954
*/
955-
dst = ip6_dst_lookup_flow(sock_net(ctl_sk), ctl_sk, &fl6, NULL);
955+
if (sk && sk->sk_state != TCP_TIME_WAIT)
956+
dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL); /*sk's xfrm_policy can be referred*/
957+
else
958+
dst = ip6_dst_lookup_flow(net, ctl_sk, &fl6, NULL);
956959
if (!IS_ERR(dst)) {
957960
skb_dst_set(buff, dst);
958961
ip6_xmit(ctl_sk, buff, &fl6, fl6.flowi6_mark, NULL,

0 commit comments

Comments
 (0)