From a74afe71f30f42ab2110c3405345bb8275eac7cc Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 24 Nov 2026 22:01:21 +0100 Subject: [PATCH] systemd-resolved: an unknown path in its directory is left and journalled once (review r1 C-7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The checks/owned check refuses a directory without unknown (ADR 0422 §8). /var/lib/node-resolver stays, its value "module" (lead answer L-1), not "program": no other program writes there. The guard looks for a path it does not know every round and, finding one, leaves it exactly as it is and says so to the journal once, never every round. Builds: design 08 sign-in-page §6, C-7 --- modules/systemd-resolved/README.md | 6 ++-- .../cmd/resolver-tools/guard.go | 31 +++++++++++++++++++ .../cmd/resolver-tools/guard_test.go | 27 ++++++++++++++++ .../cmd/resolver-tools/manifest_test.go | 11 +++++++ modules/systemd-resolved/module.json | 2 +- 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/modules/systemd-resolved/README.md b/modules/systemd-resolved/README.md index 3952968ed..09d510275 100644 --- a/modules/systemd-resolved/README.md +++ b/modules/systemd-resolved/README.md @@ -79,6 +79,8 @@ One long-running process, as root: `resolver-tools guard`. restart never infers which link to take them off; a refusal by resolved is journalled once per change, never every round. `uplink-names` only sets how resolved answers those names over a link — never the link itself, which the network manager chooses. +- **A path in its directory this module does not know** — `/var/lib/node-resolver` declares `unknown: + module` — is left exactly as it is, and said to the journal once, never every round (ADR 0422 §8). ## Its verbs @@ -86,10 +88,10 @@ On the mesh, through the node's runtime as the operator account (writes escalate | verb | does | |---|---| -| `/node-resolver.routes` (r) | the mesh's resolvers, each link given servers of its own with its routed domains and whether it is a default route, and the resolver file's outside writes. Also `uplink_names {names, link, refused}` (the applied list's names resolved holds on that link now, never the file's) and `mesh_resolvers {silent, since}` (ADR 0688's fallback). The machine's own socket answers the same | +| `/node-resolver.routes` (r) | the generated contract's `RoutesAnswer`: the mesh's resolvers, each link given servers of its own with its routed domains and whether it is a default route, and the resolver file's outside writes. Also `uplink_names {names, link, refused}` (the applied list's names resolved holds on that link now, never the file's) and `mesh_resolvers {silent, since}` (ADR 0688's fallback). The machine's own socket answers the same, without `outside_writes` (its own `displaced` verb gives the write standing now) | | `/node-resolver.route` (a) | `{link, domains, servers}`: these domains, and every name under them, to these servers over this link, and only them. The mesh's own domain and the root are refused | | `/node-resolver.unroute` (a) | `{link}`: that link's domains go to the mesh's resolvers again | -| `/node-resolver.uplink-names` (a) | `{names}`: at most 8 host names (letters, digits, hyphens and dots, lower-cased); an empty list takes them away. Writes `/var/lib/node-resolver/uplink-names` whole, through `sudo -n install -m 0644 -o root -g root /dev/stdin uplink-names.new` then `sudo -n mv -f uplink-names.new uplink-names`. Answers `{names, link, set_at}`: the names now given, the link they are answered over now (empty when no link carries a default route), and when. Refused, naming the name and why: not a host name (a wildcard, a trailing dot, a label over 63 bytes), the root, the mesh's own domain or under it, or more than 8 names | +| `/node-resolver.uplink-names` (a) | `{names}`, an array of at most 8 host names (letters, digits, hyphens and dots, lower-cased), as the generated contract's `UplinkNamesInput` says; an empty array takes them away. Writes `/var/lib/node-resolver/uplink-names` whole, through `sudo -n install -m 0644 -o root -g root /dev/stdin uplink-names.new` then `sudo -n mv -f uplink-names.new uplink-names`, within 15 s. Answers the contract's `UplinkNamesAnswer` `{names, link, set_at}`: the names now given, the link they are answered over now (empty when no link carries a default route), and when. Refused, naming the name and why: not a host name (a wildcard, a trailing dot, a label over 63 bytes), the root, the mesh's own domain or under it, or more than 8 names | **On the machine, to its own root processes**, over `/run/node-resolver/verbs.sock` (mode 0600). The path is the seat's, so a caller does not need to know which holder answers. The protocol is one JSON line diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard.go b/modules/systemd-resolved/cmd/resolver-tools/guard.go index 1511b19cf..4f54d14fa 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard.go @@ -107,6 +107,10 @@ type Guard struct { // read at start from UplinkNamesAppliedFile, so a restart takes the names off the link it names, // never inferring which. Only the uplink names' round reads and writes it. applied UplinkApplied + + // unknownSaid is every path in the uplink-names directory this module does not know, already said to + // the journal (design 08 sign-in-page C-7): said once, never every round. + unknownSaid map[string]bool } // NewGuard is the machine's. @@ -349,6 +353,7 @@ func (g *Guard) Run(ctx context.Context) { case <-routes.C: g.KeepRoutes(ctx) g.KeepUplinkNames(ctx) + g.checkUnknownPaths() } } } @@ -557,6 +562,32 @@ func (g *Guard) writeApplied() { } } +// checkUnknownPaths looks at the uplink-names directory for a path this module does not know (design 08 +// sign-in-page C-7; ADR 0422 §8, lead answer L-1): it leaves it exactly as it is, and says so to the +// journal once, never every round. +func (g *Guard) checkUnknownPaths() { + dir := filepath.Dir(g.Resolver.UplinkNamesFile) + entries, err := os.ReadDir(dir) + if err != nil { + return + } + known := map[string]bool{ + filepath.Base(g.Resolver.UplinkNamesFile): true, + filepath.Base(g.Resolver.UplinkNamesAppliedFile): true, + } + for _, e := range entries { + name := e.Name() + if known[name] || strings.HasSuffix(name, ".tmp") || g.unknownSaid[name] { + continue + } + if g.unknownSaid == nil { + g.unknownSaid = map[string]bool{} + } + g.unknownSaid[name] = true + g.Log("%s in %s is not this module's; left as it is", name, dir) + } +} + // Request is one call over the socket: a verb and its arguments, one JSON line. type Request struct { Verb string `json:"verb"` diff --git a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go index fb9723ff4..f1b1a7070 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/guard_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/guard_test.go @@ -337,6 +337,33 @@ func TestUplinkNamesMoveWithTheDefaultRoute(t *testing.T) { } } +// A path in the uplink-names directory this module does not know is left exactly as it is, and said to +// the journal once, never every round (design 08 sign-in-page C-7; ADR 0422 §8, lead answer L-1). +func TestAnUnknownPathIsLeftAndJournalledOnce(t *testing.T) { + g, _, _, r := aGuardedMachineWithLinks(t, "wlan0") + dir := filepath.Dir(r.UplinkNamesFile) + stray := filepath.Join(dir, "a-program-left-this") + if err := os.WriteFile(stray, []byte("not this module's"), 0o644); err != nil { + t.Fatal(err) + } + var said []string + g.Log = func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) } + g.checkUnknownPaths() + g.checkUnknownPaths() + count := 0 + for _, s := range said { + if strings.Contains(s, "a-program-left-this") { + count++ + } + } + if count != 1 { + t.Errorf("said %d times, want once: %v", count, said) + } + if got := contentOf(t, stray); got != "not this module's" { + t.Errorf("the unknown path was changed: %q", got) + } +} + // A changed list replaces the old one on the link, the route itself staying put (design 08 sign-in-page §9). func TestAChangedListReplacesTheOldOneOnTheLink(t *testing.T) { g, _, f, r := aGuardedMachineWithLinks(t, "wlan0") diff --git a/modules/systemd-resolved/cmd/resolver-tools/manifest_test.go b/modules/systemd-resolved/cmd/resolver-tools/manifest_test.go index 7d04d7847..193e3a51c 100644 --- a/modules/systemd-resolved/cmd/resolver-tools/manifest_test.go +++ b/modules/systemd-resolved/cmd/resolver-tools/manifest_test.go @@ -58,6 +58,17 @@ func TestTheManifestSaysWhatTheGuardKeeps(t *testing.T) { if m.Facts["private-interface"].Path != PrivateInterfacePath || m.Facts["private-interface"].Template != "mesh0\n" { t.Errorf("the private interface fact is %+v", m.Facts["private-interface"]) } + // design 08 sign-in-page C-7; ADR 0422 §8, lead answer L-1: a directory needs `unknown`, and this + // one's is "module" (the guard leaves a path it does not know as it is), never "program". + uplinkDir := false + for _, r := range m.Resources { + if r["type"] == "directory" && r["path"] == "/var/lib/node-resolver" { + uplinkDir = r["unknown"] == "module" + } + } + if !uplinkDir { + t.Error("/var/lib/node-resolver's directory resource does not say unknown: module") + } if m.Facts["resolvers"].Template != m.Facts["kept"].Template { t.Error("the resolver file and the copy the guard puts back are not one template") } diff --git a/modules/systemd-resolved/module.json b/modules/systemd-resolved/module.json index e569346a5..4f0a7c73a 100644 --- a/modules/systemd-resolved/module.json +++ b/modules/systemd-resolved/module.json @@ -113,7 +113,7 @@ "type": "directory", "path": "/var/lib/node-resolver", "mode": "0755", - "unknown": "program" + "unknown": "module" } ], "build": {