activator: every binary launch asks the runtime first, placed children included; a guard holds the cgroup starts to the two checked funnels
Plan 3 N1 round 1 (Astra P1, INV-1/F2 family): spawnInCgroupAtTier's child branch reached Cgroups.StartChildGated with no runtime check, so a flat or foreign rootfs or unknown-runtime row's seed (role-seed) or legacy sidecar (role-sidecar) could run its artifact. spawnInCgroupAtTier now asks refuseRuntimeBinaryLaunch before either branch; Seed, SeedExe and StartSidecarGen ask it before anything is written. TestBinaryLaunchesAskTheRuntimeFirst holds every cgroup start to spawnInCgroupAtTier and spawnInCgroupGated, each refusal-first, starts an exec.Cmd only for systemctl, and builds other commands only in a funnel's build closure. It found spawnClaimedBefore's unchecked manager.Start, a claims helper with no production caller; it and spawnClaimed are deleted with their two tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+557
−51
internal/activator/backend_exec.go+13 −2internal/activator/binary_launch_guard_test.go+356 −0internal/activator/claim_isolation_test.go+0 −14internal/activator/claimspawn.go+3 −17internal/activator/claimspawn_test.go+0 −17internal/activator/containment.go+10 −0internal/activator/genbackend.go+5 −1internal/activator/rootfs_staged_test.go+61 −0internal/activator/testdata/binary_launch_guard/clean.go+29 −0internal/activator/testdata/binary_launch_guard/cmdstart.go+8 −0internal/activator/testdata/binary_launch_guard/direct.go+14 −0internal/activator/testdata/binary_launch_guard/funnels.go+21 −0internal/activator/testdata/binary_launch_guard/loosecmd.go+12 −0internal/activator/testdata/binary_launch_guard/methodvalue.go+10 −0internal/activator/testdata/binary_launch_guard/raw.go+15 −0
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index bb8efb6e3..d5dccd2e5 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -481,7 +481,7 @@ func (b *ExecBackend) spawnInCgroupGated(rt registry.Route, tier cgroup.Limit, b |
| return nil, nil, refuseRootfs(fmt.Errorf("%s: %s", rt.Host, rootfsNotNestedText)) |
| } |
| // Nor does a row with a runtime this agent does not know (Plan 3 N1). |
| - if err := refuseUnknownRuntime(rt); err != nil { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| return nil, nil, err |
| } |
| if tier == (cgroup.Limit{}) { |
| @@ -930,7 +930,14 @@ var restoreReplica = lease.Restore |
| // Seed dispatches by layout, the same way Start does: a nested row seeds |
| // through its own launch file and unit, a fenced or conflicting row |
| // refuses, and a flat or foreign row seeds through today's exec child. |
| +// |
| +// The runtime first (Plan 3 N1): a seed is a binary launch, so a row with |
| +// a runtime is refused before the token, the instance key or the seed's |
| +// obligation is written. |
| func (b *ExecBackend) Seed(ctx context.Context, rt registry.Route) error { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return err |
| + } |
| p := b.pathsFor(rt) |
| switch p.layout { |
| case LayoutNested: |
| @@ -947,8 +954,12 @@ func (b *ExecBackend) Seed(ctx context.Context, rt registry.Route) error { |
| // row execs that file by path. A nested row asks root to run the seed unit |
| // on it, which root validates and binds in place of the live link. The |
| // seed runs at id's own tier (GenTier, ruling R-TIER), never the tier of |
| -// whatever the live link names by then. |
| +// whatever the live link names by then. A row with a runtime is refused |
| +// first, as Seed refuses it. |
| func (b *ExecBackend) SeedExe(ctx context.Context, rt registry.Route, id release.StagedIdentity) error { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return err |
| + } |
| if err := release.VerifyStaged(id); err != nil { |
| return fmt.Errorf("%s: not seeded with %s: %w", rt.Host, id.Version, err) |
| } |
diff --git a/internal/activator/binary_launch_guard_test.go b/internal/activator/binary_launch_guard_test.go| new file mode 100644 |
| index 000000000..2c00dcba4 |
| --- /dev/null |
| +++ b/internal/activator/binary_launch_guard_test.go |
| @@ -0,0 +1,356 @@ |
| +package activator |
| + |
| +// INV-1, the binary-launch half (rootfs apps Plan 3 N1, round 1; Task 10's |
| +// finding F2 and Astra's N1 P1, two rounds of one fault family): every |
| +// binary launch rejects a row with a runtime, and it does so in one place. |
| +// |
| +// The launch primitives are the cgroup manager's starts (Start, |
| +// StartGated, StartChild, StartChildGated), a process start on an exec.Cmd |
| +// (Start, Run, Output, CombinedOutput) and the raw process starts of os, |
| +// syscall and x/sys/unix. In this package's own code: |
| +// |
| +// - the cgroup manager's starts appear only in the launch funnels |
| +// (launchFunnels), and each funnel asks refuseRuntimeBinaryLaunch, and |
| +// returns on its refusal, before its first statement that names one; |
| +// - an exec.Cmd is started directly only for systemctl (a literal |
| +// "systemctl" command name), never for anything a row supplies; |
| +// - an exec.Cmd naming anything but systemctl is only built inside a |
| +// function literal handed to a funnel or one of its wrappers |
| +// (launchFunnelCallers), which is where the funnel starts it; |
| +// - the raw process starts are not used at all. |
| +// |
| +// Fixtures under testdata/binary_launch_guard are laid over the package |
| +// (as the INV-1 verb guard's are) and each must be reported for what it |
| +// does; in a fixture, a function named zzLaunchFunnel* stands for a funnel. |
| + |
| +import ( |
| + "fmt" |
| + "go/ast" |
| + "go/token" |
| + "go/types" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "sync" |
| + "testing" |
| + |
| + "golang.org/x/tools/go/packages" |
| +) |
| + |
| +// launchFunnels are the only functions that may name the cgroup manager's |
| +// starts. Rows are only added, each with its refusal first. |
| +var launchFunnels = []string{"spawnInCgroupAtTier", "spawnInCgroupGated"} |
| + |
| +// launchFunnelCallers are the funnels and their thin wrappers, whose build |
| +// closure is where a non-systemctl exec.Cmd may be made. |
| +var launchFunnelCallers = []string{"spawnInCgroup", "spawnInCgroupAt", "spawnInCgroupAtTier", "spawnInCgroupGated"} |
| + |
| +// launchRefusal is what each funnel asks first. |
| +const launchRefusal = "refuseRuntimeBinaryLaunch" |
| + |
| +// launchPrimitives: package path -> the functions, or Type.Method names, |
| +// that start a process. |
| +var launchPrimitives = map[string][]string{ |
| + "github.com/carlosframework/platform/internal/cgroup": {"Manager.Start", "Manager.StartGated", "Manager.StartChild", "Manager.StartChildGated"}, |
| + "os/exec": {"Cmd.Start", "Cmd.Run", "Cmd.Output", "Cmd.CombinedOutput"}, |
| + "os": {"StartProcess"}, |
| + "syscall": {"ForkExec", "Exec", "StartProcess"}, |
| + "golang.org/x/sys/unix": {"Exec"}, |
| +} |
| + |
| +const launchGuardFixtureDir = "testdata/binary_launch_guard" |
| +const launchGuardFixturePrefix = "zz_launch_guard_fixture_" |
| + |
| +// launchGuardFixtures: fixture -> what it must be reported for ("" for a |
| +// fixture that must pass). Rows are only added. |
| +var launchGuardFixtures = map[string][]string{ |
| + "clean.go": nil, |
| + "direct.go": {"zzDirectChild: launch primitive cgroup.Manager.StartChildGated outside the launch funnels"}, |
| + "methodvalue.go": {"zzMethodValue: launch primitive cgroup.Manager.StartGated outside the launch funnels"}, |
| + "raw.go": {"zzRaw: launch primitive os.StartProcess", "zzRaw: launch primitive syscall.ForkExec"}, |
| + "cmdstart.go": {"zzCmdStart: launch primitive exec.Cmd.Start on something other than systemctl", "zzCmdStart: exec.Command builds a process outside a launch funnel's build closure"}, |
| + "loosecmd.go": {"zzLooseCmd: exec.CommandContext builds a process outside a launch funnel's build closure"}, |
| + "funnels.go": {"zzLaunchFunnelLate: reaches a launch primitive before refuseRuntimeBinaryLaunch", "zzLaunchFunnelNone: reaches a launch primitive before refuseRuntimeBinaryLaunch"}, |
| +} |
| + |
| +var loadLaunchGuardPackage = sync.OnceValues(func() (*packages.Package, error) { |
| + dir, err := os.Getwd() |
| + if err != nil { |
| + return nil, err |
| + } |
| + fixtures, err := filepath.Glob(filepath.Join(launchGuardFixtureDir, "*.go")) |
| + if err != nil { |
| + return nil, err |
| + } |
| + overlay := map[string][]byte{} |
| + for _, f := range fixtures { |
| + src, err := os.ReadFile(f) |
| + if err != nil { |
| + return nil, err |
| + } |
| + overlay[filepath.Join(dir, launchGuardFixturePrefix+filepath.Base(f))] = src |
| + } |
| + cfg := &packages.Config{Dir: dir, Overlay: overlay, |
| + Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedImports} |
| + pkgs, err := packages.Load(cfg, ".") |
| + if err != nil { |
| + return nil, err |
| + } |
| + if len(pkgs) != 1 || len(pkgs[0].Errors) > 0 { |
| + return nil, packagesErr(pkgs) |
| + } |
| + return pkgs[0], nil |
| +}) |
| + |
| +func packagesErr(pkgs []*packages.Package) error { |
| + var msgs []string |
| + for _, p := range pkgs { |
| + for _, e := range p.Errors { |
| + msgs = append(msgs, e.Error()) |
| + } |
| + } |
| + return fmt.Errorf("loaded %d packages, want this one without errors: %s", len(pkgs), strings.Join(msgs, "; ")) |
| +} |
| + |
| +// launchPrimitiveName is obj's launch primitive name ("cgroup.Manager.Start", |
| +// "os.StartProcess"), or "". |
| +func launchPrimitiveName(obj types.Object) string { |
| + fn, ok := obj.(*types.Func) |
| + if !ok || fn.Pkg() == nil { |
| + return "" |
| + } |
| + name := fn.Name() |
| + if sig, ok := fn.Type().(*types.Signature); ok && sig.Recv() != nil { |
| + t := sig.Recv().Type() |
| + if p, ok := t.(*types.Pointer); ok { |
| + t = p.Elem() |
| + } |
| + n, ok := t.(*types.Named) |
| + if !ok { |
| + return "" |
| + } |
| + name = n.Obj().Name() + "." + name |
| + } |
| + if !slices.Contains(launchPrimitives[fn.Pkg().Path()], name) { |
| + return "" |
| + } |
| + return fn.Pkg().Name() + "." + name |
| +} |
| + |
| +// isExecCommand reports whether call is exec.Command or exec.CommandContext, |
| +// and its command-name argument. |
| +func isExecCommand(info *types.Info, call *ast.CallExpr) (string, ast.Expr, bool) { |
| + sel, ok := call.Fun.(*ast.SelectorExpr) |
| + if !ok { |
| + return "", nil, false |
| + } |
| + fn, ok := info.Uses[sel.Sel].(*types.Func) |
| + if !ok || fn.Pkg() == nil || fn.Pkg().Path() != "os/exec" { |
| + return "", nil, false |
| + } |
| + switch fn.Name() { |
| + case "Command": |
| + if len(call.Args) > 0 { |
| + return fn.Name(), call.Args[0], true |
| + } |
| + case "CommandContext": |
| + if len(call.Args) > 1 { |
| + return fn.Name(), call.Args[1], true |
| + } |
| + } |
| + return "", nil, false |
| +} |
| + |
| +func isSystemctl(e ast.Expr) bool { |
| + lit, ok := e.(*ast.BasicLit) |
| + return ok && lit.Kind == token.STRING && lit.Value == `"systemctl"` |
| +} |
| + |
| +// calleeName is the name of the function or method call invokes in this |
| +// package, or "". |
| +func calleeName(info *types.Info, call *ast.CallExpr) string { |
| + var id *ast.Ident |
| + switch f := call.Fun.(type) { |
| + case *ast.Ident: |
| + id = f |
| + case *ast.SelectorExpr: |
| + id = f.Sel |
| + default: |
| + return "" |
| + } |
| + if fn, ok := info.Uses[id].(*types.Func); ok { |
| + return fn.Name() |
| + } |
| + return "" |
| +} |
| + |
| +// launchGuardProblems is every rule broken in f. isFunnel names a funnel. |
| +func launchGuardProblems(info *types.Info, f *ast.File, isFunnel func(string) bool) []string { |
| + var out []string |
| + for _, decl := range f.Decls { |
| + fd, ok := decl.(*ast.FuncDecl) |
| + if !ok || fd.Body == nil { |
| + continue |
| + } |
| + name := fd.Name.Name |
| + funnel := isFunnel(name) |
| + var stack []ast.Node |
| + ast.Inspect(fd.Body, func(n ast.Node) bool { |
| + if n == nil { |
| + stack = stack[:len(stack)-1] |
| + return true |
| + } |
| + stack = append(stack, n) |
| + switch x := n.(type) { |
| + case *ast.Ident: |
| + p := launchPrimitiveName(info.Uses[x]) |
| + if p == "" { |
| + return true |
| + } |
| + if strings.HasPrefix(p, "exec.Cmd.") { |
| + // x is the Sel of a selector; its receiver must be a |
| + // direct exec.Command("systemctl", ...). |
| + if len(stack) >= 2 { |
| + if sel, ok := stack[len(stack)-2].(*ast.SelectorExpr); ok && sel.Sel == x { |
| + if call, ok := sel.X.(*ast.CallExpr); ok { |
| + if _, arg, ok := isExecCommand(info, call); ok && isSystemctl(arg) { |
| + return true |
| + } |
| + } |
| + } |
| + } |
| + out = append(out, name+": launch primitive "+p+" on something other than systemctl") |
| + return true |
| + } |
| + if !funnel || !strings.HasPrefix(p, "cgroup.") { |
| + out = append(out, name+": launch primitive "+p+" outside the launch funnels") |
| + } |
| + case *ast.CallExpr: |
| + fn, arg, ok := isExecCommand(info, x) |
| + if !ok || isSystemctl(arg) { |
| + return true |
| + } |
| + // Inside a function literal that is an argument of a call |
| + // to a funnel or a wrapper? |
| + inside := false |
| + for i := len(stack) - 2; i >= 1 && !inside; i-- { |
| + lit, ok := stack[i].(*ast.FuncLit) |
| + if !ok { |
| + continue |
| + } |
| + if call, ok := stack[i-1].(*ast.CallExpr); ok && slices.Contains(launchFunnelCallers, calleeName(info, call)) && slices.ContainsFunc(call.Args, func(a ast.Expr) bool { return a == lit }) { |
| + inside = true |
| + } |
| + } |
| + if !inside { |
| + out = append(out, name+": exec."+fn+" builds a process outside a launch funnel's build closure") |
| + } |
| + } |
| + return true |
| + }) |
| + if funnel { |
| + if p := funnelOrderProblem(info, fd); p != "" { |
| + out = append(out, name+": "+p) |
| + } |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// funnelOrderProblem checks that fd asks the refusal, and returns on it, in |
| +// a top-level statement before its first top-level statement naming a |
| +// launch primitive. A funnel that names none is reported too: a guard row |
| +// with nothing to guard is stale. |
| +func funnelOrderProblem(info *types.Info, fd *ast.FuncDecl) string { |
| + refused := false |
| + for _, st := range fd.Body.List { |
| + if !refused && isRefusalStmt(info, st) { |
| + refused = true |
| + continue |
| + } |
| + names := false |
| + ast.Inspect(st, func(n ast.Node) bool { |
| + if id, ok := n.(*ast.Ident); ok && strings.HasPrefix(launchPrimitiveName(info.Uses[id]), "cgroup.") { |
| + names = true |
| + } |
| + return !names |
| + }) |
| + if names { |
| + if !refused { |
| + return "reaches a launch primitive before " + launchRefusal |
| + } |
| + return "" |
| + } |
| + } |
| + return "names no launch primitive (a stale funnel row)" |
| +} |
| + |
| +// isRefusalStmt: `if err := refuseRuntimeBinaryLaunch(...); err != nil { |
| +// return ... }`. |
| +func isRefusalStmt(info *types.Info, st ast.Stmt) bool { |
| + ifs, ok := st.(*ast.IfStmt) |
| + if !ok || ifs.Init == nil || len(ifs.Body.List) == 0 { |
| + return false |
| + } |
| + as, ok := ifs.Init.(*ast.AssignStmt) |
| + if !ok || len(as.Rhs) != 1 { |
| + return false |
| + } |
| + call, ok := as.Rhs[0].(*ast.CallExpr) |
| + if !ok || calleeName(info, call) != launchRefusal { |
| + return false |
| + } |
| + _, ret := ifs.Body.List[len(ifs.Body.List)-1].(*ast.ReturnStmt) |
| + return ret |
| +} |
| + |
| +func TestBinaryLaunchesAskTheRuntimeFirst(t *testing.T) { |
| + pkg, err := loadLaunchGuardPackage() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + isFunnel := func(name string) bool { return slices.Contains(launchFunnels, name) } |
| + seen := map[string]bool{} |
| + for _, f := range pkg.Syntax { |
| + base := filepath.Base(pkg.Fset.Position(f.Package).Filename) |
| + if strings.HasPrefix(base, launchGuardFixturePrefix) || strings.HasSuffix(base, "_test.go") { |
| + continue |
| + } |
| + for _, p := range launchGuardProblems(pkg.TypesInfo, f, isFunnel) { |
| + t.Errorf("%s: %s", base, p) |
| + } |
| + for _, d := range f.Decls { |
| + if fd, ok := d.(*ast.FuncDecl); ok && isFunnel(fd.Name.Name) { |
| + seen[fd.Name.Name] = true |
| + } |
| + } |
| + } |
| + for _, name := range launchFunnels { |
| + if !seen[name] { |
| + t.Errorf("the funnel %s is no longer declared", name) |
| + } |
| + } |
| + files := map[string]*ast.File{} |
| + for _, f := range pkg.Syntax { |
| + files[filepath.Base(pkg.Fset.Position(f.Package).Filename)] = f |
| + } |
| + fixtureFunnel := func(name string) bool { return strings.HasPrefix(name, "zzLaunchFunnel") } |
| + for fixture, want := range launchGuardFixtures { |
| + t.Run(fixture, func(t *testing.T) { |
| + f := files[launchGuardFixturePrefix+fixture] |
| + if f == nil { |
| + t.Fatalf("fixture %s was not loaded", fixture) |
| + } |
| + got := launchGuardProblems(pkg.TypesInfo, f, fixtureFunnel) |
| + if len(want) == 0 && len(got) > 0 { |
| + t.Errorf("a clean fixture was reported:\n%s", strings.Join(got, "\n")) |
| + } |
| + for _, w := range want { |
| + if !slices.ContainsFunc(got, func(p string) bool { return strings.Contains(p, w) }) { |
| + t.Errorf("the guard did not report %q; it reported:\n%s", w, strings.Join(got, "\n")) |
| + } |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/activator/claim_isolation_test.go b/internal/activator/claim_isolation_test.go| index 65acfb8b4..c0dfb9fc3 100644 |
| --- a/internal/activator/claim_isolation_test.go |
| +++ b/internal/activator/claim_isolation_test.go |
| @@ -48,17 +48,3 @@ func TestClaimRoleLaunchRefusesWithoutIsolatedRoot(t *testing.T) { |
| }) |
| } |
| } |
| - |
| -func TestClaimSpawnDisabledKeepsLegacyBuild(t *testing.T) { |
| - calls, builds := 0, 0 |
| - dir := t.TempDir() |
| - cmd, prepared, err := spawnClaimedBefore(t.Context(), false, nil, "tenant.test", cgroup.TierDefault, claimSpawnWork{true, &calls}, hostwrite.Identity{}, func() *exec.Cmd { |
| - builds++ |
| - cmd := exec.Command(filepath.Join(dir, "absent-executable")) |
| - cmd.Dir, cmd.Env = dir, []string{} |
| - return cmd |
| - }, func() error { t.Fatal("disabled helper invoked claim callback"); return nil }) |
| - if builds != 1 || calls != 0 || cmd == nil || prepared != nil || !errors.Is(err, os.ErrNotExist) { |
| - t.Fatalf("legacy dispatch changed: builds=%d guards=%d cmd=%v prepared=%v err=%v", builds, calls, cmd, prepared, err) |
| - } |
| -} |
diff --git a/internal/activator/claimspawn.go b/internal/activator/claimspawn.go| index 09a6c7a83..2600b2b36 100644 |
| --- a/internal/activator/claimspawn.go |
| +++ b/internal/activator/claimspawn.go |
| @@ -8,27 +8,13 @@ import ( |
| "github.com/carlosframework/platform/internal/hostwrite" |
| ) |
| -func spawnClaimed(ctx context.Context, enabled bool, manager *cgroup.Manager, host string, limit cgroup.Limit, selected hostwrite.Work, identity hostwrite.Identity, build func() *exec.Cmd) (*exec.Cmd, *cgroup.Prepared, error) { |
| - return spawnClaimedBefore(ctx, enabled, manager, host, limit, selected, identity, build, nil) |
| -} |
| - |
| -func spawnClaimedBefore(ctx context.Context, enabled bool, manager *cgroup.Manager, host string, limit cgroup.Limit, selected hostwrite.Work, identity hostwrite.Identity, build func() *exec.Cmd, before func() error) (*exec.Cmd, *cgroup.Prepared, error) { |
| - if !enabled { |
| - cmd, err := manager.Start(host, limit, build) |
| - return cmd, nil, err |
| - } |
| - // Main's isolated root launcher does not yet consume claim reservations. |
| - // Keep this old helper closed even if a future caller reintroduces it. |
| - return nil, nil, ErrClaimIsolationUnavailable |
| -} |
| - |
| // spawnClaimedGen is the claims-on spawn of a generation child under the |
| // entry's parent (zero-downtime spec section 7.3), and it is closed: it |
| // pins nothing, reserves nothing, never builds the command and never |
| // enters the host write. Invalid arguments answer hostwrite.ErrUnavailable; |
| -// every other call answers ErrClaimIsolationUnavailable, like |
| -// spawnClaimedBefore. Main's RoleLaunch.Start is closed too, so routing a |
| -// claimed generation spawn there would not open it either. A later plan |
| +// every other call answers ErrClaimIsolationUnavailable. Main's |
| +// RoleLaunch.Start is closed too, so routing a claimed generation spawn |
| +// there would not open it either. A later plan |
| // needs an isolated root launch protocol that carries the claim |
| // reservations and the final authority check, rather than a re-route or |
| // an in-process path. |
diff --git a/internal/activator/claimspawn_test.go b/internal/activator/claimspawn_test.go| index 0d17f4672..dcbdcbfb0 100644 |
| --- a/internal/activator/claimspawn_test.go |
| +++ b/internal/activator/claimspawn_test.go |
| @@ -7,7 +7,6 @@ import ( |
| "testing" |
| "github.com/carlosframework/platform/internal/cgroup" |
| - "github.com/carlosframework/platform/internal/hostbox" |
| "github.com/carlosframework/platform/internal/hostwrite" |
| ) |
| @@ -23,22 +22,6 @@ func (w claimSpawnWork) With(_ context.Context, _ hostwrite.Identity, fn func() |
| } |
| return fn() |
| } |
| -func TestClaimSpawnNeverFallsBack(t *testing.T) { |
| - calls, builds := 0, 0 |
| - build := func() *exec.Cmd { builds++; return exec.Command("must-not-execute") } |
| - for _, deny := range []bool{true, false} { |
| - cmd, prepared, err := spawnClaimed(context.Background(), true, nil, "shop.example", cgroup.TierDefault, claimSpawnWork{deny, &calls}, hostwrite.Identity{}, build) |
| - if err == nil || cmd != nil || prepared != nil { |
| - t.Fatal(cmd, prepared, err) |
| - } |
| - if !errors.Is(err, hostbox.ErrUnsupported) { |
| - t.Fatal(err) |
| - } |
| - } |
| - if builds != 0 || calls != 0 { |
| - t.Fatal(builds, calls) |
| - } |
| -} |
| // spawnClaimedGen is closed: an invalid argument answers |
| // hostwrite.ErrUnavailable, and every other call answers |
diff --git a/internal/activator/containment.go b/internal/activator/containment.go| index 61fad1bea..bc10818e3 100644 |
| --- a/internal/activator/containment.go |
| +++ b/internal/activator/containment.go |
| @@ -231,7 +231,17 @@ func (b *ExecBackend) spawnInCgroupAt(rt registry.Route, child string, ownTier b |
| // child are prepared, and on the PID-move retry too. A refusal starts |
| // nothing, is cleaned up as a failed start is, and comes back as before's |
| // own error. Nil is the spawn exactly as it was. |
| +// |
| +// The runtime first, before either branch (Plan 3 N1, round 1): a placed |
| +// child (role-seed, role-sidecar, a generation's own child) is a binary |
| +// launch like any other, so a rootfs row and a row with a runtime this |
| +// agent does not know are refused here before anything is built. This and |
| +// spawnInCgroupGated are the only callers of the cgroup manager's launch |
| +// primitives (TestBinaryLaunchesAskTheRuntimeFirst). |
| func (b *ExecBackend) spawnInCgroupAtTier(rt registry.Route, child string, ownTier bool, tier cgroup.Limit, build func() *exec.Cmd, before func() error) (*exec.Cmd, func(), error) { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return nil, nil, err |
| + } |
| if b.Cfg.HostWrites.Enabled { |
| return nil, nil, ErrClaimIsolationUnavailable |
| } |
diff --git a/internal/activator/genbackend.go b/internal/activator/genbackend.go| index 61993e6f5..7d64439a3 100644 |
| --- a/internal/activator/genbackend.go |
| +++ b/internal/activator/genbackend.go |
| @@ -419,8 +419,12 @@ type SidecarGenStarter interface { |
| // generation with none (it was woken on the live link) refuses. It never |
| // reads the live link to decide: a repoint can land between that read and |
| // the start, and after a fallback the live link names a build the instance |
| -// is not serving. The supervisor retries with its backoff. |
| +// is not serving. The supervisor retries with its backoff. A row with a |
| +// runtime is refused first, on every layout, as StartSidecar refuses it. |
| func (b *ExecBackend) StartSidecarGen(ctx context.Context, rt registry.Route, g Gen) (Proc, error) { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return nil, err |
| + } |
| p := b.pathsFor(rt) |
| switch p.layout { |
| case LayoutNested: |
diff --git a/internal/activator/rootfs_staged_test.go b/internal/activator/rootfs_staged_test.go| index 5261b2806..1d6d67bf9 100644 |
| --- a/internal/activator/rootfs_staged_test.go |
| +++ b/internal/activator/rootfs_staged_test.go |
| @@ -769,3 +769,64 @@ func TestBinaryLaunchFunnelsRefuseEveryRuntime(t *testing.T) { |
| t.Fatalf("root was asked %v", calls) |
| } |
| } |
| + |
| +// N1 round 1 (Astra P1, the INV-1/F2 family): a placed child is a binary |
| +// launch too. With generations contained, a flat or foreign row's seed runs |
| +// in role-seed and its legacy sidecar in role-sidecar, both through |
| +// spawnInCgroupAtTier's child branch. A rootfs row and a row with an |
| +// unknown runtime are refused there, and at SeedExe, Seed and |
| +// StartSidecarGen before anything is written: no executable runs, and no |
| +// seed obligation is left that a host-cgroup proof could never discharge. |
| +func TestPlacedChildLaunchesRefuseEveryRuntime(t *testing.T) { |
| + t.Parallel() |
| + for _, layout := range []string{"flat", "foreign"} { |
| + b := testExecBackend(t) |
| + b.RunDirOverride = t.TempDir() |
| + b.GenContainment = true |
| + host := layout + ".example.com" |
| + stageLive(t, b, host, "v1") |
| + rt := registry.Route{Host: host, Kind: "instance", Hibernate: true, Seed: `["seed"]`, |
| + DBPath: filepath.Join(b.DataDir, host+".db"), Socket: filepath.Join(b.RunDirOverride, host+".sock")} |
| + want := LayoutFlat |
| + if layout == "foreign" { |
| + rt.Socket, want = filepath.Join(t.TempDir(), "elsewhere.sock"), LayoutForeign |
| + } |
| + if l, _ := b.Layout(rt); l != want { |
| + t.Fatalf("%s: fixture layout %v", layout, l) |
| + } |
| + mark := filepath.Join(t.TempDir(), "ran") |
| + id := markerScript(t, t.TempDir(), "v2", mark) |
| + for _, runtime := range []string{registry.RuntimeRootfsV1, "rootfs-v9"} { |
| + r := rt |
| + r.Runtime = runtime |
| + name := layout + "/" + runtime |
| + if err := b.SeedExe(context.Background(), r, id); !errors.Is(err, errRootfsRefused) { |
| + t.Errorf("%s: SeedExe = %v, want a refusal", name, err) |
| + } |
| + if err := b.Seed(context.Background(), r); !errors.Is(err, errRootfsRefused) { |
| + t.Errorf("%s: Seed = %v, want a refusal", name, err) |
| + } |
| + if p, err := b.StartSidecarGen(context.Background(), r, Gen{N: 3, Build: "v2", StagedPath: id.StagedPath, Digest: id.Digest}); !errors.Is(err, errRootfsRefused) || p != nil { |
| + t.Errorf("%s: StartSidecarGen = %v, %v; want a refusal", name, p, err) |
| + } |
| + for _, child := range []string{"role-seed", "role-sidecar", "g3"} { |
| + built := false |
| + _, _, err := b.spawnInCgroupAtTier(r, child, false, cgroup.Limit{}, func() *exec.Cmd { built = true; return exec.Command(id.StagedPath) }, nil) |
| + if !errors.Is(err, errRootfsRefused) || built { |
| + t.Errorf("%s: spawnInCgroupAtTier(%s) = %v (built %v), want a refusal before the build", name, child, err, built) |
| + } |
| + } |
| + // Nothing of the seed is written: not its obligation, not even |
| + // the admin token it mints first. |
| + for _, f := range []string{b.pathsFor(r).dbPath + seedUnprovenSuffix, b.pathsFor(r).envFile} { |
| + if _, err := os.Stat(f); !errors.Is(err, os.ErrNotExist) { |
| + t.Errorf("%s: a refused seed wrote %s: %v", name, f, err) |
| + } |
| + } |
| + } |
| + time.Sleep(200 * time.Millisecond) |
| + if _, err := os.Stat(mark); err == nil { |
| + t.Fatalf("%s: a runtime row's staged artifact ran", layout) |
| + } |
| + } |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/clean.go b/internal/activator/testdata/binary_launch_guard/clean.go| new file mode 100644 |
| index 000000000..cd495a069 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/clean.go |
| @@ -0,0 +1,29 @@ |
| +// What the guard accepts: a launch through a funnel, whose build closure |
| +// makes the exec.Cmd; a direct start of systemctl; and a funnel that asks |
| +// refuseRuntimeBinaryLaunch before it names a cgroup start. |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "os/exec" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func (b *ExecBackend) zzCleanLaunch(ctx context.Context, rt registry.Route, exe string) error { |
| + if _, _, err := b.spawnInCgroupAtTier(rt, "role-seed", false, cgroup.Limit{}, func() *exec.Cmd { |
| + return exec.CommandContext(ctx, exe, "--db", rt.DBPath) |
| + }, nil); err != nil { |
| + return err |
| + } |
| + _, err := exec.CommandContext(ctx, "systemctl", "show", "x").Output() |
| + return err |
| +} |
| + |
| +func (b *ExecBackend) zzLaunchFunnelGuarded(rt registry.Route, build func() *exec.Cmd) (*exec.Cmd, error) { |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return nil, err |
| + } |
| + return b.Cgroups.StartChildGated(rt.Host, "role-x", cgroup.Limit{}, cgroup.Limit{}, build, nil) |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/cmdstart.go b/internal/activator/testdata/binary_launch_guard/cmdstart.go| new file mode 100644 |
| index 000000000..b75a48ad8 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/cmdstart.go |
| @@ -0,0 +1,8 @@ |
| +// A row's executable started directly on an exec.Cmd. |
| +package activator |
| + |
| +import "os/exec" |
| + |
| +func zzCmdStart(exe string) error { |
| + return exec.Command(exe).Start() |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/direct.go b/internal/activator/testdata/binary_launch_guard/direct.go| new file mode 100644 |
| index 000000000..969337ff9 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/direct.go |
| @@ -0,0 +1,14 @@ |
| +// A child launch straight into the cgroup manager, past every funnel: the |
| +// shape of Astra's N1 round 1 P1. |
| +package activator |
| + |
| +import ( |
| + "os/exec" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func (b *ExecBackend) zzDirectChild(rt registry.Route, build func() *exec.Cmd) (*exec.Cmd, error) { |
| + return b.Cgroups.StartChildGated(rt.Host, "role-seed", cgroup.Limit{}, cgroup.Limit{}, build, nil) |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/funnels.go b/internal/activator/testdata/binary_launch_guard/funnels.go| new file mode 100644 |
| index 000000000..760f90fcb |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/funnels.go |
| @@ -0,0 +1,21 @@ |
| +// Funnels that reach a cgroup start before the refusal, or with none. |
| +package activator |
| + |
| +import ( |
| + "os/exec" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +func (b *ExecBackend) zzLaunchFunnelLate(rt registry.Route, build func() *exec.Cmd) (*exec.Cmd, error) { |
| + cmd, err := b.Cgroups.StartGated(rt.Host, cgroup.Limit{}, build, nil) |
| + if err := refuseRuntimeBinaryLaunch(rt); err != nil { |
| + return nil, err |
| + } |
| + return cmd, err |
| +} |
| + |
| +func (b *ExecBackend) zzLaunchFunnelNone(rt registry.Route, build func() *exec.Cmd) (*exec.Cmd, error) { |
| + return b.Cgroups.StartGated(rt.Host, cgroup.Limit{}, build, nil) |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/loosecmd.go b/internal/activator/testdata/binary_launch_guard/loosecmd.go| new file mode 100644 |
| index 000000000..3914bd214 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/loosecmd.go |
| @@ -0,0 +1,12 @@ |
| +// An exec.Cmd for a row's executable built outside any funnel's build |
| +// closure, for whoever starts it later. |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "os/exec" |
| +) |
| + |
| +func zzLooseCmd(ctx context.Context, exe string) *exec.Cmd { |
| + return exec.CommandContext(ctx, exe) |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/methodvalue.go b/internal/activator/testdata/binary_launch_guard/methodvalue.go| new file mode 100644 |
| index 000000000..11e42b723 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/methodvalue.go |
| @@ -0,0 +1,10 @@ |
| +// A cgroup start taken as a method value, to be called later. |
| +package activator |
| + |
| +import "github.com/carlosframework/platform/internal/registry" |
| + |
| +func (b *ExecBackend) zzMethodValue(rt registry.Route) { |
| + start := b.Cgroups.StartGated |
| + _ = start |
| + _ = rt |
| +} |
diff --git a/internal/activator/testdata/binary_launch_guard/raw.go b/internal/activator/testdata/binary_launch_guard/raw.go| new file mode 100644 |
| index 000000000..a490c7f33 |
| --- /dev/null |
| +++ b/internal/activator/testdata/binary_launch_guard/raw.go |
| @@ -0,0 +1,15 @@ |
| +// The raw process starts. |
| +package activator |
| + |
| +import ( |
| + "os" |
| + "syscall" |
| +) |
| + |
| +func zzRaw(exe string) error { |
| + if _, err := os.StartProcess(exe, nil, &os.ProcAttr{}); err != nil { |
| + return err |
| + } |
| + _, err := syscall.ForkExec(exe, nil, nil) |
| + return err |
| +} |