Skip to content

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

Windows restricted mode: final security at creation, list under the lock, marker last

Claudec7c5e0987b97
diff --git a/cli/beacon/internal/endpoint/sysacl/restricted_windows.go b/cli/beacon/internal/endpoint/sysacl/restricted_windows.go
index 4e2ee2aa..1f778c25 100644
--- a/cli/beacon/internal/endpoint/sysacl/restricted_windows.go
+++ b/cli/beacon/internal/endpoint/sysacl/restricted_windows.go
@@ -8,7 +8,9 @@ import (
"io/fs"
"os"
"path/filepath"
+ "runtime"
"strings"
+ "unsafe"
"golang.org/x/sys/windows"
)
@@ -160,3 +162,48 @@ func InspectRestrictedLogDir(dir, base, readersSID string) ([]string, error) {
}
return problems, nil
}
+
+// CreateRestrictedFile creates a new file at path in the restricted log directory, failing if
+// anything is there. Its security is set by the creation itself, so there is no moment in which it
+// carries anything else: owned by Administrators whoever the creator is (a policy can make an
+// administrator's files their own), with RestrictedLockDACL when lock is set and otherwise exactly
+// what the directory passes on.
+func CreateRestrictedFile(path string, lock bool) (*os.File, error) {
+ sddl := "O:" + sidAdministrators
+ if lock {
+ sddl = SDDL(sidAdministrators, true, RestrictedLockDACL())
+ }
+ sd, err := windows.SecurityDescriptorFromString(sddl)
+ if err != nil {
+ return nil, fmt.Errorf("build the security descriptor: %w", err)
+ }
+ sa := &windows.SecurityAttributes{SecurityDescriptor: sd}
+ sa.Length = uint32(unsafe.Sizeof(*sa))
+ p, err := windows.UTF16PtrFromString(path)
+ if err != nil {
+ return nil, err
+ }
+ h, err := windows.CreateFile(p, windows.GENERIC_READ|windows.GENERIC_WRITE,
+ windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, sa, windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL, 0)
+ runtime.KeepAlive(sd)
+ if err != nil {
+ return nil, &os.PathError{Op: "create", Path: path, Err: err}
+ }
+ return os.NewFile(uintptr(h), path), nil
+}
+
+// ResetRestrictedFile gives the plain file at path Administrators ownership and exactly what a new
+// file in the restricted log directory inherits, without following a link.
+func ResetRestrictedFile(path, readersSID string) error {
+ return withPrivileges(func() error {
+ h, info, err := openNoFollow(path)
+ if err != nil {
+ return err
+ }
+ defer windows.CloseHandle(h)
+ if info.FileAttributes&(windows.FILE_ATTRIBUTE_REPARSE_POINT|windows.FILE_ATTRIBUTE_DIRECTORY) != 0 || info.NumberOfLinks > 1 {
+ return fmt.Errorf("%s is not a plain file", path)
+ }
+ return setSecurity(h, false, InheritedDACL(RestrictedLogDirDACL(readersSID), false))
+ })
+}
diff --git a/cli/beacon/internal/endpoint/writer/logaccess_windows.go b/cli/beacon/internal/endpoint/writer/logaccess_windows.go
index 61a6715e..865379a1 100644
--- a/cli/beacon/internal/endpoint/writer/logaccess_windows.go
+++ b/cli/beacon/internal/endpoint/writer/logaccess_windows.go
@@ -45,17 +45,23 @@ var (
// on Windows, where the readers group is a local group named by its SID.
//
// The directory gets the restricted ACL first (sysacl.SecureRestrictedLogDir): SYSTEM and
-// Administrators, and read for the readers group, with nothing for anyone else. Then the marker
-// that tells the collector and the CLI the mode is written there, inheriting that ACL.
+// Administrators, and read for the readers group, with nothing for anyone else, and everything in it
+// is reset to match.
//
// A log that was shared is not left with the ACL change alone. Any logged-on user could open it,
// its archives or its lock while INTERACTIVE held read and write data on them, and a handle keeps
// the access it was opened with: it could go on reading new events, overwriting old ones, or hold
-// the lock. So each file is copied into a new file, which inherits the restricted ACL, and renamed
-// over the old one under the log's lock, which leaves any such handle on a file nothing writes or
-// reads any more. A rename over a file another process holds open without sharing delete fails; it
-// is retried for a few seconds, and then the call fails and names the file, so install rolls back
-// rather than reporting a restriction it could not make.
+// the lock. So each file is copied into a new file and renamed over the old one under the log's
+// lock, which leaves any such handle on a file nothing writes or reads any more. Each copy is
+// created with its final owner and DACL (sysacl.CreateRestrictedFile), so not even the readers
+// group can open the new lock in between. A file another process holds open without sharing delete
+// cannot be replaced; that is retried for a few seconds, and then the call fails and names the file,
+// so install rolls back rather than reporting a restriction it could not make.
+//
+// The marker that records the mode is written last, once every file has been replaced. On Windows
+// it does not change how the root writers open the log -- the directory's ACL decides what a new
+// file grants -- so nothing needs it earlier, and writing it last means a run that failed or was
+// cut short is not mistaken for a finished one: the next run copies the files again.
func RestrictSystemLog(path string, _ int) error {
readers, err := logReadersSID()
if err != nil {
@@ -67,29 +73,35 @@ func RestrictSystemLog(path string, _ int) error {
if err := sysacl.SecureRestrictedLogDir(dir, base, readers); err != nil {
return fmt.Errorf("secure the log directory %s: %w", dir, err)
}
- if err := asymptoteobserve.SetRuntimeLogAccess(path, asymptoteobserve.LogAccessRestricted); err != nil {
- return fmt.Errorf("record the restricted mode: %w", err)
- }
- lockPath := path + ".lock"
if !wasRestricted {
if err := replaceLogFamily(path); err != nil {
return err
}
}
- // The lock and the log exist from here on, as a shared install leaves them: the lock narrowed
- // to SYSTEM and Administrators, the log with what it inherits.
- for _, name := range []string{lockPath, path} {
- f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o640)
- if err != nil {
+ // The lock and the log exist from here on, as a shared install leaves them.
+ for name, lock := range map[string]bool{path + ".lock": true, path: false} {
+ f, err := sysacl.CreateRestrictedFile(name, lock)
+ if err == nil {
+ _ = f.Close()
+ } else if !errors.Is(err, fs.ErrExist) {
return err
}
- _ = f.Close()
}
- return sysacl.SecureRestrictedLock(lockPath)
+ if err := asymptoteobserve.SetRuntimeLogAccess(path, asymptoteobserve.LogAccessRestricted); err != nil {
+ return fmt.Errorf("record the restricted mode: %w", err)
+ }
+ // The marker counts only when an administrator owns it, which the creator's default owner may
+ // not be.
+ if err := sysacl.ResetRestrictedFile(asymptoteobserve.LogAccessMarkerPath(path), readers); err != nil {
+ return fmt.Errorf("secure the restricted mode marker: %w", err)
+ }
+ return nil
}
// replaceLogFamily replaces the log and its archives with fresh copies while holding the log's lock
// (bounded), so no line lands in an old file after its copy was taken, and then the lock itself.
+// The family is listed once the lock is held: the collector rotates under it, so a list taken
+// before could miss the archive a rotation made while this waited.
//
// The lock is replaced after it is released: a hook already waiting for it holds it open, which
// blocks the rename until that hook has taken and released it. No hook can open it afresh by
@@ -97,16 +109,6 @@ func RestrictSystemLog(path string, _ int) error {
func replaceLogFamily(path string) error {
dir := filepath.Dir(path)
base := filepath.Base(path)
- entries, err := os.ReadDir(dir)
- if err != nil {
- return err
- }
- var names []string
- for _, entry := range entries {
- if sysacl.IsLogFamily(entry.Name(), base) && !strings.EqualFold(entry.Name(), base+".lock") {
- names = append(names, entry.Name())
- }
- }
var errs []error
func() {
if lock, err := os.OpenFile(path+".lock", os.O_RDWR, 0); err == nil {
@@ -118,22 +120,30 @@ func replaceLogFamily(path string) error {
defer held.Release()
}
}
- for _, name := range names {
- if err := replaceWithCopy(filepath.Join(dir, name)); err != nil {
- errs = append(errs, err)
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ errs = append(errs, err)
+ return
+ }
+ for _, entry := range entries {
+ if sysacl.IsLogFamily(entry.Name(), base) && !strings.EqualFold(entry.Name(), base+".lock") {
+ if err := replaceWithCopy(filepath.Join(dir, entry.Name()), false); err != nil {
+ errs = append(errs, err)
+ }
}
}
}()
- if err := replaceWithCopy(path + ".lock"); err != nil {
+ if err := replaceWithCopy(path+".lock", true); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
-// replaceWithCopy copies the file at path into a new file beside it and renames that over it. A
-// file that is missing, or is not a plain file, is left alone; SecureRestrictedLogDir has already
-// removed any link where a log file belongs.
-func replaceWithCopy(path string) error {
+// replaceWithCopy copies the file at path into a new file beside it, created with its final
+// security (the lock's when lock is set), and renames that over it. A file that is missing, or is
+// not a plain file, is left alone; SecureRestrictedLogDir has already removed any link where a log
+// file belongs.
+func replaceWithCopy(path string, lock bool) error {
info, err := os.Lstat(path)
if errors.Is(err, fs.ErrNotExist) {
return nil
@@ -144,11 +154,11 @@ func replaceWithCopy(path string) error {
if !info.Mode().IsRegular() {
return nil
}
- tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".restrict-*")
+ tmpName := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s.restrict-%d", filepath.Base(path), time.Now().UnixNano()))
+ tmp, err := sysacl.CreateRestrictedFile(tmpName, lock)
if err != nil {
return err
}
- tmpName := tmp.Name()
copyErr := copyInto(tmp, path)
if err := tmp.Close(); copyErr == nil {
copyErr = err
diff --git a/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go b/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
index dfe07152..67ce4f86 100644
--- a/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
+++ b/cli/beacon/internal/endpoint/writer/logaccess_windows_test.go
@@ -142,4 +142,45 @@ func TestRestrictSystemLogFailsWhileAHandleBlocksTheReplacement(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "holds it open") {
t.Fatalf("RestrictSystemLog = %v, want it to name the held file", err)
}
+ // The marker is written only once every file was replaced, so a failed run is not taken for a
+ // finished one: the next run copies again, and once nothing holds the file it succeeds.
+ if asymptoteobserve.RuntimeLogAccessRestricted(path) {
+ t.Fatal("a restriction that failed left the marker, so a rerun would skip the copies")
+ }
+ before, _ := os.Stat(archive)
+ if err := RestrictSystemLog(path, 0); err != nil {
+ t.Fatalf("the rerun: %v", err)
+ }
+ after, _ := os.Stat(archive)
+ if os.SameFile(before, after) {
+ t.Fatal("the rerun did not replace the archive the first run could not")
+ }
+ if !asymptoteobserve.RuntimeLogAccessRestricted(path) {
+ t.Fatal("no marker after the rerun succeeded")
+ }
+}
+
+// Every file the restriction creates is owned by Administrators from the start, and the new lock
+// carries only SYSTEM and Administrators: a reader who could open it for a moment could hold it.
+func TestRestrictedCopiesHaveTheirFinalSecurityFromCreation(t *testing.T) {
+ path := restrictFixture(t)
+ if err := RestrictSystemLog(path, 0); err != nil {
+ t.Fatal(err)
+ }
+ for _, name := range []string{path, path + ".1", path + ".lock", asymptoteobserve.LogAccessMarkerPath(path)} {
+ obj, err := sysacl.ReadObject(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if obj.Owner != "S-1-5-32-544" {
+ t.Errorf("%s is owned by %s, want Administrators", name, obj.Owner)
+ }
+ }
+ lock, err := sysacl.ReadObject(path + ".lock")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if problems := sysacl.RestrictedProblems(lock, backupOperators, true); len(problems) > 0 || !lock.Protected {
+ t.Fatalf("the new lock: protected %v, problems %v", lock.Protected, problems)
+ }
}