Skip to content

Commit 6c17205

Browse files
Florian WestphalJiri Slaby
authored andcommitted
tcp: syncookies: reduce cookie lifetime to 128 seconds
commit 8c27bd7 upstream. We currently accept cookies that were created less than 4 minutes ago (ie, cookies with counter delta 0-3). Combined with the 8 mss table values, this yields 32 possible values (out of 2**32) that will be valid. Reducing the lifetime to < 2 minutes halves the guessing chance while still providing a large enough period. While at it, get rid of jiffies value -- they overflow too quickly on 32 bit platforms. getnstimeofday is used to create a counter that increments every 64s. perf shows getnstimeofday cost is negible compared to sha_transform; normal tcp initial sequence number generation uses getnstimeofday, too. Reported-by: Jakob Lell <jakob@jakoblell.com> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 5516d3f commit 6c17205

3 files changed

Lines changed: 35 additions & 38 deletions

File tree

‎include/net/tcp.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,24 @@ extern int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
482482
extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
483483
struct ip_options *opt);
484484
#ifdef CONFIG_SYN_COOKIES
485+
#include <linux/ktime.h>
486+
487+
/* Syncookies use a monotonic timer which increments every 64 seconds.
488+
* This counter is used both as a hash input and partially encoded into
489+
* the cookie value. A cookie is only validated further if the delta
490+
* between the current counter value and the encoded one is less than this,
491+
* i.e. a sent cookie is valid only at most for 128 seconds (or less if
492+
* the counter advances immediately after a cookie is generated).
493+
*/
494+
#define MAX_SYNCOOKIE_AGE 2
495+
496+
static inline u32 tcp_cookie_time(void)
497+
{
498+
struct timespec now;
499+
getnstimeofday(&now);
500+
return now.tv_sec >> 6; /* 64 seconds granularity */
501+
}
502+
485503
extern u32 __cookie_v4_init_sequence(const struct iphdr *iph,
486504
const struct tcphdr *th, u16 *mssp);
487505
extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,

‎net/ipv4/syncookies.c‎

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ __u32 cookie_init_timestamp(struct request_sock *req)
8989

9090

9191
static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
92-
__be16 dport, __u32 sseq, __u32 count,
93-
__u32 data)
92+
__be16 dport, __u32 sseq, __u32 data)
9493
{
9594
/*
9695
* Compute the secure sequence number.
@@ -102,7 +101,7 @@ static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
102101
* As an extra hack, we add a small "data" value that encodes the
103102
* MSS into the second hash value.
104103
*/
105-
104+
u32 count = tcp_cookie_time();
106105
return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
107106
sseq + (count << COOKIEBITS) +
108107
((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
@@ -114,22 +113,21 @@ static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
114113
* If the syncookie is bad, the data returned will be out of
115114
* range. This must be checked by the caller.
116115
*
117-
* The count value used to generate the cookie must be within
118-
* "maxdiff" if the current (passed-in) "count". The return value
119-
* is (__u32)-1 if this test fails.
116+
* The count value used to generate the cookie must be less than
117+
* MAX_SYNCOOKIE_AGE minutes in the past.
118+
* The return value (__u32)-1 if this test fails.
120119
*/
121120
static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
122-
__be16 sport, __be16 dport, __u32 sseq,
123-
__u32 count, __u32 maxdiff)
121+
__be16 sport, __be16 dport, __u32 sseq)
124122
{
125-
__u32 diff;
123+
u32 diff, count = tcp_cookie_time();
126124

127125
/* Strip away the layers from the cookie */
128126
cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
129127

130128
/* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
131129
diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
132-
if (diff >= maxdiff)
130+
if (diff >= MAX_SYNCOOKIE_AGE)
133131
return (__u32)-1;
134132

135133
return (cookie -
@@ -173,7 +171,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
173171

174172
return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
175173
th->source, th->dest, ntohl(th->seq),
176-
jiffies / (HZ * 60), mssind);
174+
mssind);
177175
}
178176
EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
179177

@@ -188,13 +186,6 @@ __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
188186
return __cookie_v4_init_sequence(iph, th, mssp);
189187
}
190188

191-
/*
192-
* This (misnamed) value is the age of syncookie which is permitted.
193-
* Its ideal value should be dependent on TCP_TIMEOUT_INIT and
194-
* sysctl_tcp_retries1. It's a rather complicated formula (exponential
195-
* backoff) to compute at runtime so it's currently hardcoded here.
196-
*/
197-
#define COUNTER_TRIES 4
198189
/*
199190
* Check if a ack sequence number is a valid syncookie.
200191
* Return the decoded mss if it is, or 0 if not.
@@ -204,9 +195,7 @@ int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
204195
{
205196
__u32 seq = ntohl(th->seq) - 1;
206197
__u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
207-
th->source, th->dest, seq,
208-
jiffies / (HZ * 60),
209-
COUNTER_TRIES);
198+
th->source, th->dest, seq);
210199

211200
return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
212201
}

‎net/ipv6/syncookies.c‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ static __u16 const msstab[] = {
3636
9000 - 60,
3737
};
3838

39-
/*
40-
* This (misnamed) value is the age of syncookie which is permitted.
41-
* Its ideal value should be dependent on TCP_TIMEOUT_INIT and
42-
* sysctl_tcp_retries1. It's a rather complicated formula (exponential
43-
* backoff) to compute at runtime so it's currently hardcoded here.
44-
*/
45-
#define COUNTER_TRIES 4
46-
4739
static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
4840
struct request_sock *req,
4941
struct dst_entry *dst)
@@ -86,8 +78,9 @@ static u32 cookie_hash(const struct in6_addr *saddr, const struct in6_addr *dadd
8678
static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
8779
const struct in6_addr *daddr,
8880
__be16 sport, __be16 dport, __u32 sseq,
89-
__u32 count, __u32 data)
81+
__u32 data)
9082
{
83+
u32 count = tcp_cookie_time();
9184
return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
9285
sseq + (count << COOKIEBITS) +
9386
((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
@@ -96,15 +89,14 @@ static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
9689

9790
static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr,
9891
const struct in6_addr *daddr, __be16 sport,
99-
__be16 dport, __u32 sseq, __u32 count,
100-
__u32 maxdiff)
92+
__be16 dport, __u32 sseq)
10193
{
102-
__u32 diff;
94+
__u32 diff, count = tcp_cookie_time();
10395

10496
cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
10597

10698
diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
107-
if (diff >= maxdiff)
99+
if (diff >= MAX_SYNCOOKIE_AGE)
108100
return (__u32)-1;
109101

110102
return (cookie -
@@ -125,8 +117,7 @@ u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
125117
*mssp = msstab[mssind];
126118

127119
return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source,
128-
th->dest, ntohl(th->seq),
129-
jiffies / (HZ * 60), mssind);
120+
th->dest, ntohl(th->seq), mssind);
130121
}
131122
EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence);
132123

@@ -146,8 +137,7 @@ int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
146137
{
147138
__u32 seq = ntohl(th->seq) - 1;
148139
__u32 mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr,
149-
th->source, th->dest, seq,
150-
jiffies / (HZ * 60), COUNTER_TRIES);
140+
th->source, th->dest, seq);
151141

152142
return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
153143
}

0 commit comments

Comments
 (0)