Skip to content

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

asymptoteobserve: restricted access mode for the system runtime log

Claude08ba978d4158
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.go
index 8f3bbcb3..0c5440b3 100644
--- a/cli/beacon-hooks/internal/logging/logging.go
+++ b/cli/beacon-hooks/internal/logging/logging.go
@@ -727,7 +727,7 @@ func rotateEndpointLogIfNeeded(path string, maxSize int64, archives int, nextWri
if err := os.Rename(path, path+".1"); err != nil {
return err
}
- asymptoteobserve.TightenPrivateRuntimeLogArchives(path, archives)
+ asymptoteobserve.TightenRuntimeLogArchives(path, archives)
return nil
}
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index bf0c0005..6d6c9519 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -371,7 +371,7 @@ func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int
if err := os.Rename(path, rotated); err != nil {
return err
}
- asymptoteobserve.TightenPrivateRuntimeLogArchives(path, archives)
+ asymptoteobserve.TightenRuntimeLogArchives(path, archives)
return nil
}
diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.go
index 017fac7b..aadc0b2f 100644
--- a/collector-builder/exporter/beaconjsonexporter/writer.go
+++ b/collector-builder/exporter/beaconjsonexporter/writer.go
@@ -283,7 +283,7 @@ func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int
if err := os.Rename(path, path+".1"); err != nil {
return err
}
- asymptoteobserve.TightenPrivateRuntimeLogArchives(path, archives)
+ asymptoteobserve.TightenRuntimeLogArchives(path, archives)
return nil
}
diff --git a/pkg/asymptoteobserve/runtimelogaccess.go b/pkg/asymptoteobserve/runtimelogaccess.go
new file mode 100644
index 00000000..44a72afa
--- /dev/null
+++ b/pkg/asymptoteobserve/runtimelogaccess.go
@@ -0,0 +1,115 @@
+package asymptoteobserve
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// Log access modes for a machine-wide (system-mode) runtime log.
+//
+// Shared is how the system log has always worked: hooks running as each logged-in user append to it
+// directly, so it is writable -- and readable -- by everyone (SharedRuntimeLogFileMode). Restricted
+// is opt-in: the root collector is the only writer, hooks hand it their events over the local
+// ingest socket (package localingest), and the log is readable only by root and the
+// RuntimeLogReadersGroup. A per-user log in the user's home is private in either case and is not
+// affected.
+const (
+ LogAccessShared = "shared"
+ LogAccessRestricted = "restricted"
+)
+
+const (
+ // RestrictedRuntimeLogFileMode is a restricted system log's mode: root writes, the readers
+ // group reads, nobody else has access.
+ RestrictedRuntimeLogFileMode os.FileMode = 0o640
+ // RestrictedRuntimeLogLockMode is the mode of a restricted log's lock: only root takes it.
+ RestrictedRuntimeLogLockMode os.FileMode = 0o600
+ // RestrictedRuntimeLogDirMode is its directory's: setgid, so every file the collector creates
+ // there -- the log after a rotation, its lock -- takes the readers group, as it does on macOS
+ // without the bit.
+ RestrictedRuntimeLogDirMode os.FileMode = 0o750 | os.ModeSetgid
+
+ // RuntimeLogReadersGroup is the group that may read a restricted system log: log shippers and
+ // people who should see every user's sessions.
+ RuntimeLogReadersGroup = "beacon-log"
+
+ // LogAccessMarkerName is the file, beside the log, that records the restricted mode. It is the
+ // mode's one source of truth for the root writers -- the collector, the ingest server and the
+ // CLI running as root -- which otherwise know only the log's path. It counts only when root
+ // owns it and nobody else can write it, so no other account can switch a log's mode.
+ LogAccessMarkerName = ".beacon-log-access"
+
+ maxLogAccessMarkerBytes = 64
+)
+
+// logAccessMarkerOwner is the uid that must own the marker; tests set their own.
+var logAccessMarkerOwner = 0
+
+// LogAccessMarkerPath is the marker for the runtime log at logPath.
+func LogAccessMarkerPath(logPath string) string {
+ return filepath.Join(filepath.Dir(logPath), LogAccessMarkerName)
+}
+
+// RuntimeLogAccessRestricted reports whether the system log at path is in the restricted mode. A
+// private per-user log never is. A process that cannot read the log's directory -- an ordinary user
+// once the directory is restricted -- sees false, and must not be writing there anyway.
+func RuntimeLogAccessRestricted(path string) bool {
+ if strings.TrimSpace(path) == "" || RuntimeLogIsPrivate(path) {
+ return false
+ }
+ return readLogAccessMarker(LogAccessMarkerPath(path)) == LogAccessRestricted
+}
+
+// SetRuntimeLogAccess records the access mode for the system log at logPath: restricted writes the
+// marker, shared removes it. It changes only the marker; the caller applies the modes and
+// ownership that go with it. The write goes through a temporary file and a rename, so the marker
+// is never seen half-written.
+func SetRuntimeLogAccess(logPath, mode string) error {
+ marker := LogAccessMarkerPath(logPath)
+ switch mode {
+ case LogAccessShared:
+ if err := os.Remove(marker); err != nil && !os.IsNotExist(err) {
+ return fmt.Errorf("remove %s: %w", marker, err)
+ }
+ return nil
+ case LogAccessRestricted:
+ default:
+ return fmt.Errorf("unknown log access mode %q (want %s or %s)", mode, LogAccessShared, LogAccessRestricted)
+ }
+ if RuntimeLogIsPrivate(logPath) {
+ return fmt.Errorf("%s is a per-user log; the %s mode applies only to the system log", logPath, LogAccessRestricted)
+ }
+ dir := filepath.Dir(logPath)
+ tmp, err := os.CreateTemp(dir, LogAccessMarkerName+".*")
+ if err != nil {
+ return err
+ }
+ tmpName := tmp.Name()
+ defer os.Remove(tmpName)
+ if err := tmp.Chmod(0o644); err != nil {
+ _ = tmp.Close()
+ return err
+ }
+ if _, err := tmp.WriteString(LogAccessRestricted + "\n"); err != nil {
+ _ = tmp.Close()
+ return err
+ }
+ if err := tmp.Close(); err != nil {
+ return err
+ }
+ return os.Rename(tmpName, marker)
+}
+
+// ParseLogAccess normalizes a configured log access mode. Empty is shared.
+func ParseLogAccess(value string) (string, error) {
+ switch strings.ToLower(strings.TrimSpace(value)) {
+ case "", LogAccessShared:
+ return LogAccessShared, nil
+ case LogAccessRestricted:
+ return LogAccessRestricted, nil
+ default:
+ return "", fmt.Errorf("unknown log access mode %q (want %s or %s)", value, LogAccessShared, LogAccessRestricted)
+ }
+}
diff --git a/pkg/asymptoteobserve/runtimelogaccess_other.go b/pkg/asymptoteobserve/runtimelogaccess_other.go
new file mode 100644
index 00000000..33bd2743
--- /dev/null
+++ b/pkg/asymptoteobserve/runtimelogaccess_other.go
@@ -0,0 +1,8 @@
+//go:build !unix
+
+package asymptoteobserve
+
+// readLogAccessMarker reports no marker off Unix: the restricted mode needs the local ingest
+// socket and kernel peer credentials, which Beacon does not offer on Windows, so the system log
+// there keeps the shared access its ACL grants.
+func readLogAccessMarker(string) string { return "" }
diff --git a/pkg/asymptoteobserve/runtimelogaccess_unix.go b/pkg/asymptoteobserve/runtimelogaccess_unix.go
new file mode 100644
index 00000000..91dcd888
--- /dev/null
+++ b/pkg/asymptoteobserve/runtimelogaccess_unix.go
@@ -0,0 +1,34 @@
+//go:build unix
+
+package asymptoteobserve
+
+import (
+ "io"
+ "os"
+ "strings"
+ "syscall"
+)
+
+// readLogAccessMarker returns the mode a trusted marker records, or "" when there is no marker or
+// it could have been written by someone other than root: not a regular file, owned by another
+// account, writable by group or others, or with another hard link.
+func readLogAccessMarker(path string) string {
+ f, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
+ if err != nil {
+ return ""
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o022 != 0 {
+ return ""
+ }
+ stat, ok := info.Sys().(*syscall.Stat_t)
+ if !ok || int(stat.Uid) != logAccessMarkerOwner || stat.Nlink != 1 {
+ return ""
+ }
+ data, err := io.ReadAll(io.LimitReader(f, maxLogAccessMarkerBytes+1))
+ if err != nil || len(data) > maxLogAccessMarkerBytes {
+ return ""
+ }
+ return strings.TrimSpace(string(data))
+}
diff --git a/pkg/asymptoteobserve/runtimelogaccess_unix_test.go b/pkg/asymptoteobserve/runtimelogaccess_unix_test.go
new file mode 100644
index 00000000..2bba9a81
--- /dev/null
+++ b/pkg/asymptoteobserve/runtimelogaccess_unix_test.go
@@ -0,0 +1,363 @@
+//go:build unix
+
+package asymptoteobserve
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "syscall"
+ "testing"
+)
+
+// trustMarkersFrom makes markers owned by the test's own account count, as root's do in production.
+func trustMarkersFrom(t *testing.T, uid int) {
+ t.Helper()
+ old := logAccessMarkerOwner
+ logAccessMarkerOwner = uid
+ t.Cleanup(func() { logAccessMarkerOwner = old })
+}
+
+// systemLog returns a log path outside the test's home, so it is a system log, not a private one.
+func systemLog(t *testing.T) string {
+ t.Helper()
+ setTestHome(t, t.TempDir())
+ return filepath.Join(t.TempDir(), "beacon-agent", "runtime.jsonl")
+}
+
+func mkLogDir(t *testing.T, logPath string) {
+ t.Helper()
+ if err := os.MkdirAll(filepath.Dir(logPath), 0o755); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func modesOf(path string) (os.FileMode, os.FileMode) {
+ file, dir, _ := RuntimeLogModes(path)
+ return file, dir
+}
+
+func TestRestrictedMarkerSwitchesTheSystemLogModes(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+
+ if file, dir := modesOf(log); file != SharedRuntimeLogFileMode || dir != SharedRuntimeLogDirMode {
+ t.Fatalf("without a marker: %o, %o; want the shared modes", file, dir)
+ }
+ if RuntimeLogAccessRestricted(log) {
+ t.Fatal("restricted without a marker")
+ }
+
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ if !RuntimeLogAccessRestricted(log) {
+ t.Fatal("not restricted after SetRuntimeLogAccess")
+ }
+ file, dir, private := RuntimeLogModes(log)
+ if private || file != RestrictedRuntimeLogFileMode || dir != RestrictedRuntimeLogDirMode {
+ t.Fatalf("restricted modes = %o, %v, private=%v", file, dir, private)
+ }
+ info, err := os.Stat(LogAccessMarkerPath(log))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if info.Mode().Perm() != 0o644 {
+ t.Fatalf("marker mode = %o; it must be readable by the readers group and writable by root only", info.Mode().Perm())
+ }
+ // Writing it again replaces it in place.
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ entries, _ := os.ReadDir(filepath.Dir(log))
+ if len(entries) != 1 {
+ t.Fatalf("directory holds %d entries; a temporary marker was left behind", len(entries))
+ }
+
+ if err := SetRuntimeLogAccess(log, LogAccessShared); err != nil {
+ t.Fatal(err)
+ }
+ if RuntimeLogAccessRestricted(log) {
+ t.Fatal("still restricted after switching back to shared")
+ }
+ if err := SetRuntimeLogAccess(log, LogAccessShared); err != nil {
+ t.Fatalf("removing an absent marker: %v", err)
+ }
+}
+
+func TestUntrustedMarkersAreIgnored(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ cases := map[string]func(t *testing.T, marker string){
+ "group writable": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted\n")
+ if err := os.Chmod(marker, 0o664); err != nil {
+ t.Fatal(err)
+ }
+ },
+ "world writable": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted\n")
+ if err := os.Chmod(marker, 0o646); err != nil {
+ t.Fatal(err)
+ }
+ },
+ "owned by another account": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted\n")
+ logAccessMarkerOwner = os.Geteuid() + 1
+ },
+ "a symlink": func(t *testing.T, marker string) {
+ target := filepath.Join(t.TempDir(), "elsewhere")
+ writeMarker(t, target, "restricted\n")
+ if err := os.Symlink(target, marker); err != nil {
+ t.Fatal(err)
+ }
+ },
+ "a hard link": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted\n")
+ if err := os.Link(marker, marker+"-twin"); err != nil {
+ t.Fatal(err)
+ }
+ },
+ "a directory": func(t *testing.T, marker string) {
+ if err := os.Mkdir(marker, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ },
+ "a FIFO": func(t *testing.T, marker string) {
+ if err := syscall.Mkfifo(marker, 0o644); err != nil {
+ t.Skipf("mkfifo: %v", err)
+ }
+ },
+ "another mode": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "shared\n")
+ },
+ "garbage": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted please\n")
+ },
+ "oversized": func(t *testing.T, marker string) {
+ writeMarker(t, marker, "restricted"+strings.Repeat(" ", maxLogAccessMarkerBytes))
+ },
+ }
+ for name, plant := range cases {
+ t.Run(name, func(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ plant(t, LogAccessMarkerPath(log))
+ if RuntimeLogAccessRestricted(log) {
+ t.Fatal("an untrusted marker switched the log to restricted")
+ }
+ if file, _ := modesOf(log); file != SharedRuntimeLogFileMode {
+ t.Fatalf("file mode = %o, want shared", file)
+ }
+ })
+ }
+}
+
+func TestMarkerToleratesSurroundingWhitespace(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ writeMarker(t, LogAccessMarkerPath(log), " restricted \r\n")
+ if !RuntimeLogAccessRestricted(log) {
+ t.Fatal("a marker with surrounding whitespace was not honored")
+ }
+}
+
+func writeMarker(t *testing.T, path, content string) {
+ t.Helper()
+ if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chmod(path, 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestAPrivateLogIsNeverRestricted(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ home := t.TempDir()
+ setTestHome(t, home)
+ log := filepath.Join(home, ".beacon", "endpoint", "logs", "runtime.jsonl")
+ mkLogDir(t, log)
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err == nil {
+ t.Fatal("SetRuntimeLogAccess accepted a per-user log")
+ }
+ writeMarker(t, LogAccessMarkerPath(log), "restricted\n")
+ if RuntimeLogAccessRestricted(log) {
+ t.Fatal("a marker beside a per-user log was honored")
+ }
+ if file, dir, private := RuntimeLogModes(log); !private || file != PrivateRuntimeLogFileMode || dir != PrivateRuntimeLogDirMode {
+ t.Fatalf("modes = %o, %o, %v; want private", file, dir, private)
+ }
+}
+
+func TestSetRuntimeLogAccessRejectsAnUnknownMode(t *testing.T) {
+ log := systemLog(t)
+ mkLogDir(t, log)
+ if err := SetRuntimeLogAccess(log, "open"); err == nil {
+ t.Fatal("accepted an unknown mode")
+ }
+ if _, err := os.Lstat(LogAccessMarkerPath(log)); !os.IsNotExist(err) {
+ t.Fatal("an unknown mode left a marker")
+ }
+}
+
+func TestParseLogAccess(t *testing.T) {
+ for in, want := range map[string]string{
+ "": LogAccessShared, "shared": LogAccessShared, " Shared ": LogAccessShared,
+ "restricted": LogAccessRestricted, "RESTRICTED": LogAccessRestricted,
+ } {
+ got, err := ParseLogAccess(in)
+ if err != nil || got != want {
+ t.Errorf("ParseLogAccess(%q) = %q, %v; want %q", in, got, err, want)
+ }
+ }
+ for _, bad := range []string{"open", "private", "0640"} {
+ if _, err := ParseLogAccess(bad); err == nil {
+ t.Errorf("ParseLogAccess(%q) accepted", bad)
+ }
+ }
+}
+
+// The root writers narrow a log that was shared to the restricted mode the next time they open it,
+// lock file included, and create new files restricted whatever the umask.
+func TestOpenRuntimeLogFileAppliesTheRestrictedMode(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ old := syscall.Umask(0)
+ defer syscall.Umask(old)
+ log := systemLog(t)
+ mkLogDir(t, log)
+
+ for _, path := range []string{log, log + ".lock"} {
+ if err := os.WriteFile(path, []byte("{}\n"), 0o666); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ for _, path := range []string{log, log + ".lock"} {
+ f, err := OpenRuntimeLogFile(path, log, os.O_APPEND|os.O_CREATE|os.O_WRONLY)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = f.Close()
+ want := RestrictedRuntimeLogFileMode
+ if path == log+".lock" {
+ want = RestrictedRuntimeLogLockMode
+ }
+ if info, _ := os.Stat(path); info.Mode().Perm() != want {
+ t.Fatalf("%s mode = %o after open; want it narrowed to %o", filepath.Base(path), info.Mode().Perm(), want)
+ }
+ }
+
+ if err := os.Remove(log); err != nil {
+ t.Fatal(err)
+ }
+ f, err := OpenRuntimeLogFile(log, log, os.O_APPEND|os.O_CREATE|os.O_WRONLY)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = f.Close()
+ if info, _ := os.Stat(log); info.Mode().Perm() != RestrictedRuntimeLogFileMode {
+ t.Fatalf("new log mode = %o with umask 0; want %o", info.Mode().Perm(), RestrictedRuntimeLogFileMode)
+ }
+}
+
+// Switching back to shared widens the log again at the next root write, so hooks can append.
+func TestOpenRuntimeLogFileWidensWhenSwitchedBackToShared(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ if err := os.WriteFile(log, nil, 0o640); err != nil {
+ t.Fatal(err)
+ }
+ f, err := OpenRuntimeLogFile(log, log, os.O_APPEND|os.O_WRONLY)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = f.Close()
+ if info, _ := os.Stat(log); info.Mode().Perm() != SharedRuntimeLogFileMode {
+ t.Fatalf("mode = %o; a shared log must be widened back to %o", info.Mode().Perm(), SharedRuntimeLogFileMode)
+ }
+}
+
+func TestTightenRestrictedRuntimeLogArchives(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ writeArchives := func() {
+ for i := 1; i <= 3; i++ {
+ path := fmt.Sprintf("%s.%d", log, i)
+ if err := os.WriteFile(path, nil, 0o666); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chmod(path, 0o666); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ writeArchives()
+ TightenRuntimeLogArchives(log, 3)
+ for i := 1; i <= 3; i++ {
+ if info, _ := os.Stat(fmt.Sprintf("%s.%d", log, i)); info.Mode().Perm() != 0o666 {
+ t.Fatalf("a shared log's archive %d was changed to %o", i, info.Mode().Perm())
+ }
+ }
+
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ TightenRuntimeLogArchives(log, 3)
+ for i := 1; i <= 3; i++ {
+ if info, _ := os.Stat(fmt.Sprintf("%s.%d", log, i)); info.Mode().Perm() != RestrictedRuntimeLogFileMode {
+ t.Fatalf("restricted archive %d mode = %o", i, info.Mode().Perm())
+ }
+ }
+}
+
+// A process that cannot read the restricted log's directory -- an ordinary user, once install has
+// narrowed it -- cannot read the marker either and sees the shared modes. That is safe: it cannot
+// write there at all, and hooks reach the collector through the ingest socket instead.
+func TestAnUnreadableDirectoryReadsAsNoMarker(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("root reads any directory")
+ }
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ dir := filepath.Dir(log)
+ if err := os.Chmod(dir, 0o000); err != nil {
+ t.Fatal(err)
+ }
+ defer os.Chmod(dir, 0o755)
+ if RuntimeLogAccessRestricted(log) {
+ t.Fatal("read a marker through a directory with no permissions")
+ }
+}
+
+func TestARestrictedLogsLockIsRootsAlone(t *testing.T) {
+ trustMarkersFrom(t, os.Geteuid())
+ log := systemLog(t)
+ mkLogDir(t, log)
+ if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {
+ t.Fatal(err)
+ }
+ f, err := OpenRuntimeLogFile(log+".lock", log, os.O_CREATE|os.O_RDWR)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = f.Close()
+ if info, _ := os.Stat(log + ".lock"); info.Mode().Perm() != RestrictedRuntimeLogLockMode {
+ t.Fatalf("lock mode = %o; a reader that can open the lock can hold it", info.Mode().Perm())
+ }
+ // An archive is not a lock, whatever its name contains.
+ if got := fileModeFor(log+".1", log); got != RestrictedRuntimeLogFileMode {
+ t.Fatalf("archive mode = %o", got)
+ }
+}
diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.go
index 317322be..3093d3f7 100644
--- a/pkg/asymptoteobserve/runtimelogmode.go
+++ b/pkg/asymptoteobserve/runtimelogmode.go
@@ -47,10 +47,17 @@ const unixSystemLogDir = "/var/log/beacon-agent"
// One exception keeps root from breaking a user's log: when running as root on Unix inside a home
// directory root does not own (sudo preserving HOME), the log keeps the shared mode, because a
// private root-owned file there is one the user's own hooks could never append to again.
+//
+// A system log in the restricted access mode (see RuntimeLogAccessRestricted) takes the restricted
+// modes instead of the shared ones. Its writers are root, so the narrowing an owner applies to an
+// existing file brings a log that was shared down to the restricted mode at its next write.
func RuntimeLogModes(path string) (file, dir os.FileMode, private bool) {
if RuntimeLogIsPrivate(path) {
return PrivateRuntimeLogFileMode, PrivateRuntimeLogDirMode, true
}
+ if RuntimeLogAccessRestricted(path) {
+ return RestrictedRuntimeLogFileMode, RestrictedRuntimeLogDirMode, false
+ }
return SharedRuntimeLogFileMode, SharedRuntimeLogDirMode, false
}
@@ -134,7 +141,7 @@ func SettleRuntimeLogFileMode(path, logPath string, created bool) error {
// must still not be steerable -- by a link or FIFO someone planted where it appends -- into
// appending to, chmodding or hanging on a file of the planter's choosing.
func OpenRuntimeLogFile(path, logPath string, flag int) (*os.File, error) {
- fileMode, _, _ := RuntimeLogModes(logPath)
+ fileMode := fileModeFor(path, logPath)
existed := true
if _, err := os.Lstat(path); err != nil {
if !os.IsNotExist(err) {
@@ -166,8 +173,20 @@ func OpenRuntimeLogFile(path, logPath string, flag int) (*os.File, error) {
return f, nil
}
+// fileModeFor is the mode for path, a member of logPath's family. A restricted log's lock is
+// root's alone: the readers group needs to read the log, never to lock it, and a reader that could
+// open the lock could hold it and stall every write the collector makes.
+func fileModeFor(path, logPath string) os.FileMode {
+ fileMode, _, _ := RuntimeLogModes(logPath)
+ if fileMode == RestrictedRuntimeLogFileMode && filepath.Clean(path) == filepath.Clean(logPath)+".lock" {
+ return RestrictedRuntimeLogLockMode
+ }
+ return fileMode
+}
+
func settleRuntimeLogFileMode(path, logPath string, created bool, stat func(string) (os.FileInfo, error), chmod func(os.FileMode) error) error {
- fileMode, _, private := RuntimeLogModes(logPath)
+ _, _, private := RuntimeLogModes(logPath)
+ fileMode := fileModeFor(path, logPath)
if created {
if private && hasPOSIXACL(path) {
fileMode = PrivateACLRuntimeLogFileMode
@@ -192,12 +211,13 @@ func settleRuntimeLogFileMode(path, logPath string, created bool, stat func(stri
return nil
}
-// TightenPrivateRuntimeLogArchives narrows the rotated archives of a private log (path.1 ..
+// TightenRuntimeLogArchives narrows the rotated archives of a private or restricted log (path.1 ..
// 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) {
+// the log was private or restricted still carry the old shared mode, and they hold the same
+// content as the live file. Best effort, like SettleRuntimeLogFileMode on an existing file; a
+// shared log's archives are left alone.
+func TightenRuntimeLogArchives(path string, archives int) {
+ if !RuntimeLogIsPrivate(path) && !RuntimeLogAccessRestricted(path) {
return
}
for i := 1; i <= archives; i++ {
diff --git a/pkg/asymptoteobserve/runtimelogmode_test.go b/pkg/asymptoteobserve/runtimelogmode_test.go
index d6793976..239025a5 100644
--- a/pkg/asymptoteobserve/runtimelogmode_test.go
+++ b/pkg/asymptoteobserve/runtimelogmode_test.go
@@ -58,7 +58,7 @@ func TestSystemLogIsNeverPrivate(t *testing.T) {
}
}
-func TestTightenPrivateRuntimeLogArchives(t *testing.T) {
+func TestTightenRuntimeLogArchives(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Unix permission bits do not exist on Windows")
}
@@ -76,7 +76,7 @@ func TestTightenPrivateRuntimeLogArchives(t *testing.T) {
}
writeWorldWritable(path + ".1")
writeWorldWritable(path + ".2")
- TightenPrivateRuntimeLogArchives(path, 5)
+ TightenRuntimeLogArchives(path, 5)
for _, archive := range []string{path + ".1", path + ".2"} {
info, err := os.Stat(archive)
if err != nil {
@@ -90,7 +90,7 @@ func TestTightenPrivateRuntimeLogArchives(t *testing.T) {
// A shared log's archives are left alone.
shared := filepath.Join(t.TempDir(), "runtime.jsonl")
writeWorldWritable(shared + ".1")
- TightenPrivateRuntimeLogArchives(shared, 5)
+ TightenRuntimeLogArchives(shared, 5)
info, err := os.Stat(shared + ".1")
if err != nil {
t.Fatal(err)