From bd6dcdcd0c232fcaa3ebc41c752c8002935d5b53 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 24 Nov 2026 22:01:01 +0100 Subject: [PATCH] systemd-resolved: judge "in place" from what resolved holds now (review r1 C-4, note c) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C-4: the guard reads the link's domains fresh with rawDomainsOf before skipping a round, never trusting its own applied record alone, so a network manager that rewrites the link's domains, or a restarted resolved, is found and the names are set again. note (c): fallback.json's since is the time of the guard's first round, never the zero time; it moved only on a real turn before, so the first round's write carried a zero since. Builds: design 08 sign-in-page §7, §8, C-4 --- .../cmd/resolver-tools/guard.go | 47 ++++++++++++---- .../cmd/resolver-tools/guard_test.go | 54 +++++++++++++++++-- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard.go b/modules/systemd-resolved/cmd/resolver-tools/guard.go index 3761e12e2..1511b19cf 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -364,9 +364,9 @@ func (g *Guard) KeepRoutes(ctx context.Context) { } if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) { if turn := !g.silent; turn || !g.fallbackWritten { - if turn { - g.silentSince = g.Now() - } + // since is this round's time whether it is a real turn or the guard's first round + // establishing where things stand (design 08 sign-in-page §8, note (c)): never the zero time. + g.silentSince = g.Now() g.silent = true g.writeFallback() if turn { @@ -379,9 +379,7 @@ func (g *Guard) KeepRoutes(ctx context.Context) { return } if turn := g.silent; turn || !g.fallbackWritten { - if turn { - g.silentSince = g.Now() - } + g.silentSince = g.Now() g.silent = false g.writeFallback() if turn { @@ -472,6 +470,32 @@ func (g *Guard) setLinkUplinkNames(ctx context.Context, link string, names []str return err } +// uplinkNamesHeld is whether resolved holds every name on the link now, read fresh with rawDomainsOf +// (design 08 sign-in-page §7, C-4): never the guard's own applied record alone, so a network manager that +// rewrites the link's domains, or a restarted resolved, is found and the names are set again. +func (g *Guard) uplinkNamesHeld(ctx context.Context, link string, names []string) bool { + if len(names) == 0 { + return true + } + if link == "" { + return false + } + raw, err := g.rawDomainsOf(ctx, link) + if err != nil { + return false + } + has := map[string]bool{} + for _, d := range raw { + has[d] = true + } + for _, n := range names { + if !has["~"+n] { + return false + } + } + return true +} + // KeepUplinkNames gives the names in the file to the link carrying the default route, every round (design // 08 sign-in-page §7, §8): it never sets a link's servers, its default route for names, its addresses or // its routes. A round in which they are already in place changes nothing. @@ -486,9 +510,11 @@ func (g *Guard) KeepUplinkNames(ctx context.Context) { g.Log("the kernel's default routes: %v", err) return } - if link == g.applied.Link && sameNames(given, g.applied.Names) && g.applied.Refused == "" { + held := g.uplinkNamesHeld(ctx, link, given) + if link == g.applied.Link && g.applied.Refused == "" && held { return } + missing := link == g.applied.Link && sameNames(given, g.applied.Names) && !held if g.applied.Link != "" && g.applied.Link != link { if err := g.setLinkUplinkNames(ctx, g.applied.Link, nil); err != nil { g.Log("taking the uplink's names off %s failed: %v", g.applied.Link, err) @@ -500,9 +526,12 @@ func (g *Guard) KeepUplinkNames(ctx context.Context) { refused = err.Error() } } - if link != g.applied.Link { + switch { + case link != g.applied.Link: g.Log("the uplink's names are now answered over %q, were %q", link, g.applied.Link) - } else if !sameNames(given, g.applied.Names) { + case missing: + g.Log("the uplink's names were missing from %s, as resolved holds it now; set again", link) + case !sameNames(given, g.applied.Names): g.Log("the uplink's names change on %s", link) } if refused != g.applied.Refused { diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go index 7bd693572..fb9723ff4 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -365,6 +365,53 @@ func TestAChangedListReplacesTheOldOneOnTheLink(t *testing.T) { } } +// A network manager that rewrites the link's domains (a DHCP renewal, say) loses the uplink's names; the +// guard judges "in place" from what resolved holds now, never its own record alone, and sets them again +// (design 08 sign-in-page §7; C-4). +func TestANetworkManagerRewriteOfTheLinksDomainsIsSetAgain(t *testing.T) { + g, _, f, r := aGuardedMachineWithLinks(t, "wlan0") + f.dns = "Link 2 (wlan0): 192.168.1.1\n" + f.domain = "Link 2 (wlan0): lan\n" + f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n" + if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil { + t.Fatal(err) + } + g.KeepUplinkNames(context.Background()) + if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" { + t.Fatalf("the first round gave %v", f.changed) + } + // The network manager rewrites the link's domains: the uplink's name is gone, the applied record is not. + f.domain = "Link 2 (wlan0): lan\n" + f.changed = nil + g.KeepUplinkNames(context.Background()) + if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" { + t.Errorf("a rewrite that lost the uplink's name was not set again: %v", f.changed) + } +} + +// resolved itself restarts, forgetting every link's domains; the guard's next round, judging from what +// resolved holds now, sets the uplink's names again (design 08 sign-in-page §7; C-4). +func TestAResolvedRestartLosingTheLinksDomainsIsSetAgain(t *testing.T) { + g, _, f, r := aGuardedMachineWithLinks(t, "wlan0") + f.dns = "Link 2 (wlan0): 192.168.1.1\n" + f.domain = "Link 2 (wlan0): lan\n" + f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n" + if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil { + t.Fatal(err) + } + g.KeepUplinkNames(context.Background()) + if len(f.changed) != 1 { + t.Fatalf("the first round gave %v", f.changed) + } + // resolved restarts: every link's domains are gone, the guard's applied record is not. + f.domain = "Link 2 (wlan0):\n" + f.changed = nil + g.KeepUplinkNames(context.Background()) + if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 ~check.example" { + t.Errorf("a restarted resolved was not given the uplink's names again: %v", f.changed) + } +} + // A guard restarted with an applied file takes the uplink's names off the link it names, never inferring // which (design 08 sign-in-page §8, §9; R-6). func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) { @@ -446,11 +493,12 @@ func TestTheGuardWritesTheFallbackFileAtEachTurnAndItsFirstRound(t *testing.T) { g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers } f.dns = "Global: 10.42.0.1\n" f.domain = "Global: ~.\n" - // The first round, the mesh answering, writes the file although nothing changed. + // The first round, the mesh answering, writes the file although nothing changed: since is the first + // round's time, never the zero time (design 08 sign-in-page note (c)). g.KeepRoutes(context.Background()) first := ReadFallback(g.Resolver.FallbackFile) - if first.Silent { - t.Fatalf("the first round says %+v, want not silent", first) + if first.Silent || !first.Since.Equal(*now) { + t.Fatalf("the first round says %+v, want not silent since %v", first, *now) } // A second round with nothing changed does not move `since`. *now = now.Add(time.Second)