Skip to content

Commit 5532946

Browse files
committed
Merge branch 'tcp-fix-handling-of-stale-syncookies-timestamps'
Guillaume Nault says: ==================== tcp: fix handling of stale syncookies timestamps The synflood timestamps (->ts_recent_stamp and ->synq_overflow_ts) are only refreshed when the syncookie protection triggers. Therefore, their value can become very far apart from jiffies if no synflood happens for a long time. If jiffies grows too much and wraps while the synflood timestamp isn't refreshed, then time_after32() might consider the later to be in the future. This can trick tcp_synq_no_recent_overflow() into returning erroneous values and rejecting valid ACKs. Patch 1 handles the case of ACKs using legitimate syncookies. Patch 2 handles the case of stray ACKs. Patch 3 annotates lockless timestamp operations with READ_ONCE() and WRITE_ONCE(). Changes from v3: - Fix description of time_between32() (found by Eric Dumazet). - Use more accurate Fixes tag in patch 3 (suggested by Eric Dumazet). Changes from v2: - Define and use time_between32() instead of a pair of time_before32/time_after32 (suggested by Eric Dumazet). - Use 'last_overflow - HZ' as lower bound in tcp_synq_no_recent_overflow(), to accommodate for concurrent timestamp updates (found by Eric Dumazet). - Add a third patch to annotate lockless accesses to .ts_recent_stamp. Changes from v1: - Initialising timestamps at socket creation time is not enough because jiffies wraps in 24 days with HZ=1000 (Eric Dumazet). Handle stale timestamps in tcp_synq_overflow() and tcp_synq_no_recent_overflow() instead. - Rework commit description. - Add a second patch to handle the case of stray ACKs. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 537d077 + 721c8da commit 5532946

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

include/linux/time.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,17 @@ static inline bool itimerspec64_valid(const struct itimerspec64 *its)
9797
*/
9898
#define time_after32(a, b) ((s32)((u32)(b) - (u32)(a)) < 0)
9999
#define time_before32(b, a) time_after32(a, b)
100+
101+
/**
102+
* time_between32 - check if a 32-bit timestamp is within a given time range
103+
* @t: the time which may be within [l,h]
104+
* @l: the lower bound of the range
105+
* @h: the higher bound of the range
106+
*
107+
* time_before32(t, l, h) returns true if @l <= @t <= @h. All operands are
108+
* treated as 32-bit integers.
109+
*
110+
* Equivalent to !(time_before32(@t, @l) || time_after32(@t, @h)).
111+
*/
112+
#define time_between32(t, l, h) ((u32)(h) - (u32)(l) >= (u32)(t) - (u32)(l))
100113
#endif

include/net/tcp.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,16 @@ static inline void tcp_synq_overflow(const struct sock *sk)
494494
reuse = rcu_dereference(sk->sk_reuseport_cb);
495495
if (likely(reuse)) {
496496
last_overflow = READ_ONCE(reuse->synq_overflow_ts);
497-
if (time_after32(now, last_overflow + HZ))
497+
if (!time_between32(now, last_overflow,
498+
last_overflow + HZ))
498499
WRITE_ONCE(reuse->synq_overflow_ts, now);
499500
return;
500501
}
501502
}
502503

503-
last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
504-
if (time_after32(now, last_overflow + HZ))
505-
tcp_sk(sk)->rx_opt.ts_recent_stamp = now;
504+
last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
505+
if (!time_between32(now, last_overflow, last_overflow + HZ))
506+
WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
506507
}
507508

508509
/* syncookies: no recent synqueue overflow on this listening socket? */
@@ -517,13 +518,23 @@ static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
517518
reuse = rcu_dereference(sk->sk_reuseport_cb);
518519
if (likely(reuse)) {
519520
last_overflow = READ_ONCE(reuse->synq_overflow_ts);
520-
return time_after32(now, last_overflow +
521-
TCP_SYNCOOKIE_VALID);
521+
return !time_between32(now, last_overflow - HZ,
522+
last_overflow +
523+
TCP_SYNCOOKIE_VALID);
522524
}
523525
}
524526

525-
last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
526-
return time_after32(now, last_overflow + TCP_SYNCOOKIE_VALID);
527+
last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
528+
529+
/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
530+
* then we're under synflood. However, we have to use
531+
* 'last_overflow - HZ' as lower bound. That's because a concurrent
532+
* tcp_synq_overflow() could update .ts_recent_stamp after we read
533+
* jiffies but before we store .ts_recent_stamp into last_overflow,
534+
* which could lead to rejecting a valid syncookie.
535+
*/
536+
return !time_between32(now, last_overflow - HZ,
537+
last_overflow + TCP_SYNCOOKIE_VALID);
527538
}
528539

529540
static inline u32 tcp_cookie_time(void)

0 commit comments

Comments
 (0)