fixup! ipn/ipnlocal: guard on expired key when connected to exit node

Instead of rechecking the self node's key expiry in linkChange,
reschedule the existing netmap expiry timer against the wall clock
when netmon reports a time jump. The timer runs on monotonic time,
which may not advance during sleep, so it would otherwise fire late.
This also covers peers that expired while the device was asleep.

Also don't treat a LoggedIn status as a finished interactive login
while the key remains expired.

Updates tailscale/corp#47690

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl committed 2026-11-20 09:29:34 -08:00
1 parent 90be4b7667
commit c176f7534b
3 files changed
+36 -15

No files matched your search

+9 -10
View File
@@ -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).
+8 -4
View File
@@ -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) {
+19 -1
View File
@@ -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)
}