package containment import ( "context" _ "embed" "encoding/json" "errors" "io" "os" "path/filepath" "time" "amadan.net/oficina/equipo/internal/runner/provider" ) //go:embed isolation.py var isolationScript string // IsolationTarget accepts only small, regular synthetic sentinel files. Callers // must never supply credential paths. func IsolationTarget(name, hostPath, path string) (provider.IsolationTarget, error) { target := provider.IsolationTarget{Name: name, HostPath: hostPath, Path: path} if name == "" || !cleanAbsolute(path) || !cleanAbsolute(hostPath) { return target, provider.ErrPolicy } f, err := os.OpenFile(hostPath, os.O_RDONLY, 0) if err != nil { return target, err } defer f.Close() info, err := f.Stat() if err != nil || !info.Mode().IsRegular() || info.Size() == 0 || info.Size() > 4096 { return target, provider.ErrPolicy } raw, err := io.ReadAll(io.LimitReader(f, 4097)) if err != nil || len(raw) > 4096 { return target, provider.ErrPolicy } target.SHA256 = provider.ManifestDigest(raw) return target, nil } // ProbeIsolation replaces only the attested provider command with a fixed, // trusted probe. It shares StartRecorded's mounts, UID, environment, network, // helper and lifecycle. There is no caller-supplied executable or script. func (m *Manager) ProbeIsolation(ctx context.Context, l provider.Launch, targets []provider.IsolationTarget, inner bool) (provider.IsolationReport, error) { report := provider.IsolationReport{Layer: "outer", PolicyHash: l.Policy.Hash} if len(targets) == 0 || len(targets) > 128 { return report, provider.ErrPolicy } seen := map[string]bool{} for _, target := range targets { actual, err := IsolationTarget(target.Name, target.HostPath, target.Path) if err != nil || actual != target || seen[target.Name] { return report, provider.ErrPolicy } seen[target.Name] = true } var manifest struct{ Argv []string } if json.Unmarshal(l.Policy.Manifest, &manifest) != nil || l.Policy.ManifestHash == "" { return report, provider.ErrPolicy } if _, err := provider.AttestLaunch(l, manifest.Argv); err != nil { return report, err } raw, err := json.Marshal(targets) if err != nil { return report, err } command := []string{"/usr/bin/python3", "-I", "-c", isolationScript, string(raw)} if inner { report.Layer = "provider-tool" if l.Selection.Provider != "codex" || len(manifest.Argv) < 5 || manifest.Argv[0] != "/opt/codex" || manifest.Argv[1] != "app-server" { return report, provider.ErrPolicy } // Pinned 0.165.0 exposes `sandbox`, without a `linux` subcommand. args := []string{"/opt/codex", "sandbox", "--include-managed-config", "-P", "equipo", "-C", "/workspace"} for i := 2; i < len(manifest.Argv); i++ { if manifest.Argv[i] == "-c" && i+1 < len(manifest.Argv) { args = append(args, "-c", manifest.Argv[i+1]) i++ } } command = append(append(args, "--"), command...) } ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() l.AttemptKey += "/isolation/" + report.Layer p, err := m.startRecorded(ctx, l, manifest.Argv, nil, command, false) if err != nil { return report, err } defer p.Stdin().Close() defer p.Stdout().Close() defer p.Stderr().Close() defer func() { cleanup, stop := context.WithTimeout(context.Background(), 5*time.Second) defer stop() _, _ = m.Stop(cleanup, p.ID()) }() // Discard provider diagnostics: they can contain paths but are not probe data. drained := make(chan struct{}) go func() { _, _ = io.Copy(io.Discard, p.Stderr()); close(drained) }() err = json.NewDecoder(io.LimitReader(p.Stdout(), 128<<10)).Decode(&report) exit, waitErr := p.Wait(ctx) <-drained if err != nil || waitErr != nil || exit.Code != 0 { return report, errors.Join(provider.ErrContainment, err, waitErr) } report.NamespaceManifest = p.(*process).record.ManifestHash report.PolicyHash = l.Policy.Hash if inner { report.Layer = "provider-tool" } else { report.Layer = "outer" } if len(report.Targets) != len(targets) || report.UID != os.Getuid() { return report, provider.ErrContainment } // Verify the markers still exist unchanged after the probe. for _, target := range targets { actual, err := IsolationTarget(target.Name, target.HostPath, target.Path) if err != nil || actual != target || filepath.Clean(target.Path) != target.Path { return report, provider.ErrPolicy } } if !inner { report.Safe = true for i := range report.Targets { result := &report.Targets[i] for _, target := range targets { 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" { result.Status = "expected-provider-home" } } report.Safe = report.Safe && (result.Safe || result.Status == "expected-provider-home") } for _, status := range report.SideDoors { switch status { case "ENOENT", "EACCES", "EPERM", "no-inherited-fds", "policy-denied", "same-sandbox-root": default: report.Safe = false } } } return report, nil }