| 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 |