package containment import ( "context" "net" "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/testci" ) // opencodeLaunch is an OpenCode launch on m: a sealed tree holding the // binary (a copy of true), the runtime home under the scratch root's // homes, a key, a tool socket and a workspace. func opencodeLaunch(t *testing.T, m *Manager) (provider.Launch, string) { t.Helper() root := t.TempDir() trueBin, err := os.ReadFile("/usr/bin/true") if err != nil { t.Fatal(err) } tree := filepath.Join(root, "opencode", "node_modules") binary := filepath.Join(tree, "@opencode", "cli-test", "bin", "opencode") os.MkdirAll(filepath.Dir(binary), 0755) if err := os.WriteFile(binary, trueBin, 0755); err != nil { t.Fatal(err) } if err := provider.SealTree(tree); err != nil { t.Fatal(err) } t.Cleanup(func() { unsealTree(filepath.Dir(tree)) }) sum, err := provider.TreeSHA256(tree) if err != nil { t.Fatal(err) } home := filepath.Join(m.cfg.ScratchRoot, "homes", "opencode-home") os.MkdirAll(home, 0700) os.Chmod(filepath.Join(m.cfg.ScratchRoot, "homes"), 0700) work := filepath.Join(root, "work") os.Mkdir(work, 0700) keyHome := filepath.Join(root, "key") os.Mkdir(keyHome, 0700) os.WriteFile(filepath.Join(keyHome, provider.TokenFile), []byte("sk-go-test"), 0600) proxy := filepath.Join(root, "proxy") os.WriteFile(proxy, []byte("proxy"), 0755) sockDir := filepath.Join(root, "tool") os.Mkdir(sockDir, 0700) ln, err := net.Listen("unix", filepath.Join(sockDir, "equipo.sock")) if err != nil { t.Fatal(err) } t.Cleanup(func() { ln.Close() }) os.Chmod(filepath.Join(sockDir, "equipo.sock"), 0600) tools := provider.OpenCodeTools("inspect", []string{"submit_review"}) in := provider.OpenCodePolicyInputs{Binary: binary, BinarySHA256: sha256Hex(trueBin), Tree: tree, TreeSHA256: sum, Bwrap: "/usr/bin/bwrap", ToolProxy: proxy, Home: home, Workspace: work, Action: "inspect", Provider: "opencode-go", Model: "glm-5.3", KeyEnv: "OPENCODE_GO_API_KEY", Tools: tools, EgressHosts: []string{"opencode.ai"}, Config: provider.OpenCodeConfig("opencode-go", "glm-5.3", tools, true)} if err := provider.WriteOpenCodeConfig(in); err != nil { t.Fatal(err) } p, err := provider.BuildOpenCodePolicy(in) if err != nil { t.Fatal(err) } return provider.Launch{ToolSocket: filepath.Join(sockDir, "equipo.sock"), AttemptKey: "opencode-swap", Workspace: work, Selection: provider.Selection{Provider: "opencode-go", Model: "glm-5.3"}, Binding: provider.OpenCodeBinding("opencode-go-a", home, keyHome), Policy: p}, binary } // Security round finding 2: an OpenCode launch mounts the binary it // attested, held to its seal through the descriptor bubblewrap mounts. // Between the attestation and the mount, a binary rewritten, made // writable, given a second name or replaced at its path is refused; // untouched, it starts. func TestNamespaceOpenCodeBinaryAfterAttestation(t *testing.T) { testci.Tools(t, "bwrap") for _, change := range []string{"none", "content", "writable", "second name", "replaced"} { t.Run(change, func(t *testing.T) { m, _ := fixture(t) l, binary := opencodeLaunch(t, m) dir := filepath.Dir(binary) called := false m.beforePin = func() { called = true switch change { case "content": os.Chmod(dir, 0755) os.Chmod(binary, 0755) os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0755) os.Chmod(binary, 0555) os.Chmod(dir, 0555) case "writable": os.Chmod(binary, 0755) case "second name": if err := os.Link(binary, filepath.Join(t.TempDir(), "other")); err != nil { t.Fatal(err) } case "replaced": // Other bytes, sealed as the install seals them, renamed // over the attested path. os.Chmod(dir, 0755) other := binary + ".new" os.WriteFile(other, []byte("#!/bin/sh\nexit 0\n"), 0555) if err := os.Rename(other, binary); err != nil { t.Fatal(err) } os.Chmod(dir, 0555) } } p, err := m.Start(context.Background(), l, provider.OpenCodeArgv()) if p != nil { p.(*process).kill() p.Wait(context.Background()) m.Stop(context.Background(), p.ID()) p.Stdin().Close() p.Stdout().Close() p.Stderr().Close() } if !called { t.Fatal("did not reach the attestation-to-mount boundary") } if change == "none" && err != nil { t.Fatalf("the untouched binary was refused: %v", err) } if change != "none" && err == nil { t.Fatal("a binary changed after its attestation was mounted") } }) } }