Skip to content

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

writer: read a file's link count without needing read access to it

Clauded7d7e1e08998
diff --git a/cli/beacon/internal/endpoint/writer/logaccess_windows.go b/cli/beacon/internal/endpoint/writer/logaccess_windows.go
index fe265f6c..189f49f7 100644
--- a/cli/beacon/internal/endpoint/writer/logaccess_windows.go
+++ b/cli/beacon/internal/endpoint/writer/logaccess_windows.go
@@ -159,8 +159,15 @@ func replaceLogFamily(path string) error {
continue
}
child := filepath.Join(dir, name)
- if !sysacl.IsLogFamily(name, base) && hardLinked(child) {
- continue
+ if !sysacl.IsLogFamily(name, base) {
+ linked, err := hardLinked(child)
+ if err != nil {
+ errs = append(errs, err)
+ continue
+ }
+ if linked {
+ continue
+ }
}
if err := replaceWithCopy(child, false); err != nil {
errs = append(errs, err)
@@ -173,19 +180,29 @@ func replaceLogFamily(path string) error {
return errors.Join(errs...)
}
-// hardLinked reports whether the file at path has more than one name. A file it cannot open counts
-// as linked, so nothing unknown is copied.
-func hardLinked(path string) bool {
- f, err := os.Open(path)
+// hardLinked reports whether the file at path has more than one name. It opens the file for its
+// attributes only, which no share mode can refuse, so a handle another account holds cannot make a
+// file look linked and slip past the replacement; any other failure is an error that names it.
+func hardLinked(path string) (bool, error) {
+ p, err := windows.UTF16PtrFromString(path)
+ if err != nil {
+ return false, err
+ }
+ h, err := windows.CreateFile(p, windows.FILE_READ_ATTRIBUTES,
+ windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, nil,
+ windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0)
+ if errors.Is(err, windows.ERROR_FILE_NOT_FOUND) || errors.Is(err, windows.ERROR_PATH_NOT_FOUND) {
+ return false, nil
+ }
if err != nil {
- return !errors.Is(err, fs.ErrNotExist)
+ return false, fmt.Errorf("read the link count of %s: %w", path, err)
}
- defer f.Close()
+ defer windows.CloseHandle(h)
var info windows.ByHandleFileInformation
- if err := windows.GetFileInformationByHandle(windows.Handle(f.Fd()), &info); err != nil {
- return true
+ if err := windows.GetFileInformationByHandle(h, &info); err != nil {
+ return false, fmt.Errorf("read the link count of %s: %w", path, err)
}
- return info.NumberOfLinks > 1
+ return info.NumberOfLinks > 1, nil
}
// replaceWithCopy copies the file at path into a new file beside it, created with its final
diff --git a/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go b/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
index baea2835..52600bdd 100644
--- a/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
+++ b/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
@@ -308,3 +308,31 @@ func grants(dacl []sysacl.ACE) []string {
}
return out
}
+
+// A file beside the log that another process holds without sharing reads is not taken for a
+// hard-linked one and skipped: the restriction fails and names it, rather than reporting success
+// while that handle keeps reading what the collector's neighbours write.
+func TestRestrictSystemLogFailsOnAFileBesideTheLogHeldWithoutSharingReads(t *testing.T) {
+ path := restrictFixture(t)
+ prev := replaceWait
+ replaceWait = 300 * time.Millisecond
+ t.Cleanup(func() { replaceWait = prev })
+ inventory := filepath.Join(filepath.Dir(path), "inventory_state.jsonl")
+ if err := os.WriteFile(inventory, []byte("{\"snapshot\":1}\n"), 0o666); err != nil {
+ t.Fatal(err)
+ }
+ p, _ := windows.UTF16PtrFromString(inventory)
+ h, err := windows.CreateFile(p, windows.GENERIC_READ|windows.GENERIC_WRITE, windows.FILE_SHARE_WRITE, nil, windows.OPEN_EXISTING, 0, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ held := os.NewFile(uintptr(h), inventory)
+ err = RestrictSystemLog(path, 0)
+ _ = held.Close()
+ if err == nil || !strings.Contains(err.Error(), "inventory_state.jsonl") {
+ t.Fatalf("RestrictSystemLog = %v, want it to fail naming the held file", err)
+ }
+ if asymptoteobserve.RuntimeLogAccessRestricted(path) {
+ t.Fatal("a restriction that could not replace a held file left the marker")
+ }
+}