The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
Only narrow an existing runtime log, and only as its owner
Claude2bf581f5dc62
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.goindex ef14fb14..7fb42869 100644--- a/cli/beacon-hooks/internal/logging/logging.go+++ b/cli/beacon-hooks/internal/logging/logging.go@@ -681,9 +681,9 @@ func openEndpointRuntimeFile(path string, flag int) (*os.File, error) {return openEndpointRuntimeFileAs(path, path, flag)}-// openEndpointRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for. The-// chmod runs on an existing file too: it tightens a private log an older release left world-writable-// (same owner), and a failed chmod on an existing shared log this user does not own is ignored.+// openEndpointRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for, and+// settles its mode with asymptoteobserve.SettleRuntimeLogFileMode: a new file gets that mode+// whatever the umask, and an existing one is only ever narrowed toward it, by its owner.func openEndpointRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)existed := true@@ -697,7 +697,7 @@ func openEndpointRuntimeFileAs(path, logPath string, flag int) (*os.File, error)if err != nil {return nil, err}- if err := os.Chmod(path, fileMode); err != nil && !existed {+ if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {_ = f.Close()return nil, err}diff --git a/cli/beacon/internal/endpoint/datadog/pack/README.md b/cli/beacon/internal/endpoint/datadog/pack/README.mdindex 088099d4..adee6a68 100644--- a/cli/beacon/internal/endpoint/datadog/pack/README.md+++ b/cli/beacon/internal/endpoint/datadog/pack/README.md@@ -42,11 +42,23 @@ tail `/var/log/beacon-agent/runtime.jsonl` without per-user home directory ACLs.## User-Mode macOS Permissions-The Datadog Agent usually runs as `_dd-agent`. When tailing a user-mode Beacon-log inside a home directory, `_dd-agent` must be able to traverse the parent-directories and read the log file. If `datadog-agent status` reports-`permission denied`, either use Beacon system mode or grant a narrow ACL for the-configured path.+The Datadog Agent usually runs as `_dd-agent`. A user-mode Beacon log is private+to its user (0600, in directories Beacon creates 0700), so `_dd-agent` cannot+read it by default. Prefer Beacon system mode. To tail a user-mode log anyway,+grant `_dd-agent` traverse access to the parent directories and an inheriting+read ACL on the log directory, so the files rotation creates are covered too:++```bash+# macOS+chmod +a "user:_dd-agent allow list,search,read,file_inherit" ~/.beacon/endpoint/logs+# Linux (dd-agent): the default ACL covers new files; Beacon keeps its mask readable+setfacl -m u:dd-agent:rx ~/.beacon/endpoint/logs+setfacl -d -m u:dd-agent:r ~/.beacon/endpoint/logs+setfacl -m u:dd-agent:r ~/.beacon/endpoint/logs/runtime.jsonl+```++Beacon never widens an existing log and only removes its "other" bits, so a+grant made this way survives every append and rotation.## Validatediff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.goindex 19473eec..6a201b3a 100644--- a/cli/beacon/internal/endpoint/writer/writer.go+++ b/cli/beacon/internal/endpoint/writer/writer.go@@ -260,10 +260,9 @@ func openRuntimeFile(path string, flag int) (*os.File, error) {return openRuntimeFileAs(path, path, flag)}-// openRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for. The chmod-// runs on an existing file too: it repairs a umask-narrowed shared log, and it tightens a private-// log left world-writable by an older release (same owner, so it succeeds). A failed chmod on an-// existing file is ignored -- a hook appending to a shared log it does not own cannot chmod it.+// openRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for, and settles+// its mode with asymptoteobserve.SettleRuntimeLogFileMode: a new file gets that mode whatever the+// umask, and an existing one is only ever narrowed toward it, by its owner.func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)existed := true@@ -277,7 +276,7 @@ func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {if err != nil {return nil, err}- if err := os.Chmod(path, fileMode); err != nil && !existed {+ if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {_ = f.Close()return nil, err}diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.goindex 676b6c51..a87fe908 100644--- a/collector-builder/exporter/beaconjsonexporter/writer.go+++ b/collector-builder/exporter/beaconjsonexporter/writer.go@@ -245,8 +245,9 @@ func openRuntimeFile(path string, flag int) (*os.File, error) {return openRuntimeFileAs(path, path, flag)}-// openRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for. The chmod-// runs on an existing file too, which tightens a private log an older release left world-writable.+// openRuntimeFileAs opens path (the log or its lock) with the mode logPath calls for, and settles+// its mode with asymptoteobserve.SettleRuntimeLogFileMode: a new file gets that mode whatever the+// umask, and an existing one is only ever narrowed toward it, by its owner.func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)existed := true@@ -260,7 +261,7 @@ func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {if err != nil {return nil, err}- if err := os.Chmod(path, fileMode); err != nil && !existed {+ if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {_ = f.Close()return nil, err}diff --git a/docs/log-forwarding/datadog.mdx b/docs/log-forwarding/datadog.mdxindex fc6d838d..28da833c 100644--- a/docs/log-forwarding/datadog.mdx+++ b/docs/log-forwarding/datadog.mdx@@ -187,7 +187,7 @@ When everything is wired up, Beacon events appear under `service:beacon-endpointIf Datadog status shows `permission denied`, the Agent cannot read the runtime log. The Datadog Agent usually runs as `_dd-agent`.-For local user testing, either grant `_dd-agent` access to the user-mode log path or use system mode instead. For MDM deployment, use system mode so the Agent tails `/var/log/beacon-agent/runtime.jsonl`.+A user-mode log is private to its user (0600), so the Agent cannot read it by default. For MDM deployment, use system mode so the Agent tails `/var/log/beacon-agent/runtime.jsonl`. For local user testing, either use system mode or grant `_dd-agent` an inheriting read ACL on `~/.beacon/endpoint/logs` (on macOS, `chmod +a "user:_dd-agent allow list,search,read,file_inherit" ~/.beacon/endpoint/logs`; on Linux, `setfacl -m u:dd-agent:rx` and `setfacl -d -m u:dd-agent:r` on the directory plus `setfacl -m u:dd-agent:r` on the log) and traverse access to its parent directories. Beacon keeps such a grant working across appends and rotation.If Log Explorer is empty:diff --git a/pkg/asymptoteobserve/runtimelogacl_linux.go b/pkg/asymptoteobserve/runtimelogacl_linux.gonew file mode 100644index 00000000..46eb55ba--- /dev/null+++ b/pkg/asymptoteobserve/runtimelogacl_linux.go@@ -0,0 +1,16 @@+//go:build linux++package asymptoteobserve++import (+ "errors"++ "golang.org/x/sys/unix"+)++// hasPOSIXACL reports whether the file carries a POSIX access ACL, whose mask is the file's group+// bits. A variable so tests can stand in for a filesystem with ACLs.+var hasPOSIXACL = func(path string) bool {+ _, err := unix.Getxattr(path, "system.posix_acl_access", nil)+ return err == nil || errors.Is(err, unix.ERANGE)+}diff --git a/pkg/asymptoteobserve/runtimelogacl_other.go b/pkg/asymptoteobserve/runtimelogacl_other.gonew file mode 100644index 00000000..a7268b3b--- /dev/null+++ b/pkg/asymptoteobserve/runtimelogacl_other.go@@ -0,0 +1,7 @@+//go:build !linux++package asymptoteobserve++// hasPOSIXACL is false off Linux: macOS ACLs are separate from the mode bits, so narrowing the+// group bits there never cancels a grant, and Windows does not use the mode bits at all.+var hasPOSIXACL = func(string) bool { return false }diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.goindex 63e5690b..a09626ef 100644--- a/pkg/asymptoteobserve/runtimelogmode.go+++ b/pkg/asymptoteobserve/runtimelogmode.go@@ -91,11 +91,60 @@ func RuntimeLogIsPrivate(path string) bool {return processOwnsHome(home)}+// PrivateACLRuntimeLogFileMode is the mode a newly created private log takes when the directory+// it was created in hands it a POSIX ACL (a default ACL, `setfacl -d`). On Linux an ACL's mask is+// the file's group bits, so creating the file 0600 would set the mask to nothing and cancel the+// grant an administrator made on purpose -- the narrow read access a log shipper running as its+// own account is documented to need. Read for the mask keeps that grant working and still gives+// nobody else anything.+const PrivateACLRuntimeLogFileMode os.FileMode = 0o640++// SettleRuntimeLogFileMode sets the mode of a runtime log, its lock or an archive after it was+// opened. logPath is the log the file belongs to, which decides private or shared (see+// RuntimeLogModes); created says whether this open made the file.+//+// A file this open created gets the mode the log calls for, so a umask cannot narrow a shared log+// or widen a private one; an error there is returned.+//+// An existing file is changed only by a process that owns it, and only toward what the log calls+// for. Two cases this protects:+// - root running in a user's home (sudo keeping HOME) decides "shared" for that user's log, and+// must not chmod the user's private log back to world-writable;+// - a private log another account reads through a POSIX ACL keeps that grant: only the "other"+// bits are removed, never the group bits that carry the ACL mask, so the tightening that runs+// beside every append does not reset the mask and cut the reader off each time.+//+// Errors on an existing file are ignored: the append it runs beside must not fail over a mode.+func SettleRuntimeLogFileMode(path, logPath string, created bool) error {+ fileMode, _, private := RuntimeLogModes(logPath)+ if created {+ if private && hasPOSIXACL(path) {+ fileMode = PrivateACLRuntimeLogFileMode+ }+ return os.Chmod(path, fileMode)+ }+ info, err := os.Stat(path)+ if err != nil || !info.Mode().IsRegular() || !ownedByProcess(info) {+ return nil+ }+ current := info.Mode().Perm()+ want := fileMode+ if private {+ want = current &^ 0o007+ if !hasPOSIXACL(path) {+ want &^= 0o070+ }+ }+ if want != current {+ _ = os.Chmod(path, want)+ }+ return nil+}+// TightenPrivateRuntimeLogArchives narrows the rotated archives of a private log (path.1 ..-// path.archives) to PrivateRuntimeLogFileMode. Archives rotated before logs were private still-// carry the old shared mode, and they hold the same content as the live file. Best effort: a-// missing archive or a failed chmod is not an error, since the append it runs beside must not fail-// over an older file.+// path.archives) the way SettleRuntimeLogFileMode narrows the live file. Archives rotated before+// logs were private still carry the old shared mode, and they hold the same content as the live+// file. Best effort, like SettleRuntimeLogFileMode on an existing file.func TightenPrivateRuntimeLogArchives(path string, archives int) {if !RuntimeLogIsPrivate(path) {return@@ -106,9 +155,7 @@ func TightenPrivateRuntimeLogArchives(path string, archives int) {if err != nil || !info.Mode().IsRegular() {continue}- if info.Mode().Perm() != PrivateRuntimeLogFileMode {- _ = os.Chmod(archive, PrivateRuntimeLogFileMode)- }+ _ = SettleRuntimeLogFileMode(archive, path, false)}}diff --git a/pkg/asymptoteobserve/runtimelogmode_other.go b/pkg/asymptoteobserve/runtimelogmode_other.goindex ee2468d6..1bd433f8 100644--- a/pkg/asymptoteobserve/runtimelogmode_other.go+++ b/pkg/asymptoteobserve/runtimelogmode_other.go@@ -2,6 +2,11 @@package asymptoteobserve+import "os"+// processOwnsHome is always true off Unix: file modes there do not decide who can append (Windows// uses the profile's ACL), so the root-in-another-home hazard does not arise.func processOwnsHome(string) bool { return true }++// ownedByProcess is always true off Unix, where file modes do not decide who can read the log.+func ownedByProcess(os.FileInfo) bool { return true }diff --git a/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.gonew file mode 100644index 00000000..2c12dd3e--- /dev/null+++ b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go@@ -0,0 +1,102 @@+//go:build unix++package asymptoteobserve++import (+ "os"+ "path/filepath"+ "testing"+)++func withPOSIXACL(t *testing.T, has bool) {+ t.Helper()+ old := hasPOSIXACL+ t.Cleanup(func() { hasPOSIXACL = old })+ hasPOSIXACL = func(string) bool { return has }+}++func writeMode(t *testing.T, path string, mode os.FileMode) {+ t.Helper()+ if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(path, []byte("{}\n"), mode); err != nil {+ t.Fatal(err)+ }+ if err := os.Chmod(path, mode); err != nil {+ t.Fatal(err)+ }+}++func modeOf(t *testing.T, path string) os.FileMode {+ t.Helper()+ info, err := os.Stat(path)+ if err != nil {+ t.Fatal(err)+ }+ return info.Mode().Perm()+}++// A private log another account reads through a POSIX ACL keeps that grant: an existing file loses+// only its "other" bits, never the group bits that are the ACL's mask, and a file created where a+// default ACL applies keeps a read mask.+func TestSettlePrivateLogKeepsAnACLGrant(t *testing.T) {+ home := t.TempDir()+ setTestHome(t, home)+ log := filepath.Join(home, ".beacon", "endpoint", "logs", "runtime.jsonl")++ withPOSIXACL(t, true)+ writeMode(t, log, 0o666)+ if err := SettleRuntimeLogFileMode(log, log, false); err != nil {+ t.Fatal(err)+ }+ if got := modeOf(t, log); got != 0o660 {+ t.Fatalf("existing log with an ACL = %o, want 0660 (other bits removed, mask kept)", got)+ }++ created := filepath.Join(filepath.Dir(log), "runtime.jsonl.lock")+ writeMode(t, created, 0o600)+ if err := SettleRuntimeLogFileMode(created, log, true); err != nil {+ t.Fatal(err)+ }+ if got := modeOf(t, created); got != PrivateACLRuntimeLogFileMode {+ t.Fatalf("new file under a default ACL = %o, want %o", got, PrivateACLRuntimeLogFileMode)+ }++ withPOSIXACL(t, false)+ writeMode(t, log, 0o666)+ if err := SettleRuntimeLogFileMode(log, log, false); err != nil {+ t.Fatal(err)+ }+ if got := modeOf(t, log); got != PrivateRuntimeLogFileMode {+ t.Fatalf("existing log without an ACL = %o, want %o", got, PrivateRuntimeLogFileMode)+ }+}++// A writer never widens or narrows a file it does not own. Root running in a user's home (sudo+// keeping HOME) decides "shared" for that user's log and must leave their private log as it is.+func TestSettleLeavesAFileItDoesNotOwn(t *testing.T) {+ if os.Geteuid() != 0 {+ t.Skip("needs root to own a home and a log as another user")+ }+ const otherUID, otherGID = 54321, 54321+ home := t.TempDir()+ if err := os.Chown(home, otherUID, otherGID); err != nil {+ t.Fatal(err)+ }+ setTestHome(t, home)+ log := filepath.Join(home, "runtime.jsonl")+ writeMode(t, log, 0o600)+ if err := os.Chown(log, otherUID, otherGID); err != nil {+ t.Fatal(err)+ }+ if RuntimeLogIsPrivate(log) {+ t.Fatal("root in a home it does not own must decide shared")+ }+ if err := SettleRuntimeLogFileMode(log, log, false); err != nil {+ t.Fatal(err)+ }+ if got := modeOf(t, log); got != 0o600 {+ t.Fatalf("root settled the user's private log to %o, want it left at 0600", got)+ }+}diff --git a/pkg/asymptoteobserve/runtimelogmode_unix.go b/pkg/asymptoteobserve/runtimelogmode_unix.goindex 89b1c14b..b3cff2f8 100644--- a/pkg/asymptoteobserve/runtimelogmode_unix.go+++ b/pkg/asymptoteobserve/runtimelogmode_unix.go@@ -24,3 +24,13 @@ func processOwnsHome(home string) bool {}return stat.Uid == 0}++// ownedByProcess reports whether this process owns the file, the only case in which a writer+// changes an existing log's mode.+func ownedByProcess(info os.FileInfo) bool {+ stat, ok := info.Sys().(*syscall.Stat_t)+ if !ok {+ return false+ }+ return int(stat.Uid) == os.Geteuid()+}