Skip to content

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

asymptoteobserve: settle log archives through a descriptor

Shukan Shahd6b128f86bb3
diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.go
index b7814795..619a19e0 100644
--- a/pkg/asymptoteobserve/runtimelogmode.go
+++ b/pkg/asymptoteobserve/runtimelogmode.go
@@ -218,15 +218,34 @@ func settleRuntimeLogFileMode(path, logPath string, created bool, stat func(stri
// restricted must be readable again once it is shared, or every reader of the log's history stops
// at them. Best effort, like SettleRuntimeLogFileMode on an existing file: only a file this process
// owns is changed, and a link is never followed.
+//
+// Each archive is settled through a descriptor, never by path: a log can sit in a directory another
+// account can write (a custom path, or root keeping a user's HOME), and a chmod by path would follow
+// a symlink swapped in after the check, letting that account steer root into widening any file.
func SettleRuntimeLogArchives(path string, archives int) {
for i := 1; i <= archives; i++ {
- archive := fmt.Sprintf("%s.%d", path, i)
- info, err := os.Lstat(archive)
- if err != nil || !info.Mode().IsRegular() {
- continue
- }
- _ = SettleRuntimeLogFileMode(archive, path, false)
+ settleRuntimeLogArchive(fmt.Sprintf("%s.%d", path, i), path)
+ }
+}
+
+// archiveChecked runs between checking an archive and changing its mode. Nil outside tests, which
+// use it to swap the archive for a link at exactly that moment.
+var archiveChecked func(archive string)
+
+func settleRuntimeLogArchive(archive, logPath string) {
+ f, err := os.OpenFile(archive, os.O_RDONLY|noFollowOpenFlags(), 0)
+ if err != nil {
+ return
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil || !info.Mode().IsRegular() || linkedElsewhere(info) {
+ return
+ }
+ if archiveChecked != nil {
+ archiveChecked(archive)
}
+ _ = settleRuntimeLogFileMode(archive, logPath, false, func(string) (os.FileInfo, error) { return info, nil }, f.Chmod)
}
func pathWithin(path, dir string) bool {
diff --git a/pkg/asymptoteobserve/runtimelogmode_other.go b/pkg/asymptoteobserve/runtimelogmode_other.go
index 2c49dd92..3181ae55 100644
--- a/pkg/asymptoteobserve/runtimelogmode_other.go
+++ b/pkg/asymptoteobserve/runtimelogmode_other.go
@@ -14,6 +14,9 @@ func ownedByProcess(os.FileInfo) bool { return true }
// elevatedOpenFlags adds nothing off Unix.
func elevatedOpenFlags() int { return 0 }
+// noFollowOpenFlags adds nothing off Unix.
+func noFollowOpenFlags() 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_settle_unix_test.go b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
index 2c12dd3e..4150aae1 100644
--- a/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
+++ b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
@@ -100,3 +100,36 @@ func TestSettleLeavesAFileItDoesNotOwn(t *testing.T) {
t.Fatalf("root settled the user's private log to %o, want it left at 0600", got)
}
}
+
+// Settling archives never follows a link swapped in after the archive was checked: an account that
+// can write the log's directory (a custom log path, or root keeping a user's HOME) replaces the
+// archive with a symlink to a file of its choosing between the check and the chmod, and that
+// file must keep its mode. A chmod by path would follow the link and widen it.
+func TestSettleArchivesNeverFollowsALinkSwappedIn(t *testing.T) {
+ dir := t.TempDir()
+ log := filepath.Join(dir, "runtime.jsonl")
+ writeMode(t, log, 0o666)
+ archive := log + ".1"
+ writeMode(t, archive, 0o600)
+ victim := filepath.Join(t.TempDir(), "victim")
+ writeMode(t, victim, 0o600)
+ if RuntimeLogIsPrivate(log) {
+ t.Fatal("a log outside the home must be shared")
+ }
+ t.Cleanup(func() { archiveChecked = nil })
+ archiveChecked = func(path string) {
+ link := filepath.Join(dir, "swap")
+ if err := os.Symlink(victim, link); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Rename(link, path); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ SettleRuntimeLogArchives(log, 1)
+
+ if got := modeOf(t, victim); got != 0o600 {
+ t.Fatalf("settling archives chmodded a symlink's target to %o, want it left at 0600", got)
+ }
+}
diff --git a/pkg/asymptoteobserve/runtimelogmode_unix.go b/pkg/asymptoteobserve/runtimelogmode_unix.go
index b6b0bab0..2f265d59 100644
--- a/pkg/asymptoteobserve/runtimelogmode_unix.go
+++ b/pkg/asymptoteobserve/runtimelogmode_unix.go
@@ -49,6 +49,13 @@ func elevatedOpenFlags() int {
return syscall.O_NOFOLLOW | syscall.O_NONBLOCK
}
+// noFollowOpenFlags open a file without following a symlink in the last component and without
+// blocking on a FIFO, whoever is running. Settling an archive's mode uses them always: it only
+// reads, and a file reached through a link is never one of the log's.
+func noFollowOpenFlags() int {
+ 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.