Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Confine the root inventory scan of the console user's home

Claude244be7e5f1f8
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index 8ce76642..cd5b56de 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -31,6 +31,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/service"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/writer"
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/version"
)
@@ -562,6 +563,18 @@ func writeInventoryHeartbeat(cfg endpointconfig.Config, settings endpointconfig.
previous := state.LastSnapshotDigest
writeSnapshot := false
next := state
+ release := func() {}
+ if !scope.NoConsoleUser {
+ confined, err := confineInventoryScan(scope.HomeDir)
+ if err != nil {
+ // Never scan a home root does not own without the confinement; the liveness heartbeat
+ // still goes out.
+ fmt.Fprintf(os.Stderr, "warning: inventory scan skipped: %v\n", err)
+ scope = heartbeatScope{NoConsoleUser: true}
+ } else {
+ release = confined
+ }
+ }
if scope.NoConsoleUser {
inventoryResult = endpointinventory.Result{
GeneratedAt: now.Format(time.RFC3339),
@@ -576,6 +589,7 @@ func writeInventoryHeartbeat(cfg endpointconfig.Config, settings endpointconfig.
IncludeContents: settings.IncludeContents,
MaxContentBytes: settings.MaxContentBytes,
})
+ release()
digest = endpointinventory.SnapshotDigest(inventoryResult)
// Compare against the last digest for *this* home: a multi-user machine alternates
// console users, and a switch is not a change in anyone's inventory.
@@ -1848,3 +1862,26 @@ func syntheticEvent(destination string) schema.Event {
event.Destination = &schema.DestinationInfo{Type: destination, Mode: mode, Status: "configured"}
return event
}
+
+// confineInventoryScan confines the inventory scan of home with sessionfs when this process is root
+// scanning a home it does not own -- the system job reading the console user's home. That user can
+// plant anything there: a symlink to a root-only file (whose content or hash would land in the
+// world-readable system log), a FIFO that hangs the job while it holds the inventory lock, or a
+// link to /dev/zero. Confined, those read as absent. It returns the release to call after the scan.
+func confineInventoryScan(home string) (func(), error) {
+ if strings.TrimSpace(home) == "" || os.Geteuid() != 0 {
+ return func() {}, nil
+ }
+ uid, err := sessionfs.OwnerUID(home)
+ if err != nil {
+ return nil, fmt.Errorf("could not read the owner of %s: %w", home, err)
+ }
+ if uid == 0 {
+ return func() {}, nil
+ }
+ confined, err := sessionfs.Confine(home, uid, sessionfs.Options{})
+ if err != nil {
+ return nil, err
+ }
+ return confined.Release, nil
+}
diff --git a/cli/beacon/internal/endpoint/inventory/confine_unix_test.go b/cli/beacon/internal/endpoint/inventory/confine_unix_test.go
new file mode 100644
index 00000000..ac24eee5
--- /dev/null
+++ b/cli/beacon/internal/endpoint/inventory/confine_unix_test.go
@@ -0,0 +1,95 @@
+//go:build unix
+
+package inventory
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "strings"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"
+)
+
+// The system inventory job scans the console user's home as root under a sessionfs confinement.
+// What that user plants there -- a symlink to a root-only file, a FIFO -- must read as absent
+// rather than be read with root's privileges or hang the job.
+func TestConfinedScanRefusesPlantedLinksAndFIFOs(t *testing.T) {
+ if os.Geteuid() != 0 {
+ t.Skip("needs root to give the home a different owner")
+ }
+ const uid = 4242
+ home := t.TempDir()
+ secret := filepath.Join(t.TempDir(), "root-only")
+ if err := os.WriteFile(secret, []byte(`{"mcpServers":{"leak":{"command":"TOPSECRET"}}}`), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ for _, dir := range []string{".claude", ".codex", ".cursor"} {
+ if err := os.MkdirAll(filepath.Join(home, dir), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := syscall.Mkfifo(filepath.Join(home, ".claude", "settings.json"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(secret, filepath.Join(home, ".cursor", "mcp.json")); err != nil {
+ t.Fatal(err)
+ }
+ owned := filepath.Join(home, ".codex", "config.toml")
+ if err := os.WriteFile(owned, []byte("model = \"x\"\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := filepath.Walk(home, func(path string, _ os.FileInfo, _ error) error { return os.Lchown(path, uid, uid) }); err != nil {
+ t.Fatal(err)
+ }
+
+ confined, err := sessionfs.Confine(home, uid, sessionfs.Options{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer confined.Release()
+
+ done := make(chan Result, 1)
+ go func() {
+ done <- Scan(Options{HomeDir: home, SkipProjectScope: true, Runtimes: []string{"claude_code", "codex_cli", "cursor"}, IncludeContents: true})
+ }()
+ var result Result
+ select {
+ case result = <-done:
+ case <-time.After(10 * time.Second):
+ t.Fatal("confined scan hung on a planted FIFO")
+ }
+ for _, config := range result.Configs {
+ if blob, _ := json.Marshal(config); strings.Contains(string(blob), "TOPSECRET") {
+ t.Fatalf("confined scan read a symlinked root-only file: %+v", config)
+ }
+ if config.Readable && config.PathHash == hashString(filepath.Join(home, ".cursor", "mcp.json")) {
+ t.Fatalf("confined scan read through a symlink out of the home: %+v", config)
+ }
+ }
+ if len(result.MCPServers) != 0 {
+ t.Fatalf("confined scan parsed MCP servers from a symlinked root-only file: %+v", result.MCPServers)
+ }
+ var sawOwned bool
+ for _, config := range result.Configs {
+ if config.PathHash == hashString(owned) && config.Readable {
+ sawOwned = true
+ }
+ }
+ if !sawOwned {
+ t.Fatal("confined scan did not read the user's own config")
+ }
+}
+
+func TestReadFilePathCapsSize(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "big.json")
+ if err := os.WriteFile(path, make([]byte, maxInventoryFileBytes+1), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := readFilePath(path); err == nil {
+ t.Fatal("readFilePath read past the cap")
+ }
+}
diff --git a/cli/beacon/internal/endpoint/inventory/fs.go b/cli/beacon/internal/endpoint/inventory/fs.go
new file mode 100644
index 00000000..9589125d
--- /dev/null
+++ b/cli/beacon/internal/endpoint/inventory/fs.go
@@ -0,0 +1,66 @@
+package inventory
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "io/fs"
+ "os"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/sessionfs"
+)
+
+// maxInventoryFileBytes caps one config or skill manifest read. Real ones are a few KiB; the cap
+// keeps a planted /dev/zero-sized file from exhausting memory.
+const maxInventoryFileBytes = 8 << 20
+
+// The system inventory job runs as root over the console user's home. The cmd layer confines it
+// with sessionfs for that run, so every read of a path inside the home refuses symlinks out of it,
+// FIFOs, devices and files the user does not own (they read as absent). Paths outside the home --
+// system-wide managed settings only an administrator can write -- are read directly. Unconfined
+// (user mode), these are the plain os calls.
+
+func statPath(path string) (fs.FileInfo, error) {
+ if sessionfs.Confined(path) {
+ return sessionfs.Stat(path)
+ }
+ return os.Stat(path)
+}
+
+func readDirPath(path string) ([]fs.DirEntry, error) {
+ if sessionfs.Confined(path) {
+ return sessionfs.ReadDir(path)
+ }
+ return os.ReadDir(path)
+}
+
+func readFilePath(path string) ([]byte, error) {
+ var (
+ f *os.File
+ err error
+ )
+ if sessionfs.Confined(path) {
+ f, err = sessionfs.Open(path)
+ } else {
+ f, err = os.Open(path)
+ }
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil {
+ return nil, err
+ }
+ if !info.Mode().IsRegular() {
+ return nil, &fs.PathError{Op: "read", Path: path, Err: errors.New("not a regular file")}
+ }
+ data, err := io.ReadAll(io.LimitReader(f, maxInventoryFileBytes+1))
+ if err != nil {
+ return nil, err
+ }
+ if len(data) > maxInventoryFileBytes {
+ return nil, &fs.PathError{Op: "read", Path: path, Err: fmt.Errorf("larger than %d bytes", maxInventoryFileBytes)}
+ }
+ return data, nil
+}
diff --git a/cli/beacon/internal/endpoint/inventory/inventory.go b/cli/beacon/internal/endpoint/inventory/inventory.go
index 645fe2bb..b523703c 100644
--- a/cli/beacon/internal/endpoint/inventory/inventory.go
+++ b/cli/beacon/internal/endpoint/inventory/inventory.go
@@ -730,7 +730,7 @@ func inspectCandidate(item candidate, redaction string, co contentOptions) (Conf
ParserStatus: StatusNotFound,
Redaction: redaction,
}
- info, err := os.Stat(item.path)
+ info, err := statPath(item.path)
if err != nil {
config.Reason = errReason(err)
if !os.IsNotExist(err) {
@@ -745,7 +745,7 @@ func inspectCandidate(item candidate, redaction string, co contentOptions) (Conf
config.Reason = "path is a directory"
return config, nil
}
- data, err := os.ReadFile(item.path)
+ data, err := readFilePath(item.path)
if err != nil {
config.ParserStatus = StatusUnreadable
config.Reason = errReason(err)
diff --git a/cli/beacon/internal/endpoint/inventory/skills.go b/cli/beacon/internal/endpoint/inventory/skills.go
index d3568d9c..5c143acf 100644
--- a/cli/beacon/internal/endpoint/inventory/skills.go
+++ b/cli/beacon/internal/endpoint/inventory/skills.go
@@ -101,7 +101,7 @@ func inspectSkillRoot(root skillRoot, redaction string, co contentOptions) []Ski
ParserStatus: StatusNotFound,
Redaction: redaction,
}
- info, err := os.Stat(root.path)
+ info, err := statPath(root.path)
if err != nil {
base.Reason = errReason(err)
if !os.IsNotExist(err) {
@@ -115,7 +115,7 @@ func inspectSkillRoot(root skillRoot, redaction string, co contentOptions) []Ski
base.Reason = "path is not a directory"
return []Skill{base}
}
- entries, err := os.ReadDir(root.path)
+ entries, err := readDirPath(root.path)
if err != nil {
base.ParserStatus = StatusUnreadable
base.Reason = errReason(err)
@@ -133,7 +133,7 @@ func inspectSkillRoot(root skillRoot, redaction string, co contentOptions) []Ski
continue
}
manifestPath := filepath.Join(root.path, entry.Name(), "SKILL.md")
- if _, err := os.Stat(manifestPath); os.IsNotExist(err) {
+ if _, err := statPath(manifestPath); os.IsNotExist(err) {
continue
}
skills = append(skills, inspectSkillManifest(root, entry.Name(), manifestPath, redaction, co))
@@ -157,7 +157,7 @@ func inspectSkillManifest(root skillRoot, name, manifestPath, redaction string,
ParserStatus: StatusNotFound,
Redaction: redaction,
}
- info, err := os.Stat(manifestPath)
+ info, err := statPath(manifestPath)
if err != nil {
skill.Reason = errReason(err)
if !os.IsNotExist(err) {
@@ -172,7 +172,7 @@ func inspectSkillManifest(root skillRoot, name, manifestPath, redaction string,
skill.Reason = "manifest path is a directory"
return skill
}
- data, err := os.ReadFile(manifestPath)
+ data, err := readFilePath(manifestPath)
if err != nil {
skill.ParserStatus = StatusUnreadable
skill.Reason = errReason(err)
diff --git a/cli/beacon/internal/sessionfs/sessionfs.go b/cli/beacon/internal/sessionfs/sessionfs.go
index c5df9197..91de162d 100644
--- a/cli/beacon/internal/sessionfs/sessionfs.go
+++ b/cli/beacon/internal/sessionfs/sessionfs.go
@@ -186,6 +186,18 @@ func (c *Confinement) check(op, path string, info fs.FileInfo) error {
return nil
}
+// Confined reports whether reads of path go through an active confinement: one is active and path
+// lies inside its home. A caller that also reads paths outside the home (root-owned system
+// locations) uses this to read only those directly.
+func Confined(path string) bool {
+ c := active.Load()
+ if c == nil {
+ return false
+ }
+ _, ok := c.rel(path)
+ return ok
+}
+
// Open opens a file or directory for reading.
func Open(path string) (*os.File, error) {
c := active.Load()