carlos / platform Public

host: console guard host comes from the marker itself

consoleGuardHost(optDir) now reads the guarded host from the marker's
bytes (<opt>/live/<host>, release.LiveLink): the marker must sit in the
live dir, its base must be a valid host and guardNames must agree; any
other marker is untrusted and refuses. The hold needs the guarded host
even when no declaration file names it. InstallNamedUnits checks the
guard under the config-sync lock and drops its unlocked read of the
current file. Adds oversized and no-host marker tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 66dca9a37cfd5647ffc04018484b5ceb481b8001 parent f9c8d8e
2 files changed, +71 −40
  • internal/host/unitops.go +28 −30
  • internal/host/unitops_guard_linux_test.go +43 −10

Intent

Human

Go ahead.

diff --git a/internal/host/unitops.go b/internal/host/unitops.go
index e6c4477dc..202860309 100644
--- a/internal/host/unitops.go
+++ b/internal/host/unitops.go
@@ -16,6 +16,7 @@ import (
"strings"
"github.com/carlosframework/platform/internal/appconfig"
+ "github.com/carlosframework/platform/internal/controlname"
"github.com/carlosframework/platform/internal/namedunits"
"github.com/carlosframework/platform/internal/release"
"github.com/carlosframework/platform/internal/rootstate"
@@ -44,25 +45,26 @@ func productionUnitOps(envDir string) *appconfig.UnitOps {
// InstallNamedUnits is the one carlos writer of the declaration file
// (Paul's ruling of 2026-10-28, serialisation). It refuses raw unless both
-// readers accept it (validateForInstall) and, on a box whose console guard
-// names a host, unless that host stays console's (checkGuardRule); then it
-// takes the same config-sync flock SyncUnits holds for a whole pass and
-// replaces the file atomically (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.
+// readers accept it (validateForInstall); then it takes the same
+// config-sync flock SyncUnits holds for a whole pass, and under it refuses
+// raw on a box whose console guard names a host unless that host stays
+// console's (checkInstallGuard), and replaces the file atomically
+// (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 {
if err := validateForInstall(raw); err != nil {
return err
}
- if err := checkInstallGuard(release.OptDir(), dst, raw); err != nil {
- return err
- }
unlock, err := lockConfigSync(envDir + "/.sync")
if err != nil {
return fmt.Errorf("config-sync lock: %w", err)
}
defer unlock()
+ if err := checkInstallGuard(release.OptDir(), raw); err != nil {
+ return err
+ }
return installNamedUnitsLocked(dst, raw)
}
@@ -78,21 +80,14 @@ func validateForInstall(raw []byte) error {
}
// checkInstallGuard is the box's console guard rule for an install of raw
-// (already validated) over dst. The guarded host is looked for among the
-// hosts of the candidate and of the current file; the current file is read
-// best-effort, since only the candidate's entries can break the rule.
-func checkInstallGuard(optDir, dst string, raw []byte) error {
+// (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 {
entries, err := namedunits.Decode(raw)
if err != nil {
return err
}
- candidates := namedunits.Hosts(entries)
- if current, err := os.ReadFile(dst); err == nil {
- if prior, err := namedunits.Decode(current); err == nil {
- candidates = append(candidates, namedunits.Hosts(prior)...)
- }
- }
- host, guarded, err := consoleGuardHost(optDir, candidates)
+ host, guarded, err := consoleGuardHost(optDir)
if err != nil {
return fmt.Errorf("console guard marker: %w", err)
}
@@ -159,10 +154,13 @@ var openConsoleGuardMarker = rootstate.OpenTrustedFile
// 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) is err, and
-// the caller refuses the install. Present, the host is the candidate whose
-// LiveLink the marker equals; ok=false when none does.
-func consoleGuardHost(optDir string, candidates []string) (host string, ok bool, err error) {
+// symlink, a wrong owner, a group-writable file or directory, 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))
if errors.Is(err, os.ErrNotExist) {
return "", false, nil
@@ -178,12 +176,12 @@ func consoleGuardHost(optDir string, candidates []string) (host string, ok bool,
if len(marker) > rootstate.MaxRecord {
return "", false, os.ErrPermission
}
- for _, h := range candidates {
- if guardNames(marker, optDir, h) {
- return h, true, nil
- }
+ 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")))
}
- return "", false, nil
+ return base, true, nil
}
// checkGuardRule: on a guarded box the guarded host's entry, when the file
diff --git a/internal/host/unitops_guard_linux_test.go b/internal/host/unitops_guard_linux_test.go
index 69915691d..07498e27a 100644
--- a/internal/host/unitops_guard_linux_test.go
+++ b/internal/host/unitops_guard_linux_test.go
@@ -50,28 +50,46 @@ func writeConsoleGuardMarker(t *testing.T, opt string, body []byte) string {
}
func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) {
- candidates := []string{"a.example.com", "b.example.com"}
t.Run("absent", func(t *testing.T) {
opt := consoleGuardAt(t)
- host, ok, err := consoleGuardHost(opt, candidates)
+ host, ok, err := consoleGuardHost(opt)
if err != nil || ok || host != "" {
t.Fatalf("absent marker: %q %v %v", host, ok, err)
}
})
- t.Run("names a candidate", func(t *testing.T) {
+ 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, candidates)
+ host, ok, err := consoleGuardHost(opt)
if err != nil || !ok || host != "b.example.com" {
t.Fatalf("matching marker: %q %v %v", host, ok, err)
}
})
- t.Run("names no candidate", func(t *testing.T) {
+ // A marker that names no valid host under <opt>/live is untrusted: the
+ // install can't tell which host the guard serves, so it refuses.
+ for name, body := range map[string]func(opt string) string{
+ "outside the live dir": func(opt string) string { return filepath.Join(opt, "versions", "b.example.com") },
+ "not a host": func(opt string) string { return release.LiveLink(opt, "Not_A_Host") },
+ "trailing newline": func(opt string) string { return release.LiveLink(opt, "b.example.com") + "\n" },
+ "empty": func(string) string { return "" },
+ } {
+ 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)
+ }
+ })
+ }
+ t.Run("oversized", func(t *testing.T) {
opt := consoleGuardAt(t)
- writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "c.example.com")))
- host, ok, err := consoleGuardHost(opt, candidates)
- if err != nil || ok || host != "" {
- t.Fatalf("unmatched marker: %q %v %v", host, ok, err)
+ 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)
}
})
for _, attack := range []string{"symlink", "group-writable", "hard link", "group-writable directory"} {
@@ -104,7 +122,7 @@ func TestConsoleGuardHostRefusesUntrustedMarker(t *testing.T) {
t.Fatal(err)
}
}
- host, ok, err := consoleGuardHost(opt, []string{"a.example.com"})
+ 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)
}
@@ -164,6 +182,21 @@ func TestInstallNamedUnitsKeepsTheConsoleGuard(t *testing.T) {
t.Fatal(err)
}
})
+ t.Run("guarded host named by neither file", func(t *testing.T) {
+ opt := consoleGuardAt(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 {
+ t.Fatal(err)
+ }
+ })
+ t.Run("marker names no host", func(t *testing.T) {
+ opt := consoleGuardAt(t)
+ writeConsoleGuardMarker(t, opt, []byte(filepath.Join(opt, "elsewhere", "a.example.com")))
+ dir := t.TempDir()
+ refused(t, dir, filepath.Join(dir, "named-units.json"), file(shopB))
+ })
t.Run("untrusted marker", func(t *testing.T) {
opt := consoleGuardAt(t)
marker := writeConsoleGuardMarker(t, opt, []byte(release.LiveLink(opt, "a.example.com")))