diff --git a/changelog.d/fixed-bob-preflight-per-agent-home.md b/changelog.d/fixed-bob-preflight-per-agent-home.md new file mode 100644 index 0000000000..1bebba9724 --- /dev/null +++ b/changelog.d/fixed-bob-preflight-per-agent-home.md @@ -0,0 +1 @@ +- The bob startup preflight now checks that the per-agent `~/.bob` (where bob writes its installation ID and chat recordings since per-agent homes landed) is writable, instead of probing the shared `/data/home/.bob` that only holds the bridged settings, so an unwritable state directory is reported against the directory bob actually uses (#17062). diff --git a/src/crux/agent/bob_settings.go b/src/crux/agent/bob_settings.go index ed8d29f380..4c30e41ba1 100644 --- a/src/crux/agent/bob_settings.go +++ b/src/crux/agent/bob_settings.go @@ -5,6 +5,7 @@ import ( "io/fs" "os" "path/filepath" + "syscall" "github.com/hivecommons/hive/crux/config" ) @@ -21,6 +22,11 @@ const ( // bobKeyOtherReadBit is `o+r`. Also sufficient for the agent to read, but // it means the secret is world-readable, which is worth flagging. bobKeyOtherReadBit fs.FileMode = 0o004 + // bobStateOwnerWriteBit / bobStateOwnerExecBit are `u+w` / `u+x` — what + // decides access when the agent UID itself OWNS the directory, as it does + // for the per-agent ~/.bob that bob's own mkdirSync creates (0755/0700). + bobStateOwnerWriteBit fs.FileMode = 0o200 + bobStateOwnerExecBit fs.FileMode = 0o100 // bobStateGroupWriteBit is `g+w` — the bit that lets an agent UID create // or replace files inside a dev:node-owned bob state directory. bobStateGroupWriteBit fs.FileMode = 0o020 @@ -156,6 +162,20 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui filepath.Join(workDir, config.BobStateDirName), } + // The per-agent home is provisioned by setupInteractiveHome INSIDE the + // launch, which runs after this preflight, so on a per-UID bob agent's + // very first launch $HOME does not exist yet. Walking up from a missing + // home lands on /data/home/agents, which is deliberately 0755 dev-owned + // (no agent writes there), and the group test then reports a bogus + // "chmod 2775 /data/home/agents" on every new agent. The home that is + // about to be created is agent-owned 0700, so its ~/.bob is writable by + // construction; only the workspace dir is worth probing in that shape. + if _, err := os.Stat(home); err != nil && os.IsNotExist(err) { + m.logger.Debug("bob home does not exist yet; skipping $HOME/.bob probes until the launch provisions it", + "agent", agentName, "home", home, "uid", uid) + probeDirs = probeDirs[2:] + } + for _, dir := range probeDirs { info, err := os.Stat(dir) if err != nil { @@ -171,11 +191,11 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui if parent := nearestExistingDir(filepath.Dir(dir)); parent != "" { if pInfo, pErr := os.Stat(parent); pErr == nil { pMode := pInfo.Mode().Perm() - if pMode&bobStateGroupWriteBit == 0 || pMode&bobKeyGroupExecBit == 0 { + if !bobStateDirWritableBy(pInfo, uid) { m.logger.Error("bob state dir does not exist and its parent is not writable by the agent UID; bob's first-run mkdir will fail with EACCES", "agent", agentName, "dir", dir, "parent", parent, "parent_mode", fs.FileMode(pMode).String(), "uid", uid, - "remedy", "chmod 2775 "+parent+" and ensure it is group-owned by node") + "remedy", bobStateDirRemedy(pInfo, parent, uid, "2775")) unwritable = append(unwritable, dir) } } @@ -197,17 +217,52 @@ func (m *Manager) verifyBobStateDirsWritable(agentName, home, workDir string, ui // or replace a file inside. Write alone is not enough — without // execute the path cannot be traversed to open the file by name. mode := info.Mode().Perm() - if mode&bobStateGroupWriteBit == 0 || mode&bobKeyGroupExecBit == 0 { + if !bobStateDirWritableBy(info, uid) { m.logger.Error("bob state dir is not writable by the agent UID; bob will report \"error saving your latest settings changes\" and \"Failed to initialize logger\"", "agent", agentName, "dir", dir, "mode", fs.FileMode(mode).String(), "uid", uid, - "remedy", "chmod 770 "+dir+" and ensure it is group-owned by node") + "remedy", bobStateDirRemedy(info, dir, uid, "770")) unwritable = append(unwritable, dir) } } return unwritable } +// bobStateDirRemedy returns the chmod an operator should run for a state dir +// that bobStateDirWritableBy rejected. The advice must match the branch that +// failed: a dir OWNED by the agent UID (bob's own ~/.bob) was judged by its +// owner bits, so widening the group — the remedy for dev-owned dirs reached +// through group `node` — would not fix it and would hand every agent UID on +// the node write access to one agent's private state. groupMode is the mode +// to prescribe for the dev-owned shape ("2775" for a parent, "770" for the +// dir itself). +func bobStateDirRemedy(info fs.FileInfo, dir string, uid int, groupMode string) string { + if st, ok := info.Sys().(*syscall.Stat_t); ok && int(st.Uid) == uid { + return "chmod u+rwx " + dir + " (agent-owned; do not widen group access)" + } + return "chmod " + groupMode + " " + dir + " and ensure it is group-owned by node" +} + +// bobStateDirWritableBy reports whether the agent UID can create or replace +// files inside the directory described by info. +// +// Two ownership shapes reach this probe. Dev-owned dirs (the shared home, the +// per-agent workspace) are reached through group `node`, so group write+execute +// decides. The per-agent ~/.bob is different: bob creates it itself, AS THE +// AGENT UID, with mkdirSync (0755 or 0700), and no permissions-watcher root +// widens it. Testing that dir for group bits flags it as unwritable on every +// launch after first run even though its owner can write to it just fine, so +// when the directory is owned by the agent UID the owner bits decide instead. +// Write alone is never enough — without execute the path cannot be traversed +// to open the file by name. +func bobStateDirWritableBy(info fs.FileInfo, uid int) bool { + mode := info.Mode().Perm() + if st, ok := info.Sys().(*syscall.Stat_t); ok && int(st.Uid) == uid { + return mode&bobStateOwnerWriteBit != 0 && mode&bobStateOwnerExecBit != 0 + } + return mode&bobStateGroupWriteBit != 0 && mode&bobKeyGroupExecBit != 0 +} + // ensureBobAuthSettings makes hive the owner of bob's auth-selection settings. // // Why this is needed at all: BOBSHELL_DEFAULT_AUTH_TYPE is only a fallback @@ -439,3 +494,10 @@ func setBobV2AutoUpdateDisabled(settings map[string]any) bool { shell[config.BobV2AutoUpdateKey] = false return true } + +// bobStateHome is the HOME bob writes its installation_id and tmp/ chat +// recordings under. Since per-agent homes it is not the shared bobSharedHome, +// which holds only the bridged settings. +func bobStateHome(agentName string, uid int, backend string) string { + return AgentHome(agentName, uid, backend) +} diff --git a/src/crux/agent/bob_settings_test.go b/src/crux/agent/bob_settings_test.go index ae7ce0dac8..72a1c72dac 100644 --- a/src/crux/agent/bob_settings_test.go +++ b/src/crux/agent/bob_settings_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "testing" "github.com/hivecommons/hive/crux/config" @@ -674,3 +675,159 @@ func TestVerifyBobStateDirsWritableIgnoresNonDirectoryStatePath(t *testing.T) { t.Errorf("verifyBobStateDirsWritable() = %v for non-directory state path, want advisory warning only", got) } } + +// TestBobStateHomeIsPerAgentHome pins that the preflight probes the per-agent +// ~/.bob bob actually writes to, not the shared /data/home/.bob. +func TestBobStateHomeIsPerAgentHome(t *testing.T) { + t.Setenv("HIVE_SHARED_AGENT_HOME", "") + sharedRoot := t.TempDir() + orig := sharedAgentHome + sharedAgentHome = sharedRoot + t.Cleanup(func() { sharedAgentHome = orig }) + + got := bobStateHome("tester", 2004, bobBackend) + if want := interactiveHomePath("tester"); got != want { + t.Fatalf("bobStateHome() = %q, want per-agent home %q", got, want) + } + if got == bobSharedHome || got == sharedRoot { + t.Errorf("bobStateHome() = %q, must not be the shared home", got) + } + + // An unwritable per-agent ~/.bob is reported against the per-agent dir. + agentBob := filepath.Dir(bobSettingsPath(got)) + if err := os.MkdirAll(agentBob, 0o770); err != nil { + t.Fatalf("mkdir: %v", err) + } + if err := os.Chmod(agentBob, 0o555); err != nil { + t.Fatalf("chmod: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(agentBob, 0o770) }) + workDir := t.TempDir() + if err := os.Chmod(workDir, 0o770); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + m := &Manager{logger: discardLogger()} + unwritable := m.verifyBobStateDirsWritable("tester", got, workDir, 2004) + if len(unwritable) == 0 || unwritable[0] != agentBob { + t.Errorf("verifyBobStateDirsWritable() = %v, want first entry %s", unwritable, agentBob) + } +} + +// TestVerifyBobStateDirsWritableAcceptsAgentOwnedDir pins that a per-agent +// ~/.bob created by bob itself — owned by the agent UID with mkdirSync's +// 0755/0700 and no group write bit — is NOT reported as unwritable. The probe +// must judge an agent-owned dir by its owner bits, otherwise every launch after +// first run logs a false "bob state dir is not writable" error. The same holds +// for a missing dir whose nearest existing parent is agent-owned 0700. +func TestVerifyBobStateDirsWritableAcceptsAgentOwnedDir(t *testing.T) { + uid := os.Getuid() + if uid <= 0 { + t.Skip("probe is a no-op for uid 0; cannot model an agent-owned dir as root") + } + home := t.TempDir() + workDir := t.TempDir() + // t.TempDir() is 0700 and owned by this process, which is exactly the + // shape bob's own mkdirSync leaves behind for the agent UID. + agentBob := filepath.Dir(bobSettingsPath(home)) + if err := os.Mkdir(agentBob, 0o700); err != nil { + t.Fatalf("mkdir: %v", err) + } + if err := os.Chmod(agentBob, 0o700); err != nil { + t.Fatalf("chmod: %v", err) + } + m := &Manager{logger: discardLogger()} + if got := m.verifyBobStateDirsWritable("tester", home, workDir, uid); len(got) != 0 { + t.Errorf("verifyBobStateDirsWritable() = %v for agent-owned 0700 dirs, want none", got) + } + + // An agent-owned dir with the owner write bit stripped IS still flagged: + // ownership does not short-circuit the check, it only selects which bits + // are consulted. + if err := os.Chmod(agentBob, 0o500); err != nil { + t.Fatalf("chmod: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(agentBob, 0o700) }) + // Both ~/.bob itself and the missing ~/.bob/settings (whose nearest + // existing parent is that same 0500 dir) are reported; nothing else is. + got := m.verifyBobStateDirsWritable("tester", home, workDir, uid) + wantV2 := filepath.Dir(bobV2SettingsPath(home)) + if len(got) != 2 || got[0] != agentBob || got[1] != wantV2 { + t.Errorf("verifyBobStateDirsWritable() = %v for agent-owned 0500 dir, want [%s %s]", got, agentBob, wantV2) + } + + // A dir owned by someone OTHER than the agent UID still falls back to the + // group test, so the dev-owned shared-home behaviour is unchanged. + if err := os.Chmod(agentBob, 0o700); err != nil { + t.Fatalf("chmod: %v", err) + } + if got := m.verifyBobStateDirsWritable("tester", home, workDir, uid+1); len(got) == 0 || got[0] != agentBob { + t.Errorf("verifyBobStateDirsWritable() = %v for foreign-owned 0700 dir, want [%s ...]", got, agentBob) + } +} + +// TestVerifyBobStateDirsWritableSkipsHomeProbesWhenHomeMissing pins the +// first-launch shape of a per-UID bob agent: bobPreflightUnlocked runs BEFORE +// launchInTmux, and the per-agent home is only created by setupInteractiveHome +// inside the launch. With the home absent, the ancestor walk would land on the +// home root — deliberately 0755 dev-owned, nothing is written there — and emit +// two bogus "chmod 2775 " errors on every new agent. The $HOME/.bob +// probes must be skipped in that shape; the workspace dir is still probed. +func TestVerifyBobStateDirsWritableSkipsHomeProbesWhenHomeMissing(t *testing.T) { + root := t.TempDir() + // The production /data/home/agents mode: traversable, not group-writable. + if err := os.Chmod(root, 0o755); err != nil { + t.Fatalf("chmod root: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(root, 0o770) }) + home := filepath.Join(root, "tester") + + workDir := t.TempDir() + if err := os.Chmod(workDir, 0o770); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + + m := &Manager{logger: discardLogger()} + if got := m.verifyBobStateDirsWritable("tester", home, workDir, 2004); len(got) != 0 { + t.Errorf("verifyBobStateDirsWritable() = %v for not-yet-created home, want none", got) + } + + // The workspace dir is NOT exempt: a missing home only silences the two + // $HOME/.bob probes, never the /.bob one. + if err := os.Chmod(workDir, 0o750); err != nil { + t.Fatalf("chmod workDir: %v", err) + } + t.Cleanup(func() { _ = os.Chmod(workDir, 0o770) }) + got := m.verifyBobStateDirsWritable("tester", home, workDir, 2004) + wantDir := filepath.Join(workDir, config.BobStateDirName) + if len(got) != 1 || got[0] != wantDir { + t.Errorf("verifyBobStateDirsWritable() = %v for missing home with unwritable workDir, want [%s]", got, wantDir) + } +} + +// TestBobStateDirRemedy pins that the logged remedy matches the branch that +// failed: an agent-owned dir needs its OWNER bits restored, and must never be +// told to widen group access (which would undo per-agent isolation), while a +// dev-owned dir reached through group `node` keeps the group-mode advice. +func TestBobStateDirRemedy(t *testing.T) { + uid := os.Getuid() + if uid <= 0 { + t.Skip("cannot model an agent-owned dir as root") + } + dir := t.TempDir() + info, err := os.Stat(dir) + if err != nil { + t.Fatalf("stat: %v", err) + } + + got := bobStateDirRemedy(info, dir, uid, "770") + if !strings.HasPrefix(got, "chmod u+rwx "+dir) || strings.Contains(got, "node") { + t.Errorf("agent-owned remedy = %q, want owner-bit chmod with no group widening", got) + } + + for _, mode := range []string{"770", "2775"} { + got := bobStateDirRemedy(info, dir, uid+1, mode) + if want := "chmod " + mode + " " + dir + " and ensure it is group-owned by node"; got != want { + t.Errorf("foreign-owned remedy(%s) = %q, want %q", mode, got, want) + } + } +} diff --git a/src/crux/agent/manager.go b/src/crux/agent/manager.go index f5d5043405..15ffff292e 100644 --- a/src/crux/agent/manager.go +++ b/src/crux/agent/manager.go @@ -1654,5 +1654,5 @@ func (m *Manager) bobPreflightUnlocked(agent *AgentProcess) { // across the agent fleet. m.ensureBobAuthSettings(agent.Name, bobSharedHome) m.verifyBobKeyReadable(agent.Name, m.bobKeyFilePath(), agent.UID) - _ = m.verifyBobStateDirsWritable(agent.Name, bobSharedHome, m.workDir+"/"+agent.Name, agent.UID) + _ = m.verifyBobStateDirsWritable(agent.Name, bobStateHome(agent.Name, agent.UID, backend), m.workDir+"/"+agent.Name, agent.UID) }