Skip to content

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

asymptoteobserve: settle an archive only while its path still names the opened file

Claude52dab7c4782b
diff --git a/pkg/asymptoteobserve/runtimelogmode.go b/pkg/asymptoteobserve/runtimelogmode.go
index 619a19e0..e38dffb9 100644
--- a/pkg/asymptoteobserve/runtimelogmode.go
+++ b/pkg/asymptoteobserve/runtimelogmode.go
@@ -228,9 +228,13 @@ func SettleRuntimeLogArchives(path string, archives int) {
}
}
-// 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)
+// archiveOpened runs between opening an archive and checking it, and archiveChecked between
+// checking it and changing its mode. Nil outside tests, which use them to race the archive's name
+// at exactly those moments.
+var (
+ archiveOpened func(archive string)
+ archiveChecked func(archive string)
+)
func settleRuntimeLogArchive(archive, logPath string) {
f, err := os.OpenFile(archive, os.O_RDONLY|noFollowOpenFlags(), 0)
@@ -238,10 +242,21 @@ func settleRuntimeLogArchive(archive, logPath string) {
return
}
defer f.Close()
+ if archiveOpened != nil {
+ archiveOpened(archive)
+ }
info, err := f.Stat()
if err != nil || !info.Mode().IsRegular() || linkedElsewhere(info) {
return
}
+ // The link count on the descriptor alone is not enough: someone can plant the archive as a hard
+ // link to another file, wait for the open, then remove their name, leaving the descriptor on that
+ // file with a single link. Settle only while the archive's path still names this very file with
+ // one link, so the archive is the file's only name and it cannot be anyone else's.
+ named, err := os.Lstat(archive)
+ if err != nil || !os.SameFile(info, named) || linkedElsewhere(named) {
+ return
+ }
if archiveChecked != nil {
archiveChecked(archive)
}
diff --git a/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
index 4150aae1..3dfb48f2 100644
--- a/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
+++ b/pkg/asymptoteobserve/runtimelogmode_settle_unix_test.go
@@ -133,3 +133,33 @@ func TestSettleArchivesNeverFollowsALinkSwappedIn(t *testing.T) {
t.Fatalf("settling archives chmodded a symlink's target to %o, want it left at 0600", got)
}
}
+
+// A hard link to another file planted as an archive, whose planted name is removed once root has it
+// open, leaves the descriptor on that file with a single link. Settling must not widen it.
+func TestSettleArchivesNeverWidensAFileWhoseHardLinkWasRemovedAfterTheOpen(t *testing.T) {
+ asRoot(t)
+ dir := t.TempDir()
+ log := filepath.Join(dir, "runtime.jsonl")
+ writeMode(t, log, 0o666)
+ archive := log + ".1"
+ victim := filepath.Join(dir, "victim")
+ writeMode(t, victim, 0o600)
+ if err := os.Link(victim, archive); err != nil {
+ t.Fatal(err)
+ }
+ if RuntimeLogIsPrivate(log) {
+ t.Fatal("a log outside the home must be shared")
+ }
+ t.Cleanup(func() { archiveOpened = nil })
+ archiveOpened = func(path string) {
+ if err := os.Remove(path); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ SettleRuntimeLogArchives(log, 1)
+
+ if got := modeOf(t, victim); got != 0o600 {
+ t.Fatalf("settling archives chmodded a hard link's other file to %o, want it left at 0600", got)
+ }
+}