systemd-resolved: judge "in place" from what resolved holds now (review r1 C-4, note c)

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
This commit is contained in:
jochen
2026-11-24 22:01:01 +01:00
parent 717f8ec2e5
commit bd6dcdcd0c
2 changed files with 89 additions and 12 deletions
@@ -364,9 +364,9 @@ func (g *Guard) KeepRoutes(ctx context.Context) {
} }
if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) { if !g.Resolver.TheMeshAnswers(ctx, routes.Mesh.Servers) {
if turn := !g.silent; turn || !g.fallbackWritten { if turn := !g.silent; turn || !g.fallbackWritten {
if turn { // since is this round's time whether it is a real turn or the guard's first round
g.silentSince = g.Now() // establishing where things stand (design 08 sign-in-page §8, note (c)): never the zero time.
} g.silentSince = g.Now()
g.silent = true g.silent = true
g.writeFallback() g.writeFallback()
if turn { if turn {
@@ -379,9 +379,7 @@ func (g *Guard) KeepRoutes(ctx context.Context) {
return return
} }
if turn := g.silent; turn || !g.fallbackWritten { if turn := g.silent; turn || !g.fallbackWritten {
if turn { g.silentSince = g.Now()
g.silentSince = g.Now()
}
g.silent = false g.silent = false
g.writeFallback() g.writeFallback()
if turn { if turn {
@@ -472,6 +470,32 @@ func (g *Guard) setLinkUplinkNames(ctx context.Context, link string, names []str
return err 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 // 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 // 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. // 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) g.Log("the kernel's default routes: %v", err)
return 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 return
} }
missing := link == g.applied.Link && sameNames(given, g.applied.Names) && !held
if g.applied.Link != "" && g.applied.Link != link { if g.applied.Link != "" && g.applied.Link != link {
if err := g.setLinkUplinkNames(ctx, g.applied.Link, nil); err != nil { 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) 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() 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) 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) g.Log("the uplink's names change on %s", link)
} }
if refused != g.applied.Refused { if refused != g.applied.Refused {
@@ -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 // 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). // which (design 08 sign-in-page §8, §9; R-6).
func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) { func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) {
@@ -446,11 +493,12 @@ func TestTheGuardWritesTheFallbackFileAtEachTurnAndItsFirstRound(t *testing.T) {
g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers } g.Resolver.Answers = func(context.Context, string) bool { return meshAnswers }
f.dns = "Global: 10.42.0.1\n" f.dns = "Global: 10.42.0.1\n"
f.domain = "Global: ~.\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()) g.KeepRoutes(context.Background())
first := ReadFallback(g.Resolver.FallbackFile) first := ReadFallback(g.Resolver.FallbackFile)
if first.Silent { if first.Silent || !first.Since.Equal(*now) {
t.Fatalf("the first round says %+v, want not silent", first) t.Fatalf("the first round says %+v, want not silent since %v", first, *now)
} }
// A second round with nothing changed does not move `since`. // A second round with nothing changed does not move `since`.
*now = now.Add(time.Second) *now = now.Add(time.Second)