Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(computer): trust only generated Codex capability SIDs

jeffreycb57f763c177
diff --git a/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox.go b/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox.go
new file mode 100644
index 00000000..cad2b6b8
--- /dev/null
+++ b/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox.go
@@ -0,0 +1,49 @@
+package privatefs
+
+import (
+ "encoding/json"
+ "strconv"
+ "strings"
+)
+
+// parseCodexCapSIDs returns the write capability SIDs in a Codex cap_sid file.
+// Only entries with the shape Codex generates are kept, so a corrupted file
+// cannot make a well-known or builtin SID such as Everyone trusted.
+func parseCodexCapSIDs(data []byte) map[string]bool {
+ var caps struct {
+ Workspace string `json:"workspace"`
+ WorkspaceByCWD map[string]string `json:"workspace_by_cwd"`
+ WritableRootByPath map[string]string `json:"writable_root_by_path"`
+ }
+ sids := map[string]bool{}
+ if json.Unmarshal(data, &caps) != nil {
+ return sids
+ }
+ values := []string{caps.Workspace}
+ for _, roots := range []map[string]string{caps.WorkspaceByCWD, caps.WritableRootByPath} {
+ for _, sid := range roots {
+ values = append(values, sid)
+ }
+ }
+ for _, sid := range values {
+ if isCodexCapabilitySID(sid) {
+ sids[sid] = true
+ }
+ }
+ return sids
+}
+
+// isCodexCapabilitySID reports whether value is S-1-5-21 followed by four
+// 32-bit values, the form Codex draws at random for capability SIDs.
+func isCodexCapabilitySID(value string) bool {
+ parts := strings.Split(value, "-")
+ if len(parts) != 8 || strings.Join(parts[:4], "-") != "S-1-5-21" {
+ return false
+ }
+ for _, part := range parts[4:] {
+ if _, err := strconv.ParseUint(part, 10, 32); err != nil {
+ return false
+ }
+ }
+ return true
+}
diff --git a/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox_test.go b/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox_test.go
new file mode 100644
index 00000000..9835dc3d
--- /dev/null
+++ b/apps/computer/local-daemon/internal/platform/privatefs/codex_sandbox_test.go
@@ -0,0 +1,32 @@
+package privatefs
+
+import (
+ "maps"
+ "slices"
+ "testing"
+)
+
+func TestParseCodexCapSIDsKeepsOnlyGeneratedSIDs(t *testing.T) {
+ const workspace = "S-1-5-21-1158770811-2547671072-1736395728-810166692"
+ const root = "S-1-5-21-1-2-3-4"
+ for _, test := range []struct {
+ name string
+ data string
+ want []string
+ }{
+ {"all maps", `{"workspace":"` + workspace + `","readonly":"S-1-5-21-9-9-9-9","workspace_by_cwd":{"c:/a":"` + root + `"},"writable_root_by_path":{"c:/b":"S-1-5-21-5-6-7-8"}}`, []string{root, "S-1-5-21-5-6-7-8", workspace}},
+ {"missing maps", `{"workspace":"` + workspace + `"}`, []string{workspace}},
+ {"malformed JSON", `{"workspace":`, nil},
+ {"legacy plain SID", workspace, nil},
+ {"well-known and builtin SIDs", `{"workspace":"S-1-1-0","workspace_by_cwd":{"a":"S-1-5-11","b":"S-1-5-32-545","c":"S-1-5-21-1-2-3"}}`, nil},
+ {"values beyond 32 bits", `{"workspace":"S-1-5-21-4294967296-1-2-3","workspace_by_cwd":{"a":"S-1-5-21-+1-2-3-4"}}`, nil},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ got := slices.Sorted(maps.Keys(parseCodexCapSIDs([]byte(test.data))))
+ want := slices.Sorted(slices.Values(test.want))
+ if !slices.Equal(got, want) {
+ t.Fatalf("SIDs = %v, want %v", got, want)
+ }
+ })
+ }
+}
diff --git a/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows.go b/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows.go
index 220a7244..93d44c04 100644
--- a/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows.go
+++ b/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows.go
@@ -3,7 +3,6 @@
package privatefs
import (
- "encoding/json"
"errors"
"fmt"
"os"
@@ -70,6 +69,15 @@ func pinParents(path string) (func(), error) {
parent = next
}
handles := []windows.Handle{}
+ // One walk reads the Codex sandbox principals at most once, and only when
+ // an ancestor grants replace rights to an otherwise untrusted SID.
+ var sandbox map[string]bool
+ sandboxSIDs := func() map[string]bool {
+ if sandbox == nil {
+ sandbox = codexSandboxSIDs()
+ }
+ return sandbox
+ }
closeAll := func() {
for i := len(handles) - 1; i >= 0; i-- {
_ = windows.CloseHandle(handles[i])
@@ -98,7 +106,7 @@ func pinParents(path string) (func(), error) {
}
sd, err := windows.GetSecurityInfo(handle, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
if err == nil {
- err = validateAncestorDescriptor(sd, filepath.Dir(paths[i]) == paths[i])
+ err = validateAncestorDescriptor(sd, filepath.Dir(paths[i]) == paths[i], sandboxSIDs)
}
if err != nil {
closeAll()
@@ -194,12 +202,11 @@ func validateDescriptor(sd *windows.SECURITY_DESCRIPTOR, ownerOnly bool) error {
// Ancestors need not be private, but another account must not be able to
// replace an existing component. The default C:\ ACL allows creating new
// directories; that does not grant removal or replacement of an owned child.
-func validateAncestorDescriptor(sd *windows.SECURITY_DESCRIPTOR, volumeRoot bool) error {
+func validateAncestorDescriptor(sd *windows.SECURITY_DESCRIPTOR, volumeRoot bool, sandboxSIDs func() map[string]bool) error {
user, err := CurrentSID()
if err != nil {
return err
}
- var sandbox map[string]bool
trusted := func(sid *windows.SID) bool {
return sid != nil && (windows.EqualSid(sid, user) || sid.IsWellKnown(windows.WinLocalSystemSid) || sid.IsWellKnown(windows.WinBuiltinAdministratorsSid) ||
sid.String() == "S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464") // Windows TrustedInstaller
@@ -235,10 +242,7 @@ func validateAncestorDescriptor(sd *windows.SECURITY_DESCRIPTOR, volumeRoot bool
if trusted(sid) || ace.Mask&replaceMask == 0 {
continue
}
- if sandbox == nil {
- sandbox = codexSandboxSIDs()
- }
- if !sandbox[sid.String()] {
+ if !sandboxSIDs()[sid.String()] {
return fmt.Errorf("ancestor permits another user (%s) to replace private paths", sid.String())
}
}
@@ -247,37 +251,20 @@ func validateAncestorDescriptor(sd *windows.SECURITY_DESCRIPTOR, volumeRoot bool
// codexSandboxSIDs returns the principals the Codex Windows sandbox grants on
// its writable roots: the sandbox users group and the capability SIDs Codex
-// records in its default home. Linger accepts that a sandboxed Codex
-// command may replace an ancestor such as AppData; private paths themselves
-// stay owner-only. The set is read on demand so grants Codex adds while the
-// Daemon runs are recognized.
+// records in its default home. Linger accepts that a sandboxed Codex command
+// may replace an ancestor such as AppData; private paths themselves stay
+// owner-only.
func codexSandboxSIDs() map[string]bool {
sids := map[string]bool{}
- if sid, _, _, err := windows.LookupSID("", "CodexSandboxUsers"); err == nil {
- sids[sid.String()] = true
- }
profile, err := windows.GetCurrentProcessToken().GetUserProfileDirectory()
- if err != nil {
- return sids
- }
- data, err := os.ReadFile(filepath.Join(profile, ".codex", "cap_sid"))
- if err != nil {
- return sids
- }
- var caps struct {
- Workspace string `json:"workspace"`
- WorkspaceByCWD map[string]string `json:"workspace_by_cwd"`
- WritableRootByPath map[string]string `json:"writable_root_by_path"`
- }
- if json.Unmarshal(data, &caps) != nil {
- return sids
- }
- sids[caps.Workspace] = true
- for _, roots := range []map[string]string{caps.WorkspaceByCWD, caps.WritableRootByPath} {
- for _, sid := range roots {
- sids[sid] = true
+ if err == nil {
+ if data, err := os.ReadFile(filepath.Join(profile, ".codex", "cap_sid")); err == nil {
+ sids = parseCodexCapSIDs(data)
}
}
+ if sid, _, _, err := windows.LookupSID("", "CodexSandboxUsers"); err == nil {
+ sids[sid.String()] = true
+ }
return sids
}
diff --git a/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows_test.go b/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows_test.go
index d711b932..1bd2de92 100644
--- a/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows_test.go
+++ b/apps/computer/local-daemon/internal/platform/privatefs/privatefs_windows_test.go
@@ -35,7 +35,35 @@ func TestWindowsAncestorDeleteRightsRespectVolumeRoot(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- err = validateAncestorDescriptor(sd, test.volumeRoot)
+ err = validateAncestorDescriptor(sd, test.volumeRoot, func() map[string]bool { return nil })
+ if (err == nil) != test.allowed {
+ t.Fatalf("allowed = %v, error = %v", test.allowed, err)
+ }
+ })
+ }
+}
+
+func TestWindowsAncestorTrustsOnlyCodexSandboxSIDs(t *testing.T) {
+ sid, err := CurrentSID()
+ if err != nil {
+ t.Fatal(err)
+ }
+ const sandbox = "S-1-5-21-1158770811-2547671072-1736395728-810166692"
+ sandboxSIDs := func() map[string]bool { return map[string]bool{sandbox: true} }
+ for _, test := range []struct {
+ name string
+ grantee string
+ allowed bool
+ }{
+ {"Codex sandbox modify", sandbox, true},
+ {"other account modify", "S-1-5-21-1-2-3-4", false},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ sd, err := windows.SecurityDescriptorFromString("O:" + sid.String() + "D:P(A;;FA;;;" + sid.String() + ")(A;OICI;0x1301ff;;;" + test.grantee + ")")
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = validateAncestorDescriptor(sd, false, sandboxSIDs)
if (err == nil) != test.allowed {
t.Fatalf("allowed = %v, error = %v", test.allowed, err)
}
diff --git a/scripts/local-computer-windows-bootstrap.ps1 b/scripts/local-computer-windows-bootstrap.ps1
index cf3135ef..57e8dd69 100644
--- a/scripts/local-computer-windows-bootstrap.ps1
+++ b/scripts/local-computer-windows-bootstrap.ps1
@@ -164,9 +164,18 @@ function Get-LingerCodexSandboxSids {
try { $sids += (New-Object Security.Principal.NTAccount('CodexSandboxUsers')).Translate([Security.Principal.SecurityIdentifier]).Value } catch {}
$capFile = Join-Path ([Environment]::GetFolderPath('UserProfile')) '.codex\cap_sid'
try { $caps = [IO.File]::ReadAllText($capFile) | ConvertFrom-Json } catch { return $sids }
- if ($caps.workspace) { $sids += [string]$caps.workspace }
+ $values = @([string]$caps.workspace)
foreach ($roots in @($caps.workspace_by_cwd, $caps.writable_root_by_path)) {
- if ($roots) { $sids += @($roots.PSObject.Properties | ForEach-Object { [string]$_.Value }) }
+ if ($roots) { $values += @($roots.PSObject.Properties | ForEach-Object { [string]$_.Value }) }
+ }
+ # Keep only the S-1-5-21 form with four 32-bit values that Codex generates,
+ # so a corrupted file cannot make a well-known SID such as Everyone trusted.
+ foreach ($value in $values) {
+ $parts = $value -split '-'
+ if ($parts.Count -ne 8 -or ($parts[0..3] -join '-') -cne 'S-1-5-21') { continue }
+ $valid = $true
+ foreach ($part in $parts[4..7]) { $number = [uint32]0; if (-not [uint32]::TryParse($part, [Globalization.NumberStyles]::None, [Globalization.CultureInfo]::InvariantCulture, [ref]$number)) { $valid = $false } }
+ if ($valid) { $sids += $value }
}
return $sids
}