host: the claim checks read root's nudge drop-in through instancectl's trusted reader, which takes escaped unit names (#328)
rootstate.OpenTrustedFile refuses a `\` in any path component, and systemd escapes a dash in a unit name as \x2d, so readClaimNudgeDropin refused the nudge drop-in of every unit of a host with a dash: a claim start or migration of such a unit failed once root had nudged it. instancectl.TrustedDropinReader (root's egress start check's reader, now exported and shared) walks from / with O_NOFOLLOW at every step and requires the control directory, the unit's directory and the file to be root-owned and not group- or world-writable, the file with one link; it takes any single component but / and NUL. readClaimNudgeDropin uses it (ReadPath, under /run/systemd/system.control only). Tests: the reader with an escaped unit name and every trust refusal; the claim hook on an escaped name (failed with the rootstate reader); the claim-launch egress witness now runs on a host with a dash.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+270
−22
internal/host/claimunit_effective.go+14 −10internal/host/claimunit_effective_test.go+65 −0internal/host/plan14_installed_launch_root_linux_test.go+12 −1internal/instancectl/egress.go+1 −1internal/instancectl/egress_dropin.go+48 −9internal/instancectl/egress_dropin_linux.go+7 −1internal/instancectl/egress_dropin_linux_test.go+123 −0
diff --git a/internal/host/claimunit_effective.go b/internal/host/claimunit_effective.go| index 7cb2c5a0d..e94021f8f 100644 |
| --- a/internal/host/claimunit_effective.go |
| +++ b/internal/host/claimunit_effective.go |
| @@ -219,22 +219,26 @@ func claimIPSet(raw string) ([]string, error) { |
| // slice after a change to the slice's allow (run 20261025T173953-8a3366), |
| // and systemd keeps it under /run/systemd/system.control until reboot. |
| func claimNudgeDropinPath(unit string) string { |
| - return filepath.Join("/run/systemd/system.control", unit+".d", "50-IPAddressAllow.conf") |
| + return filepath.Join(instancectl.RuntimeControlDir, unit+".d", "50-IPAddressAllow.conf") |
| } |
| // readClaimNudgeDropin reads a nudge drop-in through |
| -// rootstate.OpenTrustedFile (root-owned, not group- or world-writable, |
| -// one link). A variable only so a test can answer for a path it cannot |
| -// make trusted. |
| +// instancectl.TrustedDropinReader, root's egress start check's reader: no |
| +// symlink at any step, root-owned, not group- or world-writable, one link. |
| +// Not rootstate.OpenTrustedFile, whose component rule refuses the `\` in |
| +// a unit name systemd escaped (carlos-instance@shop\x2da.example.com.service: |
| +// every host with a dash), so the claim checks refused every such unit |
| +// once root had nudged it (Task 15 review). A variable only so a test can |
| +// answer for a path it cannot make trusted. |
| var readClaimNudgeDropin = func(path string) ([]byte, error) { |
| - f, err := rootstate.OpenTrustedFile(path) |
| - if err != nil { |
| - return nil, err |
| - } |
| - defer f.Close() |
| - return io.ReadAll(io.LimitReader(f, rootstate.MaxRecord+1)) |
| + return claimNudgeReader.ReadPath(path) |
| } |
| +// claimNudgeReader is readClaimNudgeDropin's reader: systemd's runtime |
| +// control directory, root's files. A variable only so a test can point it |
| +// at another root-owned directory. |
| +var claimNudgeReader = instancectl.TrustedDropinReader{} |
| + |
| // claimNudgeDropinBody is true when a nudge drop-in sets nothing but the |
| // empty IPAddressAllow= reset in the [Service] section: comments and |
| // blank lines aside, exactly those two lines. An empty reset of the |
diff --git a/internal/host/claimunit_effective_test.go b/internal/host/claimunit_effective_test.go| index b3de2ed16..e0d55d92a 100644 |
| --- a/internal/host/claimunit_effective_test.go |
| +++ b/internal/host/claimunit_effective_test.go |
| @@ -2,7 +2,10 @@ package host |
| import ( |
| "context" |
| + "errors" |
| + "io/fs" |
| "os" |
| + "path/filepath" |
| "slices" |
| "strings" |
| "testing" |
| @@ -181,3 +184,65 @@ func TestClaimPredecessorAcceptsOnlyTheEgressNudgeDropin(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// TestClaimNudgeDropinHookTakesEscapedNames: the production hook, with |
| +// its root-owner check, pointed at a root-owned directory every box has |
| +// (/usr), reaches a dashed unit's drop-in directory and finds it absent; |
| +// rootstate's trusted open refused the escaped name before looking. |
| +// |
| +// Not parallel: it swaps claimNudgeReader. |
| +func TestClaimNudgeDropinHookTakesEscapedNames(t *testing.T) { |
| + old := claimNudgeReader |
| + t.Cleanup(func() { claimNudgeReader = old }) |
| + claimNudgeReader = instancectl.TrustedDropinReader{ControlDir: "/usr"} |
| + for _, host := range []string{"shop.example", "shop-a.example"} { |
| + unit := instancectl.UnitName(instancectl.RoleRun, host) |
| + path := filepath.Join("/usr", unit+".d", "50-IPAddressAllow.conf") |
| + if _, err := readClaimNudgeDropin(path); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatalf("readClaimNudgeDropin(%s) = %v, want not found", path, err) |
| + } |
| + } |
| +} |
| + |
| +// TestClaimNudgeDropinReadsAnEscapedUnit (Task 15 review): the claim |
| +// checks read root's nudge drop-in through instancectl's trusted reader, |
| +// which takes a unit name as systemd escapes it. rootstate's trusted open |
| +// refused the `\` in every unit of a host with a dash, so a claim start |
| +// of such a unit failed once root had nudged it. The trust checks |
| +// themselves are instancectl's TestTrustedDropinReaderEscapedNames. |
| +func TestClaimNudgeDropinReadsAnEscapedUnit(t *testing.T) { |
| + t.Parallel() |
| + unit := instancectl.UnitName(instancectl.RoleRun, "shop-a.example.com") |
| + if !strings.Contains(unit, `\x2d`) { |
| + t.Fatalf("fixture: %s is not an escaped unit name", unit) |
| + } |
| + path := claimNudgeDropinPath(unit) |
| + if want := filepath.Join(instancectl.RuntimeControlDir, unit+".d", "50-IPAddressAllow.conf"); path != want { |
| + t.Fatalf("claimNudgeDropinPath = %s, want %s", path, want) |
| + } |
| + |
| + // The reader readClaimNudgeDropin uses, over a control directory the |
| + // test owns, reads the nudge systemd writes for the escaped unit. |
| + root, err := filepath.EvalSymlinks(t.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dir := filepath.Join(root, "system.control") |
| + if err := os.MkdirAll(filepath.Join(dir, unit+".d"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + nudge := "# This is a drop-in unit file extension, created via \"systemctl set-property\"\n# or an equivalent operation. Do not edit.\n[Service]\nIPAddressAllow=\n" |
| + local := filepath.Join(dir, unit+".d", "50-IPAddressAllow.conf") |
| + if err := os.WriteFile(local, []byte(nudge), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + body, err := instancectl.TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid())}.ReadPath(local) |
| + if err != nil || !claimNudgeDropinBody(body) { |
| + t.Fatalf("the nudge for %s: %q, %v; want it read and accepted", unit, body, err) |
| + } |
| + |
| + // The production hook reads only under systemd's control directory. |
| + if _, err := readClaimNudgeDropin(local); !errors.Is(err, os.ErrPermission) { |
| + t.Fatalf("readClaimNudgeDropin(%s) = %v, want a refusal outside %s", local, err, instancectl.RuntimeControlDir) |
| + } |
| +} |
diff --git a/internal/host/plan14_installed_launch_root_linux_test.go b/internal/host/plan14_installed_launch_root_linux_test.go| index 4396a1f3a..d089cd19d 100644 |
| --- a/internal/host/plan14_installed_launch_root_linux_test.go |
| +++ b/internal/host/plan14_installed_launch_root_linux_test.go |
| @@ -222,7 +222,18 @@ func plan14InstalledClaimLaunch(t *testing.T, boundUnit string, runtimeUpdate .. |
| if err := os.WriteFile(instancectl.SystemBinary, body, 0755); err != nil { |
| t.Fatal(err) |
| } |
| - f, _, rec := generationLaunchFixture(t) |
| + // The egress witness's host has a dash, so every unit and slice name |
| + // carries systemd's \x2d escape: the claim checks must read root's |
| + // nudge drop-in under such a name (Task 15 review; rootstate's trusted |
| + // open refused it). |
| + launchHost := "shop.example" // generationLaunchFixture's |
| + if egress { |
| + launchHost = "shop-a.example" |
| + } |
| + f, _, rec := generationLaunchFixtureHost(t, launchHost) |
| + if egress && !strings.Contains(instancectl.UnitName(instancectl.RoleRun, f.target.Host), `\x2d`) { |
| + t.Fatalf("fixture: %s does not escape to a \\x2d unit name", f.target.Host) |
| + } |
| t.Setenv("CARLOS_DATA_DIR", "/data") |
| t.Setenv("CARLOS_OPT_DIR", "/opt/carlos") |
| host := f.target.Host |
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go| index f716f2379..b381b5e28 100644 |
| --- a/internal/instancectl/egress.go |
| +++ b/internal/instancectl/egress.go |
| @@ -127,7 +127,7 @@ type EgressAsserter struct { |
| // only on such a slice, or one owing a change (mayHold, AssertStart). |
| heldSome map[string]bool |
| // controlDir is systemd's runtime control directory ("" is |
| - // runtimeControlDir) and dropinOwner the owner its drop-ins must have |
| + // RuntimeControlDir) and dropinOwner the owner its drop-ins must have |
| // (root); tests point both at a tree of their own. |
| controlDir string |
| dropinOwner uint32 |
diff --git a/internal/instancectl/egress_dropin.go b/internal/instancectl/egress_dropin.go| index 7539edaed..ee1cccd32 100644 |
| --- a/internal/instancectl/egress_dropin.go |
| +++ b/internal/instancectl/egress_dropin.go |
| @@ -4,23 +4,66 @@ import ( |
| "errors" |
| "fmt" |
| "io/fs" |
| + "os" |
| + "path/filepath" |
| "strings" |
| ) |
| // The on-disk evidence of a slice's allow (Task 15 review, ruling on |
| // finding 1): `systemctl set-property --runtime <slice> IPAddressAllow=...` |
| -// makes systemd write <runtimeControlDir>/<slice>.d/50-IPAddressAllow.conf |
| +// makes systemd write <RuntimeControlDir>/<slice>.d/50-IPAddressAllow.conf |
| // (the name Task 14 saw systemd give the nudge's drop-in on a real box), |
| // and it stays there until reboot, whatever happens to the control |
| // service. A start path whose read of the slice fails, with nothing |
| // wanted, reads it to learn whether the slice may still hold an allow a |
| // control service before this one wrote (AssertStart). |
| const ( |
| - runtimeControlDir = "/run/systemd/system.control" |
| - allowDropinName = "50-IPAddressAllow.conf" |
| - maxAllowDropin = 64 << 10 |
| + allowDropinName = "50-IPAddressAllow.conf" |
| + maxAllowDropin = 64 << 10 |
| ) |
| +// RuntimeControlDir is where systemd keeps the drop-ins `systemctl |
| +// set-property --runtime` writes, one directory per unit or slice, |
| +// <name>.d, until reboot. |
| +const RuntimeControlDir = "/run/systemd/system.control" |
| + |
| +// TrustedDropinReader reads a drop-in under a systemd control directory |
| +// as a trusted root file: no component of its path is followed through a |
| +// symlink (O_NOFOLLOW at each step from /), the control directory and the |
| +// unit's or slice's directory are directories, and the drop-in a regular |
| +// file with one link, each owned by Owner and not group- or |
| +// world-writable. Unlike rootstate's trusted open, it takes a directory |
| +// name as systemd escapes it (carlos-instance@shop\x2da.example.com.service.d). |
| +// A missing component matches fs.ErrNotExist. Root's egress start check |
| +// and the host's claim checks share it (Task 15 review). |
| +type TrustedDropinReader struct { |
| + ControlDir string // "" is RuntimeControlDir |
| + Owner uint32 // root, 0, in production; a test's own uid in tests |
| +} |
| + |
| +// Read reads <ControlDir>/<sub>/<name>; sub and name are one component |
| +// each. |
| +func (r TrustedDropinReader) Read(sub, name string) ([]byte, error) { |
| + dir := r.ControlDir |
| + if dir == "" { |
| + dir = RuntimeControlDir |
| + } |
| + return readTrustedDropin(dir, sub, name, r.Owner) |
| +} |
| + |
| +// ReadPath is Read for a path that must be exactly |
| +// <ControlDir>/<sub>/<name>, clean and absolute. |
| +func (r TrustedDropinReader) ReadPath(path string) ([]byte, error) { |
| + dir := r.ControlDir |
| + if dir == "" { |
| + dir = RuntimeControlDir |
| + } |
| + if !filepath.IsAbs(path) || filepath.Clean(path) != path || filepath.Dir(filepath.Dir(path)) != dir { |
| + return nil, os.ErrPermission |
| + } |
| + return r.Read(filepath.Base(filepath.Dir(path)), filepath.Base(path)) |
| +} |
| + |
| // sliceAllowOnDisk reports whether slice's runtime IPAddressAllow drop-in |
| // sets a non-empty allow. No drop-in (no file, or no directory) is false. |
| // A drop-in that cannot be read as a trusted root file (a symlink, another |
| @@ -28,11 +71,7 @@ const ( |
| // file), or whose content is not a [Slice] section of IPAddressAllow= |
| // lines, is an error: the caller refuses on it. |
| func (e *EgressAsserter) sliceAllowOnDisk(slice string) (bool, error) { |
| - dir := e.controlDir |
| - if dir == "" { |
| - dir = runtimeControlDir |
| - } |
| - body, err := readTrustedDropin(dir, slice+".d", allowDropinName, e.dropinOwner) |
| + body, err := TrustedDropinReader{ControlDir: e.controlDir, Owner: e.dropinOwner}.Read(slice+".d", allowDropinName) |
| if errors.Is(err, fs.ErrNotExist) { |
| return false, nil |
| } |
diff --git a/internal/instancectl/egress_dropin_linux.go b/internal/instancectl/egress_dropin_linux.go| index 4a97be3db..d89f3be21 100644 |
| --- a/internal/instancectl/egress_dropin_linux.go |
| +++ b/internal/instancectl/egress_dropin_linux.go |
| @@ -20,7 +20,7 @@ import ( |
| // world-writable. A missing component is an error that matches |
| // fs.ErrNotExist. |
| func readTrustedDropin(dir, sub, name string, owner uint32) ([]byte, error) { |
| - if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir || dir == "/" || strings.Contains(sub, "/") || strings.Contains(name, "/") { |
| + if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir || dir == "/" || !dropinComponent(sub) || !dropinComponent(name) { |
| return nil, os.ErrPermission |
| } |
| fd, err := unix.Open("/", unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| @@ -63,6 +63,12 @@ func readTrustedDropin(dir, sub, name string, owner uint32) ([]byte, error) { |
| return b, nil |
| } |
| +// dropinComponent is one path component: not empty, not . or .., no / |
| +// and no NUL. |
| +func dropinComponent(s string) bool { |
| + return s != "" && s != "." && s != ".." && !strings.ContainsAny(s, "/\x00") |
| +} |
| + |
| // trustedFD: fd is of kind, owned by owner, not group- or world-writable, |
| // and, for a file, has one link. |
| func trustedFD(fd int, owner uint32, kind uint32) error { |
diff --git a/internal/instancectl/egress_dropin_linux_test.go b/internal/instancectl/egress_dropin_linux_test.go| new file mode 100644 |
| index 000000000..bef28a2c2 |
| --- /dev/null |
| +++ b/internal/instancectl/egress_dropin_linux_test.go |
| @@ -0,0 +1,123 @@ |
| +//go:build linux |
| + |
| +package instancectl |
| + |
| +import ( |
| + "errors" |
| + "io/fs" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// TestTrustedDropinReaderEscapedNames: the shared reader (root's egress |
| +// start check, the host's claim checks) reads a drop-in whose directory |
| +// is named as systemd escapes a unit (a host with a dash: \x2d), and |
| +// every trust check still applies to it. |
| +func TestTrustedDropinReaderEscapedNames(t *testing.T) { |
| + t.Parallel() |
| + unit := UnitName(RoleRun, "shop-a.example.com") |
| + if !strings.Contains(unit, `\x2d`) { |
| + t.Fatalf("fixture: %s is not an escaped unit name", unit) |
| + } |
| + const body = "[Service]\nIPAddressAllow=\n" |
| + setup := func(t *testing.T) (TrustedDropinReader, string) { |
| + t.Helper() |
| + root, err := filepath.EvalSymlinks(t.TempDir()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dir := filepath.Join(root, "system.control") |
| + if err := os.MkdirAll(filepath.Join(dir, unit+".d"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + path := filepath.Join(dir, unit+".d", allowDropinName) |
| + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + return TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid())}, path |
| + } |
| + |
| + t.Run("read", func(t *testing.T) { |
| + r, path := setup(t) |
| + for _, read := range []func() ([]byte, error){ |
| + func() ([]byte, error) { return r.Read(unit+".d", allowDropinName) }, |
| + func() ([]byte, error) { return r.ReadPath(path) }, |
| + } { |
| + if got, err := read(); err != nil || string(got) != body { |
| + t.Fatalf("read = %q, %v; want %q", got, err, body) |
| + } |
| + } |
| + }) |
| + t.Run("absent", func(t *testing.T) { |
| + r, path := setup(t) |
| + if err := os.Remove(path); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := r.ReadPath(path); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatalf("ReadPath of a missing drop-in = %v, want fs.ErrNotExist", err) |
| + } |
| + }) |
| + for _, tc := range []struct { |
| + name string |
| + breakIt func(t *testing.T, r *TrustedDropinReader, path string) string |
| + }{ |
| + {"outside the control directory", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + return filepath.Join(filepath.Dir(r.ControlDir), unit+".d", allowDropinName) |
| + }}, |
| + {"not clean", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + return r.ControlDir + "/x/../" + unit + ".d/" + allowDropinName |
| + }}, |
| + {"a symlinked drop-in", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + target := filepath.Join(r.ControlDir, "elsewhere.conf") |
| + if err := os.Rename(path, target); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, path); err != nil { |
| + t.Fatal(err) |
| + } |
| + return path |
| + }}, |
| + {"a symlinked unit directory", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + other := filepath.Join(r.ControlDir, "other.d") |
| + if err := os.Rename(filepath.Dir(path), other); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(other, filepath.Dir(path)); err != nil { |
| + t.Fatal(err) |
| + } |
| + return path |
| + }}, |
| + {"a group-writable drop-in", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + if err := os.Chmod(path, 0o664); err != nil { |
| + t.Fatal(err) |
| + } |
| + return path |
| + }}, |
| + {"a world-writable unit directory", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + if err := os.Chmod(filepath.Dir(path), 0o777); err != nil { |
| + t.Fatal(err) |
| + } |
| + return path |
| + }}, |
| + {"a drop-in with two links", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + if err := os.Link(path, filepath.Join(r.ControlDir, "linked.conf")); err != nil { |
| + t.Fatal(err) |
| + } |
| + return path |
| + }}, |
| + {"another owner", func(t *testing.T, r *TrustedDropinReader, path string) string { |
| + r.Owner++ // root's drop-ins must be root's |
| + return path |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + r, path := setup(t) |
| + p := tc.breakIt(t, &r, path) |
| + if got, err := r.ReadPath(p); err == nil || errors.Is(err, fs.ErrNotExist) { |
| + t.Fatalf("ReadPath(%s) = %q, %v; want a refusal", p, got, err) |
| + } |
| + }) |
| + } |
| +} |