| 1 | //go:build linux |
| 2 | |
| 3 | package hostbox |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "database/sql" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "net/url" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/carlosframework/platform/internal/hostguard" |
| 19 | ) |
| 20 | |
| 21 | func plan9Fix1Inventory() RetirementInventory { |
| 22 | return RetirementInventory{Host: "app.example", Captured: hostguard.Captured{Incarnation: "inc", Stamp: 7}, Ownership: "owner", Artifacts: []Artifact{{Kind: ArtifactRoute, Root: "registry", Name: "app.example", Owner: "owner", RowID: "route-1"}}} |
| 23 | } |
| 24 | |
| 25 | // C-1: root must never follow a symlinked journal directory. The agent could |
| 26 | // otherwise point root's temp-file and rename writes at any directory. |
| 27 | func TestPlan9Fix1TypedRetirementRefusesSymlinkedJournalDir(t *testing.T) { |
| 28 | base := t.TempDir() |
| 29 | victim := filepath.Join(base, "victim") |
| 30 | if err := os.Mkdir(victim, 0700); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | link := filepath.Join(base, "retire") |
| 34 | if err := os.Symlink(victim, link); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | intent, err := NewRetirementJournal("fence", plan9Fix1Inventory()) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | target := hostguard.FenceTarget{Host: "app.example", Incarnation: "inc", Stamp: 7} |
| 42 | r := TypedRetirement{JournalDir: link, Authority: &cleanupAuthority{}, Filesystem: &rootParker{}, Rows: &rowRetirer{rows: map[string]bool{"route:route-1": true}}, Ack: func(context.Context, RetirementJournal) error { return nil }} |
| 43 | if err = r.Run(t.Context(), target, intent); err == nil { |
| 44 | t.Fatal("symlinked journal directory accepted") |
| 45 | } |
| 46 | entries, _ := os.ReadDir(victim) |
| 47 | if len(entries) != 0 { |
| 48 | t.Fatalf("root wrote through a symlinked journal directory: %v", entries) |
| 49 | } |
| 50 | // An ancestor symlink is refused too. |
| 51 | nested := TypedRetirement{JournalDir: filepath.Join(link, "deeper"), Authority: r.Authority, Filesystem: r.Filesystem, Rows: r.Rows, Ack: r.Ack} |
| 52 | if err = nested.Run(t.Context(), target, intent); err == nil { |
| 53 | t.Fatal("symlinked journal ancestor accepted") |
| 54 | } |
| 55 | if entries, _ = os.ReadDir(victim); len(entries) != 0 { |
| 56 | t.Fatalf("root created through a symlinked ancestor: %v", entries) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // C-1: a journal directory another principal can write is not root's own. |
| 61 | func TestPlan9Fix1TypedRetirementRefusesSharedJournalDir(t *testing.T) { |
| 62 | dir := filepath.Join(t.TempDir(), "retire") |
| 63 | if err := os.Mkdir(dir, 0700); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if err := os.Chmod(dir, 0770); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | intent, err := NewRetirementJournal("fence", plan9Fix1Inventory()) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | r := TypedRetirement{JournalDir: dir, Authority: &cleanupAuthority{}, Filesystem: &rootParker{}, Rows: &rowRetirer{rows: map[string]bool{"route:route-1": true}}, Ack: func(context.Context, RetirementJournal) error { return nil }} |
| 74 | if err = r.Run(t.Context(), hostguard.FenceTarget{Host: "app.example", Incarnation: "inc", Stamp: 7}, intent); err == nil { |
| 75 | t.Fatal("group-writable journal directory accepted") |
| 76 | } |
| 77 | if entries, _ := os.ReadDir(dir); len(entries) != 0 { |
| 78 | t.Fatalf("root wrote into a shared directory: %v", entries) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // C-1: cold handover recovery reads only a trusted, unlinked directory. |
| 83 | func TestPlan9Fix1ColdRecoverRefusesSymlinkedJournalDir(t *testing.T) { |
| 84 | base := t.TempDir() |
| 85 | victim := filepath.Join(base, "victim") |
| 86 | if err := os.Mkdir(victim, 0700); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | link := filepath.Join(base, "cold") |
| 90 | if err := os.Symlink(victim, link); err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | if err := (ColdHandover{JournalDir: link}).Recover(t.Context()); !errors.Is(err, ErrTypedJournal) { |
| 94 | t.Fatalf("cold recovery followed a symlinked journal directory: %v", err) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func plan9Fix1Legacy(t *testing.T, in LaunchInventory) string { |
| 99 | t.Helper() |
| 100 | legacy := t.TempDir() |
| 101 | old, err := OpenLaunchJournal(filepath.Join(legacy, "launches.db")) |
| 102 | if err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | if err = old.Create(t.Context(), in); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if err = old.Close(); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | inventory, err := OpenStopInventoryDB(filepath.Join(legacy, "inventory.db")) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | if err = inventory.WriteStopInventory(t.Context(), StopInventory{Complete: true, Host: in.Target.Host, Incarnation: in.Target.Incarnation, InstallationStamp: in.Target.Stamp, Box: "box", Enrollment: "enrollment", DirectoryRevision: "1"}); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if err = inventory.Close(); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | return legacy |
| 122 | } |
| 123 | |
| 124 | func plan9Fix1RawLegacy(t *testing.T, legacy, stmt string, args ...any) { |
| 125 | t.Helper() |
| 126 | db, err := sql.Open("sqlite", "file:"+url.PathEscape(filepath.Join(legacy, "launches.db"))+"?_pragma=busy_timeout(5000)") |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | defer db.Close() |
| 131 | if _, err = db.Exec(stmt, args...); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | var plan9Fix1Freeze = func(context.Context) (func(), error) { return func() {}, nil } |
| 137 | |
| 138 | func plan9Fix1Accept(context.Context, LaunchInventory, StopInventory, StopKernelJournal) error { |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // M3: rows a rolled-back binary writes to the legacy journal after the marker |
| 143 | // must not be silently ignored on roll forward. |
| 144 | func TestPlan9Fix1MigrationRefusesLegacyRowsWrittenAfterMarker(t *testing.T) { |
| 145 | s, _, in := journalFixture(t) |
| 146 | legacy := plan9Fix1Legacy(t, in) |
| 147 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | // Unchanged legacy files: a restart needs neither quiescence nor proof. |
| 151 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", nil, nil); err != nil { |
| 152 | t.Fatalf("unchanged legacy refused: %v", err) |
| 153 | } |
| 154 | // The old binary runs again and journals another launch. |
| 155 | old, err := OpenLaunchJournal(filepath.Join(legacy, "launches.db")) |
| 156 | if err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | second := in |
| 160 | second.Target.Session = 2 |
| 161 | if err = old.Create(t.Context(), second); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | if err = old.Close(); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | if err = s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); !errors.Is(err, ErrChanged) { |
| 168 | t.Fatalf("legacy rows written after the marker were ignored: %v", err) |
| 169 | } |
| 170 | if _, err = s.Launches.Read(t.Context(), second.Target); !errors.Is(err, sql.ErrNoRows) { |
| 171 | t.Fatalf("unproven post-marker row imported: %v", err) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // M4: agent-supplied SQLite is opened with an untrusted schema, every imported |
| 176 | // field is validated, and only canonical re-marshalled bodies are stored. |
| 177 | func TestPlan9Fix1MigrationRejectsUntrustedSchemaAndFields(t *testing.T) { |
| 178 | t.Run("trigger", func(t *testing.T) { |
| 179 | s, _, in := journalFixture(t) |
| 180 | legacy := plan9Fix1Legacy(t, in) |
| 181 | plan9Fix1RawLegacy(t, legacy, "CREATE TRIGGER planted AFTER INSERT ON launches BEGIN SELECT 1; END") |
| 182 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); err == nil { |
| 183 | t.Fatal("legacy schema with a trigger accepted") |
| 184 | } |
| 185 | }) |
| 186 | t.Run("unknown-field", func(t *testing.T) { |
| 187 | s, _, in := journalFixture(t) |
| 188 | legacy := plan9Fix1Legacy(t, in) |
| 189 | raw, _ := json.Marshal(in) |
| 190 | planted := strings.Replace(string(raw), "{", `{"Planted":true,`, 1) |
| 191 | plan9Fix1RawLegacy(t, legacy, "UPDATE launches SET body=?", []byte(planted)) |
| 192 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); err == nil { |
| 193 | t.Fatal("legacy body with an unknown field accepted") |
| 194 | } |
| 195 | }) |
| 196 | t.Run("invalid-group", func(t *testing.T) { |
| 197 | s, _, in := journalFixture(t) |
| 198 | bad := in |
| 199 | bad.Groups = []LaunchGroup{{RelativePath: "../../etc", Device: 1, Inode: 2}} |
| 200 | legacy := plan9Fix1Legacy(t, in) |
| 201 | raw, _ := json.Marshal(bad) |
| 202 | plan9Fix1RawLegacy(t, legacy, "UPDATE launches SET body=?", raw) |
| 203 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); err == nil { |
| 204 | t.Fatal("legacy launch naming an escaping cgroup accepted") |
| 205 | } |
| 206 | }) |
| 207 | t.Run("canonical", func(t *testing.T) { |
| 208 | s, _, in := journalFixture(t) |
| 209 | legacy := plan9Fix1Legacy(t, in) |
| 210 | raw, _ := json.MarshalIndent(in, "", " ") |
| 211 | plan9Fix1RawLegacy(t, legacy, "UPDATE launches SET body=?", raw) |
| 212 | if err := s.MigrateLegacy(t.Context(), legacy, "box", "enrollment", plan9Fix1Freeze, plan9Fix1Accept); err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | var stored []byte |
| 216 | if err := s.Launches.db.QueryRow("SELECT body FROM launches").Scan(&stored); err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | want, _ := json.Marshal(in) |
| 220 | if string(stored) != string(want) { |
| 221 | t.Fatalf("raw legacy body imported verbatim:\n%s", stored) |
| 222 | } |
| 223 | }) |
| 224 | } |
| 225 | |
| 226 | // M6: two distinct calls with identical payloads are distinct requests, and |
| 227 | // an uncertain request keeps its identity for an explicit replay. |
| 228 | func TestPlan9Fix1JournalDistinctCallsDoNotMerge(t *testing.T) { |
| 229 | var ids []string |
| 230 | client, _ := RemoteJournals(func(_ context.Context, q JournalRequest) (JournalResponse, error) { |
| 231 | ids = append(ids, q.ID) |
| 232 | return JournalResponse{V: 1, ID: q.ID}, nil |
| 233 | }) |
| 234 | target := hostguard.FenceTarget{Host: "app.example", Incarnation: "inc", Stamp: 1, Session: 1, Box: "box", Enrollment: "enrollment", BootID: "boot", ControllerID: "controller"} |
| 235 | for i := 0; i < 2; i++ { |
| 236 | if err := client.RecordProducer(t.Context(), target, "p"); err != nil { |
| 237 | t.Fatal(err) |
| 238 | } |
| 239 | } |
| 240 | if len(ids) != 2 || ids[0] == ids[1] { |
| 241 | t.Fatalf("distinct calls merged into one request: %v", ids) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | func TestPlan9Fix1JournalReceiptsPruneAndExpiredRequestsStayUncertain(t *testing.T) { |
| 246 | s, _, in := journalFixture(t) |
| 247 | now := time.Now() |
| 248 | s.Now = func() time.Time { return now } |
| 249 | call := func(ctx context.Context, q JournalRequest) (JournalResponse, error) { return s.Execute(ctx, "peer", q) } |
| 250 | client, _ := RemoteJournals(call) |
| 251 | if err := client.Create(t.Context(), in); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | count := func() int { |
| 255 | var n int |
| 256 | if err := s.Launches.db.QueryRow("SELECT count(*) FROM journal_requests").Scan(&n); err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | return n |
| 260 | } |
| 261 | if count() != 1 { |
| 262 | t.Fatal("mutation receipt missing") |
| 263 | } |
| 264 | // Past retention, the next mutation prunes the settled receipt. |
| 265 | now = now.Add(JournalReceiptRetention + time.Minute) |
| 266 | if err := client.RecordProducer(t.Context(), in.Target, "p1"); !errors.Is(err, ErrJournalUncertain) { |
| 267 | // The client's own clock issued this request; root's clock is ahead |
| 268 | // by more than the execution window, so root refuses to execute it. |
| 269 | t.Fatalf("request outside root's execution window executed: %v", err) |
| 270 | } |
| 271 | q := JournalRequest{Op: "producer", Target: in.Target, Producer: "p1"} |
| 272 | q, err := journalPrepare(q, now) |
| 273 | if err != nil { |
| 274 | t.Fatal(err) |
| 275 | } |
| 276 | if _, err := s.Execute(t.Context(), "peer", q); err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | if n := count(); n != 1 { |
| 280 | t.Fatalf("settled receipts were not pruned: %d", n) |
| 281 | } |
| 282 | // A request older than the execution window never executes fresh. |
| 283 | stale, err := journalPrepare(JournalRequest{Op: "producer", Target: in.Target, Producer: "p2"}, now.Add(-JournalRequestWindow-time.Minute)) |
| 284 | if err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | out, err := s.Execute(t.Context(), "peer", stale) |
| 288 | if !errors.Is(err, ErrJournalUncertain) { |
| 289 | t.Fatalf("expired request executed: %+v %v", out, err) |
| 290 | } |
| 291 | got, err := s.Launches.Read(t.Context(), in.Target) |
| 292 | if err != nil || fmt.Sprint(got.ProducerIDs) != "[p1]" { |
| 293 | t.Fatalf("expired request changed the journal: %+v %v", got, err) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // M11: a nil journal refuses instead of panicking. |
| 298 | func TestPlan9Fix1NilJournalsRefuse(t *testing.T) { |
| 299 | var j *LaunchJournal |
| 300 | var inv *StopInventoryDB |
| 301 | target := hostguard.FenceTarget{Host: "app.example", Incarnation: "inc", Stamp: 1} |
| 302 | calls := map[string]func() error{ |
| 303 | "HasHost": func() error { _, err := j.HasHost(t.Context(), "app.example"); return err }, |
| 304 | "HasInstallationEffects": func() error { _, err := j.HasInstallationEffects(t.Context(), "app.example", "inc", 1); return err }, |
| 305 | "IntendRole": func() error { return j.IntendRole(t.Context(), LaunchReservation{}) }, |
| 306 | "Create": func() error { return j.Create(t.Context(), LaunchInventory{}) }, |
| 307 | "Prelaunch": func() error { return j.Prelaunch(t.Context(), LaunchInventory{}) }, |
| 308 | "SessionLaunches": func() error { _, err := j.SessionLaunches(t.Context(), "a", "b", 1, 1, "c", "d"); return err }, |
| 309 | "Read": func() error { _, err := j.Read(t.Context(), target); return err }, |
| 310 | "Reserve": func() error { return j.Reserve(t.Context(), LaunchReservation{}) }, |
| 311 | "Started": func() error { return j.Started(t.Context(), LaunchReservation{}, 1, "1") }, |
| 312 | "Settled": func() error { return j.Settled(t.Context(), LaunchReservation{}) }, |
| 313 | "RecordFile": func() error { return j.RecordFile(t.Context(), target, LaunchFile{Device: 1, Inode: 1}) }, |
| 314 | "RecordProducer": func() error { return j.RecordProducer(t.Context(), target, "p") }, |
| 315 | "CompleteProducer": func() error { _, err := j.CompleteProducer(t.Context(), target, "p", ProducerCompletion{}); return err }, |
| 316 | "Close": func() error { return j.Close() }, |
| 317 | "WriteStopInventory": func() error { return inv.WriteStopInventory(t.Context(), StopInventory{}) }, |
| 318 | "ReadStopInventory": func() error { _, err := inv.ReadStopInventory(t.Context(), "app.example"); return err }, |
| 319 | "ReadExactStopInventory": func() error { _, err := inv.ReadExactStopInventory(t.Context(), target); return err }, |
| 320 | "InventoryClose": func() error { return inv.Close() }, |
| 321 | } |
| 322 | for name, call := range calls { |
| 323 | t.Run(name, func(t *testing.T) { |
| 324 | defer func() { |
| 325 | if r := recover(); r != nil { |
| 326 | t.Fatalf("nil journal panicked: %v", r) |
| 327 | } |
| 328 | }() |
| 329 | if err := call(); name != "Close" && name != "InventoryClose" && !errors.Is(err, ErrUnsupported) { |
| 330 | t.Fatalf("nil journal did not refuse: %v", err) |
| 331 | } |
| 332 | }) |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // M12: the inventory database waits for a concurrent writer from its first |
| 337 | // statement instead of failing with SQLITE_BUSY. |
| 338 | func TestPlan9Fix1StopInventoryOpenWaitsForBusyDatabase(t *testing.T) { |
| 339 | path := filepath.Join(t.TempDir(), "inventory.db") |
| 340 | holder, err := sql.Open("sqlite", "file:"+url.PathEscape(path)) |
| 341 | if err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | defer holder.Close() |
| 345 | conn, err := holder.Conn(t.Context()) |
| 346 | if err != nil { |
| 347 | t.Fatal(err) |
| 348 | } |
| 349 | defer conn.Close() |
| 350 | if _, err = conn.ExecContext(t.Context(), "BEGIN EXCLUSIVE"); err != nil { |
| 351 | t.Fatal(err) |
| 352 | } |
| 353 | if _, err = conn.ExecContext(t.Context(), "CREATE TABLE held(x)"); err != nil { |
| 354 | t.Fatal(err) |
| 355 | } |
| 356 | released := make(chan error, 1) |
| 357 | go func() { |
| 358 | time.Sleep(300 * time.Millisecond) |
| 359 | _, err := conn.ExecContext(context.Background(), "COMMIT") |
| 360 | released <- err |
| 361 | }() |
| 362 | inventory, err := OpenStopInventoryDB(path) |
| 363 | if err != nil { |
| 364 | t.Fatalf("open did not wait for the busy database: %v", err) |
| 365 | } |
| 366 | inventory.Close() |
| 367 | if err = <-released; err != nil { |
| 368 | t.Fatal(err) |
| 369 | } |
| 370 | } |
| 371 | |