diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 4d688939e..648c014ab 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -345,6 +345,7 @@ type LocalBackend struct { // hostinfo is mutated in-place while mu is held. hostinfo *tailcfg.Hostinfo // TODO(nickkhyl): move to nodeBackend nmExpiryTimer tstime.TimerController // for updating netMap on node expiry; can be nil; TODO(nickkhyl): move to nodeBackend + nmExpiryAt time.Time // wall time at which nmExpiryTimer should fire activeLogin string // last logged LoginName from netMap; TODO(nickkhyl): move to nodeBackend (or remove? it's in [ipn.LoginProfile]). engineStatus ipn.EngineStatus endpoints []tailcfg.Endpoint @@ -1177,15 +1178,12 @@ func (b *LocalBackend) linkChange(delta *netmon.ChangeDelta) { b.interfaceState = delta.CurrentState() - if delta.TimeJumped() { - nm := b.currentNode().NetMap() - if nm != nil { - expiry := nm.SelfKeyExpiry() - if !expiry.IsZero() && expiry.Before(b.clock.Now()) { - b.keyExpired = true - b.stateMachineLocked() - } - } + if delta.TimeJumped() && b.nmExpiryTimer != nil { + // The timer runs on monotonic time, which on most platforms + // does not advance while the device is asleep. Reschedule it + // against the wall clock so that a key that expired during + // sleep is handled now rather than a sleep's duration late. + b.nmExpiryTimer.Reset(max(0, b.nmExpiryAt.Sub(b.clock.Now()))) } b.pauseOrResumeControlClientLocked() @@ -1880,6 +1878,7 @@ func (b *LocalBackend) setControlClientStatusLocked(c controlclient.Client, st c nextExpiry := b.em.nextPeerExpiry(st.NetMap, now) if !nextExpiry.IsZero() { tmrDuration := nextExpiry.Sub(now) + 10*time.Second + b.nmExpiryAt = now.Add(tmrDuration) b.nmExpiryTimer = b.clock.AfterFunc(tmrDuration, func() { b.handleNetmapExpiry(c, st, currCall, tmrDuration) }) @@ -1938,7 +1937,7 @@ func (b *LocalBackend) setControlClientStatusLocked(c controlclient.Client, st c prefs.Persist = st.Persist.AsStruct() } } - if st.LoggedIn { + if st.LoggedIn && !b.keyExpired { if authWasInProgress { b.resetAuthURLLocked() // Interactive login finished successfully (URL visited). diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 87b423fa0..0871eec21 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -2514,18 +2514,22 @@ type expiryCallbackClock struct { tstime.StdClock now time.Time afterFunc func() + resets []time.Duration // durations passed to the timer's Reset } -type expiryCallbackTimer struct{} +type expiryCallbackTimer struct{ c *expiryCallbackClock } -func (*expiryCallbackTimer) Reset(time.Duration) bool { return false } -func (*expiryCallbackTimer) Stop() bool { return true } +func (t *expiryCallbackTimer) Reset(d time.Duration) bool { + t.c.resets = append(t.c.resets, d) + return false +} +func (*expiryCallbackTimer) Stop() bool { return true } func (c *expiryCallbackClock) Now() time.Time { return c.now } func (c *expiryCallbackClock) AfterFunc(_ time.Duration, f func()) tstime.TimerController { c.afterFunc = f - return new(expiryCallbackTimer) + return &expiryCallbackTimer{c} } func TestNetmapExpiryTimerPreservesPeerDeltas(t *testing.T) { diff --git a/ipn/ipnlocal/state_test.go b/ipn/ipnlocal/state_test.go index 414b43f24..fa2d5d2d6 100644 --- a/ipn/ipnlocal/state_test.go +++ b/ipn/ipnlocal/state_test.go @@ -1624,7 +1624,8 @@ func TestExpiredKeyLoggedInStatusDoesNotUnblockEngine(t *testing.T) { // A subsequent control status can still say LoggedIn even though the // node key remains expired. This must not be treated as completed - // reauthentication. + // reauthentication, including when an interactive login is pending. + cc().sendAuthURL(&expiredNetmap) cc().send(sendOpt{ loginFinished: true, nm: &expiredNetmap, @@ -1639,12 +1640,19 @@ func TestExpiredKeyLoggedInStatusDoesNotUnblockEngine(t *testing.T) { if cfg := engine.Config(); len(cfg.Addresses) != 0 { t.Errorf("stale LoggedIn status restored engine addresses: %v", cfg.Addresses) } + lb.mu.Lock() + authURL := lb.authURL + lb.mu.Unlock() + if authURL == "" { + t.Error("stale LoggedIn status reset the pending auth URL") + } } func TestTimeJumpRechecksSelfKeyExpiry(t *testing.T) { tests := []struct { name string wakeAfter time.Duration + wantReset time.Duration // new duration of the expiry timer wantState ipn.State wantBlocked bool wantConfigured bool @@ -1652,6 +1660,7 @@ func TestTimeJumpRechecksSelfKeyExpiry(t *testing.T) { { name: "before_expiry", wakeAfter: 4 * time.Minute, + wantReset: time.Minute + 10*time.Second, wantState: ipn.Running, wantBlocked: false, wantConfigured: true, @@ -1659,6 +1668,7 @@ func TestTimeJumpRechecksSelfKeyExpiry(t *testing.T) { { name: "after_expiry", wakeAfter: 7 * time.Minute, + wantReset: 0, wantState: ipn.NeedsLogin, wantBlocked: true, wantConfigured: false, @@ -1727,6 +1737,14 @@ func TestTimeJumpRechecksSelfKeyExpiry(t *testing.T) { lb.linkChange(delta) + if got, want := clock.resets, []time.Duration{tt.wantReset}; !slices.Equal(got, want) { + t.Fatalf("expiry timer resets = %v; want %v", got, want) + } + if tt.wantReset == 0 { + // The rescheduled timer fires immediately. + clock.afterFunc() + } + if got := lb.State(); got != tt.wantState { t.Errorf("state after wake = %v; want %v", got, tt.wantState) }