From 262640ff4ea7fd48277cf33668b9fbc9fb0d96e5 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Thu, 19 Nov 2026 22:26:29 -0800 Subject: [PATCH] util/syspolicy/source: notify sessions when a forced read finds a change A Reader is shared by all effective policies that include its source. A forced read through one of them updated the Reader's cached snapshot and marked it up to date, but didn't notify the other sessions, so their policies kept the old settings. Since a89b32904, "tailscale syspolicy reload" reloads the caller's user policy rather than the device policy used by LocalBackend. On Windows, a registry change made without a Group Policy refresh was therefore reported as configured by the CLI but never applied by the backend. Updates tailscale/corp#51820 Signed-off-by: Nick Khyl --- util/syspolicy/rsop/resultant_policy_test.go | 55 ++++++++++++++++++++ util/syspolicy/source/policy_reader.go | 15 +++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/util/syspolicy/rsop/resultant_policy_test.go b/util/syspolicy/rsop/resultant_policy_test.go index 60132eae7..3f83afa72 100644 --- a/util/syspolicy/rsop/resultant_policy_test.go +++ b/util/syspolicy/rsop/resultant_policy_test.go @@ -10,6 +10,7 @@ import ( "strconv" "sync" "testing" + "testing/synctest" "time" "github.com/google/go-cmp/cmp" @@ -701,6 +702,60 @@ drain: } } +// unobservableStore is a [source.Store] that doesn't report policy changes, +// like the Windows Registry when edited without a Group Policy refresh. +type unobservableStore struct{ source.Store } + +func TestForcedReloadUpdatesOtherPolicies(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + settingA := setting.NewDefinition("TestSettingA", setting.DeviceSetting, setting.StringValue) + if err := setting.SetDefinitionsForTest(t, settingA); err != nil { + t.Fatalf("SetDefinitionsForTest failed: %v", err) + } + + store := source.NewTestStoreOf[string](t) + if _, err := RegisterStoreForTest(t, "TestSource", setting.DeviceScope, unobservableStore{store}); err != nil { + t.Fatalf("Failed to register policy store: %v", err) + } + + // Both the device and the user policies read from the same device store. + devicePolicy, err := policyForTest(t, setting.DeviceScope) + if err != nil { + t.Fatalf("Failed to get device policy: %v", err) + } + userPolicy, err := policyForTest(t, setting.UserScopeOf("test-user")) + if err != nil { + t.Fatalf("Failed to get user policy: %v", err) + } + + policyChanged := make(chan policyclient.PolicyChange, 1) + t.Cleanup(devicePolicy.RegisterChangeCallback(func(pc policyclient.PolicyChange) { policyChanged <- pc })) + + // Forcibly reloading the user policy must also update the device policy. + store.SetStrings(source.TestSettingOf(settingA.Key(), "A")) + if _, err := userPolicy.Reload(); err != nil { + t.Fatalf("Reload failed: %v", err) + } + if got, want := userPolicy.Get().Get(settingA.Key()), "A"; got != want { + t.Errorf("User policy: setting %q: got %q; want %q", settingA.Key(), got, want) + } + + time.Sleep(policyReloadMaxDelay) + synctest.Wait() + select { + case pc := <-policyChanged: + if !pc.HasChanged(settingA.Key()) { + t.Errorf("Policy setting %q has not changed", settingA.Key()) + } + default: + t.Fatal("Device policy change callback was not invoked") + } + if got, want := devicePolicy.Get().Get(settingA.Key()), "A"; got != want { + t.Errorf("Device policy: setting %q: got %q; want %q", settingA.Key(), got, want) + } + }) +} + func TestClosePolicySource(t *testing.T) { testSetting := setting.NewDefinition("TestSetting", setting.DeviceSetting, setting.StringValue) if err := setting.SetDefinitionsForTest(t, testSetting); err != nil { diff --git a/util/syspolicy/source/policy_reader.go b/util/syspolicy/source/policy_reader.go index ece08e3cb..064fc75d9 100644 --- a/util/syspolicy/source/policy_reader.go +++ b/util/syspolicy/source/policy_reader.go @@ -130,7 +130,8 @@ func (r *Reader) reload(force bool) (*setting.Snapshot, error) { // Return the last known policy instead of reading from the store. return r.lastPolicy, nil } - if r.upToDate && !force { + wasUpToDate := r.upToDate + if wasUpToDate && !force { return r.lastPolicy, nil } @@ -179,6 +180,12 @@ func (r *Reader) reload(force bool) (*setting.Snapshot, error) { newPolicy := setting.NewSnapshot(m, setting.SummaryWith(r.origin)) if r.lastPolicy == nil || !newPolicy.EqualItems(r.lastPolicy) { r.lastPolicy = newPolicy + if wasUpToDate { + // A forced read found a change the store never reported. + // The sessions other than the caller's still hold the old + // policy and won't re-read it unless notified. + r.notifySessionsLocked() + } } return r.lastPolicy, nil } @@ -241,6 +248,12 @@ func (r *Reader) onPolicyChange() { r.mu.Lock() defer r.mu.Unlock() r.upToDate = false + r.notifySessionsLocked() +} + +// notifySessionsLocked notifies the active [ReadingSession]s of a policy change. +// r.mu must be held. +func (r *Reader) notifySessionsLocked() { for _, s := range r.sessions { select { case s.policyChangedCh <- struct{}{}: