a783c6ebbce84dbc202e962ece8df7c0a147d17c
[openwrt/openwrt.git] / target / linux / generic / patches-4.4 / 096-tcp-make-challenge-acks-less-predictable.patch
1 From 75ff39ccc1bd5d3c455b6822ab09e533c551f758 Mon Sep 17 00:00:00 2001
2 From: Eric Dumazet <edumazet@google.com>
3 Date: Sun, 10 Jul 2016 10:04:02 +0200
4 Subject: [PATCH] tcp: make challenge acks less predictable
5
6 Yue Cao claims that current host rate limiting of challenge ACKS
7 (RFC 5961) could leak enough information to allow a patient attacker
8 to hijack TCP sessions. He will soon provide details in an academic
9 paper.
10
11 This patch increases the default limit from 100 to 1000, and adds
12 some randomization so that the attacker can no longer hijack
13 sessions without spending a considerable amount of probes.
14
15 Based on initial analysis and patch from Linus.
16
17 Note that we also have per socket rate limiting, so it is tempting
18 to remove the host limit in the future.
19
20 v2: randomize the count of challenge acks per second, not the period.
21
22 Fixes: 282f23c6ee34 ("tcp: implement RFC 5961 3.2")
23 Reported-by: Yue Cao <ycao009@ucr.edu>
24 Signed-off-by: Eric Dumazet <edumazet@google.com>
25 Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
26 Cc: Yuchung Cheng <ycheng@google.com>
27 Cc: Neal Cardwell <ncardwell@google.com>
28 Acked-by: Neal Cardwell <ncardwell@google.com>
29 Acked-by: Yuchung Cheng <ycheng@google.com>
30 Signed-off-by: David S. Miller <davem@davemloft.net>
31 ---
32 net/ipv4/tcp_input.c | 15 ++++++++++-----
33 1 file changed, 10 insertions(+), 5 deletions(-)
34
35 --- a/net/ipv4/tcp_input.c
36 +++ b/net/ipv4/tcp_input.c
37 @@ -89,7 +89,7 @@ int sysctl_tcp_adv_win_scale __read_most
38 EXPORT_SYMBOL(sysctl_tcp_adv_win_scale);
39
40 /* rfc5961 challenge ack rate limiting */
41 -int sysctl_tcp_challenge_ack_limit = 100;
42 +int sysctl_tcp_challenge_ack_limit = 1000;
43
44 int sysctl_tcp_stdurg __read_mostly;
45 int sysctl_tcp_rfc1337 __read_mostly;
46 @@ -3427,7 +3427,7 @@ static void tcp_send_challenge_ack(struc
47 static u32 challenge_timestamp;
48 static unsigned int challenge_count;
49 struct tcp_sock *tp = tcp_sk(sk);
50 - u32 now;
51 + u32 count, now;
52
53 /* First check our per-socket dupack rate limit. */
54 if (tcp_oow_rate_limited(sock_net(sk), skb,
55 @@ -3435,13 +3435,18 @@ static void tcp_send_challenge_ack(struc
56 &tp->last_oow_ack_time))
57 return;
58
59 - /* Then check the check host-wide RFC 5961 rate limit. */
60 + /* Then check host-wide RFC 5961 rate limit. */
61 now = jiffies / HZ;
62 if (now != challenge_timestamp) {
63 + u32 half = (sysctl_tcp_challenge_ack_limit + 1) >> 1;
64 +
65 challenge_timestamp = now;
66 - challenge_count = 0;
67 + WRITE_ONCE(challenge_count, half +
68 + prandom_u32_max(sysctl_tcp_challenge_ack_limit));
69 }
70 - if (++challenge_count <= sysctl_tcp_challenge_ack_limit) {
71 + count = READ_ONCE(challenge_count);
72 + if (count > 0) {
73 + WRITE_ONCE(challenge_count, count - 1);
74 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
75 tcp_send_ack(sk);
76 }