named units: an untrusted guard chain is unguarded, not refused
On a box whose /opt/carlos is not root-owned (dennis, the v0.40.0 dev rehearsal), the console guard read failed the trust walk before it could see guard/ was absent, so every install was refused with "console guard marker: permission denied". Paul's ruling (2026-11-19): a box whose directory chain to the marker is not root-trusted can hold no guard root honours; proofnamed reads the same chain with the same rules. consoleGuardHost now walks the directory first: os.ErrPermission there is ok=false with a reason, and the fetch step's record, its journal line and the install verb say it. A trusted chain with an untrusted, oversized or hostless marker still refuses. proofnamed.go is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+253
−77
internal/host/namedunitsfetch.go+18 −6internal/host/namedunitsfetch_test.go+1 −1internal/host/unitops.go+75 −31internal/host/unitops_guard_linux_test.go+150 −30internal/host/unitops_test.go+9 −9
diff --git a/internal/host/namedunitsfetch.go b/internal/host/namedunitsfetch.go| index b5b233e0b..057fda251 100644 |
| --- a/internal/host/namedunitsfetch.go |
| +++ b/internal/host/namedunitsfetch.go |
| @@ -69,8 +69,9 @@ type namedUnitsFetch struct { |
| // inUse is declarationBindingInUse, bound to the box's stores. |
| inUse func(host string) (bool, error) |
| // guardHost is consoleGuardHost(release.OptDir()); an error refuses |
| - // the value (invalid). |
| - guardHost func() (host string, ok bool, err error) |
| + // the value (invalid). unguarded, when set, is the record's Reason for |
| + // a value installed or unchanged on a box that can hold no guard. |
| + guardHost func() (host string, ok bool, unguarded string, err error) |
| now func() time.Time |
| } |
| @@ -134,10 +135,12 @@ func (f namedUnitsFetch) read(ctx context.Context, region, iid string) ([]byte, |
| // flock: the box's guard rule (its read under the same lock as the write), |
| // the current file, the hold, and the write. |
| func (f namedUnitsFetch) installLocked(st status.NamedUnitsStatus, raw []byte, entries []namedunits.Entry) status.NamedUnitsStatus { |
| - guarded, armed, err := f.guardHost() |
| + guarded, armed, unguarded, err := f.guardHost() |
| if err != nil { |
| return f.settle(st, namedUnitsInvalid, fmt.Sprintf("console guard marker: %v", err)) |
| } |
| + // Held, failed and invalid outcomes set their own Reason over this. |
| + st.Reason = unguarded |
| if err := checkGuardRule(entries, guarded, armed); err != nil { |
| return f.settle(st, namedUnitsInvalid, err.Error()) |
| } |
| @@ -238,7 +241,7 @@ func productionNamedUnitsFetch() namedUnitsFetch { |
| inUse: func(host string) (bool, error) { |
| return productionDeclarationBindingInUse(host, time.Now()) |
| }, |
| - guardHost: func() (string, bool, error) { return consoleGuardHost(release.OptDir()) }, |
| + guardHost: func() (string, bool, string, error) { return consoleGuardHost(release.OptDir()) }, |
| now: time.Now, |
| } |
| } |
| @@ -352,11 +355,11 @@ func namedUnitsLogLine(st status.NamedUnitsStatus) string { |
| } |
| return fmt.Sprintf("named-units: unreadable: %s; the installed declarations stand", st.Reason) |
| case namedUnitsInstalled: |
| - return fmt.Sprintf("named-units: installed %s version %d from %s (sha256 %s)", appconfig.DefaultNamedUnitsPath, st.Version, st.Parameter, st.Digest) |
| + return fmt.Sprintf("named-units: installed %s version %d from %s (sha256 %s)", appconfig.DefaultNamedUnitsPath, st.Version, st.Parameter, st.Digest) + reasonSuffix(st.Reason) |
| case namedUnitsNotFound: |
| return fmt.Sprintf("named-units: no parameter %s; the installed declarations stand", st.Parameter) |
| case namedUnitsUnchanged: |
| - return fmt.Sprintf("named-units: %s version %d unchanged", st.Parameter, st.Version) |
| + return fmt.Sprintf("named-units: %s version %d unchanged", st.Parameter, st.Version) + reasonSuffix(st.Reason) |
| default: |
| if st.Parameter != "" { |
| return fmt.Sprintf("named-units: %s version %d %s: %s", st.Parameter, st.Version, st.Outcome, st.Reason) |
| @@ -364,3 +367,12 @@ func namedUnitsLogLine(st status.NamedUnitsStatus) string { |
| return fmt.Sprintf("named-units: %s: %s", st.Outcome, st.Reason) |
| } |
| } |
| + |
| +// reasonSuffix is an installed or unchanged line's note (the unguarded |
| +// reason), "" when there is none. |
| +func reasonSuffix(reason string) string { |
| + if reason == "" { |
| + return "" |
| + } |
| + return "; " + reason |
| +} |
diff --git a/internal/host/namedunitsfetch_test.go b/internal/host/namedunitsfetch_test.go| index 4dbcac681..9317e1227 100644 |
| --- a/internal/host/namedunitsfetch_test.go |
| +++ b/internal/host/namedunitsfetch_test.go |
| @@ -96,7 +96,7 @@ func (x *fetchFixture) fetch() namedUnitsFetch { |
| x.asked = append(x.asked, host) |
| return x.inUse[host], x.inUseErr |
| }, |
| - guardHost: func() (string, bool, error) { return x.guard, x.guard != "", x.guardErr }, |
| + guardHost: func() (string, bool, string, error) { return x.guard, x.guard != "", "", x.guardErr }, |
| now: func() time.Time { return fetchClock }, |
| } |
| } |
diff --git a/internal/host/unitops.go b/internal/host/unitops.go| index bdd1c96ce..19c68f955 100644 |
| --- a/internal/host/unitops.go |
| +++ b/internal/host/unitops.go |
| @@ -52,20 +52,23 @@ func productionUnitOps(envDir string) *appconfig.UnitOps { |
| // (installNamedUnitsLocked). A pass and an install therefore never |
| // interleave: the pass's snapshot is the file for the whole pass. envDir |
| // is where the lock lives (appconfig.DefaultEnvDir in production); dst is |
| -// the declaration path. |
| -func InstallNamedUnits(envDir, dst string, raw []byte) error { |
| +// the declaration path. unguarded is consoleGuardHost's: non-empty when |
| +// the box's guard could not apply and the install went ahead, so the |
| +// caller can say why. |
| +func InstallNamedUnits(envDir, dst string, raw []byte) (unguarded string, err error) { |
| if err := validateForInstall(raw); err != nil { |
| - return err |
| + return "", err |
| } |
| unlock, err := lockConfigSync(envDir + "/.sync") |
| if err != nil { |
| - return fmt.Errorf("config-sync lock: %w", err) |
| + return "", fmt.Errorf("config-sync lock: %w", err) |
| } |
| defer unlock() |
| - if err := checkInstallGuard(release.OptDir(), raw); err != nil { |
| - return err |
| + unguarded, err = checkInstallGuard(release.OptDir(), raw) |
| + if err != nil { |
| + return "", err |
| } |
| - return installNamedUnitsLocked(dst, raw) |
| + return unguarded, installNamedUnitsLocked(dst, raw) |
| } |
| // validateForInstall: both readers of the declaration file must accept |
| @@ -87,16 +90,17 @@ func ValidateNamedUnitsDeclaration(raw []byte) error { return validateForInstall |
| // checkInstallGuard is the box's console guard rule for an install of raw |
| // (already validated). The guarded host comes from the marker itself |
| // (consoleGuardHost), so it is found whether or not any file names it. |
| -func checkInstallGuard(optDir string, raw []byte) error { |
| +// unguarded is consoleGuardHost's reason when the box can hold no guard. |
| +func checkInstallGuard(optDir string, raw []byte) (unguarded string, err error) { |
| entries, err := namedunits.Decode(raw) |
| if err != nil { |
| - return err |
| + return "", err |
| } |
| - host, guarded, err := consoleGuardHost(optDir) |
| + host, guarded, unguarded, err := consoleGuardHost(optDir) |
| if err != nil { |
| - return fmt.Errorf("console guard marker: %w", err) |
| + return "", fmt.Errorf("console guard marker: %w", err) |
| } |
| - return checkGuardRule(entries, host, guarded) |
| + return unguarded, checkGuardRule(entries, host, guarded) |
| } |
| // installNamedUnitsLocked writes raw to dst atomically: a temp file in the |
| @@ -151,42 +155,77 @@ func guardNames(marker []byte, optDir, host string) bool { |
| return string(marker) == release.LiveLink(optDir, host) |
| } |
| -// openConsoleGuardMarker is the trusted opener consoleGuardHost reads the |
| -// marker with; a variable so tests can anchor the trust checks at a |
| -// directory they own. |
| -var openConsoleGuardMarker = rootstate.OpenTrustedFile |
| +// openConsoleGuardDir is the trusted walk consoleGuardHost opens the |
| +// marker's directory with (rootstate.OpenTrustedDir: every directory from |
| +// / root-owned, not group- or other-writable, r-x for both); a variable so |
| +// tests can anchor the walk at a directory they own. The marker itself is |
| +// opened in it with rootstate.OpenTrustedFileIn. |
| +var openConsoleGuardDir = rootstate.OpenTrustedDir |
| + |
| +// consoleGuardUntrustedChain is consoleGuardHost's unguarded reason for a |
| +// box whose directory chain to the marker fails the trust walk. |
| +const consoleGuardUntrustedChain = "console guard not readable here (an ancestor directory is not root-owned), treated as unguarded" |
| // consoleGuardHost is for the install callers only; proofnamed.go keeps its |
| // own read, its error handling and its binding evidence. A trusted read of |
| -// the marker: absent is ok=false with no error; any other failure (a |
| -// symlink, a wrong owner, a group-writable file or directory, a marker |
| -// over rootstate.MaxRecord) is err, and the caller refuses the install. |
| +// the marker in two steps, so an untrusted directory chain is told apart |
| +// from an untrusted marker: |
| +// |
| +// - the directory walk to <opt>/system/guard: absent anywhere is |
| +// ok=false with no error; refused by the trust checks (os.ErrPermission: |
| +// a directory not root-owned, group- or other-writable, or not r-x for |
| +// group and others) is ok=false with no error and unguarded set to |
| +// consoleGuardUntrustedChain (Paul's ruling, 2026-11-19). Such a box |
| +// can hold no guard root honours: proofnamed.go reads the same chain |
| +// with the same rules and refuses there too. The guard rule only keeps |
| +// a declaration consistent with what the release path accepts; it |
| +// grants no authority, and the declaration's content comes only from |
| +// the operator's parameter or local file, so a box agent that breaks |
| +// the chain changes nothing root installs. Any other walk failure is |
| +// err. |
| +// - the marker in that trusted directory: absent is ok=false with no |
| +// error; any other failure (a symlink, a wrong owner, a group-writable |
| +// file, a second link, a marker over rootstate.MaxRecord) is err, and |
| +// the caller refuses the install. |
| +// |
| // Present, the marker's bytes are the guarded host's LiveLink |
| // (<opt>/live/<host>), and the host is its base name: only when the marker |
| // sits in the live directory, the base is a valid host and guardNames |
| // agrees. A marker that names no valid host is untrusted, so err. |
| -func consoleGuardHost(optDir string) (host string, ok bool, err error) { |
| - f, err := openConsoleGuardMarker(consoleReadyMarker(optDir)) |
| +func consoleGuardHost(optDir string) (host string, ok bool, unguarded string, err error) { |
| + path := consoleReadyMarker(optDir) |
| + dir, err := openConsoleGuardDir(filepath.Dir(path)) |
| if errors.Is(err, os.ErrNotExist) { |
| - return "", false, nil |
| + return "", false, "", nil |
| + } |
| + if errors.Is(err, os.ErrPermission) { |
| + return "", false, consoleGuardUntrustedChain, nil |
| } |
| if err != nil { |
| - return "", false, err |
| + return "", false, "", err |
| + } |
| + defer dir.Close() |
| + f, err := rootstate.OpenTrustedFileIn(dir, filepath.Base(path)) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return "", false, "", nil |
| + } |
| + if err != nil { |
| + return "", false, "", err |
| } |
| defer f.Close() |
| marker, err := io.ReadAll(io.LimitReader(f, rootstate.MaxRecord+1)) |
| if err != nil { |
| - return "", false, err |
| + return "", false, "", err |
| } |
| if len(marker) > rootstate.MaxRecord { |
| - return "", false, os.ErrPermission |
| + return "", false, "", os.ErrPermission |
| } |
| - path := string(marker) |
| - base := filepath.Base(path) |
| - if filepath.Dir(path) != filepath.Dir(release.LiveLink(optDir, "x")) || !controlname.ValidHostShape(base) || !guardNames(marker, optDir, base) { |
| - return "", false, fmt.Errorf("%w: %q names no host under %s", os.ErrPermission, path, filepath.Dir(release.LiveLink(optDir, "x"))) |
| + named := string(marker) |
| + base := filepath.Base(named) |
| + if filepath.Dir(named) != filepath.Dir(release.LiveLink(optDir, "x")) || !controlname.ValidHostShape(base) || !guardNames(marker, optDir, base) { |
| + return "", false, "", fmt.Errorf("%w: %q names no host under %s", os.ErrPermission, named, filepath.Dir(release.LiveLink(optDir, "x"))) |
| } |
| - return base, true, nil |
| + return base, true, "", nil |
| } |
| // checkGuardRule: on a guarded box the guarded host's entry, when the file |
| @@ -223,9 +262,14 @@ func RunSystemNamedUnitsInstall(args []string) { |
| if err != nil { |
| log.Fatalf("carlos system named-units install: %v", err) |
| } |
| - if err := InstallNamedUnits(appconfig.DefaultEnvDir, appconfig.DefaultNamedUnitsPath, raw); err != nil { |
| + unguarded, err := InstallNamedUnits(appconfig.DefaultEnvDir, appconfig.DefaultNamedUnitsPath, raw) |
| + if err != nil { |
| log.Fatalf("carlos system named-units install: %v", err) |
| } |
| + if unguarded != "" { |
| + fmt.Printf("installed %s (%s)\n", appconfig.DefaultNamedUnitsPath, unguarded) |
| + return |
| + } |
| fmt.Printf("installed %s\n", appconfig.DefaultNamedUnitsPath) |
| } |
diff --git a/internal/host/unitops_guard_linux_test.go b/internal/host/unitops_guard_linux_test.go| index 07498e27a..83d317701 100644 |
| --- a/internal/host/unitops_guard_linux_test.go |
| +++ b/internal/host/unitops_guard_linux_test.go |
| @@ -1,9 +1,11 @@ |
| package host |
| import ( |
| + "context" |
| "errors" |
| "os" |
| "path/filepath" |
| + "strings" |
| "testing" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -11,9 +13,9 @@ import ( |
| ) |
| // consoleGuardAt points the install path at a fresh opt directory and |
| -// reads the marker with the trusted opener anchored there, so the real |
| -// symlink, owner and mode checks run without root. Not parallel: it sets |
| -// CARLOS_OPT_DIR and the opener variable. |
| +// walks to the marker's directory with the trusted walk anchored there, so |
| +// the real symlink, owner and mode checks run without root. Not parallel: |
| +// it sets CARLOS_OPT_DIR and the walk variable. |
| func consoleGuardAt(t *testing.T) string { |
| t.Helper() |
| opt := t.TempDir() |
| @@ -23,9 +25,9 @@ func consoleGuardAt(t *testing.T) string { |
| t.Fatal(err) |
| } |
| t.Setenv("CARLOS_OPT_DIR", opt) |
| - prior := openConsoleGuardMarker |
| - openConsoleGuardMarker = func(path string) (*os.File, error) { return rootstate.OpenTrustedFileFrom(opt, path) } |
| - t.Cleanup(func() { openConsoleGuardMarker = prior }) |
| + prior := openConsoleGuardDir |
| + openConsoleGuardDir = func(dir string) (*os.File, error) { return rootstate.OpenTrustedDirFrom(opt, dir) } |
| + t.Cleanup(func() { openConsoleGuardDir = prior }) |
| return opt |
| } |
| @@ -52,17 +54,17 @@ func writeConsoleGuardMarker(t *testing.T, opt string, body []byte) string { |
| func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) { |
| t.Run("absent", func(t *testing.T) { |
| opt := consoleGuardAt(t) |
| - host, ok, err := consoleGuardHost(opt) |
| - if err != nil || ok || host != "" { |
| - t.Fatalf("absent marker: %q %v %v", host, ok, err) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err != nil || ok || host != "" || unguarded != "" { |
| + t.Fatalf("absent marker: %q %v %q %v", host, ok, unguarded, err) |
| } |
| }) |
| t.Run("names a host", func(t *testing.T) { |
| opt := consoleGuardAt(t) |
| writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "b.example.com"))) |
| - host, ok, err := consoleGuardHost(opt) |
| - if err != nil || !ok || host != "b.example.com" { |
| - t.Fatalf("matching marker: %q %v %v", host, ok, err) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err != nil || !ok || host != "b.example.com" || unguarded != "" { |
| + t.Fatalf("matching marker: %q %v %q %v", host, ok, unguarded, err) |
| } |
| }) |
| // A marker that names no valid host under <opt>/live is untrusted: the |
| @@ -76,9 +78,9 @@ func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) { |
| t.Run(name, func(t *testing.T) { |
| opt := consoleGuardAt(t) |
| writeConsoleGuardMarker(t, opt, []byte(body(opt))) |
| - host, ok, err := consoleGuardHost(opt) |
| - if err == nil || errors.Is(err, os.ErrNotExist) { |
| - t.Fatalf("marker %q read as %q ok=%v err=%v; want a refusing error", body(opt), host, ok, err) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err == nil || errors.Is(err, os.ErrNotExist) || unguarded != "" { |
| + t.Fatalf("marker %q read as %q ok=%v unguarded=%q err=%v; want a refusing error", body(opt), host, ok, unguarded, err) |
| } |
| }) |
| } |
| @@ -87,12 +89,14 @@ func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) { |
| body := []byte(release.LiveLink(opt, "b.example.com")) |
| body = append(body, make([]byte, rootstate.MaxRecord)...) |
| writeConsoleGuardMarker(t, opt, body) |
| - host, ok, err := consoleGuardHost(opt) |
| - if err == nil || errors.Is(err, os.ErrNotExist) { |
| - t.Fatalf("an oversized marker read as %q ok=%v err=%v; want a refusing error", host, ok, err) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err == nil || errors.Is(err, os.ErrNotExist) || unguarded != "" { |
| + t.Fatalf("an oversized marker read as %q ok=%v unguarded=%q err=%v; want a refusing error", host, ok, unguarded, err) |
| } |
| }) |
| - for _, attack := range []string{"symlink", "group-writable", "hard link", "group-writable directory"} { |
| + // A group-writable guard directory is part of the chain: see |
| + // TestConsoleGuardHostUntrustedChainIsUnguarded. |
| + for _, attack := range []string{"symlink", "group-writable", "hard link"} { |
| t.Run(attack, func(t *testing.T) { |
| opt := consoleGuardAt(t) |
| body := []byte(release.LiveLink(opt, "a.example.com")) |
| @@ -117,14 +121,10 @@ func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) { |
| if err := os.Link(marker, filepath.Join(opt, "second-name")); err != nil { |
| t.Fatal(err) |
| } |
| - case "group-writable directory": |
| - if err := os.Chmod(filepath.Dir(marker), 0o775); err != nil { |
| - t.Fatal(err) |
| - } |
| } |
| - host, ok, err := consoleGuardHost(opt) |
| - if err == nil || errors.Is(err, os.ErrNotExist) { |
| - t.Fatalf("an untrusted marker read as %q ok=%v err=%v; want a refusing error", host, ok, err) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err == nil || errors.Is(err, os.ErrNotExist) || unguarded != "" { |
| + t.Fatalf("an untrusted marker read as %q ok=%v unguarded=%q err=%v; want a refusing error", host, ok, unguarded, err) |
| } |
| }) |
| } |
| @@ -148,7 +148,7 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) { |
| } |
| refused := func(t *testing.T, dir, dst string, raw []byte) { |
| t.Helper() |
| - if err := InstallNamedUnits(dir, dst, raw); err == nil { |
| + if _, err := InstallNamedUnits(dir, dst, raw); err == nil { |
| t.Fatal("install succeeded") |
| } |
| if _, err := os.Stat(dst); !os.IsNotExist(err) { |
| @@ -166,7 +166,7 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) { |
| writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "a.example.com"))) |
| dir := t.TempDir() |
| dst := filepath.Join(dir, "named-units.json") |
| - if err := InstallNamedUnits(dir, dst, file(consoleA, shopB)); err != nil { |
| + if _, err := InstallNamedUnits(dir, dst, file(consoleA, shopB)); err != nil { |
| t.Fatal(err) |
| } |
| }) |
| @@ -178,7 +178,7 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) { |
| if err := os.WriteFile(dst, file(consoleA), 0o644); err != nil { |
| t.Fatal(err) |
| } |
| - if err := InstallNamedUnits(dir, dst, file(shopB)); err != nil { |
| + if _, err := InstallNamedUnits(dir, dst, file(shopB)); err != nil { |
| t.Fatal(err) |
| } |
| }) |
| @@ -187,7 +187,7 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) { |
| writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "c.example.com"))) |
| dir := t.TempDir() |
| dst := filepath.Join(dir, "named-units.json") |
| - if err := InstallNamedUnits(dir, dst, file(shopB)); err != nil { |
| + if _, err := InstallNamedUnits(dir, dst, file(shopB)); err != nil { |
| t.Fatal(err) |
| } |
| }) |
| @@ -207,3 +207,123 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) { |
| refused(t, dir, filepath.Join(dir, "named-units.json"), file(consoleA, shopB)) |
| }) |
| } |
| + |
| +// breakGuardChain makes one directory on the walk to the marker fail the |
| +// trust checks the way a box agent's /opt/carlos does in production (a |
| +// test can't chown, so a group-writable or others-unreadable mode stands |
| +// in for the wrong owner: the walk refuses all three alike). |
| +func breakGuardChain(t *testing.T, opt, where string) { |
| + t.Helper() |
| + dir, mode := opt, os.FileMode(0o775) |
| + switch where { |
| + case "opt group-writable": |
| + case "system group-writable": |
| + dir = filepath.Join(opt, "system") |
| + case "system unreadable by others": |
| + dir, mode = filepath.Join(opt, "system"), 0o750 |
| + case "guard group-writable": |
| + dir = filepath.Join(opt, "system", "guard") |
| + default: |
| + t.Fatalf("unknown chain break %q", where) |
| + } |
| + if err := os.MkdirAll(dir, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(dir, mode); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +var guardChainBreaks = []string{"opt group-writable", "system group-writable", "system unreadable by others", "guard group-writable"} |
| + |
| +// Paul's ruling of 2026-11-19: a box whose directory chain to the marker |
| +// is not root-trusted can't hold a guard root honours (the release path |
| +// reads the same chain with the same rules), so the install treats it as |
| +// unguarded rather than refusing every declaration. |
| +func TestConsoleGuardHostUntrustedChainIsUnguarded(t *testing.T) { |
| + for _, where := range guardChainBreaks { |
| + for _, marker := range []bool{false, true} { |
| + name := where + ", no marker" |
| + if marker { |
| + name = where + ", marker present" |
| + } |
| + t.Run(name, func(t *testing.T) { |
| + opt := consoleGuardAt(t) |
| + if marker { |
| + writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "a.example.com"))) |
| + } |
| + breakGuardChain(t, opt, where) |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err != nil || ok || host != "" || unguarded != consoleGuardUntrustedChain { |
| + t.Fatalf("untrusted chain: %q ok=%v unguarded=%q err=%v; want unguarded", host, ok, unguarded, err) |
| + } |
| + }) |
| + } |
| + } |
| + t.Run("trusted chain, no marker", func(t *testing.T) { |
| + opt := consoleGuardAt(t) |
| + if err := os.MkdirAll(filepath.Join(opt, "system", "guard"), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, p := range []string{filepath.Join(opt, "system"), filepath.Join(opt, "system", "guard")} { |
| + if err := os.Chmod(p, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + host, ok, unguarded, err := consoleGuardHost(opt) |
| + if err != nil || ok || host != "" || unguarded != "" { |
| + t.Fatalf("absent marker in a trusted guard dir: %q ok=%v unguarded=%q err=%v", host, ok, unguarded, err) |
| + } |
| + }) |
| +} |
| + |
| +// On a box whose chain is untrusted the install goes ahead: the empty |
| +// declaration dennis refused on 2026-11-19, and even a file the marker |
| +// would refuse, since nothing on that box can honour the marker. |
| +func TestInstallNamedUnitsUntrustedGuardChain(t *testing.T) { |
| + for _, raw := range []string{ |
| + `{"schema":1,"units":[]}`, |
| + `{"schema":1,"units":[{"host":"a.example.com","app":"shop","unit":"shop.service","addr":"127.0.0.1:1","health":{"path":"/"}}]}`, |
| + } { |
| + t.Run(raw, func(t *testing.T) { |
| + opt := consoleGuardAt(t) |
| + writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "a.example.com"))) |
| + breakGuardChain(t, opt, "opt group-writable") |
| + dir := t.TempDir() |
| + dst := filepath.Join(dir, "named-units.json") |
| + unguarded, err := InstallNamedUnits(dir, dst, []byte(raw)) |
| + if err != nil { |
| + t.Fatalf("install on an untrusted chain: %v", err) |
| + } |
| + if unguarded != consoleGuardUntrustedChain { |
| + t.Fatalf("unguarded = %q, want the untrusted-chain reason", unguarded) |
| + } |
| + if got, err := os.ReadFile(dst); err != nil || string(got) != raw { |
| + t.Fatalf("installed = %q, %v", got, err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The fetch step with the production guard read on an untrusted chain: |
| +// the empty declaration installs, and the record says why the guard was |
| +// not applied. |
| +func TestNamedUnitsFetchUntrustedGuardChainInstalls(t *testing.T) { |
| + opt := consoleGuardAt(t) |
| + breakGuardChain(t, opt, "system group-writable") |
| + x := newFetchFixture(t) |
| + x.store.raw = declFile() |
| + f := x.fetch() |
| + f.guardHost = func() (string, bool, string, error) { return consoleGuardHost(opt) } |
| + st := f.run(context.Background()) |
| + wantOutcome(t, st, "installed") |
| + if st.Reason != consoleGuardUntrustedChain { |
| + t.Fatalf("Reason = %q, want the untrusted-chain reason", st.Reason) |
| + } |
| + if line := namedUnitsLogLine(st); !strings.Contains(line, consoleGuardUntrustedChain) { |
| + t.Fatalf("journal line %q does not say why the guard was not applied", line) |
| + } |
| + if got, err := os.ReadFile(x.dst); err != nil || string(got) != string(declFile()) { |
| + t.Fatalf("file = %q, %v", got, err) |
| + } |
| +} |
diff --git a/internal/host/unitops_test.go b/internal/host/unitops_test.go| index c38c61a75..01439ed8b 100644 |
| --- a/internal/host/unitops_test.go |
| +++ b/internal/host/unitops_test.go |
| @@ -80,7 +80,7 @@ func TestInstallNamedUnitsWaitsForTheConfigSyncLock(t *testing.T) { |
| dir := t.TempDir() |
| dst := filepath.Join(dir, "named-units.json") |
| body := []byte(`{"schema":1,"units":[{"host":"relay.example.com","account":"bab","app":"relay","unit":"relay.service","addr":"127.0.0.1:8451","health":{"path":"/ready"}}]}`) |
| - if err := InstallNamedUnits(dir, dst, []byte(`{"schema":1,"units":[]}garbage`)); err == nil { |
| + if _, err := InstallNamedUnits(dir, dst, []byte(`{"schema":1,"units":[]}garbage`)); err == nil { |
| t.Fatal("an invalid file was installed") |
| } |
| if err := appconfig.ValidateDeclarations(body); err != nil { |
| @@ -91,7 +91,7 @@ func TestInstallNamedUnitsWaitsForTheConfigSyncLock(t *testing.T) { |
| t.Fatal(err) |
| } |
| done := make(chan error, 1) |
| - go func() { done <- InstallNamedUnits(dir, dst, body) }() |
| + go func() { _, err := InstallNamedUnits(dir, dst, body); done <- err }() |
| select { |
| case err := <-done: |
| t.Fatalf("install finished while the pass held the lock: %v", err) |
| @@ -133,7 +133,7 @@ func TestInstallNamedUnitsRefusesWhatTheReleasePathRefuses(t *testing.T) { |
| noConsoleGuard(t) |
| dir := t.TempDir() |
| dst := filepath.Join(dir, "named-units.json") |
| - if err := InstallNamedUnits(dir, dst, []byte(body)); err == nil { |
| + if _, err := InstallNamedUnits(dir, dst, []byte(body)); err == nil { |
| t.Fatal("installed a file the release path refuses") |
| } |
| if _, err := os.Stat(dst); !os.IsNotExist(err) { |
| @@ -143,14 +143,14 @@ func TestInstallNamedUnitsRefusesWhatTheReleasePathRefuses(t *testing.T) { |
| } |
| } |
| -// noConsoleGuard makes the box's console-guard marker absent for one test, |
| -// so an install never reads the machine's real /opt/carlos. The opener is |
| -// a package variable: tests using it must not run in parallel. |
| +// noConsoleGuard makes the box's console-guard directory absent for one |
| +// test, so an install never reads the machine's real /opt/carlos. The |
| +// walk is a package variable: tests using it must not run in parallel. |
| func noConsoleGuard(t *testing.T) { |
| t.Helper() |
| - prior := openConsoleGuardMarker |
| - openConsoleGuardMarker = func(string) (*os.File, error) { return nil, os.ErrNotExist } |
| - t.Cleanup(func() { openConsoleGuardMarker = prior }) |
| + prior := openConsoleGuardDir |
| + openConsoleGuardDir = func(string) (*os.File, error) { return nil, os.ErrNotExist } |
| + t.Cleanup(func() { openConsoleGuardDir = prior }) |
| } |
| func TestCheckGuardRuleRefusesNonConsoleForGuardedHost(t *testing.T) { |