Skip to content

Commit 50a6866

Browse files
committed
Mitigate panics due to falsely monotonic clocks
Example backtrace on arm64: /lib/arm64/libnativetunnel.so (core::option::expect_failed::h4b77ebe6e62ec3a1+64) /lib/arm64/libnativetunnel.so (std::time::Instant::duration_since::h632e3fc95ad5458d+68) /lib/arm64/libnativetunnel.so (boringtun::noise::timers::_$LT$impl$u20$boringtun..noise..Tunn$GT$::update_timers::hc9bb6fc49d2aed16+2688) This should never happen, but yet it does - so apply some defensive programming.
1 parent 0b980a2 commit 50a6866

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/noise/timers.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Timers {
7878
// We don't really clear the timers, but we set them to the current time to
7979
// so the reference time frame is the same
8080
pub(super) fn clear(&self) {
81-
let now = Instant::now().duration_since(self.time_started);
81+
let now = self.time_started.elapsed();
8282
for t in &self.timers[..] {
8383
t.set(now);
8484
}
@@ -164,6 +164,10 @@ impl Tunn {
164164

165165
let time = Instant::now();
166166
let timers = &self.timers;
167+
// This should be unnecessary, but we observe actual panics in the wild
168+
// where both clock monotonicity *and* the Rust stdlib protections
169+
// against nonmonotonic clocks violate their contracts.
170+
let time = if time > timers.time_started { time } else { timers.time_started };
167171

168172
if timers.should_reset_rr {
169173
self.rate_limiter.reset_count();

0 commit comments

Comments
 (0)