windows: adopt GP-managed NRPT catch-all

This commit is contained in:
Dev Scribe
2026-08-21 14:46:44 +07:00
committed by Cuong Manh Le
parent 779fe015f0
commit 4d026d836c
13 changed files with 3627 additions and 294 deletions
+7 -8
View File
@@ -19,6 +19,10 @@ import (
"github.com/Control-D-Inc/ctrld"
)
// skipInitialDNSReset is Windows-only; macOS keeps the normal adapter cleanup
// before installing its pf redirect.
func (p *prog) skipInitialDNSReset() bool { return false }
const (
// pfWatchdogInterval is how often the periodic pf watchdog checks
// that our anchor references are still present in the running ruleset.
@@ -1855,14 +1859,9 @@ func (p *prog) probePFIntercept() bool {
// Generate unique probe domain
probeID := fmt.Sprintf("_pf-probe-%x.%s", time.Now().UnixNano()&0xFFFFFFFF, pfProbeDomain)
// Register probe so DNS handler can detect and signal it
probeCh := make(chan struct{}, 1)
p.pfProbeExpected.Store(probeID)
p.pfProbeCh.Store(&probeCh)
defer func() {
p.pfProbeExpected.Store("")
p.pfProbeCh.Store((*chan struct{})(nil))
}()
// Register this attempt's own domain: overlapping probes must not cancel each other.
probeCh, deregister := p.registerInterceptProbe(probeID)
defer deregister()
// Build a minimal DNS query packet for the probe domain.
// We use exec.Command to send from a subprocess with GID=0 (wheel),
@@ -0,0 +1,319 @@
//go:build windows
package cli
import (
"runtime"
"testing"
"time"
)
// newInterceptTestProg returns a prog with a published intercept state, fake NRPT
// operations already installed, and no WFP engine (engineHandle 0).
//
// The fake is installed here, before anything can inspect registry state, and it is the
// safety boundary - not the empty wfpState. A zero-valued state has owner None, and
// shutdown's None branch sweeps orphaned ctrld rules, so an unfaked stopDNSIntercept would
// reach the production nrptCatchAllRuleExists / removeNRPTCatchAllRule / signalNRPTChange.
// On a host that has ctrld's deterministic key - a developer box, or a CI runner where
// ctrld is installed - that deletes live policy and forces a Group Policy refresh, a
// Dnscache paramchange and a cache flush. A green run on a clean runner proves nothing
// about that.
func newInterceptTestProg(t *testing.T) (*prog, *wfpState, *fakeNRPTOps) {
t.Helper()
f := fakeNRPTOpsForTest(t)
// Prove the fake is in effect before anything can inspect registry state. Asserting
// zero side effects afterwards cannot do that: an uninstalled fake reports zero
// whether it was consulted or bypassed.
requireFakeNRPTOpsInstalled(t, f)
state := &wfpState{stopCh: make(chan struct{}), listenerIP: "127.0.0.1"}
p := &prog{}
p.dnsInterceptState = state
return p, state, f
}
// assertNoNRPTSideEffects fails when a lifecycle path wrote NRPT policy or signalled the
// DNS Client. Every test in this file exercises a guard that is supposed to stand down, so
// any registry write or signal here means the guard did not hold - and, without the fake,
// would have hit the host's real policy.
func assertNoNRPTSideEffects(t *testing.T, f *fakeNRPTOps) {
t.Helper()
add, remove, signal, _ := f.counts()
if add != 0 || remove != 0 || signal != 0 {
t.Errorf("addRule = %d, removeRule = %d, signal = %d, want 0/0/0: this path must not write NRPT policy",
add, remove, signal)
}
if flush := f.flushCount(); flush != 0 {
t.Errorf("flush calls = %d, want 0: this path must not flush the resolver cache", flush)
}
}
// TestStopDNSInterceptRevokesBeforeTeardown pins the ordering the shutdown/monitor race
// depends on. Teardown deletes our WFP sublayer, and a missing sublayer is precisely what
// the health monitor treats as "our filters were wiped, rebuild everything". Were the
// state revoked only after teardown, a monitor tick inside that window would rebuild the
// intercept during shutdown.
func TestStopDNSInterceptRevokesBeforeTeardown(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
if p.interceptStateRevoked(state) {
t.Fatal("a freshly published intercept state must not read as retired")
}
if err := p.stopDNSIntercept(); err != nil {
t.Fatalf("stopDNSIntercept() = %v", err)
}
if !p.interceptStateRevoked(state) {
t.Error("state still reads live after shutdown: the monitor and heal flows would keep writing host DNS state")
}
if p.dnsInterceptState != nil {
t.Error("dnsInterceptState survived shutdown")
}
if p.dnsInterceptStopRequested.Load() {
t.Error("stop-requested flag was left set; a later start would see a phantom shutdown")
}
}
// TestRebuildDNSInterceptRefusedAfterShutdown is the regression test for the reported
// race: SCM stop runs resetDNS -> stopDNSIntercept while the health monitor is mid-tick,
// and the monitor then reaches the rebuild path before the process exits. The rebuild
// must refuse - completing it would re-add the NRPT catch-all and the WFP filters moments
// before ctrld disappears, leaving Windows resolving through a listener that is gone.
//
// That refusal is also what keeps this test safe on a real Windows host: a rebuild that
// did not refuse would run startDNSIntercept and write NRPT policy to the machine
// running the tests.
func TestRebuildDNSInterceptRefusedAfterShutdown(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
if err := p.stopDNSIntercept(); err != nil {
t.Fatalf("stopDNSIntercept() = %v", err)
}
if got := p.rebuildDNSIntercept(state, "WFP sublayer missing during health check"); got != interceptRebuildRetired {
t.Fatalf("rebuildDNSIntercept() = %v, want interceptRebuildRetired - a post-shutdown rebuild resurrects DNS interception", got)
}
if p.dnsInterceptState != nil {
t.Error("rebuild published new intercept state after shutdown")
}
}
// TestRebuildDNSInterceptRefusedForReplacedState covers the other stale-owner case: an
// earlier rebuild already replaced the state, so a goroutine still holding the old one
// must not tear down its successor.
func TestRebuildDNSInterceptRefusedForReplacedState(t *testing.T) {
p, old, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
current := &wfpState{stopCh: make(chan struct{}), listenerIP: "127.0.0.1"}
p.dnsInterceptState = current
if got := p.rebuildDNSIntercept(old, "WFP sublayer missing during health check"); got != interceptRebuildRetired {
t.Fatalf("rebuildDNSIntercept() = %v, want interceptRebuildRetired for a superseded state", got)
}
if p.dnsInterceptState != any(current) {
t.Error("a superseded state's rebuild replaced the live intercept")
}
if p.interceptStateRevoked(current) {
t.Error("the live state was revoked by a superseded rebuild")
}
}
// TestRepairMissingWFPStandsDownAfterShutdown checks the monitor's entry point. It must
// not even query WFP for a retired state - the sublayer it looks for is what teardown
// just deleted - and it must tell the monitor goroutine to exit.
func TestRepairMissingWFPStandsDownAfterShutdown(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
if err := p.stopDNSIntercept(); err != nil {
t.Fatalf("stopDNSIntercept() = %v", err)
}
// Set the handle only after teardown. A fake handle proves the revocation check
// comes first, but must never reach the real WFP calls in cleanupWFPFilters.
state.engineHandle = 1
if !p.repairMissingWFP(state) {
t.Error("repairMissingWFP() = false after shutdown; the health monitor would keep running for a dead intercept")
}
if p.dnsInterceptState != nil {
t.Error("repairMissingWFP rebuilt the intercept after shutdown")
}
}
// TestPendingStopSignalsRevocation covers how a stop avoids waiting: while it is blocked
// on the lifecycle lock it must already read as revoked, so an in-flight NRPT heal
// abandons its probe backoff instead of making the service stop wait it out. A stop that
// waits too long is killed by the Service Control Manager, which cleans up nothing.
func TestPendingStopSignalsRevocation(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
p.dnsInterceptMu.Lock()
stopped := make(chan struct{})
go func() {
defer close(stopped)
_ = p.stopDNSIntercept()
}()
// Wait for the stop to announce itself while it is blocked on the lock.
deadline := time.Now().Add(5 * time.Second)
for !p.dnsInterceptStopRequested.Load() {
if time.Now().After(deadline) {
p.dnsInterceptMu.Unlock()
<-stopped
t.Fatal("stop never announced itself before waiting for the lifecycle lock")
}
runtime.Gosched()
}
if !p.interceptStateRevoked(state) {
t.Error("a pending stop does not read as revoked; the heal flows would keep it waiting")
}
p.dnsInterceptMu.Unlock()
<-stopped
if p.dnsInterceptState != nil {
t.Error("the pending stop did not tear down the intercept once it acquired the lock")
}
}
// TestInterceptWaitAbandonsPromptlyOnPendingStop is the bound on how long a stop can be
// delayed by a recovery flow: the heal sequence's waits add up to tens of seconds, and
// each one must end as soon as a stop is pending.
func TestInterceptWaitAbandonsPromptlyOnPendingStop(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
p.dnsInterceptStopRequested.Store(true)
start := time.Now()
if p.interceptWait(state, 30*time.Second) {
t.Fatal("interceptWait() = true with a stop pending; the caller would carry on writing host DNS state")
}
if elapsed := time.Since(start); elapsed > 2*time.Second {
t.Errorf("interceptWait took %v to notice a pending stop; shutdown would inherit that delay", elapsed)
}
}
// TestInterceptWaitRunsToCompletionWhileLive guards the other direction: the cancellable
// wait must still actually wait, or the recovery flows lose their backoff.
func TestInterceptWaitRunsToCompletionWhileLive(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
start := time.Now()
if !p.interceptWait(state, 250*time.Millisecond) {
t.Fatal("interceptWait() = false for a live intercept")
}
if elapsed := time.Since(start); elapsed < 250*time.Millisecond {
t.Errorf("interceptWait returned after %v, want at least 250ms", elapsed)
}
}
// TestNRPTNeedsCtrldActivation covers the recovery gap that left a machine unfiltered
// until restart: a failed NRPT write clears ownership, and an owner-None tick used to do
// nothing at all, so nothing ever retried the write.
func TestNRPTNeedsCtrldActivation(t *testing.T) {
tests := []struct {
name string
owner nrptRuleOwner
ruleExists bool
want bool
}{
{
// The reported hole: activation failed, ownership was cleared, and no
// other path re-arms it. In hard mode WFP keeps blocking DNS meanwhile.
name: "no owner retries the failed write",
owner: nrptRuleOwnerNone,
want: true,
},
{
name: "no owner retries even if a rule is somehow present",
owner: nrptRuleOwnerNone,
ruleExists: true,
want: true,
},
{
name: "ctrld-owned rule removed externally is re-added",
owner: nrptRuleOwnerCtrld,
want: true,
},
{
name: "healthy ctrld-owned rule is left alone",
owner: nrptRuleOwnerCtrld,
ruleExists: true,
want: false,
},
{
// Writing beside external policy would be ambiguous policy, not recovery.
name: "external policy is never overwritten",
owner: nrptRuleOwnerGroupPolicy,
want: false,
},
{
name: "external policy is never overwritten even with a ctrld rule present",
owner: nrptRuleOwnerGroupPolicy,
ruleExists: true,
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := nrptNeedsCtrldActivation(tc.owner, tc.ruleExists); got != tc.want {
t.Errorf("nrptNeedsCtrldActivation(%v, %v) = %v, want %v", tc.owner, tc.ruleExists, got, tc.want)
}
})
}
}
// TestActivateCtrldNRPTFallbackRefusedAfterShutdown guards the worst leftover. A
// catch-all re-added after shutdown points every DNS query on the machine at a listener
// that no longer exists, so nothing resolves at all. Refusing early also keeps this test
// from writing NRPT policy on the machine running it.
func TestActivateCtrldNRPTFallbackRefusedAfterShutdown(t *testing.T) {
p, state, f := newInterceptTestProg(t)
defer assertNoNRPTSideEffects(t, f)
if err := p.stopDNSIntercept(); err != nil {
t.Fatalf("stopDNSIntercept() = %v", err)
}
if p.activateCtrldNRPTFallback(state, "ctrld-owned rule missing during health check") {
t.Error("activateCtrldNRPTFallback() = true after shutdown: the catch-all would outlive ctrld")
}
if owner, _ := state.nrptPolicyOwner(); owner != nrptRuleOwnerNone {
t.Errorf("NRPT owner = %v after a refused fallback, want nrptRuleOwnerNone", owner)
}
}
// TestAllowHandbackAttemptRateLimits covers the throttle on testing an external
// catch-all. Each attempt takes ctrld's rule out of the way for a probe, so a rule that
// never routes would cost a brief DNS outage on every 30s health tick without this - in
// hard mode a window where WFP blocks DNS and nothing redirects it.
func TestHandbackThrottleIsPerRule(t *testing.T) {
state := &wfpState{stopCh: make(chan struct{})}
now := time.Now()
if !state.handbackAllowed(now, "{GP-RULE}", nrptHandbackRetryInterval) {
t.Fatal("first handback attempt must be allowed")
}
// Checking alone must not spend the budget: a pre-probe can still abort the attempt
// without disturbing NRPT, and that must not cost the rule its next window.
if !state.handbackAllowed(now, "{GP-RULE}", nrptHandbackRetryInterval) {
t.Error("handbackAllowed must not consume the budget by itself")
}
state.recordHandbackAttempt(now, "{GP-RULE}", nrptHandbackRetryInterval)
if state.handbackAllowed(now.Add(nrptHandbackRetryInterval-time.Second), "{GP-RULE}", nrptHandbackRetryInterval) {
t.Error("re-testing the same rule inside the interval must be suppressed")
}
// Group Policy alternating between two names must not erase either one's memory:
// with a single slot every swap costs another removal of the live rule.
if !state.handbackAllowed(now.Add(time.Second), "{OTHER-RULE}", nrptHandbackRetryInterval) {
t.Error("a different rule name means the administrator changed policy: test it now")
}
state.recordHandbackAttempt(now.Add(time.Second), "{OTHER-RULE}", nrptHandbackRetryInterval)
if state.handbackAllowed(now.Add(2*time.Second), "{GP-RULE}", nrptHandbackRetryInterval) {
t.Error("testing another rule must not clear the first rule's throttle")
}
if !state.handbackAllowed(now.Add(2*nrptHandbackRetryInterval), "{GP-RULE}", nrptHandbackRetryInterval) {
t.Error("the same rule must be testable again after the interval")
}
}
+3
View File
@@ -18,6 +18,9 @@ func (p *prog) stopDNSIntercept() error {
return nil
}
// skipInitialDNSReset is Windows-only; other platforms keep the normal reset.
func (p *prog) skipInitialDNSReset() bool { return false }
// exemptVPNDNSServers is a no-op on unsupported platforms.
func (p *prog) exemptVPNDNSServers(exemptions []vpnDNSExemption) error {
return nil
File diff suppressed because it is too large Load Diff
+1 -7
View File
@@ -130,13 +130,7 @@ func (p *prog) serveDNS(listenerNum string) error {
// signal the prober and respond NXDOMAIN. Used by both macOS pf probes
// (_pf-probe-*) and Windows NRPT probes (_nrpt-probe-*) to verify that
// DNS interception is actually routing queries to ctrld's listener.
if probeID, ok := p.pfProbeExpected.Load().(string); ok && probeID != "" && domain == probeID {
if chPtr, ok := p.pfProbeCh.Load().(*chan struct{}); ok && chPtr != nil {
select {
case *chPtr <- struct{}{}:
default:
}
}
if p.signalInterceptProbe(domain) {
answer := new(dns.Msg)
answer.SetRcode(m, dns.RcodeNameError) // NXDOMAIN
_ = w.WriteMsg(answer)
+68
View File
@@ -0,0 +1,68 @@
package cli
// Interception probe registry.
//
// A probe sends a DNS query for a unique synthetic domain through the OS resolver and
// waits for ctrld's own handler to receive it. That is the only way to tell "the rules are
// present" from "the rules are actually redirecting packets", and both the macOS pf path
// and the Windows NRPT path use it.
//
// Each attempt registers its own domain, so overlapping probes cannot cancel each other,
// and deregistration only removes the entry it owns.
// registerInterceptProbe registers domain and returns the channel it will be signalled on
// plus the function that removes the registration.
//
//lint:ignore U1000 used on darwin (pf probes) and windows (NRPT probes)
func (p *prog) registerInterceptProbe(domain string) (<-chan struct{}, func()) {
ch := make(chan struct{}, 1)
p.interceptProbeMu.Lock()
current, _ := p.interceptProbes.Load().(map[string]chan struct{})
next := make(map[string]chan struct{}, len(current)+1)
for k, v := range current {
next[k] = v
}
next[domain] = ch
p.interceptProbes.Store(next)
p.interceptProbeMu.Unlock()
return ch, func() {
p.interceptProbeMu.Lock()
defer p.interceptProbeMu.Unlock()
current, _ := p.interceptProbes.Load().(map[string]chan struct{})
// Only drop the entry while it is still this attempt's channel. A later probe
// that reused the domain owns the slot now, and clearing it would make that one
// wait out its timeout for a query it already received.
if existing, ok := current[domain]; !ok || existing != ch {
return
}
next := make(map[string]chan struct{}, len(current))
for k, v := range current {
if k != domain {
next[k] = v
}
}
p.interceptProbes.Store(next)
}
}
// signalInterceptProbe reports whether domain is a pending probe, signalling its waiter
// when it is. Called from the DNS handler for every query, so the common case is a nil or
// empty map and no allocation.
func (p *prog) signalInterceptProbe(domain string) bool {
probes, _ := p.interceptProbes.Load().(map[string]chan struct{})
if len(probes) == 0 {
return false
}
ch, ok := probes[domain]
if !ok {
return false
}
select {
case ch <- struct{}{}:
default:
// Buffered channel already holds a signal: the waiter has what it needs.
}
return true
}
+63
View File
@@ -0,0 +1,63 @@
package cli
import (
"errors"
"net/netip"
"strings"
)
const nrptRuleName = `CtrldCatchAll`
// errGPNRPTVerified marks an intercept startup failure that happened while an externally
// managed (Group Policy) NRPT catch-all was proved - by probe, not by registry shape
// alone - to be routing DNS to this listener. It is the difference between "intercept
// failed but DNS still reaches ctrld" and "intercept failed and nothing is filtering",
// which is what decides whether the interface-DNS fallback must run.
//
// Only the Windows path produces it, but setDNS is shared, so the sentinel and its
// predicate live here with the other platform-neutral NRPT helpers.
var errGPNRPTVerified = errors.New("GP-managed NRPT verified routing to ctrld")
// errGPNRPTIneffective marks a startup that ends with externally managed NRPT owning the
// namespace while no probe has proved it routes to ctrld. DNS is not reaching ctrld, but
// adapter DNS was deliberately preserved and no ctrld rule may be written beside an
// administrator's catch-all - so this is a failed start that must not take the
// interface-DNS fallback either.
var errGPNRPTIneffective = errors.New("GP-managed NRPT owns the namespace but no probe reached ctrld")
// interceptFailedWithVerifiedExternalDNS reports whether an intercept startup failure
// happened while externally managed DNS policy was verified to be routing to ctrld.
func interceptFailedWithVerifiedExternalDNS(err error) bool {
return errors.Is(err, errGPNRPTVerified)
}
// interceptFailedUnderExternalDNSPolicy reports whether an intercept startup failure
// happened while externally managed DNS policy owned the namespace, whether or not it was
// proved to route. Either way the interface-DNS fallback must not run: adapter DNS was
// preserved on purpose, and rewriting it would violate the policy ctrld just deferred to.
// Only the verified case is a successful start.
func interceptFailedUnderExternalDNSPolicy(err error) bool {
return errors.Is(err, errGPNRPTVerified) || errors.Is(err, errGPNRPTIneffective)
}
// isExternalGPCatchAll recognizes only a single catch-all namespace that is not
// ctrld's deterministic GP key. Registry access stays in the Windows file; this
// pure classifier is shared with host-runnable tests.
func isExternalGPCatchAll(ruleName string, namespaces []string) bool {
return ruleName != "" && !strings.EqualFold(ruleName, nrptRuleName) && len(namespaces) == 1 && strings.TrimSpace(namespaces[0]) == "."
}
func isMatchingGPNRPTRule(ruleName string, namespaces []string, dnsServers, listenerIP string) bool {
if !isExternalGPCatchAll(ruleName, namespaces) {
return false
}
server, err := netip.ParseAddr(strings.TrimSpace(dnsServers))
if err != nil {
return false
}
listener, err := netip.ParseAddr(strings.TrimSpace(listenerIP))
if err != nil {
return false
}
return server.Unmap() == listener.Unmap()
}
+129
View File
@@ -0,0 +1,129 @@
package cli
import (
"errors"
"fmt"
"testing"
)
func TestIsMatchingGPNRPTRule(t *testing.T) {
tests := []struct {
name string
ruleName string
namespaces []string
servers string
listener string
want bool
}{
{
name: "exact IPv4 catch-all",
ruleName: "{A1B2C3D4}",
namespaces: []string{"."},
servers: "127.0.0.1",
listener: "127.0.0.1",
want: true,
},
{
name: "normalized IPv4-mapped listener",
ruleName: "{A1B2C3D4}",
namespaces: []string{"."},
servers: "::ffff:127.0.0.1",
listener: "127.0.0.1",
want: true,
},
{
name: "ctrld GP key is not external",
ruleName: "ctrldcatchall",
namespaces: []string{"."},
servers: "127.0.0.1",
listener: "127.0.0.1",
},
{
name: "partial namespace",
ruleName: "{A1B2C3D4}",
namespaces: []string{"corp.example"},
servers: "127.0.0.1",
listener: "127.0.0.1",
},
{
name: "multiple namespaces",
ruleName: "{A1B2C3D4}",
namespaces: []string{".", "corp.example"},
servers: "127.0.0.1",
listener: "127.0.0.1",
},
{
name: "wrong listener",
ruleName: "{A1B2C3D4}",
namespaces: []string{"."},
servers: "127.0.0.2",
listener: "127.0.0.1",
},
{
name: "multiple nameservers",
ruleName: "{A1B2C3D4}",
namespaces: []string{"."},
servers: "127.0.0.1;127.0.0.2",
listener: "127.0.0.1",
},
{
name: "malformed nameserver",
ruleName: "{A1B2C3D4}",
namespaces: []string{"."},
servers: "localhost",
listener: "127.0.0.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isMatchingGPNRPTRule(tt.ruleName, tt.namespaces, tt.servers, tt.listener); got != tt.want {
t.Fatalf("isMatchingGPNRPTRule() = %t, want %t", got, tt.want)
}
})
}
}
func TestIsExternalGPCatchAll(t *testing.T) {
tests := []struct {
name string
ruleName string
namespaces []string
want bool
}{
{name: "external catch-all", ruleName: "{GP-RULE}", namespaces: []string{"."}, want: true},
{name: "ctrld key", ruleName: nrptRuleName, namespaces: []string{"."}},
{name: "partial namespace", ruleName: "{GP-RULE}", namespaces: []string{"corp.example"}},
{name: "multiple namespaces", ruleName: "{GP-RULE}", namespaces: []string{".", "corp.example"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isExternalGPCatchAll(tt.ruleName, tt.namespaces); got != tt.want {
t.Fatalf("isExternalGPCatchAll() = %t, want %t", got, tt.want)
}
})
}
}
// TestInterceptFailedWithVerifiedExternalDNS covers the distinction the interface-DNS
// fallback turns on. "A GP rule exists" is not enough: if it is not actually routing and
// intercept failed too, skipping the fallback leaves the machine with no NRPT, no WFP and
// no adapter DNS - that is, unfiltered. Only a probe-verified route earns the skip.
func TestInterceptFailedWithVerifiedExternalDNS(t *testing.T) {
wfpErr := errors.New("FwpmEngineOpen0 failed: HRESULT 0x5")
verified := fmt.Errorf("dns intercept: WFP setup failed: %w: %w", wfpErr, errGPNRPTVerified)
if !interceptFailedWithVerifiedExternalDNS(verified) {
t.Error("a failure carrying errGPNRPTVerified must skip the interface-DNS fallback")
}
if !errors.Is(verified, wfpErr) {
t.Error("the underlying cause must stay inspectable for logs and callers")
}
if interceptFailedWithVerifiedExternalDNS(fmt.Errorf("dns intercept: WFP setup failed: %w", wfpErr)) {
t.Error("an unverified failure must take the interface-DNS fallback rather than leave the machine unfiltered")
}
if interceptFailedWithVerifiedExternalDNS(nil) {
t.Error("no error must not read as a verified external route")
}
}
+20
View File
@@ -0,0 +1,20 @@
//go:build windows
package cli
import "testing"
func TestWFPStateNRPTPolicyOwner(t *testing.T) {
state := &wfpState{}
state.setNRPTPolicyOwner(nrptRuleOwnerGroupPolicy, "{GP-RULE}")
owner, ruleName := state.nrptPolicyOwner()
if owner != nrptRuleOwnerGroupPolicy || ruleName != "{GP-RULE}" {
t.Fatalf("owner = %v, rule = %q", owner, ruleName)
}
state.setNRPTPolicyOwner(nrptRuleOwnerCtrld, "")
owner, ruleName = state.nrptPolicyOwner()
if owner != nrptRuleOwnerCtrld || ruleName != "" {
t.Fatalf("owner = %v, rule = %q", owner, ruleName)
}
}
File diff suppressed because it is too large Load Diff
+74 -11
View File
@@ -172,6 +172,22 @@ type prog struct {
// On Windows: *wfpState, on macOS: *pfState, nil on other platforms.
dnsInterceptState any
// dnsInterceptMu serializes DNS intercept lifecycle transitions - start, stop and
// the health monitor's rebuild - and guards every write to dnsInterceptState, so a
// service stop can never interleave with a monitor-driven rebuild.
dnsInterceptMu sync.Mutex //lint:ignore U1000 used on windows
// dnsInterceptStopRequested is set while a stop waits for dnsInterceptMu. The
// health and recovery flows read it as a shutdown signal and abandon their work,
// rather than making the stop wait out their probe backoffs.
dnsInterceptStopRequested atomic.Bool //lint:ignore U1000 used on windows
// nrptTransitionMu makes one NRPT ownership transition - observe, mutate, signal,
// record owner - atomic against shutdown and against another transition. It is
// deliberately finer-grained than dnsInterceptMu: it is taken for the duration of a
// single transition, never across the recovery flows' probe backoffs.
nrptTransitionMu sync.Mutex //lint:ignore U1000 used on windows
// lastTunnelIfaces tracks the tunnel set included in the last successfully loaded
// pf anchor. Pending tunnel state is kept separately so failed PF work is retried
// instead of being mistaken for an applied update. Protected by mu.
@@ -220,15 +236,22 @@ type prog struct {
// existing delayed checks provide a trailing reconciliation after churn.
pfIgnoredChangeLastReconcile atomic.Int64 //lint:ignore U1000 used on darwin
// pfProbeExpected holds the domain name of a pending pf interception probe.
// When non-empty, the DNS handler checks incoming queries against this value
// and signals pfProbeCh if matched. The probe verifies that pf's rdr rules
// are actually translating packets (not just present in rule text).
pfProbeExpected atomic.Value // string
// pfProbeCh is signaled when the DNS handler receives the expected probe query.
// The channel is created by probePFIntercept() and closed when the probe arrives.
pfProbeCh atomic.Value // *chan struct{}
// interceptProbes maps the domain of each pending interception probe to the channel
// that probe waits on. A probe verifies that interception is actually translating or
// redirecting packets, not merely present in rule text: the DNS handler looks up
// incoming queries here and signals the matching waiter.
//
// It holds one entry per in-flight probe rather than a single slot, because probes do
// overlap - the health monitor, a handback and a heal cycle can each have one out at
// the same time - and a single slot means the last registration wins and the loser
// waits out its timeout for a query that was answered. A false failure then triggers
// recovery work that was not needed.
//
// Registrations are rare and lookups happen on every query, so the map is stored as
// an immutable snapshot behind an atomic: readers never take a lock, writers copy
// under interceptProbeMu.
interceptProbes atomic.Value // map[string]chan struct{}
interceptProbeMu sync.Mutex //lint:ignore U1000 written only by registerInterceptProbe, used on darwin/windows
// VPN DNS manager for split DNS routing when intercept mode is active.
vpnDNS *vpnDNSManager
@@ -424,7 +447,12 @@ func (p *prog) postRun() {
p.runningOnDomainController = isDC
mainLog.Load().Debug().Msgf("running on domain controller: %t, role: %d", p.runningOnDomainController, roleInt)
}
p.resetDNS(false, false)
// A Windows organization can install a GP-owned NRPT catch-all before
// starting ctrld. Detect that policy before resetDNS touches adapter DNS;
// startDNSIntercept will then prove the rule functionally before adopting it.
if !p.skipInitialDNSReset() {
p.resetDNS(false, false)
}
ns := ctrld.InitializeOsResolver(false)
mainLog.Load().Debug().Msgf("initialized OS resolver with nameservers: %v", ns)
p.setDNS()
@@ -907,7 +935,7 @@ func (p *prog) setDNS() {
mainLog.Load().Fatal().Msgf("invalid --intercept-mode value %q: must be 'off', 'dns', or 'hard'", interceptMode)
}
if interceptMode == "" || interceptMode == "off" {
interceptMode = cfg.Service.InterceptMode
interceptMode = p.configuredInterceptMode()
if interceptMode != "" && interceptMode != "off" {
mainLog.Load().Info().Msgf("Intercept mode enabled via config (intercept_mode = %q)", interceptMode)
}
@@ -927,6 +955,30 @@ func (p *prog) setDNS() {
// software that also manages DNS. See issue #489.
if dnsIntercept {
if err := startDNSInterceptFn(p); err != nil {
// This check comes first: it is the one failure where DNS already works
// without ctrld touching anything else, so neither the refusal below nor the
// fallback applies.
//
// An externally managed rule was proved - by probe, not by registry shape -
// to be routing DNS to this listener. Falling through would rewrite adapter
// DNS after explicitly preserving it, and DNS still works, so stop here.
//
// Only a verified route earns this. A rule that merely exists does not: if it
// is not actually routing and intercept failed too, the machine would be left
// with no NRPT, no WFP and no adapter fallback - that is, unfiltered - so
// every other failure takes the paths below.
if interceptFailedUnderExternalDNSPolicy(err) {
if interceptFailedWithVerifiedExternalDNS(err) {
mainLog.Load().Error().Err(err).Msg("DNS intercept mode failed but externally managed DNS policy is verified routing to ctrld — not falling back to interface DNS settings")
} else {
// Owned by external policy but not proved to route: DNS is not
// reaching ctrld. Adapter DNS still stays as the organization set it,
// and setDnsOK stays false, so this start reports as failed until a
// probe succeeds.
mainLog.Load().Error().Err(err).Msg("DNS intercept mode failed and externally managed DNS policy is not routing to ctrld — leaving interface DNS settings untouched; the service is not ready")
}
return
}
// Interface DNS cannot express a port: macOS interface settings and Windows
// NRPT rules both name a resolver by IP alone. So it is only a usable
// fallback when the listener actually bound :53. When something else owns
@@ -1037,6 +1089,17 @@ func (p *prog) setDNS() {
}
}
// configuredInterceptMode resolves the service's effective intercept mode without
// mutating package state. Platform startup preflights use the same precedence as
// setDNS so they do not make adapter-DNS decisions from a different mode value.
func (p *prog) configuredInterceptMode() string {
im := interceptMode
if im == "" || im == "off" {
im = p.cfg.Service.InterceptMode
}
return im
}
func (p *prog) setDnsForRunningIface(nameservers []string) (runningIface *net.Interface) {
if p.runningIface == "" {
return
+44 -23
View File
@@ -67,28 +67,27 @@ Separating them into modes means most users get `dns` mode (safe, can never brea
#### Startup Sequence (dns mode)
1. Creates NRPT catch-all registry rule (`.``127.0.0.1`) under `HKLM\...\DnsPolicyConfig\CtrldCatchAll`
2. Triggers Group Policy refresh via `RefreshPolicyEx` (userenv.dll) so DNS Client loads NRPT immediately
3. Flushes DNS cache to clear stale entries
4. **Activates loopback WFP protect** — adds 4 permit filters (IPv4/IPv6 × UDP/TCP) for DNS to localhost with `FWPM_FILTER_FLAG_CLEAR_ACTION_RIGHT`. These prevent third-party WFP block filters from blocking the NRPT → `127.0.0.1` path (see [Loopback WFP Protect](#loopback-wfp-protect) below). Non-fatal if this fails.
5. Starts NRPT health monitor (30s periodic check)
6. Launches async NRPT probe-and-heal to verify NRPT is actually routing queries
1. Checks for a non-ctrld GP child whose only namespace is `.` and whose only nameserver is ctrld's actual listener IP.
2. When that candidate exists, preserves adapter DNS, sends a DNS Client probe before any NRPT mutation, and re-reads the same GP child. A matching before/after rule plus a received probe enters **GP-managed mode**; ctrld does not write NRPT, call `RefreshPolicyEx`/`paramchange`, or flush DNS for policy activation.
3. Without a still-matching GP candidate, creates the normal ctrld-owned catch-all, signals DNS Client, and flushes stale cache entries.
4. **Activates loopback WFP protect** — adds 4 permit filters (IPv4/IPv6 × UDP/TCP) for DNS to localhost with `FWPM_FILTER_FLAG_CLEAR_ACTION_RIGHT`. These prevent third-party WFP block filters from blocking the NRPT → listener path (see [Loopback WFP Protect](#loopback-wfp-protect) below). Non-fatal if this fails.
5. Starts the 30-second ownership-aware NRPT health monitor.
6. Re-verifies an initially ineffective GP candidate synchronously after WFP setup; ctrld-owned NRPT uses the asynchronous probe-and-heal sequence.
#### Startup Sequence (hard mode)
1. Creates NRPT catch-all rule + GP refresh + DNS flush (same as dns mode)
2. Opens WFP engine with `RPC_C_AUTHN_DEFAULT` (0xFFFFFFFF)
3. Cleans up any stale sublayer from a previous unclean shutdown
4. Creates sublayer with maximum weight (0xFFFF)
5. Adds **permit** filters (weight 10) for DNS to localhost (`127.0.0.1`/`::1` port 53)
6. Adds **permit** filters (weight 10) for DNS to RFC1918 + CGNAT subnets (10/8, 172.16/12, 192.168/16, 100.64/10)
7. Adds **block** filters (weight 1) for all other outbound DNS (port 53 UDP+TCP)
8. Starts NRPT health monitor (also verifies WFP sublayer in hard mode)
9. Launches async NRPT probe-and-heal
1. Establishes NRPT routing using the same GP-managed adoption or ctrld-owned fallback sequence as `dns` mode.
2. Opens WFP engine with `RPC_C_AUTHN_DEFAULT` (0xFFFFFFFF).
3. Cleans up any stale sublayer from a previous unclean shutdown.
4. Creates sublayer with maximum weight (0xFFFF).
5. Adds **permit** filters (weight 10) for DNS to localhost (`127.0.0.1`/`::1` port 53).
6. Adds **permit** filters (weight 10) for DNS to RFC1918 + CGNAT subnets (10/8, 172.16/12, 192.168/16, 100.64/10).
7. Adds **block** filters (weight 1) for all other outbound DNS (port 53 UDP+TCP).
8. Starts the NRPT/WFP health monitor.
**Atomic guarantee:** NRPT must succeed before WFP starts. If NRPT fails, WFP is not attempted. If WFP fails, NRPT is rolled back. This prevents DNS blackholes where WFP blocks everything but nothing routes to ctrld.
**Atomic guarantee:** NRPT routing must exist before WFP starts. If WFP setup fails, ctrld rolls back only a rule it owns. A GP-managed child is never deleted, rewritten, or replaced with interface DNS merely because ctrld's WFP setup failed.
On shutdown: stops health monitor, removes NRPT rule, flushes DNS, then (hard mode only) removes all WFP filters and closes engine.
On shutdown, ctrld stops its monitor and WFP session. It removes and signals only ctrld-owned NRPT state; a GP-managed catch-all remains untouched.
#### NRPT Details
@@ -103,7 +102,27 @@ The **Name Resolution Policy Table** is a Windows feature (originally for Direct
**Registry path**: `HKLM\SOFTWARE\Policies\Microsoft\Windows NT\DNSClient\DnsPolicyConfig\CtrldCatchAll`
**Group Policy refresh**: The DNS Client service only reads NRPT from registry during Group Policy processing cycles (default: every 90 minutes). ctrld calls `RefreshPolicyEx(bMachine=TRUE, dwOptions=RP_FORCE)` from `userenv.dll` to trigger an immediate refresh. Falls back to `gpupdate /target:computer /force` if the DLL call fails.
**Group Policy refresh**: The DNS Client service only reads NRPT from registry during Group Policy processing cycles (default: every 90 minutes). ctrld calls `RefreshPolicyEx(bMachine=TRUE, dwOptions=RP_FORCE)` when activating or repairing rules it owns. While Group Policy remains the owner, ctrld does not run NRPT activation/heal signaling; the one transition that removes a ctrld fallback is signaled after the external rule has been proven.
#### GP-managed NRPT ownership
Enterprise deployments may install a computer-scoped GP child before starting ctrld with:
- exactly one namespace: `.`;
- exactly one `GenericDNSServers` value; and
- that nameserver equal to ctrld's actual loopback listener (`127.0.0.1` or the alternate loopback selected on an AD DNS server).
At service startup ctrld reads that candidate before the normal adapter reset, probes through Windows DNS Client while its listener is already bound, and re-reads the same child. When the rule remains present and the probe arrives, ctrld records **Group Policy** as the NRPT owner. Adapter DNS stays on the organization's resolvers, and ctrld does not create, delete, refresh, or flush NRPT policy.
The health monitor keeps using functional probes:
- matching GP rule + successful probe: observe only;
- matching GP rule + failed probe: retry loopback WFP protection, then report the external policy as ineffective without running NRPT heal signals;
- matching GP rule disappears: create the normal ctrld-owned fallback and verify it, unless another GP catch-all targets a different resolver;
- GP catch-all targets another resolver: report the conflict and do not create a second ambiguous catch-all;
- matching GP rule returns: prove it with a probe, remove only ctrld's deterministic fallback keys, and return ownership to Group Policy.
Deploy the GPO **before** starting or restarting ctrld if adapter DNS must remain completely untouched. Remove or unlink the GP rule before intentionally removing the ctrld service. A GP catch-all that remains pointed at loopback while no listener is running causes DNS failure by design; ctrld cannot safely delete an administrator-owned policy during uninstall.
#### WFP Filter Architecture
@@ -145,17 +164,19 @@ See: [Issue #526](https://gitlab.int.windscribe.com/controld/clients/ctrld/-/iss
ctrld verifies NRPT is actually working by sending a probe DNS query (`_nrpt-probe-<hex>.nrpt-probe.ctrld.test`) through Go's `net.Resolver` (which calls `GetAddrInfoW` → DNS Client → NRPT path). If ctrld receives the probe on its listener, NRPT is active.
**Startup probe (async, non-blocking):** After NRPT setup, an async goroutine probes with escalating remediation: (1) immediate probe, (2) GP refresh + retry, (3) DNS Client service restart + retry, (4) final retry. Only one probe sequence runs at a time.
**Startup probes:** A matching GP candidate is probed synchronously before any NRPT mutation and re-read afterward. ctrld-owned rules keep the asynchronous activation/heal sequence: immediate probe, bounded policy signaling retries, then two-phase delete/re-add recovery. Only one probe sequence runs at a time.
**DNS Client restart (nuclear option):** If GP refresh alone isn't enough, ctrld restarts the `Dnscache` service to force full NRPT re-initialization. This briefly interrupts all DNS (~100ms) but only fires when NRPT is already not working.
**Ownership boundary:** When the active owner is Group Policy, a failed probe never enters ctrld's NRPT refresh/delete/re-add sequence. ctrld may repair its narrowly scoped loopback WFP permits, but leaves the external registry child and DNS Client policy signaling to the administrator.
#### NRPT Health Monitor
A dedicated background goroutine (`nrptHealthMonitor`) runs every 30 seconds and now performs active probing:
1. **Registry check:** If the NRPT catch-all rule is missing from the registry, restore it + GP refresh + probe-and-heal
2. **Active probe:** If the rule exists, send a probe query to verify it's actually routing — catches cases where the registry key is present but DNS Client hasn't loaded it
3. **(hard mode)** Verify WFP sublayer exists; full restart on loss
1. **Ownership check:** Distinguish a matching external GP child from ctrld's deterministic local/GP keys.
2. **Active probe:** Verify Windows DNS Client still routes to the listener.
3. **Transition:** If the external child disappears, activate ctrld's normal fallback. If it returns while the fallback is active, prove it before removing only ctrld's keys.
4. **Owned recovery:** Restore/heal only when ctrld owns the NRPT rule.
5. **(hard mode)** Verify the WFP sublayer exists and fully restart intercept state on loss.
This is periodic (not just network-event-driven) because VPN software can clear NRPT at any time. Additionally, `scheduleDelayedRechecks()` (called on network change events) performs immediate NRPT verification at 2s and 4s after changes.
+51 -55
View File
@@ -7,9 +7,7 @@ On Windows, DNS intercept mode uses a two-layer architecture:
- **`dns` mode (default)**: NRPT only — graceful DNS routing via the Windows DNS Client service
- **`hard` mode**: NRPT + WFP — full enforcement with kernel-level block filters
This dual-mode design ensures that `dns` mode can never break DNS (at worst, a VPN
overwrites NRPT and queries bypass ctrld temporarily), while `hard` mode provides
the same enforcement guarantees as macOS pf.
`dns` mode avoids ctrld's outbound block filters and therefore degrades more gracefully when owned NRPT is removed. Resolution still depends on the active NRPT target being reachable; an administrator-owned GP catch-all intentionally remains fail-closed if its loopback listener is stopped.
## Architecture: dns vs hard Mode
@@ -24,9 +22,8 @@ the same enforcement guarantees as macOS pf.
│ localhost, CLEAR_ACTION_RIGHT) prevent third-party VPN WFP │
│ blocks (e.g., OpenVPN block-outside-dns) from breaking NRPT. │
│ │
If VPN clears NRPT: health monitor re-adds within 30s
Worst case: queries go to VPN DNS until NRPT restored
│ DNS never breaks — graceful degradation │
Owned rule missing → restore; GP missing → owned fallback
GP rule + dead listener remains intentionally fail-closed
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
@@ -37,9 +34,8 @@ the same enforcement guarantees as macOS pf.
│ Bypass attempt (raw 8.8.8.8:53) → WFP BLOCK filter │
│ VPN DNS on private IP → WFP subnet PERMIT filter → allowed │
│ │
│ NRPT must be active before WFP starts (atomic guarantee)
If NRPT fails → WFP not started (avoids DNS blackhole)
│ If WFP fails → NRPT rolled back (all-or-nothing) │
│ NRPT route is established before WFP starts
WFP failure rolls back only ctrld-owned NRPT; GP is untouched
└─────────────────────────────────────────────────────────────────┘
```
@@ -79,7 +75,7 @@ unreliable. If we write to the GP path, DNS Client enters GP mode but the rules
never activate — resulting in `Get-DnsClientNrptPolicy` returning empty even though
`Get-DnsClientNrptRule` shows the rule in registry.
ctrld uses an adaptive strategy (matching [Tailscale's approach](https://github.com/tailscale/tailscale/blob/main/net/dns/nrpt_windows.go)):
ctrld uses an adaptive strategy (matching [Tailscale's approach](https://github.com/tailscale/tailscale/blob/main/net/dns/nrpt_windows.go)) when it owns NRPT:
1. **Always write to the local path** using a deterministic GUID key name
(`{B2E9A3C1-7F4D-4A8E-9D6B-5C1E0F3A2B8D}`). This is the baseline that works
@@ -91,6 +87,23 @@ ctrld uses an adaptive strategy (matching [Tailscale's approach](https://github.
the empty GP parent key. This ensures DNS Client stays in "local mode" where
the local-path rule activates immediately via `paramchange`.
### Adopting an Organization-Owned GP Catch-All
Before applying that ctrld-owned strategy, service startup looks for a non-ctrld GP child with exactly `Name=["."]` and one `GenericDNSServers` value equal to the actual listener IP. The registry match is only an ownership candidate: ctrld sends its unique DNS Client probe before any NRPT write and re-reads the same child afterward.
Intercept state stays unpublished while that happens. Startup publishes nothing until it has fully succeeded, which is why the probe and heal flows take the state as an argument instead of reading the published field — publishing early would expose a half-built `wfpState`, with no engine handle and filter IDs still being assigned, to callers such as the VPN DNS exemption path. The one deliberate exception is hard mode when WFP setup fails while GP-managed NRPT is verified routing: that state is published so the health monitor can keep retrying WFP, and the service start is still reported as failed.
When the rule remains unchanged and the probe arrives, Group Policy owns NRPT:
- the startup adapter reset is skipped;
- no ctrld NRPT key is created;
- `RefreshPolicyEx`, Dnscache `paramchange`, and cache flush are not used for that policy;
- shutdown/uninstall leave the GP child untouched.
A matching GP child that remains present but fails its probe is reported as ineffective and is not rewritten. If the child disappears, ctrld activates its normal owned fallback. A GP catch-all that instead changes to another resolver is reported as a conflict; ctrld does not create a second ambiguous catch-all. If the matching rule later returns, ctrld probes first, removes only its deterministic fallback keys, signals the ownership transition once, and resumes observing Group Policy.
**Deployment ordering:** apply the GPO before starting ctrld to guarantee adapter DNS is never reset. Remove/unlink it before intentionally stopping or uninstalling ctrld. A GP catch-all still targeting loopback with no listener running is a deliberate fail-closed state and will break DNS.
### Reproducing the Empty GP Parent Case
This is a production code reference, so the temporary repro script is not kept in
@@ -209,28 +222,28 @@ by the VPN's own WFP rules.
**Startup (hard mode):**
```
1. Add NRPT catch-all rule + GP refresh + DNS flush
1. Adopt a proven matching GP catch-all, or install ctrld-owned NRPT
2. FwpmEngineOpen0() with RPC_C_AUTHN_DEFAULT (0xFFFFFFFF)
3. Delete stale sublayer (crash recovery)
4. FwpmSubLayerAdd0() — weight 0xFFFF
5. Add 4 localhost permit filters
6. Add 4 block filters
7. Add RFC1918 + CGNAT subnet permits
8. Start NRPT health monitor goroutine
8. Start ownership-aware NRPT/WFP health monitor
```
**Startup (dns mode):**
```
1. Add NRPT catch-all rule + GP refresh + DNS flush
1. Adopt a proven matching GP catch-all, or install ctrld-owned NRPT
2. Activate loopback WFP protect (4 hard-permit filters for localhost DNS)
3. Start NRPT health monitor goroutine
3. Start ownership-aware NRPT health monitor
```
**Shutdown:**
```
1. Stop NRPT health monitor
2. Remove NRPT catch-all rule + DNS flush
3. (hard mode only) Clean up all WFP filters, sublayer, close engine
2. Remove + signal only ctrld-owned NRPT; leave GP-managed policy untouched
3. Clean up ctrld WFP filters, sublayer, and engine session
```
**Crash Recovery:**
@@ -259,42 +272,24 @@ Windows DNS Client path to verify NRPT is actually working:
4. ctrld's DNS handler recognizes the probe prefix and signals success
5. If the probe times out (2s), NRPT isn't loaded yet → retry with remediation
### Startup Probe (Async)
### Startup Probes
After NRPT setup, an async goroutine runs the probe-and-heal sequence without
blocking startup:
For a matching GP candidate, startup blocks for one 2-second probe before any NRPT mutation and then re-reads the same child. A received query plus the unchanged rule proves both routing and external ownership. If that first probe fails, ctrld installs its normal WFP protection and performs one more ownership-safe probe before advertising startup readiness.
ctrld-owned NRPT retains the asynchronous sequence:
```
Probe attempt 1 (2s timeout)
Immediate probe
├─ Success → "NRPT verified working", done
└─ Timeout → GP refresh + DNS flush, sleep 1s
Probe attempt 2 (2s timeout)
├─ Success → done
└─ Timeout → Restart DNS Client service (nuclear), sleep 2s
Re-add NRPT + GP refresh + DNS flush
Probe attempt 3 (2s timeout)
├─ Success → done
└─ Timeout → GP refresh + DNS flush, sleep 4s
Probe attempt 4 (2s timeout)
├─ Success → done
└─ Timeout → log error, continue
└─ Timeout
├─ Empty GP parent → clean once, signal once, re-probe
└─ Otherwise → bounded 1s/2s/4s signal + probe retries
└─ Still failing → two-phase remove/signal/re-add/final probe
```
### DNS Client Restart (Nuclear Option)
### GP-Managed Probe Failure
If GP refresh alone isn't enough, ctrld restarts the Windows DNS Client service
(`Dnscache`). This forces the DNS Client to fully re-initialize, including
re-reading all NRPT rules from the registry. This is the equivalent of macOS
`forceReloadPFMainRuleset()`.
**Trade-offs:**
- Briefly interrupts ALL DNS resolution (few hundred ms during restart)
- Clears the system DNS cache (all apps need to re-resolve)
- VPN NRPT rules survive (they're in registry, re-read on restart)
- Enterprise security tools may log the service restart event
This only fires as attempt #3 after two GP refresh attempts fail — at that point
DNS isn't working through ctrld anyway, so a brief DNS blip is acceptable.
A matching GP child still owns Windows' effective NRPT store even when its probe fails. Writing a local rule cannot override that precedence, and rewriting the GP child would violate administrator ownership. ctrld therefore retries only its loopback WFP permit protection, reports the ineffective external policy, and leaves NRPT registry values and policy signals untouched.
### Health Monitor Integration
@@ -302,19 +297,20 @@ The 30s periodic health monitor now does actual probing, not just registry check
```
Every 30s:
├─ Registry check: nrptCatchAllRuleExists()?
│ ├─ Missing → re-add + GP refresh + flush + probe-and-heal
Present → probe to verify it's actually routing
├─ Probe success → OK
│ └─ Probe failure → probe-and-heal cycle
├─ GP-managed owner
│ ├─ Matching child + probe success → observe only
Matching child + probe failure → WFP-only retry; no NRPT mutation
└─ Matching child gone → activate ctrld-owned fallback + verify
(hard mode only) Check: wfpSublayerExists()?
├─ Missing → full restart (stopDNSIntercept + startDNSIntercept)
Present → OK
ctrld-owned owner
├─ Working matching GP child returns → remove only ctrld keys; adopt GP
ctrld key missing → restore + signal + verify
│ └─ ctrld key present → probe; run owned heal sequence on failure
└─ (hard mode) Check WFP sublayer; full intercept restart if missing
```
**Singleton guard:** Only one probe-and-heal sequence runs at a time (atomic bool).
The startup probe and health monitor cannot overlap.
**Singleton guard:** Only one asynchronous probe-and-heal sequence runs at a time (atomic bool). Startup's GP-candidate probe completes before the health monitor starts; direct periodic probes finish before they schedule a heal sequence.
**Why periodic, not just network-event?** VPN software or Group Policy updates can
clear NRPT at any time, not just during network changes. A 30s periodic check ensures