Files
Martin Minkus 4e80553a9d wgengine/router/osrouter: only remove PF anchor refs this process added
removePFAnchorRef ran at shutdown (and from the startup cleanup hook) and
unconditionally rewrote the main PF ruleset to strip the "tailscale"
anchor references. That is wrong twice over for an operator who
configured those references statically in /etc/pf.conf -- the durable
setup the ensurePFAnchorRef error message recommends:

  - it drifts the running ruleset away from /etc/pf.conf on every
    tailscaled shutdown, and
  - the rewrite reconstructs the ruleset from "pfctl -s" output, which
    names PF tables but never their contents, so any table the operator
    populates out-of-band is silently emptied (same failure mode
    ensurePFAnchorRef now refuses).

Track whether this process inserted the references and only remove what
we added, and even then leave them alone if the ruleset now references
tables. Skipped removal is harmless: callers flush the anchor's contents
first, and a reference to an empty anchor has no effect on traffic. The
flag is process-local by design, so the cross-process startup cleanup
hook never removes references it cannot prove are tailscaled's.

The only-remove-what-we-added semantics were first identified and
implemented by Ross Williams (@overhacked) on a fork of this branch;
this is an independent implementation of the same idea alongside the
table guard.

Updates #5573

Change-Id: Idf579a8b4190c37211782feac4e5bfd057c3ace0
Signed-off-by: Martin Minkus <[email protected]>
2026-08-31 20:31:01 -07:00
..