drill: root takes nothing from <data>/monitor (ls284, census P1)
The drill refuses effective uid 0 first of all (one line, exit 1), before reading self.json or verify-assign.json or opening the store or registry: self.json, which the carlos user writes, names the record a run replaces. A root-run agent neither writes self.json or verify-assign.json nor reads verify.json (drillRecordsArmed, as restartWatchFile and engineBeatArmed). Both shipped units run as carlos. Seams count the monitor readers, the store and ReadVerify in the tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+187
−7
internal/host/agententry.go+21 −1internal/host/metrics_loop.go+24 −3internal/host/verifyassign.go+6 −1internal/host/verifyassign_test.go+65 −0internal/ops/hibernate_verify.go+1 −1internal/ops/hibernate_verify_test.go+49 −0internal/ops/ops.go+21 −1
Intent
Human
<task-notification> <task-id>b063oehnx</task-id> <tool-use-id>toolu_01D2PjJ4K8huvWS59dHfSEnr</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ab362fc0-f177-4a7f-a92e-4a29ea210aad/tasks/b063oehnx.output</output-file> <status>completed</status> <summary>Background command "Astra census review for slice 2a" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/host/agententry.go b/internal/host/agententry.go| index 35bb3fd90..779b57f09 100644 |
| --- a/internal/host/agententry.go |
| +++ b/internal/host/agententry.go |
| @@ -54,7 +54,15 @@ func agentEntry(getenv func(string) string, resolve func() (string, error), back |
| // cannot work it out itself (boxself). The record is written now and |
| // every boxSelfEvery while the agent runs, so one that can no longer |
| // be written goes stale and the drill (boxself.ReadFresh) refuses it; |
| - // a failed write also removes the earlier record where it can. |
| + // a failed write also removes the earlier record where it can. A |
| + // root-run agent writes nothing there (ls284, drillRecordsArmed): the |
| + // drill refuses to run as root, and nothing root should publish lives |
| + // in the carlos user's <data>/monitor. |
| + if !drillRecordsArmed(agentEuid()) { |
| + logf("agent: runs as root, so the restore drill's records under <data>/monitor are neither written nor read") |
| + run() |
| + return |
| + } |
| // A write the agent or the drill crashed in the middle of leaves a |
| // staging directory in <data>/monitor; clear the stale ones first. |
| if err := boxself.ReapStaging(dataDirFrom(getenv)); err != nil { |
| @@ -75,6 +83,18 @@ func agentEntry(getenv func(string) string, resolve func() (string, error), back |
| run() |
| } |
| +// agentEuid is the agent's effective uid; a test pins it. |
| +var agentEuid = os.Geteuid |
| + |
| +// drillRecordsArmed reports whether an agent running as euid writes the |
| +// restore drill's records (self.json, verify-assign.json) and reads its |
| +// local verdicts (verify.json), all under <data>/monitor, which the |
| +// carlos user owns. A root-run agent does none of it (census ruling |
| +// ls284, #280, as restartWatchFile and engineBeatArmed): root takes no |
| +// input or authority from a file an agent-user process can write. The |
| +// shipped unit runs the agent as carlos, so this costs nothing there. |
| +func drillRecordsArmed(euid int) bool { return euid != 0 } |
| + |
| // boxSelfEvery is how often the agent rewrites its record for the drill. |
| var boxSelfEvery = 5 * time.Minute |
diff --git a/internal/host/metrics_loop.go b/internal/host/metrics_loop.go| index e05d07f60..8d077a615 100644 |
| --- a/internal/host/metrics_loop.go |
| +++ b/internal/host/metrics_loop.go |
| @@ -617,14 +617,32 @@ type alertReadings struct { |
| // verifyWatch reads the restore drill's local copy of this box's record |
| // (dbverify.LocalPath) for the alert rules. A read that fails is logged |
| -// once, not every minute, until it reads again. |
| +// once, not every minute, until it reads again. With no path (a root-run |
| +// agent, newVerifyWatch) it reads nothing and the verify rules hold. |
| type verifyWatch struct { |
| path string |
| lastErr string |
| } |
| +// readVerify is metrics.ReadVerify; a test counts its calls. |
| +var readVerify = metrics.ReadVerify |
| + |
| +// newVerifyWatch is the watch for an agent running as euid. A root-run |
| +// agent reads no verify.json, a file the carlos user can write (ls284, |
| +// drillRecordsArmed): the decision is made before path is called, as |
| +// newRestartWatchFile makes it. |
| +func newVerifyWatch(euid int, path func() string) *verifyWatch { |
| + if !drillRecordsArmed(euid) { |
| + return &verifyWatch{} |
| + } |
| + return &verifyWatch{path: path()} |
| +} |
| + |
| func (w *verifyWatch) read(at time.Time) *metrics.VerifyReading { |
| - v, err := metrics.ReadVerify(w.path, at) |
| + if w.path == "" { |
| + return nil |
| + } |
| + v, err := readVerify(w.path, at) |
| switch { |
| case err == nil: |
| w.lastErr = "" |
| @@ -980,7 +998,10 @@ func metricsLoop(execRoot string, src unitSources, owners func() map[string]metr |
| log.Printf("agent: restart watch: agent runs as root, so the watch is kept in memory only") |
| } |
| restarts := watchFile.load() |
| - verify := &verifyWatch{path: dbverify.LocalPath(dataDir())} |
| + verify := newVerifyWatch(euid, func() string { return dbverify.LocalPath(dataDir()) }) |
| + if verify.path == "" { |
| + log.Printf("agent: restore test record: agent runs as root, so its alerts are off") |
| + } |
| restartTick := func(at time.Time) alertReadings { |
| extra := alertReadings{restarts: restarts.tick(at, probes.restarts()), verify: verify.read(at)} |
| if _, total, ok := probes.diskBytes(); ok { |
diff --git a/internal/host/verifyassign.go b/internal/host/verifyassign.go| index e4d9bcc04..fb62eb68f 100644 |
| --- a/internal/host/verifyassign.go |
| +++ b/internal/host/verifyassign.go |
| @@ -82,8 +82,13 @@ func computeAssignment(routes []registry.Route, view dutyViewFunc, now time.Time |
| } |
| // verifyAssignLoop writes the assignment now and every verifyAssignEvery |
| -// until ctx ends. |
| +// until ctx ends; a root-run agent returns at once, writing nothing. |
| func verifyAssignLoop(ctx context.Context, list func() ([]registry.Route, error), view dutyViewFunc, dataDir string, logf func(string, ...any)) { |
| + // A root-run agent writes nothing under <data>/monitor (ls284). |
| + if !drillRecordsArmed(agentEuid()) { |
| + logf("agent: drill assignment: runs as root, so no assignment is written") |
| + return |
| + } |
| t := time.NewTicker(verifyAssignEvery) |
| defer t.Stop() |
| runVerifyAssign(ctx, list, view, dataDir, t.C, time.Now, logf) |
diff --git a/internal/host/verifyassign_test.go b/internal/host/verifyassign_test.go| index b38283dfd..8c4ae1ff7 100644 |
| --- a/internal/host/verifyassign_test.go |
| +++ b/internal/host/verifyassign_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "fmt" |
| "net/http" |
| "os" |
| + "path/filepath" |
| "reflect" |
| "slices" |
| "strings" |
| @@ -14,12 +15,23 @@ import ( |
| "github.com/carlosframework/platform/internal/boxchoice" |
| "github.com/carlosframework/platform/internal/boxself" |
| + "github.com/carlosframework/platform/internal/dbverify" |
| "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/hostenv" |
| "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/metrics" |
| "github.com/carlosframework/platform/internal/registry" |
| ) |
| +// asAgentEuid runs the rest of the test as an agent with effective uid |
| +// euid (agentEuid), whatever user runs the test. |
| +func asAgentEuid(t *testing.T, euid int) { |
| + t.Helper() |
| + was := agentEuid |
| + agentEuid = func() int { return euid } |
| + t.Cleanup(func() { agentEuid = was }) |
| +} |
| + |
| type offEC2 struct{} |
| func (offEC2) RoundTrip(*http.Request) (*http.Response, error) { |
| @@ -30,6 +42,7 @@ func (offEC2) RoundTrip(*http.Request) (*http.Response, error) { |
| // for the nightly drill is the one its lease locks name, which is the |
| // label its metrics publish under, on a fixed box and on a group box. |
| func TestAgentWritesSelfRefEqualToLeaseRef(t *testing.T) { |
| + asAgentEuid(t, 1000) |
| quick := func(int) time.Duration { return time.Millisecond } |
| const account, instance = "123456789012", "i-0a61f580df1acfa65" |
| for name, group := range map[string]bool{"fixed": false, "group": true} { |
| @@ -70,6 +83,7 @@ func TestAgentWritesSelfRefEqualToLeaseRef(t *testing.T) { |
| // its reference, an earlier box's record does not survive for the drill |
| // to publish under. |
| func TestAgentFailedSelfRefLeavesNoIdentity(t *testing.T) { |
| + asAgentEuid(t, 1000) |
| dir := t.TempDir() |
| if err := boxself.Write(dir, boxself.Self{Ref: "g-0123456789abcdef0123", Group: true, At: time.Now()}); err != nil { |
| t.Fatal(err) |
| @@ -91,6 +105,7 @@ func TestAgentFailedSelfRefLeavesNoIdentity(t *testing.T) { |
| // tick while the agent runs, so it stays fresh, and no longer once run |
| // returns. |
| func TestAgentRewritesSelfRefUntilItStops(t *testing.T) { |
| + asAgentEuid(t, 1000) |
| was := boxSelfEvery |
| boxSelfEvery = 10 * time.Millisecond |
| t.Cleanup(func() { boxSelfEvery = was }) |
| @@ -243,3 +258,53 @@ func TestAssignmentLoopWritesUntilStopped(t *testing.T) { |
| t.Fatalf("assignment after a failed listing %+v, %v; want %+v", b, err, a) |
| } |
| } |
| + |
| +// TestRootAgentLeavesTheDrillRecordsAlone: a root-run agent neither |
| +// writes self.json or verify-assign.json nor reads verify.json (ls284): |
| +// agentEntry starts the agent without touching <data>/monitor, the |
| +// assignment loop returns before listing anything, and the verify watch |
| +// never works out its path or calls ReadVerify. |
| +func TestRootAgentLeavesTheDrillRecordsAlone(t *testing.T) { |
| + asAgentEuid(t, 0) |
| + dir := t.TempDir() |
| + var lines []string |
| + logf := func(f string, args ...any) { lines = append(lines, fmt.Sprintf(f, args...)) } |
| + ran := false |
| + agentEntry(groupEnv(map[string]string{"CARLOS_DATA_DIR": dir}), resolveGroupBoxReference, nil, logf, |
| + func() hafresh.Self { return hafresh.Self{Ref: "jolly-otter"} }, func() { ran = true }) |
| + if !ran { |
| + t.Fatal("the agent did not start") |
| + } |
| + if _, err := os.Lstat(filepath.Join(dir, "monitor")); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("a root-run agent touched <data>/monitor: %v", err) |
| + } |
| + if !slices.ContainsFunc(lines, func(l string) bool { return strings.Contains(l, "runs as root") }) { |
| + t.Fatalf("the refusal was not logged: %q", lines) |
| + } |
| + |
| + listed := 0 |
| + list := func() ([]registry.Route, error) { |
| + listed++ |
| + return []registry.Route{{Host: "shop.example.com", HA: true}}, nil |
| + } |
| + view := func(registry.Route) boxchoice.WakeSnapshot { |
| + return boxchoice.WakeSnapshot{Usable: true, Serving: []boxchoice.Candidate{{BoxID: "g-a"}}} |
| + } |
| + verifyAssignLoop(context.Background(), list, view, dir, logf) |
| + if _, err := boxself.ReadAssignment(dir); !errors.Is(err, os.ErrNotExist) || listed != 0 { |
| + t.Fatalf("a root-run agent's assignment loop listed %d times and left %v", listed, err) |
| + } |
| + |
| + reads := 0 |
| + wasRead := readVerify |
| + readVerify = func(string, time.Time) (*metrics.VerifyReading, error) { reads++; return nil, nil } |
| + t.Cleanup(func() { readVerify = wasRead }) |
| + w := newVerifyWatch(0, func() string { t.Fatal("a root-run agent worked out the verify path"); return "" }) |
| + if v := w.read(time.Now()); v != nil || reads != 0 { |
| + t.Fatalf("a root-run agent's verify watch read %d times, got %+v", reads, v) |
| + } |
| + // The carlos user's agent reads it. |
| + if newVerifyWatch(1000, func() string { return dbverify.LocalPath(dir) }).read(time.Now()); reads != 1 { |
| + t.Fatalf("a non-root agent's verify watch read %d times, want 1", reads) |
| + } |
| +} |
diff --git a/internal/ops/hibernate_verify.go b/internal/ops/hibernate_verify.go| index fbf5c5382..d8cb36454 100644 |
| --- a/internal/ops/hibernate_verify.go |
| +++ b/internal/ops/hibernate_verify.go |
| @@ -510,7 +510,7 @@ func selectHosts(self boxself.Self, dataDir string, now time.Time, hosts []Hiber |
| } |
| return set, "" |
| } |
| - a, err := boxself.ReadAssignment(dataDir) |
| + a, err := readDrillAssignment(dataDir) |
| switch { |
| case err != nil: |
| why = fmt.Sprintf("no usable drill assignment at %s: %v", boxself.AssignmentPath(dataDir), err) |
diff --git a/internal/ops/hibernate_verify_test.go b/internal/ops/hibernate_verify_test.go| index 1ea06165b..a765e35cd 100644 |
| --- a/internal/ops/hibernate_verify_test.go |
| +++ b/internal/ops/hibernate_verify_test.go |
| @@ -1594,3 +1594,52 @@ func TestRunHibernateVerifyDropsAMovedHost(t *testing.T) { |
| t.Fatalf("mid-move host: %+v", v) |
| } |
| } |
| + |
| +// TestDrillRefusesRoot: a root run stops before it reads a monitor file |
| +// (self.json names the record it would replace, and the carlos user |
| +// writes it) or touches the deployment store or the registry (ls284). |
| +// Even a fully armed box with a fresh self record and an assignment is |
| +// refused, and nothing is published. |
| +func TestDrillRefusesRoot(t *testing.T) { |
| + fx := newRunFx(t, "a.example.com") |
| + fx.writeSelf(fxSelf, true) |
| + fx.writeAssignment(fx.clock.Now(), map[string]string{"a.example.com": fxSelf}) |
| + var reads, stores int |
| + swap(t, &readBoxSelf, func(dir string, now time.Time, maxAge time.Duration) (boxself.Self, error) { |
| + reads++ |
| + return boxself.ReadFresh(dir, now, maxAge) |
| + }) |
| + swap(t, &readDrillAssignment, func(dir string) (boxself.Assignment, error) { |
| + reads++ |
| + return boxself.ReadAssignment(dir) |
| + }) |
| + r := fx.runner() |
| + r.Root = true |
| + r.OpenStore = func(context.Context) (release.Store, error) { |
| + stores++ |
| + return fx.store, nil |
| + } |
| + r.RegPath = filepath.Join(t.TempDir(), "never", "registry.db") |
| + if code := r.run(context.Background()); code == 0 { |
| + t.Fatal("a root run exited 0") |
| + } |
| + if reads != 0 || stores != 0 { |
| + t.Fatalf("a root run read %d monitor files and opened the store %d times; want none", reads, stores) |
| + } |
| + if !strings.Contains(fx.logs.String(), "refusing to run as root") || strings.Count(strings.TrimSpace(fx.logs.String()), "\n") != 0 { |
| + t.Fatalf("log %q, want the one refusal line", fx.logs.String()) |
| + } |
| + if _, err := os.Stat(filepath.Dir(r.RegPath)); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("the registry was opened: %v", err) |
| + } |
| + if rec := fx.published(fxSelf); rec != nil { |
| + t.Fatalf("a root run published %+v", rec) |
| + } |
| + |
| + // The same run as the carlos user goes through both readers and the store. |
| + r.Root, r.RegPath = false, fx.regPath |
| + r.run(context.Background()) |
| + if reads != 2 || stores != 1 { |
| + t.Fatalf("a non-root run read %d monitor files and opened the store %d times; want 2 and 1", reads, stores) |
| + } |
| +} |
diff --git a/internal/ops/ops.go b/internal/ops/ops.go| index d18870a14..05a5a361a 100644 |
| --- a/internal/ops/ops.go |
| +++ b/internal/ops/ops.go |
| @@ -356,6 +356,7 @@ func runHibernateVerify(args []string) { |
| rp = dir + "/registry.db" |
| } |
| r := hibernateRun{ |
| + Root: os.Geteuid() == 0, |
| Cfg: cfg, |
| OpenStore: openDeployStore, |
| Region: strings.TrimSpace(os.Getenv("CARLOS_REGION")), |
| @@ -374,9 +375,19 @@ func runHibernateVerify(args []string) { |
| } |
| } |
| +// readBoxSelf and readDrillAssignment are the drill's two monitor-file |
| +// readers (boxself.ReadFresh, boxself.ReadAssignment); a test counts their |
| +// calls. |
| +var ( |
| + readBoxSelf = boxself.ReadFresh |
| + readDrillAssignment = boxself.ReadAssignment |
| +) |
| + |
| // hibernateRun is one run of the verb with everything it reads from the |
| // box named, so a test can run it whole. |
| type hibernateRun struct { |
| + // Root is a run as effective uid 0, which run refuses first of all. |
| + Root bool |
| Cfg *activator.Config |
| OpenStore func(context.Context) (release.Store, error) |
| Region string // CARLOS_REGION: the region an instance's record must name |
| @@ -397,11 +408,20 @@ type hibernateRun struct { |
| // run that guessed one (the hostname) would publish under a name no reader |
| // looks for, or another box's. |
| func (r hibernateRun) run(ctx context.Context) int { |
| + // Root is refused before any monitor file is read or the deployment |
| + // store is touched (census ruling ls284, #280): self.json, which names |
| + // the record this run would replace, is written by the agent as the |
| + // carlos user, so root would be taking its authority from a file an |
| + // agent-user process controls. The unit runs as carlos. |
| + if r.Root { |
| + r.Logf("hibernate-verify: NOT RUN: refusing to run as root; the drill trusts records the carlos user writes, so run it as carlos (carlos-hibernate-verify.service does)") |
| + return 1 |
| + } |
| if r.Cfg == nil { |
| fmt.Println("hibernate-verify: CARLOS_HIBERNATE_BUCKET unset — no hibernating replicas on this box") |
| return 0 |
| } |
| - self, err := boxself.ReadFresh(r.DataDir, r.Now(), selfMaxAge) |
| + self, err := readBoxSelf(r.DataDir, r.Now(), selfMaxAge) |
| if err != nil { |
| r.Logf("hibernate-verify: NOT RUN: no fresh self record at %s (%v); publishing nothing rather than under a name that may not be this box's", boxself.SelfPath(r.DataDir), err) |
| return 1 |