| index 2c00dcba4..e8efdbdb4 100644 |
| --- a/internal/activator/binary_launch_guard_test.go |
| +++ b/internal/activator/binary_launch_guard_test.go |
| @@ -5,13 +5,18 @@ package activator |
| // 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: |
| +// StartGated, StartChild, StartChildGated), a strict group's start |
| +// (cgroup.Prepared.Start, strictProc.Start, and any method named Start |
| +// that takes an *exec.Cmd), 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, function bodies and package-level |
| +// initializers alike (Astra N1 r2 P2 2): |
| // |
| -// - 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; |
| +// - the cgroup and strict starts appear only in the launch funnels |
| +// (launchFunnels), and each funnel refuses before its first statement |
| +// that names one: `if err := refuseRuntimeBinaryLaunch(<its route |
| +// parameter>); err != nil { return ..., err }`, the condition, the |
| +// route and the propagation all checked (Astra N1 r2 P2 3); |
| // - 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 |
| @@ -52,7 +57,7 @@ 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"}, |
| + "github.com/carlosframework/platform/internal/cgroup": {"Manager.Start", "Manager.StartGated", "Manager.StartChild", "Manager.StartChildGated", "Prepared.Start"}, |
| "os/exec": {"Cmd.Start", "Cmd.Run", "Cmd.Output", "Cmd.CombinedOutput"}, |
| "os": {"StartProcess"}, |
| "syscall": {"ForkExec", "Exec", "StartProcess"}, |
| @@ -72,6 +77,17 @@ var launchGuardFixtures = map[string][]string{ |
| "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"}, |
| + // Astra N1 round 2, P2 1: the strict starts. |
| + "strict.go": {"zzStrictOutside: launch primitive activator.strictProc.Start outside the launch funnels", |
| + "zzPreparedOutside: launch primitive cgroup.Prepared.Start outside the launch funnels", |
| + "zzLaunchFunnelStrictLate: reaches a launch primitive before refuseRuntimeBinaryLaunch"}, |
| + // P2 2: package-level initializers. |
| + "pkgvar.go": {"var zzLaunchVar: launch primitive exec.Cmd.Start on something other than systemctl", |
| + "var zzStartChildValue: launch primitive cgroup.Manager.StartChildGated outside the launch funnels"}, |
| + // P2 3: a refusal that does not refuse the row being launched. |
| + "refusals.go": {"zzLaunchFunnelInverted: reaches a launch primitive before refuseRuntimeBinaryLaunch", |
| + "zzLaunchFunnelWrongRoute: reaches a launch primitive before refuseRuntimeBinaryLaunch", |
| + "zzLaunchFunnelSwallowed: reaches a launch primitive before refuseRuntimeBinaryLaunch"}, |
| } |
| |
| var loadLaunchGuardPackage = sync.OnceValues(func() (*packages.Package, error) { |
| @@ -114,23 +130,30 @@ func packagesErr(pkgs []*packages.Package) error { |
| } |
| |
| // launchPrimitiveName is obj's launch primitive name ("cgroup.Manager.Start", |
| -// "os.StartProcess"), or "". |
| +// "os.StartProcess", "activator.strictProc.Start"), or "". Besides the |
| +// listed names, any method named Start whose parameters include an |
| +// *exec.Cmd is a strict start, whatever its receiver (a named or an |
| +// anonymous interface, or a concrete type). |
| func launchPrimitiveName(obj types.Object) string { |
| fn, ok := obj.(*types.Func) |
| if !ok || fn.Pkg() == nil { |
| return "" |
| } |
| + sig, _ := fn.Type().(*types.Signature) |
| name := fn.Name() |
| - if sig, ok := fn.Type().(*types.Signature); ok && sig.Recv() != nil { |
| + if sig != nil && 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 "" |
| + recv := "(interface)" |
| + if n, ok := t.(*types.Named); ok { |
| + recv = n.Obj().Name() |
| + } |
| + name = recv + "." + name |
| + if fn.Name() == "Start" && fn.Pkg().Path() != "os/exec" && takesExecCmd(sig) { |
| + return fn.Pkg().Name() + "." + name |
| } |
| - name = n.Obj().Name() + "." + name |
| } |
| if !slices.Contains(launchPrimitives[fn.Pkg().Path()], name) { |
| return "" |
| @@ -138,6 +161,23 @@ func launchPrimitiveName(obj types.Object) string { |
| return fn.Pkg().Name() + "." + name |
| } |
| |
| +func takesExecCmd(sig *types.Signature) bool { |
| + for i := 0; i < sig.Params().Len(); i++ { |
| + if types.TypeString(sig.Params().At(i).Type(), nil) == "*os/exec.Cmd" { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// funnelOnly reports whether primitive p may appear in a launch funnel: |
| +// the cgroup and strict starts. An exec.Cmd start is for systemctl alone, |
| +// and a raw start is never used. |
| +func funnelOnly(p string) bool { |
| + return p != "" && !strings.HasPrefix(p, "exec.Cmd.") && !strings.HasPrefix(p, "os.") && |
| + !strings.HasPrefix(p, "syscall.") && !strings.HasPrefix(p, "unix.") |
| +} |
| + |
| // 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) { |
| @@ -185,100 +225,129 @@ func calleeName(info *types.Info, call *ast.CallExpr) string { |
| return "" |
| } |
| |
| -// launchGuardProblems is every rule broken in f. isFunnel names a funnel. |
| +// launchGuardProblems is every rule broken in f: in each function body, |
| +// and in each package-level initializer (a function literal or a method |
| +// value stored in a var or const). 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 |
| + switch d := decl.(type) { |
| + case *ast.FuncDecl: |
| + if d.Body == nil { |
| + continue |
| + } |
| + funnel := isFunnel(d.Name.Name) |
| + out = append(out, launchSiteProblems(info, d.Name.Name, d.Body, funnel)...) |
| + if funnel { |
| + if p := funnelOrderProblem(info, d); p != "" { |
| + out = append(out, d.Name.Name+": "+p) |
| + } |
| + } |
| + case *ast.GenDecl: |
| + for _, spec := range d.Specs { |
| + vs, ok := spec.(*ast.ValueSpec) |
| + if !ok { |
| + continue |
| + } |
| + for i, v := range vs.Values { |
| + name := "var" |
| + if i < len(vs.Names) { |
| + name += " " + vs.Names[i].Name |
| + } |
| + out = append(out, launchSiteProblems(info, name, v, false)...) |
| + } |
| + } |
| } |
| - 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 out |
| +} |
| + |
| +// launchSiteProblems is the launch-site rules over node, reported under |
| +// name; funnel says node is a launch funnel's body. |
| +func launchSiteProblems(info *types.Info, name string, node ast.Node, funnel bool) []string { |
| + var out []string |
| + var stack []ast.Node |
| + ast.Inspect(node, 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 |
| } |
| - 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 |
| - } |
| + 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 |
| - } |
| + out = append(out, name+": launch primitive "+p+" on something other than systemctl") |
| + return true |
| + } |
| + if !funnel || !funnelOnly(p) { |
| + 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 !inside { |
| - out = append(out, name+": exec."+fn+" builds a process outside a launch funnel's build closure") |
| + 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 |
| } |
| } |
| - return true |
| - }) |
| - if funnel { |
| - if p := funnelOrderProblem(info, fd); p != "" { |
| - out = append(out, name+": "+p) |
| + if !inside { |
| + out = append(out, name+": exec."+fn+" builds a process outside a launch funnel's build closure") |
| } |
| } |
| - } |
| + return true |
| + }) |
| 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. |
| +// funnelOrderProblem checks that fd refuses (isRefusalStmt) in a |
| +// top-level statement before its first top-level statement naming a |
| +// funnel-only 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 { |
| + route := routeParam(info, fd) |
| + if route == nil { |
| + return "has no registry.Route parameter to refuse" |
| + } |
| refused := false |
| for _, st := range fd.Body.List { |
| - if !refused && isRefusalStmt(info, st) { |
| + if !refused && isRefusalStmt(info, st, route) { |
| 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.") { |
| + if id, ok := n.(*ast.Ident); ok && funnelOnly(launchPrimitiveName(info.Uses[id])) { |
| names = true |
| } |
| return !names |
| }) |
| if names { |
| if !refused { |
| - return "reaches a launch primitive before " + launchRefusal |
| + return "reaches a launch primitive before " + launchRefusal + " refuses its route" |
| } |
| return "" |
| } |
| @@ -286,23 +355,67 @@ func funnelOrderProblem(info *types.Info, fd *ast.FuncDecl) string { |
| 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 { |
| +// routeParam is fd's registry.Route parameter: the row being launched. |
| +func routeParam(info *types.Info, fd *ast.FuncDecl) types.Object { |
| + for _, field := range fd.Type.Params.List { |
| + if types.TypeString(info.TypeOf(field.Type), nil) != "github.com/carlosframework/platform/internal/registry.Route" || len(field.Names) == 0 { |
| + continue |
| + } |
| + return info.Defs[field.Names[0]] |
| + } |
| + return nil |
| +} |
| + |
| +// isRefusalStmt is exactly a refusal of route (Astra N1 r2 P2 3): |
| +// |
| +// if err := refuseRuntimeBinaryLaunch(route); err != nil { |
| +// return ..., err, ... |
| +// } |
| +// |
| +// The call's one argument is the funnel's own route parameter, the |
| +// condition is err != nil on the error the call returned, and the body's |
| +// first statement returns that same error. |
| +func isRefusalStmt(info *types.Info, st ast.Stmt, route types.Object) 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 { |
| + if !ok || as.Tok != token.DEFINE || len(as.Lhs) != 1 || len(as.Rhs) != 1 { |
| return false |
| } |
| + errID, ok := as.Lhs[0].(*ast.Ident) |
| + if !ok { |
| + return false |
| + } |
| + errObj := info.Defs[errID] |
| call, ok := as.Rhs[0].(*ast.CallExpr) |
| - if !ok || calleeName(info, call) != launchRefusal { |
| + if !ok || errObj == nil || calleeName(info, call) != launchRefusal || len(call.Args) != 1 { |
| + return false |
| + } |
| + if arg, ok := call.Args[0].(*ast.Ident); !ok || info.Uses[arg] != route { |
| + return false |
| + } |
| + cond, ok := ifs.Cond.(*ast.BinaryExpr) |
| + if !ok || cond.Op != token.NEQ { |
| + return false |
| + } |
| + x, xok := cond.X.(*ast.Ident) |
| + y, yok := cond.Y.(*ast.Ident) |
| + if !xok || !yok || info.Uses[x] != errObj { |
| + return false |
| + } |
| + if _, isNil := info.Uses[y].(*types.Nil); !isNil { |
| + return false |
| + } |
| + ret, ok := ifs.Body.List[0].(*ast.ReturnStmt) |
| + if !ok { |
| return false |
| } |
| - _, ret := ifs.Body.List[len(ifs.Body.List)-1].(*ast.ReturnStmt) |
| - return ret |
| + return slices.ContainsFunc(ret.Results, func(e ast.Expr) bool { |
| + id, ok := e.(*ast.Ident) |
| + return ok && info.Uses[id] == errObj |
| + }) |
| } |
| |
| func TestBinaryLaunchesAskTheRuntimeFirst(t *testing.T) { |