diff --git a/wgengine/router/osrouter/router_freebsd.go b/wgengine/router/osrouter/router_freebsd.go index e06ce5b2c..2115332da 100644 --- a/wgengine/router/osrouter/router_freebsd.go +++ b/wgengine/router/osrouter/router_freebsd.go @@ -7,6 +7,8 @@ import ( "fmt" "net" "os/exec" + "regexp" + "slices" "strings" "github.com/tailscale/wireguard-go/tun" @@ -237,11 +239,45 @@ func ensurePFAnchorRef() error { return nil // already present } + // "pfctl -sn"/"-sr" print only a table's name, never the addresses it + // holds, so a ruleset reconstructed from them re-declares every table + // as empty. Reloading it would silently drop the contents of any table + // the operator populates out-of-band (a "persist file" table, "pfctl -T + // add", etc.), breaking whatever rules reference it. There is no way to + // insert an anchor reference into a running ruleset without a reload, so + // refuse rather than clobber, and tell the operator how to fix it + // permanently. + if t := pfTablesReferenced(natRules + filterRules); len(t) > 0 { + return fmt.Errorf("refusing to reload main PF ruleset: it references table(s) %s "+ + "whose contents cannot be preserved across a reload; add %q and %q to "+ + "/etc/pf.conf instead", + strings.Join(t, ", "), natAnchorRef, anchorRef) + } + // Prepend our anchor references so they're evaluated, then include // all existing rules so we don't disrupt the user's configuration. return loadPFMainRuleset(additions + natRules + filterRules) } +// pfTableRx matches a table reference in "pfctl -sn"/"-sr" output, e.g. the +// "" in "pass in on em0 from to any". +var pfTableRx = regexp.MustCompile(`<([a-zA-Z0-9_.:-]+)>`) + +// pfTablesReferenced returns the sorted, deduplicated names of PF tables +// referenced by the given ruleset text. +func pfTablesReferenced(rules string) []string { + seen := map[string]bool{} + var names []string + for _, m := range pfTableRx.FindAllStringSubmatch(rules, -1) { + if !seen[m[1]] { + seen[m[1]] = true + names = append(names, m[1]) + } + } + slices.Sort(names) + return names +} + // removePFAnchorRef removes the nat-anchor and anchor references for // "tailscale" from the main PF ruleset via read-modify-write, leaving // all other rules intact. diff --git a/wgengine/router/osrouter/router_freebsd_test.go b/wgengine/router/osrouter/router_freebsd_test.go new file mode 100644 index 000000000..054632feb --- /dev/null +++ b/wgengine/router/osrouter/router_freebsd_test.go @@ -0,0 +1,67 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package osrouter + +import ( + "slices" + "testing" +) + +func TestPFTablesReferenced(t *testing.T) { + tests := []struct { + name string + rules string + want []string + }{ + {"empty", "", nil}, + { + // What a bare FreeBSD box looks like: only our anchor refs. + "anchors only", + "nat-anchor \"tailscale\"\nanchor \"tailscale\"\n", + nil, + }, + { + "no tables", + "scrub in all fragment reassemble\npass in all flags S/SA keep state\n", + nil, + }, + { + // pfctl emits automatic tables like this for interface groups; a + // reload without table preservation would empty them too. + "automatic table", + "block drop in log inet from <__automatic_591ee83a_0> to any\n", + []string{"__automatic_591ee83a_0"}, + }, + { + "named table", + "pass out log on em0 proto tcp from to any port = ssh\n", + []string{"zabbix_proxies"}, + }, + { + "deduplicated and sorted", + "pass in log from to any\n" + + "pass in log from to any\n" + + "pass out log from any to \n", + []string{"admins", "trusted"}, + }, + { + "names with punctuation", + "pass in log from to any\npass in log from to any\n", + []string{"fw-cluster.v4", "mon_hosts"}, + }, + { + // A bare "<" with no closing ">" is not a table reference. + "unclosed angle bracket", + "pass in all # < not a table\n", + nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := pfTablesReferenced(tt.rules); !slices.Equal(got, tt.want) { + t.Errorf("pfTablesReferenced() = %v, want %v", got, tt.want) + } + }) + } +}