Skip to content

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

security: never let a root log writer follow a link or open a FIFO (#810)

Justin D'Souza44ed7e3aa4b4
diff --git a/cli/beacon-hooks/internal/logging/logging.go b/cli/beacon-hooks/internal/logging/logging.go
index 3f76b8ed..ea46bc14 100644
--- a/cli/beacon-hooks/internal/logging/logging.go
+++ b/cli/beacon-hooks/internal/logging/logging.go
@@ -690,26 +690,11 @@ func openEndpointRuntimeFile(path string, flag int) (*os.File, error) {
}
// 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.
+// settles its mode through asymptoteobserve.OpenRuntimeLogFile: a new file gets that mode
+// whatever the umask, an existing one is only ever narrowed toward it, by its owner, and root never
+// follows a symlink or opens anything but a regular file there.
func openEndpointRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {
- fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)
- existed := true
- if _, err := os.Stat(path); err != nil {
- if !os.IsNotExist(err) {
- return nil, err
- }
- existed = false
- }
- f, err := os.OpenFile(path, flag, fileMode)
- if err != nil {
- return nil, err
- }
- if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {
- _ = f.Close()
- return nil, err
- }
- return f, nil
+ return asymptoteobserve.OpenRuntimeLogFile(path, logPath, flag)
}
func rotateEndpointLogIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int64) error {
diff --git a/cli/beacon/internal/endpoint/writer/writer.go b/cli/beacon/internal/endpoint/writer/writer.go
index cd2eca78..bf0c0005 100644
--- a/cli/beacon/internal/endpoint/writer/writer.go
+++ b/cli/beacon/internal/endpoint/writer/writer.go
@@ -330,26 +330,11 @@ func openRuntimeFile(path string, flag int) (*os.File, error) {
}
// 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.
+// its mode through asymptoteobserve.OpenRuntimeLogFile: a new file gets that mode whatever the
+// umask, an existing one is only ever narrowed toward it, by its owner, and root never follows a
+// symlink or opens anything but a regular file there.
func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {
- fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)
- existed := true
- if _, err := os.Stat(path); err != nil {
- if !os.IsNotExist(err) {
- return nil, err
- }
- existed = false
- }
- f, err := os.OpenFile(path, flag, fileMode)
- if err != nil {
- return nil, err
- }
- if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {
- _ = f.Close()
- return nil, err
- }
- return f, nil
+ return asymptoteobserve.OpenRuntimeLogFile(path, logPath, flag)
}
// RetainedLogPaths returns the runtime log at path followed by every archive the rotation contract
diff --git a/collector-builder/exporter/beaconjsonexporter/writer.go b/collector-builder/exporter/beaconjsonexporter/writer.go
index a87fe908..017fac7b 100644
--- a/collector-builder/exporter/beaconjsonexporter/writer.go
+++ b/collector-builder/exporter/beaconjsonexporter/writer.go
@@ -246,26 +246,11 @@ func openRuntimeFile(path string, flag int) (*os.File, error) {
}
// 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.
+// its mode through asymptoteobserve.OpenRuntimeLogFile: a new file gets that mode whatever the
+// umask, an existing one is only ever narrowed toward it, by its owner, and root never follows a
+// symlink or opens anything but a regular file there.
func openRuntimeFileAs(path, logPath string, flag int) (*os.File, error) {
- fileMode, _, _ := asymptoteobserve.RuntimeLogModes(logPath)
- existed := true
- if _, err := os.Stat(path); err != nil {
- if !os.IsNotExist(err) {
- return nil, err
- }
- existed = false
- }
- f, err := os.OpenFile(path, flag, fileMode)
- if err != nil {
- return nil, err
- }
- if err := asymptoteobserve.SettleRuntimeLogFileMode(path, logPath, !existed); err != nil {
- _ = f.Close()
- return nil, err
- }
- return f, nil
+ return asymptoteobserve.OpenRuntimeLogFile(path, logPath, flag)
}
func rotateIfNeeded(path string, maxSize int64, archives int, nextWriteBytes int64) error {
diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.go
index a09626ef..317322be 100644
--- a/pkg/asymptoteobserve/runtimelogmode.go
+++ b/pkg/asymptoteobserve/runtimelogmode.go
@@ -115,15 +115,66 @@ const PrivateACLRuntimeLogFileMode os.FileMode = 0o640
// 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.
+//
+// The path is never followed: a symlink is left alone, so root cannot be steered into chmodding
+// whatever a link in a directory someone else can write points at.
func SettleRuntimeLogFileMode(path, logPath string, created bool) error {
+ return settleRuntimeLogFileMode(path, logPath, created, os.Lstat, func(mode os.FileMode) error {
+ return os.Chmod(path, mode)
+ })
+}
+
+// OpenRuntimeLogFile opens path (the runtime log at logPath, or its lock) with the mode logPath
+// calls for and settles that mode the way SettleRuntimeLogFileMode does, on the open file rather
+// than by path. It is the one way the CLI, the hook adapter and the collector exporter open the log.
+//
+// Running as root it refuses a symlink in the last component, opens without blocking, and refuses
+// anything that is not a regular file or that has another hard link. The shared log's directory is
+// root-owned, but a root writer
+// 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)
+ existed := true
+ if _, err := os.Lstat(path); err != nil {
+ if !os.IsNotExist(err) {
+ return nil, err
+ }
+ existed = false
+ }
+ f, err := os.OpenFile(path, flag|elevatedOpenFlags(), fileMode)
+ if err != nil {
+ return nil, err
+ }
+ info, err := f.Stat()
+ if err != nil {
+ _ = f.Close()
+ return nil, err
+ }
+ if !info.Mode().IsRegular() {
+ _ = f.Close()
+ return nil, fmt.Errorf("runtime log %s is not a regular file", path)
+ }
+ if linkedElsewhere(info) {
+ _ = f.Close()
+ return nil, fmt.Errorf("runtime log %s has another hard link", path)
+ }
+ if err := settleRuntimeLogFileMode(path, logPath, !existed, func(string) (os.FileInfo, error) { return info, nil }, f.Chmod); err != nil {
+ _ = f.Close()
+ return nil, err
+ }
+ return f, nil
+}
+
+func settleRuntimeLogFileMode(path, logPath string, created bool, stat func(string) (os.FileInfo, error), chmod func(os.FileMode) error) error {
fileMode, _, private := RuntimeLogModes(logPath)
if created {
if private && hasPOSIXACL(path) {
fileMode = PrivateACLRuntimeLogFileMode
}
- return os.Chmod(path, fileMode)
+ return chmod(fileMode)
}
- info, err := os.Stat(path)
+ info, err := stat(path)
if err != nil || !info.Mode().IsRegular() || !ownedByProcess(info) {
return nil
}
@@ -136,7 +187,7 @@ func SettleRuntimeLogFileMode(path, logPath string, created bool) error {
}
}
if want != current {
- _ = os.Chmod(path, want)
+ _ = chmod(want)
}
return nil
}
diff --git a/pkg/asymptoteobserve/runtimelogmode_open_unix_test.go b/pkg/asymptoteobserve/runtimelogmode_open_unix_test.go
new file mode 100644
index 00000000..2416bd52
--- /dev/null
+++ b/pkg/asymptoteobserve/runtimelogmode_open_unix_test.go
@@ -0,0 +1,128 @@
+//go:build unix
+
+package asymptoteobserve
+
+import (
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
+ "time"
+)
+
+// asRoot makes OpenRuntimeLogFile take its root path for the rest of the test. The refusals it adds
+// (O_NOFOLLOW, O_NONBLOCK, the regular-file and link-count checks) behave the same for any user,
+// so the tests need not run as root to prove them.
+func asRoot(t *testing.T) {
+ t.Helper()
+ prev := runningAsRoot
+ runningAsRoot = func() bool { return true }
+ t.Cleanup(func() { runningAsRoot = prev })
+}
+
+// A shared log path that someone replaced with a symlink must not lead a root writer to append to
+// or chmod the link's target (a binary root later runs, say).
+func TestOpenRuntimeLogFileAsRootRefusesSymlink(t *testing.T) {
+ asRoot(t)
+ dir := t.TempDir()
+ target := filepath.Join(dir, "beacon")
+ writeMode(t, target, 0o755)
+ log := filepath.Join(dir, "runtime.jsonl")
+ if err := os.Symlink(target, log); err != nil {
+ t.Fatal(err)
+ }
+ if f, err := OpenRuntimeLogFile(log, log, os.O_CREATE|os.O_WRONLY|os.O_APPEND); err == nil {
+ _ = f.Close()
+ t.Fatal("root opened a symlinked runtime log")
+ }
+ info, err := os.Stat(target)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if info.Mode().Perm() != 0o755 {
+ t.Fatalf("target mode = %v, want unchanged 0755", info.Mode().Perm())
+ }
+ if data, _ := os.ReadFile(target); string(data) != "{}\n" {
+ t.Fatalf("target content = %q, want unchanged", data)
+ }
+}
+
+func TestOpenRuntimeLogFileAsRootRefusesFIFOWithoutBlocking(t *testing.T) {
+ asRoot(t)
+ log := filepath.Join(t.TempDir(), "runtime.jsonl")
+ if err := syscall.Mkfifo(log, 0o666); err != nil {
+ t.Skip(err)
+ }
+ done := make(chan error, 1)
+ go func() {
+ // The writers' own flags: O_WRONLY on a FIFO with no reader blocks unless O_NONBLOCK is set.
+ f, err := OpenRuntimeLogFile(log, log, os.O_APPEND|os.O_CREATE|os.O_WRONLY)
+ if f != nil {
+ _ = f.Close()
+ }
+ done <- err
+ }()
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Fatal("root opened a FIFO as the runtime log")
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("opening a FIFO runtime log blocked")
+ }
+}
+
+// A hard link to a root-owned file is a regular file, so it passes O_NOFOLLOW and the regular-file
+// check; a root writer must still not append to it.
+func TestOpenRuntimeLogFileAsRootRefusesAHardLink(t *testing.T) {
+ asRoot(t)
+ dir := t.TempDir()
+ target := filepath.Join(dir, "hosts")
+ writeMode(t, target, 0o644)
+ log := filepath.Join(dir, "runtime.jsonl")
+ if err := os.Link(target, log); err != nil {
+ t.Skip(err)
+ }
+ if f, err := OpenRuntimeLogFile(log, log, os.O_CREATE|os.O_WRONLY|os.O_APPEND); err == nil {
+ _ = f.Close()
+ t.Fatal("root opened a hard-linked runtime log")
+ }
+ if data, _ := os.ReadFile(target); string(data) != "{}\n" {
+ t.Fatalf("target content = %q, want unchanged", data)
+ }
+}
+
+func TestSettleRuntimeLogFileModeLeavesSymlinkTargetAlone(t *testing.T) {
+ dir := t.TempDir()
+ target := filepath.Join(dir, "target")
+ writeMode(t, target, 0o755)
+ archive := filepath.Join(dir, "runtime.jsonl.1")
+ if err := os.Symlink(target, archive); err != nil {
+ t.Fatal(err)
+ }
+ _ = SettleRuntimeLogFileMode(archive, filepath.Join(dir, "runtime.jsonl"), false)
+ info, err := os.Stat(target)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if info.Mode().Perm() != 0o755 {
+ t.Fatalf("target mode = %v, want unchanged 0755", info.Mode().Perm())
+ }
+}
+
+func TestOpenRuntimeLogFileCreatesAndAppends(t *testing.T) {
+ log := filepath.Join(t.TempDir(), "runtime.jsonl")
+ for i := 0; i < 2; i++ {
+ f, err := OpenRuntimeLogFile(log, log, os.O_CREATE|os.O_WRONLY|os.O_APPEND)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := f.WriteString("{}\n"); err != nil {
+ t.Fatal(err)
+ }
+ _ = f.Close()
+ }
+ if data, _ := os.ReadFile(log); string(data) != "{}\n{}\n" {
+ t.Fatalf("log = %q", data)
+ }
+}
diff --git a/pkg/asymptoteobserve/runtimelogmode_other.go b/pkg/asymptoteobserve/runtimelogmode_other.go
index 1bd433f8..2c49dd92 100644
--- a/pkg/asymptoteobserve/runtimelogmode_other.go
+++ b/pkg/asymptoteobserve/runtimelogmode_other.go
@@ -10,3 +10,10 @@ 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 }
+
+// elevatedOpenFlags adds nothing off Unix.
+func elevatedOpenFlags() int { return 0 }
+
+// linkedElsewhere is never true off Unix, where no root writer opens the log through a directory
+// another user can plant links in.
+func linkedElsewhere(os.FileInfo) bool { return false }
diff --git a/pkg/asymptoteobserve/runtimelogmode_unix.go b/pkg/asymptoteobserve/runtimelogmode_unix.go
index b3cff2f8..b6b0bab0 100644
--- a/pkg/asymptoteobserve/runtimelogmode_unix.go
+++ b/pkg/asymptoteobserve/runtimelogmode_unix.go
@@ -34,3 +34,28 @@ func ownedByProcess(info os.FileInfo) bool {
}
return int(stat.Uid) == os.Geteuid()
}
+
+// runningAsRoot reports whether this process writes the log as root, the only case in which a link
+// or FIFO someone planted where it appends could escalate. A variable so the refusals can be tested
+// without root.
+var runningAsRoot = func() bool { return os.Geteuid() == 0 }
+
+// elevatedOpenFlags are added when root opens a runtime log: never follow a symlink in the last
+// component, and never block opening a FIFO.
+func elevatedOpenFlags() int {
+ if !runningAsRoot() {
+ return 0
+ }
+ return syscall.O_NOFOLLOW | syscall.O_NONBLOCK
+}
+
+// linkedElsewhere reports whether root opened a log that has another hard link. A runtime log has
+// exactly one, rotation included (it renames), so a second is one someone planted: a hard link to
+// a root-owned file passes O_NOFOLLOW and the regular-file check, and root would append to it.
+func linkedElsewhere(info os.FileInfo) bool {
+ if !runningAsRoot() {
+ return false
+ }
+ stat, ok := info.Sys().(*syscall.Stat_t)
+ return ok && stat.Nlink > 1
+}