| 1 | package containment |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | _ "embed" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | ) |
| 15 | |
| 16 | //go:embed isolation.py |
| 17 | var isolationScript string |
| 18 | |
| 19 | // IsolationTarget accepts only small, regular synthetic sentinel files. Callers |
| 20 | // must never supply credential paths. |
| 21 | func IsolationTarget(name, hostPath, path string) (provider.IsolationTarget, error) { |
| 22 | target := provider.IsolationTarget{Name: name, HostPath: hostPath, Path: path} |
| 23 | if name == "" || !cleanAbsolute(path) || !cleanAbsolute(hostPath) { |
| 24 | return target, provider.ErrPolicy |
| 25 | } |
| 26 | f, err := os.OpenFile(hostPath, os.O_RDONLY, 0) |
| 27 | if err != nil { |
| 28 | return target, err |
| 29 | } |
| 30 | defer f.Close() |
| 31 | info, err := f.Stat() |
| 32 | if err != nil || !info.Mode().IsRegular() || info.Size() == 0 || info.Size() > 4096 { |
| 33 | return target, provider.ErrPolicy |
| 34 | } |
| 35 | raw, err := io.ReadAll(io.LimitReader(f, 4097)) |
| 36 | if err != nil || len(raw) > 4096 { |
| 37 | return target, provider.ErrPolicy |
| 38 | } |
| 39 | target.SHA256 = provider.ManifestDigest(raw) |
| 40 | return target, nil |
| 41 | } |
| 42 | |
| 43 | // ProbeIsolation replaces only the attested provider command with a fixed, |
| 44 | // trusted probe. It shares StartRecorded's mounts, UID, environment, network, |
| 45 | // helper and lifecycle. There is no caller-supplied executable or script. |
| 46 | func (m *Manager) ProbeIsolation(ctx context.Context, l provider.Launch, targets []provider.IsolationTarget, inner bool) (provider.IsolationReport, error) { |
| 47 | report := provider.IsolationReport{Layer: "outer", PolicyHash: l.Policy.Hash} |
| 48 | if len(targets) == 0 || len(targets) > 128 { |
| 49 | return report, provider.ErrPolicy |
| 50 | } |
| 51 | seen := map[string]bool{} |
| 52 | for _, target := range targets { |
| 53 | actual, err := IsolationTarget(target.Name, target.HostPath, target.Path) |
| 54 | if err != nil || actual != target || seen[target.Name] { |
| 55 | return report, provider.ErrPolicy |
| 56 | } |
| 57 | seen[target.Name] = true |
| 58 | } |
| 59 | var manifest struct{ Argv []string } |
| 60 | if json.Unmarshal(l.Policy.Manifest, &manifest) != nil || l.Policy.ManifestHash == "" { |
| 61 | return report, provider.ErrPolicy |
| 62 | } |
| 63 | if _, err := provider.AttestLaunch(l, manifest.Argv); err != nil { |
| 64 | return report, err |
| 65 | } |
| 66 | raw, err := json.Marshal(targets) |
| 67 | if err != nil { |
| 68 | return report, err |
| 69 | } |
| 70 | command := []string{"/usr/bin/python3", "-I", "-c", isolationScript, string(raw)} |
| 71 | if inner { |
| 72 | report.Layer = "provider-tool" |
| 73 | if l.Selection.Provider != "codex" || len(manifest.Argv) < 5 || manifest.Argv[0] != "/opt/codex" || manifest.Argv[1] != "app-server" { |
| 74 | return report, provider.ErrPolicy |
| 75 | } |
| 76 | // Pinned 0.165.0 exposes `sandbox`, without a `linux` subcommand. |
| 77 | args := []string{"/opt/codex", "sandbox", "--include-managed-config", "-P", "equipo", "-C", "/workspace"} |
| 78 | for i := 2; i < len(manifest.Argv); i++ { |
| 79 | if manifest.Argv[i] == "-c" && i+1 < len(manifest.Argv) { |
| 80 | args = append(args, "-c", manifest.Argv[i+1]) |
| 81 | i++ |
| 82 | } |
| 83 | } |
| 84 | command = append(append(args, "--"), command...) |
| 85 | } |
| 86 | ctx, cancel := context.WithTimeout(ctx, 20*time.Second) |
| 87 | defer cancel() |
| 88 | l.AttemptKey += "/isolation/" + report.Layer |
| 89 | p, err := m.startRecorded(ctx, l, manifest.Argv, nil, command, false) |
| 90 | if err != nil { |
| 91 | return report, err |
| 92 | } |
| 93 | defer p.Stdin().Close() |
| 94 | defer p.Stdout().Close() |
| 95 | defer p.Stderr().Close() |
| 96 | defer func() { |
| 97 | cleanup, stop := context.WithTimeout(context.Background(), 5*time.Second) |
| 98 | defer stop() |
| 99 | _, _ = m.Stop(cleanup, p.ID()) |
| 100 | }() |
| 101 | // Discard provider diagnostics: they can contain paths but are not probe data. |
| 102 | drained := make(chan struct{}) |
| 103 | go func() { _, _ = io.Copy(io.Discard, p.Stderr()); close(drained) }() |
| 104 | err = json.NewDecoder(io.LimitReader(p.Stdout(), 128<<10)).Decode(&report) |
| 105 | exit, waitErr := p.Wait(ctx) |
| 106 | <-drained |
| 107 | if err != nil || waitErr != nil || exit.Code != 0 { |
| 108 | return report, errors.Join(provider.ErrContainment, err, waitErr) |
| 109 | } |
| 110 | report.NamespaceManifest = p.(*process).record.ManifestHash |
| 111 | report.PolicyHash = l.Policy.Hash |
| 112 | if inner { |
| 113 | report.Layer = "provider-tool" |
| 114 | } else { |
| 115 | report.Layer = "outer" |
| 116 | } |
| 117 | if len(report.Targets) != len(targets) || report.UID != os.Getuid() { |
| 118 | return report, provider.ErrContainment |
| 119 | } |
| 120 | // Verify the markers still exist unchanged after the probe. |
| 121 | for _, target := range targets { |
| 122 | actual, err := IsolationTarget(target.Name, target.HostPath, target.Path) |
| 123 | if err != nil || actual != target || filepath.Clean(target.Path) != target.Path { |
| 124 | return report, provider.ErrPolicy |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if !inner { |
| 129 | report.Safe = true |
| 130 | for i := range report.Targets { |
| 131 | result := &report.Targets[i] |
| 132 | for _, target := range targets { |
| 133 | if target.Name == result.Name && provider.ProviderHomeTarget(target.Name, target.Path) && target.HostPath == filepath.Join(l.Binding.Home, filepath.Base(target.Path)) && result.Operations["open"] == "sentinel-exposed" { |
| 134 | result.Status = "expected-provider-home" |
| 135 | } |
| 136 | } |
| 137 | report.Safe = report.Safe && (result.Safe || result.Status == "expected-provider-home") |
| 138 | } |
| 139 | for _, status := range report.SideDoors { |
| 140 | switch status { |
| 141 | case "ENOENT", "EACCES", "EPERM", "no-inherited-fds", "policy-denied", "same-sandbox-root": |
| 142 | default: |
| 143 | report.Safe = false |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | return report, nil |
| 148 | } |
| 149 | |