Skip to content

Commit cca9bab

Browse files
arndbdavem330
authored andcommitted
tcp: use monotonic timestamps for PAWS
Using get_seconds() for timestamps is deprecated since it can lead to overflows on 32-bit systems. While the interface generally doesn't overflow until year 2106, the specific implementation of the TCP PAWS algorithm breaks in 2038 when the intermediate signed 32-bit timestamps overflow. A related problem is that the local timestamps in CLOCK_REALTIME form lead to unexpected behavior when settimeofday is called to set the system clock backwards or forwards by more than 24 days. While the first problem could be solved by using an overflow-safe method of comparing the timestamps, a nicer solution is to use a monotonic clocksource with ktime_get_seconds() that simply doesn't overflow (at least not until 136 years after boot) and that doesn't change during settimeofday(). To make 32-bit and 64-bit architectures behave the same way here, and also save a few bytes in the tcp_options_received structure, I'm changing the type to a 32-bit integer, which is now safe on all architectures. Finally, the ts_recent_stamp field also (confusingly) gets used to store a jiffies value in tcp_synq_overflow()/tcp_synq_no_recent_overflow(). This is currently safe, but changing the type to 32-bit requires some small changes there to keep it working. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d2bdd26 commit cca9bab

6 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎drivers/crypto/chelsio/chtls/chtls_cm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,7 @@ static void chtls_timewait(struct sock *sk)
16731673
struct tcp_sock *tp = tcp_sk(sk);
16741674

16751675
tp->rcv_nxt++;
1676-
tp->rx_opt.ts_recent_stamp = get_seconds();
1676+
tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
16771677
tp->srtt_us = 0;
16781678
tcp_time_wait(sk, TCP_TIME_WAIT, 0);
16791679
}

‎include/linux/tcp.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct tcp_sack_block {
8989

9090
struct tcp_options_received {
9191
/* PAWS/RTTM data */
92-
long ts_recent_stamp;/* Time we stored ts_recent (for aging) */
92+
int ts_recent_stamp;/* Time we stored ts_recent (for aging) */
9393
u32 ts_recent; /* Time stamp to echo next */
9494
u32 rcv_tsval; /* Time stamp value */
9595
u32 rcv_tsecr; /* Time stamp echo reply */
@@ -426,7 +426,7 @@ struct tcp_timewait_sock {
426426
/* The time we sent the last out-of-window ACK: */
427427
u32 tw_last_oow_ack_time;
428428

429-
long tw_ts_recent_stamp;
429+
int tw_ts_recent_stamp;
430430
#ifdef CONFIG_TCP_MD5SIG
431431
struct tcp_md5sig_key *tw_md5_key;
432432
#endif

‎include/net/tcp.h‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,20 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
472472
*/
473473
static inline void tcp_synq_overflow(const struct sock *sk)
474474
{
475-
unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
476-
unsigned long now = jiffies;
475+
unsigned int last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
476+
unsigned int now = jiffies;
477477

478-
if (time_after(now, last_overflow + HZ))
478+
if (time_after32(now, last_overflow + HZ))
479479
tcp_sk(sk)->rx_opt.ts_recent_stamp = now;
480480
}
481481

482482
/* syncookies: no recent synqueue overflow on this listening socket? */
483483
static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
484484
{
485-
unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
485+
unsigned int last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
486+
unsigned int now = jiffies;
486487

487-
return time_after(jiffies, last_overflow + TCP_SYNCOOKIE_VALID);
488+
return time_after32(now, last_overflow + TCP_SYNCOOKIE_VALID);
488489
}
489490

490491
static inline u32 tcp_cookie_time(void)
@@ -1375,7 +1376,8 @@ static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
13751376
{
13761377
if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
13771378
return true;
1378-
if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1379+
if (unlikely(!time_before32(ktime_get_seconds(),
1380+
rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
13791381
return true;
13801382
/*
13811383
* Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
@@ -1405,7 +1407,8 @@ static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
14051407
14061408
However, we can relax time bounds for RST segments to MSL.
14071409
*/
1408-
if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1410+
if (rst && !time_before32(ktime_get_seconds(),
1411+
rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
14091412
return false;
14101413
return true;
14111414
}

‎net/ipv4/tcp_input.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3462,7 +3462,7 @@ static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
34623462
static void tcp_store_ts_recent(struct tcp_sock *tp)
34633463
{
34643464
tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3465-
tp->rx_opt.ts_recent_stamp = get_seconds();
3465+
tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
34663466
}
34673467

34683468
static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
155155
and use initial timestamp retrieved from peer table.
156156
*/
157157
if (tcptw->tw_ts_recent_stamp &&
158-
(!twp || (reuse && get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
158+
(!twp || (reuse && time_after32(ktime_get_seconds(),
159+
tcptw->tw_ts_recent_stamp)))) {
159160
tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
160161
if (tp->write_seq == 0)
161162
tp->write_seq = 1;

‎net/ipv4/tcp_minisocks.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
144144
tw->tw_substate = TCP_TIME_WAIT;
145145
tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
146146
if (tmp_opt.saw_tstamp) {
147-
tcptw->tw_ts_recent_stamp = get_seconds();
147+
tcptw->tw_ts_recent_stamp = ktime_get_seconds();
148148
tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
149149
}
150150

@@ -189,7 +189,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
189189

190190
if (tmp_opt.saw_tstamp) {
191191
tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
192-
tcptw->tw_ts_recent_stamp = get_seconds();
192+
tcptw->tw_ts_recent_stamp = ktime_get_seconds();
193193
}
194194

195195
inet_twsk_put(tw);
@@ -537,7 +537,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
537537

538538
if (newtp->rx_opt.tstamp_ok) {
539539
newtp->rx_opt.ts_recent = req->ts_recent;
540-
newtp->rx_opt.ts_recent_stamp = get_seconds();
540+
newtp->rx_opt.ts_recent_stamp = ktime_get_seconds();
541541
newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
542542
} else {
543543
newtp->rx_opt.ts_recent_stamp = 0;
@@ -603,7 +603,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
603603
* it can be estimated (approximately)
604604
* from another data.
605605
*/
606-
tmp_opt.ts_recent_stamp = get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->num_timeout);
606+
tmp_opt.ts_recent_stamp = ktime_get_seconds() - ((TCP_TIMEOUT_INIT/HZ)<<req->num_timeout);
607607
paws_reject = tcp_paws_reject(&tmp_opt, th->rst);
608608
}
609609
}

0 commit comments

Comments
 (0)