Mac slice F task 5 fix 1: check the chain from home, hold the hash
Review round 1. The pinned CLI's chain is checked from the runner's home down to the binary: every directory a real directory (Lstat), owned by the runner uid, no group or other write bit, the opened root the one examined; tools and claude-2.1.312 exactly 0700 (Provision tightens a looser one of ours, Verify and Launch refuse it); the binary the runner uid's. NewClaudeCLI takes the home. Launch hashes the open descriptor, refuses a change while hashing, and just before start checks that the path still names the held file with the same device, inode, size, mtime and ctime; the descriptor stays open until start returns. Argv is removed. Seams race a rename during hashing and before start, and an in-place write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+564
−85
internal/runner/mac/claude.go+207 −74internal/runner/mac/claude_chain_test.go+290 −0internal/runner/mac/claude_stat_darwin.go+25 −0internal/runner/mac/claude_stat_linux.go+25 −0internal/runner/mac/claude_test.go+14 −8internal/runner/thin/mac.go+1 −1internal/runner/thin/mac_claude_test.go+2 −2
Intent
diff --git a/internal/runner/mac/claude.go b/internal/runner/mac/claude.go| index ebfd1342..670d0de1 100644 |
| --- a/internal/runner/mac/claude.go |
| +++ b/internal/runner/mac/claude.go |
| @@ -87,13 +87,15 @@ func (e *ClaudeUnavailable) Reason() string { return wire.ReasonEngineeringUnava |
| // ClaudeFetch opens url's body. Provision only ever asks for ClaudeURL. |
| type ClaudeFetch func(ctx context.Context, url string) (io.ReadCloser, error) |
| -// ClaudeCLI is the pinned CLI under a runner's install dir. Its path is |
| -// handed out only by Argv and Launch, after the file there has been |
| -// hashed and matched, so nothing can run an unverified claude through |
| -// it. The zero value has no CLI: it verifies nothing and launches |
| -// nothing. |
| +// ClaudeCLI is the pinned CLI under a runner's install dir. Nothing can |
| +// start it but Launch, which hashes the file and checks that the path |
| +// still names exactly what it hashed, immediately before start. The zero |
| +// value has no CLI: it verifies nothing and launches nothing. |
| type ClaudeCLI struct { |
| - installDir string |
| + // home is the trusted ancestor: the runner user's home. installDir |
| + // must lie inside it; every directory from home down to the binary |
| + // is checked (openClaudeDir). |
| + home, installDir string |
| // pin is the digest the file must have (a test pins its own bytes). |
| pin string |
| // limit bounds a download; 0 is MaxClaudeBinary. |
| @@ -101,11 +103,21 @@ type ClaudeCLI struct { |
| } |
| // NewClaudeCLI is the pinned darwin-arm64 CLI under installDir |
| -// (~/.local/lib/equipo-runner on a Mac). |
| -func NewClaudeCLI(installDir string) ClaudeCLI { |
| - return ClaudeCLI{installDir: installDir, pin: claude.ExecutableSHA256DarwinArm64} |
| +// (~/.local/lib/equipo-runner on a Mac), which lies in home. |
| +func NewClaudeCLI(home, installDir string) ClaudeCLI { |
| + return ClaudeCLI{home: home, installDir: installDir, pin: claude.ExecutableSHA256DarwinArm64} |
| } |
| +// Seams. ownerOf is the uid owning a file; runnerUID the uid that must |
| +// own every directory and the binary. claudeDuringHash runs once in the |
| +// middle of a launch's hash, claudeBeforeStart after the hash and before |
| +// the final check (tests race a swap there; nil in a release). |
| +var ( |
| + ownerOf = fileOwner |
| + runnerUID = os.Getuid() |
| + claudeDuringHash, claudeBeforeStart func() |
| +) |
| + |
| func (c ClaudeCLI) bound() int64 { |
| if c.limit > 0 { |
| return c.limit |
| @@ -114,21 +126,16 @@ func (c ClaudeCLI) bound() int64 { |
| } |
| // Verify hashes the CLI at its place, once, and returns its path when it |
| -// matches the pin: a plain file, mode 0700, reached through no symbolic |
| -// link from the install dir. |
| +// matches the pin: a plain 0700 file of the runner user, reached through |
| +// private directories and no symbolic link from the home. The path is |
| +// for reports and policy records only; start the CLI with Launch. |
| func (c ClaudeCLI) Verify() (string, error) { |
| - if c.installDir == "" || c.pin == "" { |
| - return "", ErrClaudeUnpinned |
| - } |
| - d, err := openClaudeDir(c.installDir, false) |
| + v, err := c.open() |
| if err != nil { |
| - return "", ErrClaudeUnpinned |
| - } |
| - defer d.Close() |
| - if err := c.verifyIn(d); err != nil { |
| return "", err |
| } |
| - return ClaudePath(c.installDir), nil |
| + v.close() |
| + return v.path, nil |
| } |
| // Version is the pinned version while the CLI verifies, else "". It |
| @@ -140,46 +147,154 @@ func (c ClaudeCLI) Version() string { |
| return provider.ClaudeVersion |
| } |
| -// Argv verifies the CLI now and returns the argv that starts it: its |
| -// path, then args. Every start of the CLI (an Engineering attempt, an |
| -// ask, a token check, a sign-in, a probe launch) takes its argv from |
| -// here immediately before it starts, never from an earlier call. A CLI |
| -// that does not verify is a *ClaudeUnavailable. |
| -func (c ClaudeCLI) Argv(args ...string) ([]string, error) { |
| - path, err := c.Verify() |
| +// Launch starts the pinned CLI through start with argv [path, args...]. |
| +// Every start of the CLI (an Engineering attempt, an ask, a token check, |
| +// a sign-in, a probe launch) goes through here. It opens the file and |
| +// hashes the open descriptor, then, immediately before start, checks |
| +// that the path still names that same file (device, inode, size, |
| +// modification and change times) and that it did not change while it |
| +// was hashed. The descriptor stays open until start returns, so the |
| +// inode cannot be reused by another file meanwhile; start should have |
| +// started the process when it returns. Nothing starts when any check |
| +// fails: *ClaudeUnavailable, engineering_unavailable. |
| +// |
| +// The honest limit: macOS cannot exec a descriptor, so the kernel looks |
| +// the path up again after the last check. Only the runner user can |
| +// rename inside the private chain, sandboxed commands cannot write the |
| +// install dir, and the runner user is dedicated to the runner. |
| +func (c ClaudeCLI) Launch(ctx context.Context, start func(ctx context.Context, argv []string) error, args ...string) error { |
| + v, err := c.open() |
| if err != nil { |
| - return nil, &ClaudeUnavailable{Err: err} |
| + return &ClaudeUnavailable{Err: err} |
| + } |
| + defer v.close() |
| + if claudeBeforeStart != nil { |
| + claudeBeforeStart() |
| } |
| - return append([]string{path}, args...), nil |
| + if err := v.still(); err != nil { |
| + return &ClaudeUnavailable{Err: err} |
| + } |
| + return start(ctx, append([]string{v.path}, args...)) |
| } |
| -// Launch verifies the CLI and hands start its argv; nothing starts when |
| -// it does not verify (*ClaudeUnavailable, engineering_unavailable). |
| -func (c ClaudeCLI) Launch(ctx context.Context, start func(ctx context.Context, argv []string) error, args ...string) error { |
| - argv, err := c.Argv(args...) |
| +// verified is an open, hashed CLI: its descriptor and the state it had. |
| +type verified struct { |
| + f *os.File |
| + path string |
| + key statKey |
| +} |
| + |
| +type statKey struct { |
| + dev, ino uint64 |
| + size int64 |
| + mtime, ctime int64 |
| +} |
| + |
| +func (v *verified) close() { v.f.Close() } |
| + |
| +// still: the path names the hashed file, unchanged. |
| +func (v *verified) still() error { |
| + named, err := os.Lstat(v.path) |
| if err != nil { |
| - return err |
| + return ErrClaudeUnpinned |
| + } |
| + held, err := v.f.Stat() |
| + if err != nil || !named.Mode().IsRegular() || !os.SameFile(named, held) { |
| + return fmt.Errorf("%w: replaced after it was hashed", ErrClaudeUnpinned) |
| + } |
| + nk, ok1 := fileKey(named) |
| + hk, ok2 := fileKey(held) |
| + if !ok1 || !ok2 || nk != v.key || hk != v.key { |
| + return fmt.Errorf("%w: changed after it was hashed", ErrClaudeUnpinned) |
| + } |
| + return nil |
| +} |
| + |
| +// open validates the chain, opens the CLI and hashes the descriptor, |
| +// once: a regular file, not a link, mode 0700, the runner user's, the |
| +// file opened the one examined, unchanged from before the hash to after. |
| +func (c ClaudeCLI) open() (*verified, error) { |
| + if c.installDir == "" || c.pin == "" { |
| + return nil, ErrClaudeUnpinned |
| + } |
| + d, err := c.openDir(false) |
| + if err != nil { |
| + return nil, ErrClaudeUnpinned |
| + } |
| + defer d.Close() |
| + f, err := c.hashIn(d, claudeDuringHash) |
| + if err != nil { |
| + return nil, err |
| + } |
| + return f, nil |
| +} |
| + |
| +func (c ClaudeCLI) hashIn(d *os.Root, during func()) (*verified, error) { |
| + info, err := d.Lstat(claudeName) |
| + if err != nil || !info.Mode().IsRegular() || info.Mode().Perm() != 0700 || ownerOf(info) != runnerUID || info.Size() > c.bound() { |
| + return nil, ErrClaudeUnpinned |
| + } |
| + f, err := d.OpenFile(claudeName, os.O_RDONLY, 0) |
| + if err != nil { |
| + return nil, ErrClaudeUnpinned |
| + } |
| + before, err := f.Stat() |
| + key, ok := fileKey(before) |
| + if err != nil || !ok || !os.SameFile(info, before) { |
| + f.Close() |
| + return nil, ErrClaudeUnpinned |
| } |
| - return start(ctx, argv) |
| + h := sha256.New() |
| + var r io.Reader = io.LimitReader(f, c.bound()+1) |
| + if during != nil { |
| + r = &midway{r: r, at: during} |
| + } |
| + if _, err := io.Copy(h, r); err != nil || hex.EncodeToString(h.Sum(nil)) != c.pin { |
| + f.Close() |
| + return nil, ErrClaudeUnpinned |
| + } |
| + after, err := f.Stat() |
| + if k, ok := fileKey(after); err != nil || !ok || k != key { |
| + f.Close() |
| + return nil, fmt.Errorf("%w: changed while it was hashed", ErrClaudeUnpinned) |
| + } |
| + return &verified{f: f, path: ClaudePath(c.installDir), key: key}, nil |
| +} |
| + |
| +// midway calls at once, after the first read. |
| +type midway struct { |
| + r io.Reader |
| + at func() |
| +} |
| + |
| +func (m *midway) Read(p []byte) (int, error) { |
| + n, err := m.r.Read(p) |
| + if m.at != nil && n > 0 { |
| + m.at() |
| + m.at = nil |
| + } |
| + return n, err |
| } |
| // Provision puts the pinned CLI in place unless it already verifies: |
| // fetch(ClaudeURL()) is streamed into a new file beside it, mode 0600, |
| // bounded by MaxClaudeBinary and hashed as it is written; only a match |
| // is made 0700 and renamed into place, replacing whatever was there. A |
| -// refused download is removed. Every write goes through an os.Root on a |
| -// directory chain with no symbolic link in it. |
| +// refused download is removed. Every write goes through an os.Root on |
| +// the checked chain (openClaudeDir), whose tool directories it makes or |
| +// tightens to 0700. |
| func (c ClaudeCLI) Provision(ctx context.Context, fetch ClaudeFetch) error { |
| if c.installDir == "" || c.pin == "" { |
| return ErrClaudeUnpinned |
| } |
| - d, err := openClaudeDir(c.installDir, true) |
| + d, err := c.openDir(true) |
| if err != nil { |
| return err |
| } |
| defer d.Close() |
| removeDownloads(d) |
| - if c.verifyIn(d) == nil { |
| + if v, err := c.hashIn(d, nil); err == nil { |
| + v.close() |
| return nil |
| } |
| body, err := fetch(ctx, ClaudeURL()) |
| @@ -225,45 +340,44 @@ func (c ClaudeCLI) Provision(ctx context.Context, fetch ClaudeFetch) error { |
| return err |
| } |
| placed = true |
| - return c.verifyIn(d) |
| -} |
| - |
| -// verifyIn hashes d/claude: a regular file, not a link, mode 0700, the |
| -// file opened the one examined, read once. |
| -func (c ClaudeCLI) verifyIn(d *os.Root) error { |
| - info, err := d.Lstat(claudeName) |
| - if err != nil || !info.Mode().IsRegular() || info.Mode().Perm() != 0700 || info.Size() > c.bound() { |
| - return ErrClaudeUnpinned |
| - } |
| - f, err := d.OpenFile(claudeName, os.O_RDONLY, 0) |
| + v, err := c.hashIn(d, nil) |
| if err != nil { |
| - return ErrClaudeUnpinned |
| - } |
| - defer f.Close() |
| - if opened, err := f.Stat(); err != nil || !os.SameFile(info, opened) { |
| - return ErrClaudeUnpinned |
| - } |
| - h := sha256.New() |
| - if _, err := io.Copy(h, io.LimitReader(f, c.bound()+1)); err != nil { |
| - return ErrClaudeUnpinned |
| - } |
| - if hex.EncodeToString(h.Sum(nil)) != c.pin { |
| - return ErrClaudeUnpinned |
| + return err |
| } |
| + v.close() |
| return nil |
| } |
| -// openClaudeDir opens ClaudeDir(installDir) as an os.Root, one component |
| -// at a time from the install dir, each a real directory (Lstat: never a |
| -// symbolic link) that no group or other user may write, and the one |
| -// opened the one examined. create makes a missing component 0700. |
| -func openClaudeDir(installDir string, create bool) (*os.Root, error) { |
| - r, err := os.OpenRoot(installDir) |
| +// openDir opens ClaudeDir(installDir) as an os.Root, walking from the |
| +// home one component at a time: each a real directory (Lstat, never a |
| +// symbolic link), owned by the runner user, that no group or other user |
| +// may write, and the one opened the one examined. The tool directories |
| +// (tools, claude-<version>) must be exactly 0700: create makes a missing |
| +// one 0700 and tightens a looser one of ours; without create a looser |
| +// one is refused. The install dir itself is never created. |
| +func (c ClaudeCLI) openDir(create bool) (*os.Root, error) { |
| + rel, err := filepath.Rel(c.home, c.installDir) |
| + if c.home == "" || !filepath.IsAbs(c.home) || !filepath.IsAbs(c.installDir) || err != nil || rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { |
| + return nil, fmt.Errorf("%w: the install dir is not inside the home", ErrClaudeUnpinned) |
| + } |
| + info, err := os.Lstat(c.home) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if err := private(info, "the home"); err != nil { |
| + return nil, err |
| + } |
| + r, err := os.OpenRoot(c.home) |
| if err != nil { |
| return nil, err |
| } |
| - for _, name := range claudeRel { |
| - next, err := openSubdir(r, name, create) |
| + if opened, err := r.Stat("."); err != nil || !os.SameFile(info, opened) { |
| + r.Close() |
| + return nil, fmt.Errorf("%w: the home moved while it was opened", ErrClaudeUnpinned) |
| + } |
| + install := strings.Split(rel, string(filepath.Separator)) |
| + for i, name := range append(install, claudeRel...) { |
| + next, err := openSubdir(r, name, i >= len(install), create) |
| r.Close() |
| if err != nil { |
| return nil, err |
| @@ -273,8 +387,17 @@ func openClaudeDir(installDir string, create bool) (*os.Root, error) { |
| return r, nil |
| } |
| -func openSubdir(parent *os.Root, name string, create bool) (*os.Root, error) { |
| - if create { |
| +// private: a real directory of the runner user that no group or other |
| +// user may write. |
| +func private(info fs.FileInfo, name string) error { |
| + if !info.IsDir() || info.Mode()&fs.ModeSymlink != 0 || ownerOf(info) != runnerUID || info.Mode().Perm()&0022 != 0 { |
| + return fmt.Errorf("%w: %s is not a private directory of this user", ErrClaudeUnpinned, name) |
| + } |
| + return nil |
| +} |
| + |
| +func openSubdir(parent *os.Root, name string, tool, create bool) (*os.Root, error) { |
| + if create && tool { |
| if err := parent.Mkdir(name, 0700); err != nil && !errors.Is(err, fs.ErrExist) { |
| return nil, err |
| } |
| @@ -283,8 +406,8 @@ func openSubdir(parent *os.Root, name string, create bool) (*os.Root, error) { |
| if err != nil { |
| return nil, err |
| } |
| - if !info.IsDir() || info.Mode().Perm()&0022 != 0 { |
| - return nil, fmt.Errorf("%w: %s is not a private directory", ErrClaudeUnpinned, name) |
| + if err := private(info, name); err != nil { |
| + return nil, err |
| } |
| sub, err := parent.OpenRoot(name) |
| if err != nil { |
| @@ -294,6 +417,16 @@ func openSubdir(parent *os.Root, name string, create bool) (*os.Root, error) { |
| sub.Close() |
| return nil, fmt.Errorf("%w: %s moved while it was opened", ErrClaudeUnpinned, name) |
| } |
| + if tool && info.Mode().Perm() != 0700 { |
| + if !create { |
| + sub.Close() |
| + return nil, fmt.Errorf("%w: %s is not 0700", ErrClaudeUnpinned, name) |
| + } |
| + if err := sub.Chmod(".", 0700); err != nil { |
| + sub.Close() |
| + return nil, err |
| + } |
| + } |
| return sub, nil |
| } |
diff --git a/internal/runner/mac/claude_chain_test.go b/internal/runner/mac/claude_chain_test.go| new file mode 100644 |
| index 00000000..c8304e82 |
| --- /dev/null |
| +++ b/internal/runner/mac/claude_chain_test.go |
| @@ -0,0 +1,290 @@ |
| +package mac |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "io/fs" |
| + "os" |
| + "path/filepath" |
| + "slices" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// install makes a runner's home and install dir as the installer does |
| +// (0700 each) and returns both. |
| +func install(t *testing.T) (home, dir string) { |
| + t.Helper() |
| + home = t.TempDir() |
| + dir = filepath.Join(home, ".local", "lib", "equipo-runner") |
| + // A umask of 002 leaves t.TempDir's group-writable. |
| + if err := os.Chmod(home, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.MkdirAll(dir, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + return home, dir |
| +} |
| + |
| +func homeCLI(home, dir string, body []byte) ClaudeCLI { |
| + c := NewClaudeCLI(home, dir) |
| + c.pin = digest(body) |
| + return c |
| +} |
| + |
| +// Every directory from the home down to the binary is a real directory |
| +// (never a link), the runner user's, written by no one else; the tool |
| +// directories are exactly 0700 (a looser one of ours is tightened by |
| +// Provision and refused by Verify). |
| +func TestClaudeChainIsPrivate(t *testing.T) { |
| + ctx := context.Background() |
| + body := fakeClaude() |
| + refused := func(t *testing.T, cli ClaudeCLI, why string) { |
| + t.Helper() |
| + if err := cli.Provision(ctx, serve(body, nil)); err == nil { |
| + t.Fatalf("provisioned %s", why) |
| + } |
| + if _, err := cli.Verify(); err == nil { |
| + t.Fatalf("verified %s", why) |
| + } |
| + } |
| + |
| + t.Run("install dir outside the home", func(t *testing.T) { |
| + home, _ := install(t) |
| + other := t.TempDir() |
| + refused(t, homeCLI(home, other, body), "outside the home") |
| + }) |
| + |
| + t.Run("the home is a symlink", func(t *testing.T) { |
| + home, dir := install(t) |
| + link := filepath.Join(t.TempDir(), "home") |
| + if err := os.Symlink(home, link); err != nil { |
| + t.Fatal(err) |
| + } |
| + rel, _ := filepath.Rel(home, dir) |
| + refused(t, homeCLI(link, filepath.Join(link, rel), body), "under a symlinked home") |
| + }) |
| + |
| + t.Run("symlinked install dir", func(t *testing.T) { |
| + home, dir := install(t) |
| + // A sibling inside lib: os.Root alone would follow it. |
| + elsewhere := filepath.Join(filepath.Dir(dir), "elsewhere") |
| + if err := os.Mkdir(elsewhere, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Remove(dir); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink("elsewhere", dir); err != nil { |
| + t.Fatal(err) |
| + } |
| + refused(t, homeCLI(home, dir, body), "through a symlinked install dir") |
| + if left := entries(t, elsewhere); len(left) != 0 { |
| + t.Fatalf("wrote through the link: %v", left) |
| + } |
| + }) |
| + |
| + t.Run("group-writable ancestor", func(t *testing.T) { |
| + home, dir := install(t) |
| + lib := filepath.Join(home, ".local") |
| + if err := os.Chmod(lib, 0770); err != nil { |
| + t.Fatal(err) |
| + } |
| + refused(t, homeCLI(home, dir, body), "under a group-writable .local") |
| + if left := entries(t, dir); len(left) != 0 { |
| + t.Fatalf("wrote %v", left) |
| + } |
| + if err := os.Chmod(lib, 0702); err != nil { |
| + t.Fatal(err) |
| + } |
| + refused(t, homeCLI(home, dir, body), "under an other-writable .local") |
| + }) |
| + |
| + t.Run("a group-writable home", func(t *testing.T) { |
| + home, dir := install(t) |
| + if err := os.Chmod(home, 0770); err != nil { |
| + t.Fatal(err) |
| + } |
| + defer os.Chmod(home, 0700) |
| + refused(t, homeCLI(home, dir, body), "in a group-writable home") |
| + }) |
| + |
| + t.Run("a 0755 tool directory", func(t *testing.T) { |
| + home, dir := install(t) |
| + tools := filepath.Join(dir, "tools") |
| + if err := os.Mkdir(tools, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Chmod(tools, 0755); err != nil { |
| + t.Fatal(err) |
| + } |
| + cli := homeCLI(home, dir, body) |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatalf("a 0755 tools directory of ours: %v", err) |
| + } |
| + if info, _ := os.Lstat(tools); info.Mode().Perm() != 0700 { |
| + t.Fatalf("tools left %v", info.Mode()) |
| + } |
| + // Loosened after provisioning: no launch until Provision tightens it. |
| + if err := os.Chmod(ClaudeDir(dir), 0755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := cli.Verify(); err == nil { |
| + t.Fatal("verified under a 0755 version directory") |
| + } |
| + if err := cli.Launch(ctx, func(context.Context, []string) error { t.Fatal("started"); return nil }); !isUnavailable(err) { |
| + t.Fatalf("launch under a 0755 version directory: %v", err) |
| + } |
| + if err := cli.Provision(ctx, noFetch(t)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := cli.Verify(); err != nil { |
| + t.Fatalf("after tightening: %v", err) |
| + } |
| + }) |
| + |
| + t.Run("a foreign-owned directory", func(t *testing.T) { |
| + home, dir := install(t) |
| + defer func(was func(fs.FileInfo) int) { ownerOf = was }(ownerOf) |
| + real := ownerOf |
| + for _, name := range []string{filepath.Base(home), "lib", "tools", "claude-2.1.312", "claude"} { |
| + cli := homeCLI(home, dir, body) |
| + ownerOf = real |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + ownerOf = func(fi fs.FileInfo) int { |
| + if fi.Name() == name || name == filepath.Base(home) && fi.Name() == "." { |
| + return 4242 |
| + } |
| + return real(fi) |
| + } |
| + if _, err := cli.Verify(); err == nil { |
| + t.Fatalf("verified with %s owned by another user", name) |
| + } |
| + if err := cli.Provision(ctx, serve(body, nil)); err == nil && name != "claude" { |
| + t.Fatalf("provisioned with %s owned by another user", name) |
| + } |
| + } |
| + }) |
| +} |
| + |
| +func isUnavailable(err error) bool { |
| + var u *ClaudeUnavailable |
| + return errors.As(err, &u) && u.Reason() == "engineering_unavailable" |
| +} |
| + |
| +// swapIn renames a changed CLI over the verified one, as a writer racing |
| +// the launch would. |
| +func swapIn(t *testing.T, dir string, body []byte) { |
| + t.Helper() |
| + b := slices.Clone(body) |
| + b[0] ^= 1 |
| + next := ClaudePath(dir) + ".next" |
| + if err := os.WriteFile(next, b, 0700); err != nil { |
| + t.Error(err) |
| + } |
| + if err := os.Rename(next, ClaudePath(dir)); err != nil { |
| + t.Error(err) |
| + } |
| +} |
| + |
| +// A file swapped while it is being hashed, or after the hash and before |
| +// start, stops the launch: what the hash read is not what the path names. |
| +func TestLaunchRefusesARacingSwap(t *testing.T) { |
| + ctx := context.Background() |
| + body := fakeClaude() |
| + start := func(t *testing.T) func(context.Context, []string) error { |
| + return func(context.Context, []string) error { |
| + t.Fatal("started a CLI swapped during its launch") |
| + return nil |
| + } |
| + } |
| + for _, at := range []struct { |
| + name string |
| + hook *func() |
| + }{{"during hashing", &claudeDuringHash}, {"before start", &claudeBeforeStart}} { |
| + t.Run(at.name+": renamed over", func(t *testing.T) { |
| + home, dir := install(t) |
| + cli := homeCLI(home, dir, body) |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + defer func() { *at.hook = nil }() |
| + *at.hook = func() { swapIn(t, dir, body) } |
| + if err := cli.Launch(ctx, start(t)); !isUnavailable(err) { |
| + t.Fatalf("a swap %s: %v", at.name, err) |
| + } |
| + }) |
| + } |
| + // Written in place during hashing (a part already hashed), the |
| + // modification time put back: the change time still moves. |
| + t.Run("during hashing: written in place", func(t *testing.T) { |
| + home, dir := install(t) |
| + cli := homeCLI(home, dir, body) |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + info, err := os.Lstat(ClaudePath(dir)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer func() { claudeDuringHash = nil }() |
| + claudeDuringHash = func() { |
| + time.Sleep(10 * time.Millisecond) |
| + f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0) |
| + if err != nil { |
| + t.Error(err) |
| + return |
| + } |
| + _, err = f.WriteAt([]byte{body[0] ^ 1}, 0) |
| + f.Close() |
| + if err == nil { |
| + err = os.Chtimes(ClaudePath(dir), info.ModTime(), info.ModTime()) |
| + } |
| + if err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + if err := cli.Launch(ctx, start(t)); !isUnavailable(err) { |
| + t.Fatalf("written in place during hashing: %v", err) |
| + } |
| + }) |
| + // The capability report too: a file written while Verify hashes it |
| + // does not verify. |
| + t.Run("Verify: written in place during hashing", func(t *testing.T) { |
| + home, dir := install(t) |
| + cli := homeCLI(home, dir, body) |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + defer func() { claudeDuringHash = nil }() |
| + claudeDuringHash = func() { |
| + time.Sleep(10 * time.Millisecond) |
| + if f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0); err == nil { |
| + _, _ = f.WriteAt(body[:1], 0) |
| + f.Close() |
| + } |
| + } |
| + if _, err := cli.Verify(); err == nil { |
| + t.Fatal("verified a file written while it was hashed") |
| + } |
| + }) |
| + // Untouched: it starts, and the CLI's file stays open until start |
| + // returns (its inode cannot be reused for another file meanwhile). |
| + t.Run("untouched", func(t *testing.T) { |
| + home, dir := install(t) |
| + cli := homeCLI(home, dir, body) |
| + if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| + t.Fatal(err) |
| + } |
| + ran := false |
| + if err := cli.Launch(ctx, func(_ context.Context, argv []string) error { |
| + ran = argv[0] == ClaudePath(dir) |
| + return nil |
| + }); err != nil || !ran { |
| + t.Fatalf("untouched: %v %v", err, ran) |
| + } |
| + }) |
| +} |
diff --git a/internal/runner/mac/claude_stat_darwin.go b/internal/runner/mac/claude_stat_darwin.go| new file mode 100644 |
| index 00000000..61100712 |
| --- /dev/null |
| +++ b/internal/runner/mac/claude_stat_darwin.go |
| @@ -0,0 +1,25 @@ |
| +package mac |
| + |
| +import ( |
| + "io/fs" |
| + "syscall" |
| +) |
| + |
| +// fileKey names one state of one file: its device and inode, size, |
| +// modification and change times. The change time moves on any write, |
| +// chmod or rename, and no process can set it back. |
| +func fileKey(fi fs.FileInfo) (statKey, bool) { |
| + st, ok := fi.Sys().(*syscall.Stat_t) |
| + if !ok { |
| + return statKey{}, false |
| + } |
| + return statKey{dev: uint64(st.Dev), ino: uint64(st.Ino), size: st.Size, mtime: st.Mtimespec.Nano(), ctime: st.Ctimespec.Nano()}, true |
| +} |
| + |
| +// fileOwner is the uid that owns fi (-1 when unknown). |
| +func fileOwner(fi fs.FileInfo) int { |
| + if st, ok := fi.Sys().(*syscall.Stat_t); ok { |
| + return int(st.Uid) |
| + } |
| + return -1 |
| +} |
diff --git a/internal/runner/mac/claude_stat_linux.go b/internal/runner/mac/claude_stat_linux.go| new file mode 100644 |
| index 00000000..a0148b98 |
| --- /dev/null |
| +++ b/internal/runner/mac/claude_stat_linux.go |
| @@ -0,0 +1,25 @@ |
| +package mac |
| + |
| +import ( |
| + "io/fs" |
| + "syscall" |
| +) |
| + |
| +// fileKey names one state of one file: its device and inode, size, |
| +// modification and change times. The change time moves on any write, |
| +// chmod or rename, and no process can set it back. |
| +func fileKey(fi fs.FileInfo) (statKey, bool) { |
| + st, ok := fi.Sys().(*syscall.Stat_t) |
| + if !ok { |
| + return statKey{}, false |
| + } |
| + return statKey{dev: uint64(st.Dev), ino: uint64(st.Ino), size: st.Size, mtime: st.Mtim.Nano(), ctime: st.Ctim.Nano()}, true |
| +} |
| + |
| +// fileOwner is the uid that owns fi (-1 when unknown). |
| +func fileOwner(fi fs.FileInfo) int { |
| + if st, ok := fi.Sys().(*syscall.Stat_t); ok { |
| + return int(st.Uid) |
| + } |
| + return -1 |
| +} |
diff --git a/internal/runner/mac/claude_test.go b/internal/runner/mac/claude_test.go| index 8400f95e..1c5a14a7 100644 |
| --- a/internal/runner/mac/claude_test.go |
| +++ b/internal/runner/mac/claude_test.go |
| @@ -33,8 +33,17 @@ func digest(b []byte) string { |
| } |
| // claudeFor is the pinned CLI under installDir, pinned to body's digest. |
| +// Its home is installDir's parent (a test's own temporary directory); |
| +// both are made 0700, as a umask of 002 leaves t.TempDir's group-writable. |
| func claudeFor(installDir string, body []byte) ClaudeCLI { |
| - return ClaudeCLI{installDir: installDir, pin: digest(body)} |
| + for _, d := range []string{filepath.Dir(installDir), installDir} { |
| + if err := os.Chmod(d, 0700); err != nil { |
| + panic(err) |
| + } |
| + } |
| + c := NewClaudeCLI(filepath.Dir(installDir), installDir) |
| + c.pin = digest(body) |
| + return c |
| } |
| // serve answers every fetch with body and records the URLs asked. |
| @@ -127,7 +136,7 @@ func TestClaudePinDarwin(t *testing.T) { |
| t.Fatal("windows has a pin") |
| } |
| // The Mac worker's CLI checks against exactly that pin. |
| - if got := NewClaudeCLI("/Users/u/.local/lib/equipo-runner"); got.pin != want || got.installDir != "/Users/u/.local/lib/equipo-runner" { |
| + if got := NewClaudeCLI("/Users/u", "/Users/u/.local/lib/equipo-runner"); got.pin != want || got.home != "/Users/u" || got.installDir != "/Users/u/.local/lib/equipo-runner" { |
| t.Fatalf("NewClaudeCLI %+v", got) |
| } |
| } |
| @@ -187,7 +196,7 @@ func TestProvisionBoundsAndPlace(t *testing.T) { |
| if MaxClaudeBinary != 512<<20 { |
| t.Fatalf("bound %d", MaxClaudeBinary) |
| } |
| - if got := NewClaudeCLI("/i").bound(); got != MaxClaudeBinary { |
| + if got := NewClaudeCLI("/", "/i").bound(); got != MaxClaudeBinary { |
| t.Fatalf("the worker's bound is %d", got) |
| } |
| ctx := context.Background() |
| @@ -461,9 +470,6 @@ func TestVerifyBeforeEveryLaunch(t *testing.T) { |
| if err := cli.Launch(context.Background(), start); !unavailable(err) { |
| t.Fatalf("a swapped CLI: %v", err) |
| } |
| - if _, err := cli.Argv("--version"); !unavailable(err) { |
| - t.Fatalf("Argv of a swapped CLI: %v", err) |
| - } |
| // None in place at all, or no CLI configured: the same stop. |
| if err := os.Remove(ClaudePath(dir)); err != nil { |
| t.Fatal(err) |
| @@ -590,8 +596,8 @@ func TestClaudeDarwinBinaryMatchesPin(t *testing.T) { |
| if got := hex.EncodeToString(h.Sum(nil)); got != claude.ExecutableSHA256DarwinArm64 { |
| t.Fatalf("%s is %s, the pin is %s", path, got, claude.ExecutableSHA256DarwinArm64) |
| } |
| - dir := t.TempDir() |
| - cli := NewClaudeCLI(dir) |
| + home, dir := install(t) |
| + cli := NewClaudeCLI(home, dir) |
| var urls []string |
| fetch := func(_ context.Context, url string) (io.ReadCloser, error) { |
| urls = append(urls, url) |
diff --git a/internal/runner/thin/mac.go b/internal/runner/thin/mac.go| index f9720352..4eab1f44 100644 |
| --- a/internal/runner/thin/mac.go |
| +++ b/internal/runner/thin/mac.go |
| @@ -513,7 +513,7 @@ func macTools(installDir, home string, getenv func(string) string) mac.Tools { |
| return mac.Tools{Xcodebuild: "/usr/bin/xcodebuild", Xcrun: "/usr/bin/xcrun", Swift: "/usr/bin/swift", Git: getenv("EQUIPO_GIT_PATH"), |
| SRT: find("EQUIPO_SRT_PATH", "srt"), Node: find("EQUIPO_NODE_PATH", "node"), RG: find("EQUIPO_RG_PATH", "rg"), Codex: find("EQUIPO_CODEX_PATH", "codex"), |
| // Never looked up: the pinned CLI the worker provisions itself. |
| - Claude: mac.NewClaudeCLI(installDir)} |
| + Claude: mac.NewClaudeCLI(home, installDir)} |
| } |
| func executable(p string) bool { |
diff --git a/internal/runner/thin/mac_claude_test.go b/internal/runner/thin/mac_claude_test.go| index a451d992..91c11519 100644 |
| --- a/internal/runner/thin/mac_claude_test.go |
| +++ b/internal/runner/thin/mac_claude_test.go |
| @@ -14,7 +14,7 @@ import ( |
| // one under its install dir, which it verifies and never looks up. |
| func TestMacToolsNeverFindAClaude(t *testing.T) { |
| root := t.TempDir() |
| - install, home := filepath.Join(root, "lib"), filepath.Join(root, "home") |
| + home, install := filepath.Join(root, "home"), filepath.Join(root, "home", ".local", "lib", "equipo-runner") |
| planted := []string{filepath.Join(root, "env", "claude"), filepath.Join(home, ".local", "bin", "claude")} |
| for _, p := range planted { |
| if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { |
| @@ -36,7 +36,7 @@ func TestMacToolsNeverFindAClaude(t *testing.T) { |
| } |
| } |
| } |
| - if tools.Claude != mac.NewClaudeCLI(install) || tools.Claude.Version() != "" { |
| + if tools.Claude != mac.NewClaudeCLI(home, install) || tools.Claude.Version() != "" { |
| t.Fatalf("Tools.Claude %+v", tools.Claude) |
| } |
| } |